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 16088 2007-03-28 21:42:19Z jimg $"
00041     };
00042 
00043 
00044 #include <stdio.h>
00045 #ifdef WIN32
00046 #include <io.h>
00047 #else
00048 #include <unistd.h>
00049 #endif
00050 
00051 #include <iostream>
00052 #include <string>
00053 
00054 #include "DAS.h"  // follows pragma since DAS.h is interface
00055 #include "Error.h"
00056 #include "InternalErr.h"
00057 #include "parser.h"
00058 #include "escaping.h"
00059 #include "debug.h"
00060 
00061 using std::cerr;
00062 using std::endl;
00063 
00064 // Glue routines declared in das.lex
00065 extern void das_switch_to_buffer(void *new_buffer);
00066 extern void das_delete_buffer(void * buffer);
00067 extern void *das_buffer(FILE *fp);
00068 
00069 extern void dasrestart(FILE *yyin);
00070 extern int dasparse(void *arg); // defined in das.tab.c
00071 
00072 AttrTable *
00073 DAS::das_find(string name)
00074 {
00075     return find_container(name); // Only containers at the top level.
00076 }
00077 
00091 DAS::DAS(AttrTable *, unsigned int)
00092 {}
00093 
00102 DAS::DAS(AttrTable *attr, string name)
00103 {
00104     append_container(attr, www2id(name));
00105 }
00106 
00107 // The class DASVHMap knows that it contains pointers to things and correctly
00108 // makes copies of those things when its copy ctor is called, so DAS can do a
00109 // simple member-wise copy. Similarly, we don't need to define our own op=.
00110 
00111 // This deletes the pointers to AttrTables allocated during the parse (and at
00112 // other times). jhrg 7/29/94
00113 
00114 DAS::~DAS()
00115 {}
00116 
00117 AttrTable::Attr_iter
00118 DAS::var_begin()
00119 {
00120     return AttrTable::attr_begin() ;
00121 }
00122 
00123 AttrTable::Attr_iter
00124 DAS::var_end()
00125 {
00126     return AttrTable::attr_end() ;
00127 }
00128 
00129 
00130 string
00131 DAS::get_name(Attr_iter &i)
00132 {
00133     return AttrTable::get_name(i);
00134 }
00135 
00137 AttrTable *
00138 DAS::get_table(Attr_iter &i)
00139 {
00140     return AttrTable::get_attr_table(i);
00141 }
00142 
00145 AttrTable *
00146 DAS::get_table(const string &name)
00147 {
00148     return AttrTable::get_attr_table(name);
00149 }
00150 
00152 
00157 
00159 AttrTable *
00160 DAS::add_table(const string &name, AttrTable *at)
00161 {
00162     DBG(cerr << "Adding table: " << name << "(" << at << ")" << endl);
00163     return AttrTable::append_container(at, name);
00164 }
00165 
00167 
00173 
00174 
00179 void
00180 DAS::parse(string fname)
00181 {
00182     FILE *in = fopen(fname.c_str(), "r");
00183 
00184     if (!in) {
00185         throw Error(can_not_read_file, "Could not open: " + fname);
00186     }
00187 
00188     parse(in);
00189 
00190     int res = fclose(in);
00191     if (res) {
00192         DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl ;) ;
00193     }
00194 }
00195 
00206 void
00207 DAS::parse(int fd)
00208 {
00209 #ifdef WIN32
00210     FILE *in = fdopen(_dup(fd), "r");
00211 #else
00212     FILE *in = fdopen(dup(fd), "r");
00213 #endif
00214 
00215     if (!in) {
00216         throw InternalErr(__FILE__, __LINE__, "Could not access file.");
00217     }
00218 
00219     parse(in);
00220 
00221     int res = fclose(in);
00222     if (res) {
00223         DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl ;) ;
00224     }
00225 }
00226 
00227 
00228 
00235 void
00236 DAS::parse(FILE *in)
00237 {
00238     if (!in) {
00239         throw InternalErr(__FILE__, __LINE__, "Null input stream.");
00240     }
00241 
00242     void *buffer = das_buffer(in);
00243     das_switch_to_buffer(buffer);
00244 
00245     parser_arg arg(this);
00246 
00247     bool status = dasparse((void *) & arg) == 0;
00248 
00249     das_delete_buffer(buffer);
00250 
00251     //  STATUS is the result of the parser function; if a recoverable error
00252     //  was found it will be true but arg.status() will be false.
00253     if (!status || !arg.status()) {// Check parse result
00254         if (arg.error())
00255             throw *arg.error();
00256     }
00257 }
00258 
00260 
00273 void
00274 DAS::print(FILE *out, bool dereference)
00275 {
00276     fprintf(out, "Attributes {\n") ;
00277 
00278     AttrTable::print(out, "    ", dereference);
00279 
00280     fprintf(out, "}\n") ;
00281 }
00282 
00290 void
00291 DAS::dump(ostream &strm) const
00292 {
00293     strm << DapIndent::LMarg << "DAS::dump - ("
00294     << (void *)this << ")" << endl ;
00295     DapIndent::Indent() ;
00296     AttrTable::dump(strm) ;
00297     DapIndent::UnIndent() ;
00298 }
00299 

Generated on Wed Jun 27 12:56:38 2007 for libdap++ by  doxygen 1.4.7