libdap++  Updated for version 3.8.2
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 static char rcsid[] not_used =
40  {"$Id: DAS.cc 24281 2011-03-09 00:22:31Z jimg $"
41  };
42 
43 
44 #include <cstdio>
45 
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 
50 #ifdef WIN32
51 #include <io.h>
52 #endif
53 
54 #include <iostream>
55 #include <string>
56 
57 #include "DAS.h"
58 #include "AttrTable.h"
59 #include "Error.h"
60 #include "InternalErr.h"
61 #include "parser.h"
62 #include "escaping.h"
63 #include "debug.h"
64 
65 using std::cerr;
66 using std::endl;
67 
68 // Glue routines declared in das.lex
69 extern void das_switch_to_buffer(void *new_buffer);
70 extern void das_delete_buffer(void * buffer);
71 extern void *das_buffer(FILE *fp);
72 
73 extern void dasrestart(FILE *yyin);
74 extern int dasparse(void *arg); // defined in das.tab.c
75 
76 namespace libdap {
77 
80 DAS::DAS() : DapObj(), d_container( 0 )
81 {}
82 
83 #if 0
84 DAS::DAS(AttrTable *attr, string name)
85 {
86  append_container(attr, www2id(name));
87 }
88 #endif
89 
90 // FIXME: Need to create copy constructor and op=.
91 
96 {}
97 
101 string
103 {
104  return _container_name ;
105 }
106 
112 void
113 DAS::container_name( const string &cn )
114 {
115  // We want to find a top level attribute table with the given name. So
116  // set d_container to null first so that we aren't searching some
117  // previous container
118  if( cn != _container_name )
119  {
120  d_container = 0 ;
121  if( !cn.empty() )
122  {
123  d_container = get_table( cn ) ;
124  if( !d_container )
125  {
126  d_container = add_table( cn, new AttrTable ) ;
127  }
128  }
129  _container_name = cn;
130  }
131 }
132 
138 AttrTable *
140 {
141  return d_container ;
142 }
143 
150 unsigned int
152 {
153  if( d_container )
154  {
155  return d_container->get_size() ;
156  }
157  return d_attrs.get_size() ;
158 }
159 
162 void
164 {
165  if( d_container )
166  {
167  d_container->erase() ;
168  }
169  else
170  {
171  d_attrs.erase() ;
172  }
173 }
174 
179 {
180  if( d_container )
181  {
182  return d_container->attr_begin() ;
183  }
184  return d_attrs.attr_begin() ;
185 }
186 
192 {
193  if( d_container )
194  {
195  return d_container->attr_end() ;
196  }
197  return d_attrs.attr_end() ;
198 }
199 
202 string
204 {
205  if( d_container )
206  {
207  return d_container->get_name( i ) ;
208  }
209  return d_attrs.get_name( i ) ;
210 }
211 
214 AttrTable *
216 {
217  if( d_container )
218  {
219  return d_container->get_attr_table( i ) ;
220  }
221  return d_attrs.get_attr_table( i ) ;
222 }
223 
226 AttrTable *
227 DAS::get_table( const string &name )
228 {
229  if( d_container )
230  {
231  return d_container->get_attr_table( name ) ;
232  }
233  return d_attrs.get_attr_table( name ) ;
234 }
235 
237 
242 
246 AttrTable *
247 DAS::add_table( const string &name, AttrTable *at )
248 {
249  if( d_container )
250  {
251  at->set_is_global_attribute( false ) ;
252  return d_container->append_container( at, name ) ;
253  }
254  return d_attrs.append_container( at, name ) ;
255 }
256 
258 
264 
265 
270 void
271 DAS::parse(string fname)
272 {
273  FILE *in = fopen(fname.c_str(), "r");
274 
275  if (!in) {
276  throw Error(cannot_read_file, "Could not open: " + fname);
277  }
278 
279  parse(in);
280 
281  int res = fclose(in);
282  if (res) {
283  DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl ;) ;
284  }
285 }
286 
297 void
298 DAS::parse(int fd)
299 {
300 #ifdef WIN32
301  FILE *in = fdopen(_dup(fd), "r");
302 #else
303  FILE *in = fdopen(dup(fd), "r");
304 #endif
305 
306  if (!in) {
307  throw InternalErr(__FILE__, __LINE__, "Could not access file.");
308  }
309 
310  parse(in);
311 
312  int res = fclose(in);
313  if (res) {
314  DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl ;) ;
315  }
316 }
317 
318 
319 
326 void
327 DAS::parse(FILE *in)
328 {
329  if (!in) {
330  throw InternalErr(__FILE__, __LINE__, "Null input stream.");
331  }
332 
333  void *buffer = das_buffer(in);
334  das_switch_to_buffer(buffer);
335 
336  parser_arg arg(this);
337 
338  bool status = dasparse((void *) & arg) == 0;
339 
340  das_delete_buffer(buffer);
341 
342  // STATUS is the result of the parser function; if a recoverable error
343  // was found it will be true but arg.status() will be false.
344  if (!status || !arg.status()) {// Check parse result
345  if (arg.error())
346  throw *arg.error();
347  }
348 }
349 
351 
364 void
365 DAS::print(FILE *out, bool dereference)
366 {
367  fprintf(out, "Attributes {\n") ;
368 
369  d_attrs.print(out, " ", dereference);
370 
371  fprintf(out, "}\n") ;
372 }
373 
386 void
387 DAS::print(ostream &out, bool dereference)
388 {
389  out << "Attributes {\n" ;
390 
391  d_attrs.print(out, " ", dereference);
392 
393  out << "}\n" ;
394 }
395 
403 void
404 DAS::dump(ostream &strm) const
405 {
406  strm << DapIndent::LMarg << "DAS::dump - ("
407  << (void *)this << ")" << endl ;
409  if( d_container )
410  {
411  strm << DapIndent::LMarg << "current container: " << _container_name
412  << endl ;
413  }
414  else
415  {
416  strm << DapIndent::LMarg << "current container: NONE" << endl ;
417  }
418  d_attrs.dump(strm) ;
420 }
421 
422 } // namespace libdap
423 
std::vector< entry * >::iterator Attr_iter
Definition: AttrTable.h:233
virtual AttrTable * container()
Returns the current attribute container when multiple files used to build this DAS.
Definition: DAS.cc:139
static void UnIndent()
Definition: DapIndent.cc:49
AttrTable * get_table(AttrTable::Attr_iter &i)
Returns the referenced variable attribute table.
Definition: DAS.cc:215
void dasrestart(FILE *yyin)
virtual Attr_iter attr_end()
Definition: AttrTable.cc:652
Contains the attributes for a dataset.
Definition: AttrTable.h:146
#define not_used
Definition: config.h:850
AttrTable::Attr_iter var_begin()
Returns a reference to the attribute table for the first variable.
Definition: DAS.cc:178
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:247
virtual void set_is_global_attribute(bool ga)
Definition: AttrTable.h:281
virtual void print(FILE *out, string pad=" ", bool dereference=false)
Prints the attribute table.
Definition: AttrTable.cc:1158
virtual string get_name() const
Get the name of this attribute table.
Definition: AttrTable.cc:208
virtual void print(FILE *out, bool dereference=false)
Definition: DAS.cc:365
DAS()
Definition: DAS.cc:80
A class for software fault reporting.
Definition: InternalErr.h:64
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:342
static void Indent()
Definition: DapIndent.cc:43
#define cannot_read_file
Definition: Error.h:66
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition: AttrTable.cc:539
string get_name(AttrTable::Attr_iter &i)
Returns the name of the referenced variable attribute table.
Definition: DAS.cc:203
virtual void erase()
Erase the attribute table.
Definition: AttrTable.cc:976
Error * error()
Definition: parser.h:93
virtual void erase()
erase all attributes in this DAS
Definition: DAS.cc:163
virtual Attr_iter attr_begin()
Definition: AttrTable.cc:643
string www2id(const string &in, const string &escape, const string &except)
Definition: escaping.cc:218
AttrTable::Attr_iter var_end()
Definition: DAS.cc:191
virtual void dump(ostream &strm) const
dumps information about this object
Definition: DAS.cc:404
static ostream & LMarg(ostream &strm)
Definition: DapIndent.cc:78
virtual void parse(string fname)
Reads a DAS from the named file.
Definition: DAS.cc:271
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:95
int dasparse(void *arg)
virtual void dump(ostream &strm) const
dumps information about this object
Definition: AttrTable.cc:1312
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:151
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition: AttrTable.cc:200
virtual string container_name()
Returns the name of the current attribute container when multiple files used to build this DAS...
Definition: DAS.cc:102