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 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"
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
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);
00074
00075 namespace libdap {
00076
00077 AttrTable *
00078 DAS::das_find(string name)
00079 {
00080 return find_container(name);
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
00113
00114
00115
00116
00117
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
00257
00258 if (!status || !arg.status()) {
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 }
00328