libdap++
Updated for version 3.8.2
|
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 24886 2011-09-13 22:58:35Z 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" 00058 #include "AttrTable.h" 00059 #include "Error.h" 00060 #include "InternalErr.h" 00061 #include "parser.h" 00062 #include "escaping.h" 00063 #include "debug.h" 00064 00065 using std::cerr; 00066 using std::endl; 00067 00068 // Glue routines declared in das.lex 00069 extern void das_switch_to_buffer(void *new_buffer); 00070 extern void das_delete_buffer(void * buffer); 00071 extern void *das_buffer(FILE *fp); 00072 00073 extern void dasrestart(FILE *yyin); 00074 extern int dasparse(void *arg); // defined in das.tab.c 00075 00076 namespace libdap { 00077 00080 DAS::DAS() : DapObj(), d_container( 0 ) 00081 {} 00082 00083 #if 0 00084 DAS::DAS(AttrTable *attr, string name) 00085 { 00086 append_container(attr, www2id(name)); 00087 } 00088 #endif 00089 00090 // FIXME: Need to create copy constructor and op=. 00091 00095 DAS::~DAS() 00096 {} 00097 00101 string 00102 DAS::container_name() 00103 { 00104 return _container_name ; 00105 } 00106 00112 void DAS::container_name(const string &cn) 00113 { 00114 // We want to find a top level attribute table with the given name. So 00115 // set d_container to null first so that we aren't searching some 00116 // previous container 00117 if (cn != _container_name) { 00118 d_container = 0; 00119 if (!cn.empty()) { 00120 d_container = get_table(cn); 00121 if (!d_container) { 00122 d_container = add_table(cn, new AttrTable); 00123 } 00124 } 00125 _container_name = cn; 00126 } 00127 } 00128 00134 AttrTable * 00135 DAS::container() 00136 { 00137 return d_container ; 00138 } 00139 00146 unsigned int DAS::get_size() const 00147 { 00148 if (d_container) { 00149 return d_container->get_size(); 00150 } 00151 return d_attrs.get_size(); 00152 } 00153 00156 void DAS::erase() 00157 { 00158 if (d_container) { 00159 d_container->erase(); 00160 } 00161 else { 00162 d_attrs.erase(); 00163 } 00164 } 00165 00168 AttrTable::Attr_iter DAS::var_begin() 00169 { 00170 if (d_container) { 00171 return d_container->attr_begin(); 00172 } 00173 return d_attrs.attr_begin(); 00174 } 00175 00179 AttrTable::Attr_iter DAS::var_end() 00180 { 00181 if (d_container) { 00182 return d_container->attr_end(); 00183 } 00184 return d_attrs.attr_end(); 00185 } 00186 00189 string DAS::get_name(AttrTable::Attr_iter &i) 00190 { 00191 if (d_container) { 00192 return d_container->get_name(i); 00193 } 00194 return d_attrs.get_name(i); 00195 } 00196 00199 AttrTable * 00200 DAS::get_table(AttrTable::Attr_iter &i) 00201 { 00202 if (d_container) { 00203 return d_container->get_attr_table(i); 00204 } 00205 return d_attrs.get_attr_table(i); 00206 } 00207 00210 AttrTable * 00211 DAS::get_table(const string &name) 00212 { 00213 if (d_container) { 00214 return d_container->get_attr_table(name); 00215 } 00216 return d_attrs.get_attr_table(name); 00217 } 00218 00220 00225 00229 AttrTable * 00230 DAS::add_table( const string &name, AttrTable *at ) 00231 { 00232 if (d_container) { 00233 at->set_is_global_attribute(false); 00234 return d_container->append_container(at, name); 00235 } 00236 return d_attrs.append_container( at, name ) ; 00237 } 00238 00240 00246 00247 00252 void 00253 DAS::parse(string fname) 00254 { 00255 FILE *in = fopen(fname.c_str(), "r"); 00256 00257 if (!in) { 00258 throw Error(cannot_read_file, "Could not open: " + fname); 00259 } 00260 00261 parse(in); 00262 00263 int res = fclose(in); 00264 if (res) { 00265 DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl ;) ; 00266 } 00267 } 00268 00279 void 00280 DAS::parse(int fd) 00281 { 00282 #ifdef WIN32 00283 FILE *in = fdopen(_dup(fd), "r"); 00284 #else 00285 FILE *in = fdopen(dup(fd), "r"); 00286 #endif 00287 00288 if (!in) { 00289 throw InternalErr(__FILE__, __LINE__, "Could not access file."); 00290 } 00291 00292 parse(in); 00293 00294 int res = fclose(in); 00295 if (res) { 00296 DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl ;) ; 00297 } 00298 } 00299 00300 00301 00308 void 00309 DAS::parse(FILE *in) 00310 { 00311 if (!in) { 00312 throw InternalErr(__FILE__, __LINE__, "Null input stream."); 00313 } 00314 00315 void *buffer = das_buffer(in); 00316 das_switch_to_buffer(buffer); 00317 00318 parser_arg arg(this); 00319 00320 bool status = dasparse((void *) & arg) == 0; 00321 00322 das_delete_buffer(buffer); 00323 00324 // STATUS is the result of the parser function; if a recoverable error 00325 // was found it will be true but arg.status() will be false. 00326 if (!status || !arg.status()) {// Check parse result 00327 if (arg.error()) 00328 throw *arg.error(); 00329 } 00330 } 00331 00333 00346 void 00347 DAS::print(FILE *out, bool dereference) 00348 { 00349 fprintf(out, "Attributes {\n") ; 00350 00351 d_attrs.print(out, " ", dereference); 00352 00353 fprintf(out, "}\n") ; 00354 } 00355 00368 void 00369 DAS::print(ostream &out, bool dereference) 00370 { 00371 out << "Attributes {\n" ; 00372 00373 d_attrs.print(out, " ", dereference); 00374 00375 out << "}\n" ; 00376 } 00377 00385 void 00386 DAS::dump(ostream &strm) const 00387 { 00388 strm << DapIndent::LMarg << "DAS::dump - (" 00389 << (void *)this << ")" << endl ; 00390 DapIndent::Indent() ; 00391 if( d_container ) 00392 { 00393 strm << DapIndent::LMarg << "current container: " << _container_name 00394 << endl ; 00395 } 00396 else 00397 { 00398 strm << DapIndent::LMarg << "current container: NONE" << endl ; 00399 } 00400 d_attrs.dump(strm) ; 00401 DapIndent::UnIndent() ; 00402 } 00403 00404 } // namespace libdap 00405