DAS.cc

Go to the documentation of this file.
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2002,2003 OPeNDAP, Inc.
00008 // Author: James Gallagher <jgallagher@opendap.org>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 // (c) COPYRIGHT URI/MIT 1994-1999
00027 // Please read the full copyright statement in the file COPYRIGHT_URI.
00028 //
00029 // Authors:
00030 //      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>
00031 
00032 // Methods for the class DAS - a class used to parse the dataset attribute
00033 // structure.
00034 //
00035 // jhrg 7/25/94
00036 
00037 #include "config.h"
00038 
00039 static char rcsid[] not_used =
00040     {"$Id: DAS.cc 18319 2008-03-03 21:42:36Z jimg $"
00041     };
00042 
00043 
00044 #include <cstdio>
00045 
00046 #ifdef HAVE_UNISTD_H
00047 #include <unistd.h>
00048 #endif
00049 
00050 #ifdef WIN32
00051 #include <io.h>
00052 #endif
00053 
00054 #include <iostream>
00055 #include <string>
00056 
00057 #include "DAS.h"  // follows pragma since DAS.h is interface
00058 #include "Error.h"
00059 #include "InternalErr.h"
00060 #include "parser.h"
00061 #include "escaping.h"
00062 #include "debug.h"
00063 
00064 using std::cerr;
00065 using std::endl;
00066 
00067 // Glue routines declared in das.lex
00068 extern void das_switch_to_buffer(void *new_buffer);
00069 extern void das_delete_buffer(void * buffer);
00070 extern void *das_buffer(FILE *fp);
00071 
00072 extern void dasrestart(FILE *yyin);
00073 extern int dasparse(void *arg); // defined in das.tab.c
00074 
00075 namespace libdap {
00076 
00077 AttrTable *
00078 DAS::das_find(string name)
00079 {
00080     return find_container(name); // Only containers at the top level.
00081 }
00082 
00096 DAS::DAS(AttrTable *, unsigned int)
00097 {}
00098 
00107 DAS::DAS(AttrTable *attr, string name)
00108 {
00109     append_container(attr, www2id(name));
00110 }
00111 
00112 // The class DASVHMap knows that it contains pointers to things and correctly
00113 // makes copies of those things when its copy ctor is called, so DAS can do a
00114 // simple member-wise copy. Similarly, we don't need to define our own op=.
00115 
00116 // This deletes the pointers to AttrTables allocated during the parse (and at
00117 // other times). jhrg 7/29/94
00118 
00119 DAS::~DAS()
00120 {}
00121 
00122 AttrTable::Attr_iter
00123 DAS::var_begin()
00124 {
00125     return AttrTable::attr_begin() ;
00126 }
00127 
00128 AttrTable::Attr_iter
00129 DAS::var_end()
00130 {
00131     return AttrTable::attr_end() ;
00132 }
00133 
00134 
00135 string
00136 DAS::get_name(Attr_iter &i)
00137 {
00138     return AttrTable::get_name(i);
00139 }
00140 
00142 AttrTable *
00143 DAS::get_table(Attr_iter &i)
00144 {
00145     return AttrTable::get_attr_table(i);
00146 }
00147 
00150 AttrTable *
00151 DAS::get_table(const string &name)
00152 {
00153     return AttrTable::get_attr_table(name);
00154 }
00155 
00157 
00162 
00164 AttrTable *
00165 DAS::add_table(const string &name, AttrTable *at)
00166 {
00167     DBG(cerr << "Adding table: " << name << "(" << at << ")" << endl);
00168     return AttrTable::append_container(at, name);
00169 }
00170 
00172 
00178 
00179 
00184 void
00185 DAS::parse(string fname)
00186 {
00187     FILE *in = fopen(fname.c_str(), "r");
00188 
00189     if (!in) {
00190         throw Error(cannot_read_file, "Could not open: " + fname);
00191     }
00192 
00193     parse(in);
00194 
00195     int res = fclose(in);
00196     if (res) {
00197         DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl ;) ;
00198     }
00199 }
00200 
00211 void
00212 DAS::parse(int fd)
00213 {
00214 #ifdef WIN32
00215     FILE *in = fdopen(_dup(fd), "r");
00216 #else
00217     FILE *in = fdopen(dup(fd), "r");
00218 #endif
00219 
00220     if (!in) {
00221         throw InternalErr(__FILE__, __LINE__, "Could not access file.");
00222     }
00223 
00224     parse(in);
00225 
00226     int res = fclose(in);
00227     if (res) {
00228         DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl ;) ;
00229     }
00230 }
00231 
00232 
00233 
00240 void
00241 DAS::parse(FILE *in)
00242 {
00243     if (!in) {
00244         throw InternalErr(__FILE__, __LINE__, "Null input stream.");
00245     }
00246 
00247     void *buffer = das_buffer(in);
00248     das_switch_to_buffer(buffer);
00249 
00250     parser_arg arg(this);
00251 
00252     bool status = dasparse((void *) & arg) == 0;
00253 
00254     das_delete_buffer(buffer);
00255 
00256     //  STATUS is the result of the parser function; if a recoverable error
00257     //  was found it will be true but arg.status() will be false.
00258     if (!status || !arg.status()) {// Check parse result
00259         if (arg.error())
00260             throw *arg.error();
00261     }
00262 }
00263 
00265 
00278 void
00279 DAS::print(FILE *out, bool dereference)
00280 {
00281     fprintf(out, "Attributes {\n") ;
00282 
00283     AttrTable::print(out, "    ", dereference);
00284 
00285     fprintf(out, "}\n") ;
00286 }
00287 
00300 void
00301 DAS::print(ostream &out, bool dereference)
00302 {
00303     out << "Attributes {\n" ;
00304 
00305     AttrTable::print(out, "    ", dereference);
00306 
00307     out << "}\n" ;
00308 }
00309 
00317 void
00318 DAS::dump(ostream &strm) const
00319 {
00320     strm << DapIndent::LMarg << "DAS::dump - ("
00321          << (void *)this << ")" << endl ;
00322     DapIndent::Indent() ;
00323     AttrTable::dump(strm) ;
00324     DapIndent::UnIndent() ;
00325 }
00326 
00327 } // namespace libdap
00328 

Generated on Tue Jun 10 18:00:29 2008 for libdap++ by  doxygen 1.5.4