libdap++  Updated for version 3.14.0
DAS.cc
Go to the documentation of this file.
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2002,2003 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // (c) COPYRIGHT URI/MIT 1994-1999
27 // Please read the full copyright statement in the file COPYRIGHT_URI.
28 //
29 // Authors:
30 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31 
32 // Methods for the class DAS - a class used to parse the dataset attribute
33 // structure.
34 //
35 // jhrg 7/25/94
36 
37 #include "config.h"
38 
39 #include <cstdio>
40 
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 
45 #ifdef WIN32
46 #include <io.h>
47 #endif
48 
49 #include <iostream>
50 #include <string>
51 
52 #include "DAS.h"
53 #include "AttrTable.h"
54 #include "Error.h"
55 #include "InternalErr.h"
56 #include "parser.h"
57 #include "escaping.h"
58 #include "debug.h"
59 
60 using std::cerr;
61 using std::endl;
62 
63 // Glue routines declared in das.lex
64 extern void das_switch_to_buffer(void *new_buffer);
65 extern void das_delete_buffer(void * buffer);
66 extern void *das_buffer(FILE *fp);
67 
68 //extern void dasrestart(FILE *yyin);
69 //extern int dasparse(void *arg); // defined in das.tab.c
70 extern int dasparse(libdap::parser_arg *arg); // defined in das.tab.c
71 
72 namespace libdap {
73 
76 DAS::DAS() : DapObj(), d_container( 0 )
77 {}
78 
79 #if 0
80 DAS::DAS(AttrTable *attr, string name)
81 {
82  append_container(attr, www2id(name));
83 }
84 #endif
85 
86 // FIXME: Need to create copy constructor and op=.
87 
92 {}
93 
97 string
99 {
100  return _container_name ;
101 }
102 
108 void DAS::container_name(const string &cn)
109 {
110  // We want to find a top level attribute table with the given name. So
111  // set d_container to null first so that we aren't searching some
112  // previous container
113  if (cn != _container_name) {
114  d_container = 0;
115  if (!cn.empty()) {
116  d_container = get_table(cn);
117  if (!d_container) {
118  d_container = add_table(cn, new AttrTable);
119  }
120  }
121  _container_name = cn;
122  }
123 }
124 
130 AttrTable *
132 {
133  return d_container ;
134 }
135 
142 unsigned int DAS::get_size() const
143 {
144  if (d_container) {
145  return d_container->get_size();
146  }
147  return d_attrs.get_size();
148 }
149 
153 {
154  if (d_container) {
155  d_container->erase();
156  }
157  else {
158  d_attrs.erase();
159  }
160 }
161 
165 {
166  if (d_container) {
167  return d_container->attr_begin();
168  }
169  return d_attrs.attr_begin();
170 }
171 
176 {
177  if (d_container) {
178  return d_container->attr_end();
179  }
180  return d_attrs.attr_end();
181 }
182 
186 {
187  if (d_container) {
188  return d_container->get_name(i);
189  }
190  return d_attrs.get_name(i);
191 }
192 
195 AttrTable *
197 {
198  if (d_container) {
199  return d_container->get_attr_table(i);
200  }
201  return d_attrs.get_attr_table(i);
202 }
203 
206 AttrTable *
207 DAS::get_table(const string &name)
208 {
209  if (d_container) {
210  return d_container->get_attr_table(name);
211  }
212  return d_attrs.get_attr_table(name);
213 }
214 
216 
221 
225 AttrTable *
226 DAS::add_table( const string &name, AttrTable *at )
227 {
228  if (d_container) {
229  at->set_is_global_attribute(false);
230  return d_container->append_container(at, name);
231  }
232  return d_attrs.append_container( at, name ) ;
233 }
234 
236 
242 
243 
248 void
249 DAS::parse(string fname)
250 {
251  FILE *in = fopen(fname.c_str(), "r");
252 
253  if (!in) {
254  throw Error(cannot_read_file, "Could not open: " + fname);
255  }
256 
257  parse(in);
258 
259  int res = fclose(in);
260  if (res) {
261  DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl ;) ;
262  }
263 }
264 
275 void
276 DAS::parse(int fd)
277 {
278 #ifdef WIN32
279  FILE *in = fdopen(_dup(fd), "r");
280 #else
281  FILE *in = fdopen(dup(fd), "r");
282 #endif
283 
284  if (!in) {
285  throw InternalErr(__FILE__, __LINE__, "Could not access file.");
286  }
287 
288  parse(in);
289 
290  int res = fclose(in);
291  if (res) {
292  DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl ;) ;
293  }
294 }
295 
296 
297 
304 void
305 DAS::parse(FILE *in)
306 {
307  if (!in) {
308  throw InternalErr(__FILE__, __LINE__, "Null input stream.");
309  }
310 
311  void *buffer = das_buffer(in);
312  das_switch_to_buffer(buffer);
313 
314  parser_arg arg(this);
315 
316  //bool status = dasparse((void *) & arg) == 0;
317  bool status = dasparse(&arg) == 0;
318 
319  das_delete_buffer(buffer);
320 
321  // STATUS is the result of the parser function; if a recoverable error
322  // was found it will be true but arg.status() will be false.
323  if (!status || !arg.status()) {// Check parse result
324  if (arg.error())
325  throw *arg.error();
326  }
327 }
328 
330 
343 void
344 DAS::print(FILE *out, bool dereference)
345 {
346  fprintf(out, "Attributes {\n") ;
347 
348  d_attrs.print(out, " ", dereference);
349 
350  fprintf(out, "}\n") ;
351 }
352 
365 void
366 DAS::print(ostream &out, bool dereference)
367 {
368  out << "Attributes {\n" ;
369 
370  d_attrs.print(out, " ", dereference);
371 
372  out << "}\n" ;
373 }
374 
382 void
383 DAS::dump(ostream &strm) const
384 {
385  strm << DapIndent::LMarg << "DAS::dump - ("
386  << (void *)this << ")" << endl ;
388  if( d_container )
389  {
390  strm << DapIndent::LMarg << "current container: " << _container_name
391  << endl ;
392  }
393  else
394  {
395  strm << DapIndent::LMarg << "current container: NONE" << endl ;
396  }
397  d_attrs.dump(strm) ;
399 }
400 
401 } // namespace libdap
402 
std::vector< entry * >::iterator Attr_iter
Definition: AttrTable.h:229
virtual AttrTable * container()
Returns the current attribute container when multiple files used to build this DAS.
Definition: DAS.cc:131
static void UnIndent()
Definition: DapIndent.cc:51
AttrTable * get_table(AttrTable::Attr_iter &i)
Returns the referenced variable attribute table.
Definition: DAS.cc:196
virtual Attr_iter attr_end()
Definition: AttrTable.cc:718
Contains the attributes for a dataset.
Definition: AttrTable.h:142
AttrTable::Attr_iter var_begin()
Returns a reference to the attribute table for the first variable.
Definition: DAS.cc:164
virtual AttrTable * add_table(const string &name, AttrTable *at)
Adds a variable attribute table to the DAS or the current dataset container attribute table...
Definition: DAS.cc:226
virtual void set_is_global_attribute(bool ga)
Definition: AttrTable.h:277
virtual void print(FILE *out, string pad=" ", bool dereference=false)
Prints the attribute table.
Definition: AttrTable.cc:1242
virtual string get_name() const
Get the name of this attribute table.
Definition: AttrTable.cc:237
virtual void print(FILE *out, bool dereference=false)
Definition: DAS.cc:344
DAS()
Definition: DAS.cc:76
A class for software fault reporting.
Definition: InternalErr.h:64
int dasparse(libdap::parser_arg *arg)
void * das_buffer(FILE *fp)
#define DBG(x)
Definition: debug.h:58
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition: AttrTable.cc:409
static void Indent()
Definition: DapIndent.cc:45
#define cannot_read_file
Definition: Error.h:66
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition: AttrTable.cc:606
string get_name(AttrTable::Attr_iter &i)
Returns the name of the referenced variable attribute table.
Definition: DAS.cc:185
virtual void erase()
Erase the attribute table.
Definition: AttrTable.cc:1035
Error * error()
Definition: parser.h:93
virtual void erase()
erase all attributes in this DAS
Definition: DAS.cc:152
virtual Attr_iter attr_begin()
Definition: AttrTable.cc:710
string www2id(const string &in, const string &escape, const string &except)
Definition: escaping.cc:220
AttrTable::Attr_iter var_end()
Definition: DAS.cc:175
virtual void dump(ostream &strm) const
dumps information about this object
Definition: DAS.cc:383
static ostream & LMarg(ostream &strm)
Definition: DapIndent.cc:80
virtual void parse(string fname)
Reads a DAS from the named file.
Definition: DAS.cc:249
void das_delete_buffer(void *buffer)
libdap base object for common functionality of libdap objects
Definition: DapObj.h:55
Pass parameters by reference to a parser.
Definition: parser.h:68
virtual ~DAS()
This deletes the pointers to AttrTables allocated during the parse (and at other times). jhrg 7/29/94.
Definition: DAS.cc:91
virtual void dump(ostream &strm) const
dumps information about this object
Definition: AttrTable.cc:1509
void das_switch_to_buffer(void *new_buffer)
A class for error processing.
Definition: Error.h:90
virtual unsigned int get_size() const
Returns the number of attributes in the current attribute table.
Definition: DAS.cc:142
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition: AttrTable.cc:230
virtual string container_name()
Returns the name of the current attribute container when multiple files used to build this DAS...
Definition: DAS.cc:98