libdap++  Updated for version 3.11.7
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 24886 2011-09-13 22:58:35Z 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 DAS::container_name(const string &cn)
113 {
114  // We want to find a top level attribute table with the given name. So
115  // set d_container to null first so that we aren't searching some
116  // previous container
117  if (cn != _container_name) {
118  d_container = 0;
119  if (!cn.empty()) {
120  d_container = get_table(cn);
121  if (!d_container) {
122  d_container = add_table(cn, new AttrTable);
123  }
124  }
125  _container_name = cn;
126  }
127 }
128 
134 AttrTable *
136 {
137  return d_container ;
138 }
139 
146 unsigned int DAS::get_size() const
147 {
148  if (d_container) {
149  return d_container->get_size();
150  }
151  return d_attrs.get_size();
152 }
153 
157 {
158  if (d_container) {
159  d_container->erase();
160  }
161  else {
162  d_attrs.erase();
163  }
164 }
165 
169 {
170  if (d_container) {
171  return d_container->attr_begin();
172  }
173  return d_attrs.attr_begin();
174 }
175 
180 {
181  if (d_container) {
182  return d_container->attr_end();
183  }
184  return d_attrs.attr_end();
185 }
186 
190 {
191  if (d_container) {
192  return d_container->get_name(i);
193  }
194  return d_attrs.get_name(i);
195 }
196 
199 AttrTable *
201 {
202  if (d_container) {
203  return d_container->get_attr_table(i);
204  }
205  return d_attrs.get_attr_table(i);
206 }
207 
210 AttrTable *
211 DAS::get_table(const string &name)
212 {
213  if (d_container) {
214  return d_container->get_attr_table(name);
215  }
216  return d_attrs.get_attr_table(name);
217 }
218 
220 
225 
229 AttrTable *
230 DAS::add_table( const string &name, AttrTable *at )
231 {
232  if (d_container) {
233  at->set_is_global_attribute(false);
234  return d_container->append_container(at, name);
235  }
236  return d_attrs.append_container( at, name ) ;
237 }
238 
240 
246 
247 
252 void
253 DAS::parse(string fname)
254 {
255  FILE *in = fopen(fname.c_str(), "r");
256 
257  if (!in) {
258  throw Error(cannot_read_file, "Could not open: " + fname);
259  }
260 
261  parse(in);
262 
263  int res = fclose(in);
264  if (res) {
265  DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl ;) ;
266  }
267 }
268 
279 void
280 DAS::parse(int fd)
281 {
282 #ifdef WIN32
283  FILE *in = fdopen(_dup(fd), "r");
284 #else
285  FILE *in = fdopen(dup(fd), "r");
286 #endif
287 
288  if (!in) {
289  throw InternalErr(__FILE__, __LINE__, "Could not access file.");
290  }
291 
292  parse(in);
293 
294  int res = fclose(in);
295  if (res) {
296  DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl ;) ;
297  }
298 }
299 
300 
301 
308 void
309 DAS::parse(FILE *in)
310 {
311  if (!in) {
312  throw InternalErr(__FILE__, __LINE__, "Null input stream.");
313  }
314 
315  void *buffer = das_buffer(in);
316  das_switch_to_buffer(buffer);
317 
318  parser_arg arg(this);
319 
320  bool status = dasparse((void *) & arg) == 0;
321 
322  das_delete_buffer(buffer);
323 
324  // STATUS is the result of the parser function; if a recoverable error
325  // was found it will be true but arg.status() will be false.
326  if (!status || !arg.status()) {// Check parse result
327  if (arg.error())
328  throw *arg.error();
329  }
330 }
331 
333 
346 void
347 DAS::print(FILE *out, bool dereference)
348 {
349  fprintf(out, "Attributes {\n") ;
350 
351  d_attrs.print(out, " ", dereference);
352 
353  fprintf(out, "}\n") ;
354 }
355 
368 void
369 DAS::print(ostream &out, bool dereference)
370 {
371  out << "Attributes {\n" ;
372 
373  d_attrs.print(out, " ", dereference);
374 
375  out << "}\n" ;
376 }
377 
385 void
386 DAS::dump(ostream &strm) const
387 {
388  strm << DapIndent::LMarg << "DAS::dump - ("
389  << (void *)this << ")" << endl ;
391  if( d_container )
392  {
393  strm << DapIndent::LMarg << "current container: " << _container_name
394  << endl ;
395  }
396  else
397  {
398  strm << DapIndent::LMarg << "current container: NONE" << endl ;
399  }
400  d_attrs.dump(strm) ;
402 }
403 
404 } // namespace libdap
405