00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "config.h"
00038
00039 static char rcsid[] not_used =
00040 {"$Id: DAS.cc 22804 2010-05-19 18:28:46Z pwest $"
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
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);
00075
00076 namespace libdap {
00077
00080 DAS::DAS() : DapObj(), d_container( 0 )
00081 {}
00082
00090
00091
00092
00093
00094
00095
00096
00097
00098
00102 DAS::~DAS()
00103 {}
00104
00108 string
00109 DAS::container_name()
00110 {
00111 return _container_name ;
00112 }
00113
00119 void
00120 DAS::container_name( const string &cn )
00121 {
00122
00123
00124
00125 if( cn != _container_name )
00126 {
00127 d_container = 0 ;
00128 if( !cn.empty() )
00129 {
00130 d_container = get_table( cn ) ;
00131 if( !d_container )
00132 {
00133 d_container = add_table( cn, new AttrTable ) ;
00134 }
00135 }
00136 _container_name = cn;
00137 }
00138 }
00139
00145 AttrTable *
00146 DAS::container()
00147 {
00148 return d_container ;
00149 }
00150
00157 unsigned int
00158 DAS::get_size() const
00159 {
00160 if( d_container )
00161 {
00162 return d_container->get_size() ;
00163 }
00164 return d_attrs.get_size() ;
00165 }
00166
00169 void
00170 DAS::erase()
00171 {
00172 if( d_container )
00173 {
00174 d_container->erase() ;
00175 }
00176 else
00177 {
00178 d_attrs.erase() ;
00179 }
00180 }
00181
00184 AttrTable::Attr_iter
00185 DAS::var_begin()
00186 {
00187 if( d_container )
00188 {
00189 return d_container->attr_begin() ;
00190 }
00191 return d_attrs.attr_begin() ;
00192 }
00193
00197 AttrTable::Attr_iter
00198 DAS::var_end()
00199 {
00200 if( d_container )
00201 {
00202 return d_container->attr_end() ;
00203 }
00204 return d_attrs.attr_end() ;
00205 }
00206
00209 string
00210 DAS::get_name(AttrTable::Attr_iter &i)
00211 {
00212 if( d_container )
00213 {
00214 return d_container->get_name( i ) ;
00215 }
00216 return d_attrs.get_name( i ) ;
00217 }
00218
00221 AttrTable *
00222 DAS::get_table(AttrTable::Attr_iter &i)
00223 {
00224 if( d_container )
00225 {
00226 return d_container->get_attr_table( i ) ;
00227 }
00228 return d_attrs.get_attr_table( i ) ;
00229 }
00230
00233 AttrTable *
00234 DAS::get_table( const string &name )
00235 {
00236 if( d_container )
00237 {
00238 return d_container->get_attr_table( name ) ;
00239 }
00240 return d_attrs.get_attr_table( name ) ;
00241 }
00242
00244
00249
00253 AttrTable *
00254 DAS::add_table( const string &name, AttrTable *at )
00255 {
00256 if( d_container )
00257 {
00258 at->set_is_global_attribute( false ) ;
00259 return d_container->append_container( at, name ) ;
00260 }
00261 return d_attrs.append_container( at, name ) ;
00262 }
00263
00265
00271
00272
00277 void
00278 DAS::parse(string fname)
00279 {
00280 FILE *in = fopen(fname.c_str(), "r");
00281
00282 if (!in) {
00283 throw Error(cannot_read_file, "Could not open: " + fname);
00284 }
00285
00286 parse(in);
00287
00288 int res = fclose(in);
00289 if (res) {
00290 DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl ;) ;
00291 }
00292 }
00293
00304 void
00305 DAS::parse(int fd)
00306 {
00307 #ifdef WIN32
00308 FILE *in = fdopen(_dup(fd), "r");
00309 #else
00310 FILE *in = fdopen(dup(fd), "r");
00311 #endif
00312
00313 if (!in) {
00314 throw InternalErr(__FILE__, __LINE__, "Could not access file.");
00315 }
00316
00317 parse(in);
00318
00319 int res = fclose(in);
00320 if (res) {
00321 DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl ;) ;
00322 }
00323 }
00324
00325
00326
00333 void
00334 DAS::parse(FILE *in)
00335 {
00336 if (!in) {
00337 throw InternalErr(__FILE__, __LINE__, "Null input stream.");
00338 }
00339
00340 void *buffer = das_buffer(in);
00341 das_switch_to_buffer(buffer);
00342
00343 parser_arg arg(this);
00344
00345 bool status = dasparse((void *) & arg) == 0;
00346
00347 das_delete_buffer(buffer);
00348
00349
00350
00351 if (!status || !arg.status()) {
00352 if (arg.error())
00353 throw *arg.error();
00354 }
00355 }
00356
00358
00371 void
00372 DAS::print(FILE *out, bool dereference)
00373 {
00374 fprintf(out, "Attributes {\n") ;
00375
00376 d_attrs.print(out, " ", dereference);
00377
00378 fprintf(out, "}\n") ;
00379 }
00380
00393 void
00394 DAS::print(ostream &out, bool dereference)
00395 {
00396 out << "Attributes {\n" ;
00397
00398 d_attrs.print(out, " ", dereference);
00399
00400 out << "}\n" ;
00401 }
00402
00410 void
00411 DAS::dump(ostream &strm) const
00412 {
00413 strm << DapIndent::LMarg << "DAS::dump - ("
00414 << (void *)this << ")" << endl ;
00415 DapIndent::Indent() ;
00416 if( d_container )
00417 {
00418 strm << DapIndent::LMarg << "current container: " << _container_name
00419 << endl ;
00420 }
00421 else
00422 {
00423 strm << DapIndent::LMarg << "current container: NONE" << endl ;
00424 }
00425 d_attrs.dump(strm) ;
00426 DapIndent::UnIndent() ;
00427 }
00428
00429 }
00430