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 22804 2010-05-19 18:28:46Z pwest $"
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 
90 /*
91 DAS::DAS(AttrTable *attr, string name)
92 {
93  append_container(attr, www2id(name));
94 }
95 */
96 
97 // FIXME: Need to create copy constructor and op=.
98 
103 {}
104 
108 string
110 {
111  return _container_name ;
112 }
113 
119 void
120 DAS::container_name( const string &cn )
121 {
122  // We want to find a top level attribute table with the given name. So
123  // set d_container to null first so that we aren't searching some
124  // previous container
125  if( cn != _container_name )
126  {
127  d_container = 0 ;
128  if( !cn.empty() )
129  {
130  d_container = get_table( cn ) ;
131  if( !d_container )
132  {
133  d_container = add_table( cn, new AttrTable ) ;
134  }
135  }
136  _container_name = cn;
137  }
138 }
139 
145 AttrTable *
147 {
148  return d_container ;
149 }
150 
157 unsigned int
159 {
160  if( d_container )
161  {
162  return d_container->get_size() ;
163  }
164  return d_attrs.get_size() ;
165 }
166 
169 void
171 {
172  if( d_container )
173  {
174  d_container->erase() ;
175  }
176  else
177  {
178  d_attrs.erase() ;
179  }
180 }
181 
186 {
187  if( d_container )
188  {
189  return d_container->attr_begin() ;
190  }
191  return d_attrs.attr_begin() ;
192 }
193 
199 {
200  if( d_container )
201  {
202  return d_container->attr_end() ;
203  }
204  return d_attrs.attr_end() ;
205 }
206 
209 string
211 {
212  if( d_container )
213  {
214  return d_container->get_name( i ) ;
215  }
216  return d_attrs.get_name( i ) ;
217 }
218 
221 AttrTable *
223 {
224  if( d_container )
225  {
226  return d_container->get_attr_table( i ) ;
227  }
228  return d_attrs.get_attr_table( i ) ;
229 }
230 
233 AttrTable *
234 DAS::get_table( const string &name )
235 {
236  if( d_container )
237  {
238  return d_container->get_attr_table( name ) ;
239  }
240  return d_attrs.get_attr_table( name ) ;
241 }
242 
244 
249 
253 AttrTable *
254 DAS::add_table( const string &name, AttrTable *at )
255 {
256  if( d_container )
257  {
258  at->set_is_global_attribute( false ) ;
259  return d_container->append_container( at, name ) ;
260  }
261  return d_attrs.append_container( at, name ) ;
262 }
263 
265 
271 
272 
277 void
278 DAS::parse(string fname)
279 {
280  FILE *in = fopen(fname.c_str(), "r");
281 
282  if (!in) {
283  throw Error(cannot_read_file, "Could not open: " + fname);
284  }
285 
286  parse(in);
287 
288  int res = fclose(in);
289  if (res) {
290  DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl ;) ;
291  }
292 }
293 
304 void
305 DAS::parse(int fd)
306 {
307 #ifdef WIN32
308  FILE *in = fdopen(_dup(fd), "r");
309 #else
310  FILE *in = fdopen(dup(fd), "r");
311 #endif
312 
313  if (!in) {
314  throw InternalErr(__FILE__, __LINE__, "Could not access file.");
315  }
316 
317  parse(in);
318 
319  int res = fclose(in);
320  if (res) {
321  DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl ;) ;
322  }
323 }
324 
325 
326 
333 void
334 DAS::parse(FILE *in)
335 {
336  if (!in) {
337  throw InternalErr(__FILE__, __LINE__, "Null input stream.");
338  }
339 
340  void *buffer = das_buffer(in);
341  das_switch_to_buffer(buffer);
342 
343  parser_arg arg(this);
344 
345  bool status = dasparse((void *) & arg) == 0;
346 
347  das_delete_buffer(buffer);
348 
349  // STATUS is the result of the parser function; if a recoverable error
350  // was found it will be true but arg.status() will be false.
351  if (!status || !arg.status()) {// Check parse result
352  if (arg.error())
353  throw *arg.error();
354  }
355 }
356 
358 
371 void
372 DAS::print(FILE *out, bool dereference)
373 {
374  fprintf(out, "Attributes {\n") ;
375 
376  d_attrs.print(out, " ", dereference);
377 
378  fprintf(out, "}\n") ;
379 }
380 
393 void
394 DAS::print(ostream &out, bool dereference)
395 {
396  out << "Attributes {\n" ;
397 
398  d_attrs.print(out, " ", dereference);
399 
400  out << "}\n" ;
401 }
402 
410 void
411 DAS::dump(ostream &strm) const
412 {
413  strm << DapIndent::LMarg << "DAS::dump - ("
414  << (void *)this << ")" << endl ;
416  if( d_container )
417  {
418  strm << DapIndent::LMarg << "current container: " << _container_name
419  << endl ;
420  }
421  else
422  {
423  strm << DapIndent::LMarg << "current container: NONE" << endl ;
424  }
425  d_attrs.dump(strm) ;
427 }
428 
429 } // namespace libdap
430 
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:146
static void UnIndent()
Definition: DapIndent.cc:49
AttrTable * get_table(AttrTable::Attr_iter &i)
Returns the referenced variable attribute table.
Definition: DAS.cc:222
void dasrestart(FILE *yyin)
virtual Attr_iter attr_end()
Definition: AttrTable.cc:649
Contains the attributes for a dataset.
Definition: AttrTable.h:146
#define not_used
Definition: config.h:521
AttrTable::Attr_iter var_begin()
Returns a reference to the attribute table for the first variable.
Definition: DAS.cc:185
virtual string get_name() const
Get the name of this attribute table.
Definition: AttrTable.cc:208
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:254
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:1155
virtual void print(FILE *out, bool dereference=false)
Definition: DAS.cc:372
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:341
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:536
string get_name(AttrTable::Attr_iter &i)
Returns the name of the referenced variable attribute table.
Definition: DAS.cc:210
virtual void erase()
Erase the attribute table.
Definition: AttrTable.cc:973
virtual unsigned int get_size() const
Returns the number of attributes in the current attribute table.
Definition: DAS.cc:158
virtual void erase()
erase all attributes in this DAS
Definition: DAS.cc:170
virtual Attr_iter attr_begin()
Definition: AttrTable.cc:640
AttrTable::Attr_iter var_end()
Definition: DAS.cc:198
static ostream & LMarg(ostream &strm)
Definition: DapIndent.cc:78
virtual void parse(string fname)
Reads a DAS from the named file.
Definition: DAS.cc:278
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:102
int dasparse(void *arg)
virtual void dump(ostream &strm) const
dumps information about this object
Definition: DAS.cc:411
void das_switch_to_buffer(void *new_buffer)
A class for error processing.
Definition: Error.h:90
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition: AttrTable.cc:200
virtual void dump(ostream &strm) const
dumps information about this object
Definition: AttrTable.cc:1309
virtual string container_name()
Returns the name of the current attribute container when multiple files used to build this DAS...
Definition: DAS.cc:109