libdap Updated for version 3.18.1
DAS.cc
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
60using std::cerr;
61using std::endl;
62
63// Glue routines declared in das.lex
64extern void das_switch_to_buffer(void *new_buffer);
65extern void das_delete_buffer(void * buffer);
66extern void *das_buffer(FILE *fp);
67
68//extern void dasrestart(FILE *yyin);
69//extern int dasparse(void *arg); // defined in das.tab.c
70extern int dasparse(libdap::parser_arg *arg); // defined in das.tab.c
71
72namespace libdap {
73
74void DAS::duplicate(const DAS &src)
75{
76 // If the container field is set, perform a deep copy
77 if (src.d_container)
78 d_container = new AttrTable(*src.d_container);
79 else
80 d_container = 0;
81
82 d_container_name = src.d_container_name;
83 d_attrs = src.d_attrs;
84}
85
86DAS &DAS::operator=(const DAS &rhs)
87{
88 if (this == &rhs)
89 return *this;
90
91 duplicate(rhs);
92
93 return *this;
94}
95
101void DAS::container_name(const string &cn)
102{
103 // We want to find a top level attribute table with the given name. So
104 // set d_container to null first so that we aren't searching some
105 // previous container
106 if (cn != d_container_name) {
107 d_container = 0;
108 if (!cn.empty()) {
109 d_container = get_table(cn);
110 if (!d_container) {
111 d_container = add_table(cn, new AttrTable);
112 }
113 }
114 d_container_name = cn;
115 }
116}
117
124unsigned int DAS::get_size() const
125{
126 if (d_container) {
127 return d_container->get_size();
128 }
129 return d_attrs.get_size();
130}
131
135{
136 if (d_container) {
137 d_container->erase();
138 }
139 else {
140 d_attrs.erase();
141 }
142}
143
146AttrTable::Attr_iter DAS::var_begin()
147{
148 if (d_container) {
149 return d_container->attr_begin();
150 }
151 return d_attrs.attr_begin();
152}
153
157AttrTable::Attr_iter DAS::var_end()
158{
159 if (d_container) {
160 return d_container->attr_end();
161 }
162 return d_attrs.attr_end();
163}
164
167string DAS::get_name(AttrTable::Attr_iter &i)
168{
169 if (d_container) {
170 return d_container->get_name(i);
171 }
172 return d_attrs.get_name(i);
173}
174
177AttrTable *
178DAS::get_table(AttrTable::Attr_iter &i)
179{
180 if (d_container) {
181 return d_container->get_attr_table(i);
182 }
183 return d_attrs.get_attr_table(i);
184}
185
188AttrTable *
189DAS::get_table(const string &name)
190{
191 if (d_container) {
192 return d_container->get_attr_table(name);
193 }
194 return d_attrs.get_attr_table(name);
195}
196
198
203
207AttrTable *
208DAS::add_table( const string &name, AttrTable *at )
209{
210 if (d_container) {
211 at->set_is_global_attribute(false);
212 return d_container->append_container(at, name);
213 }
214 return d_attrs.append_container( at, name ) ;
215}
216
218
224
225
230void
231DAS::parse(string fname)
232{
233 FILE *in = fopen(fname.c_str(), "r");
234
235 if (!in) {
236 throw Error(cannot_read_file, "Could not open: " + fname);
237 }
238
239 parse(in);
240
241 int res = fclose(in);
242 if (res) {
243 DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl ;) ;
244 }
245}
246
257void
259{
260#ifdef WIN32
261 int new_fd = _dup(fd);
262#else
263 int new_fd = dup(fd);
264#endif
265
266 if (new_fd < 0)
267 throw InternalErr(__FILE__, __LINE__, "Could not access file.");
268 FILE *in = fdopen(new_fd, "r");
269
270 if (!in) {
271 throw InternalErr(__FILE__, __LINE__, "Could not access file.");
272 }
273
274 parse(in);
275
276 int res = fclose(in);
277 if (res) {
278 DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl ;) ;
279 }
280}
281
282
283
290void
291DAS::parse(FILE *in)
292{
293 if (!in) {
294 throw InternalErr(__FILE__, __LINE__, "Null input stream.");
295 }
296
297 void *buffer = das_buffer(in);
298 das_switch_to_buffer(buffer);
299
300 parser_arg arg(this);
301
302 //bool status = dasparse((void *) & arg) == 0;
303 bool status = dasparse(&arg) == 0;
304
305 das_delete_buffer(buffer);
306
307 // STATUS is the result of the parser function; if a recoverable error
308 // was found it will be true but arg.status() will be false.
309 if (!status || !arg.status()) {// Check parse result
310 if (arg.error())
311 throw *arg.error();
312 }
313}
314
316
329void
330DAS::print(FILE *out, bool dereference)
331{
332 fprintf(out, "Attributes {\n") ;
333
334 d_attrs.print(out, " ", dereference);
335
336 fprintf(out, "}\n") ;
337}
338
351void
352DAS::print(ostream &out, bool dereference)
353{
354 out << "Attributes {\n" ;
355
356 d_attrs.print(out, " ", dereference);
357
358 out << "}\n" ;
359}
360
368void DAS::dump(ostream &strm) const
369{
370 strm << DapIndent::LMarg << "DAS::dump - (" << (void *) this << ")" << endl;
371 DapIndent::Indent();
372 if (d_container) {
373 strm << DapIndent::LMarg << "current container: " << d_container_name << endl;
374 }
375 else {
376 strm << DapIndent::LMarg << "current container: NONE" << endl;
377 }
378 d_attrs.dump(strm);
379 DapIndent::UnIndent();
380}
381
382} // namespace libdap
383
Contains the attributes for a dataset.
Definition: AttrTable.h:143
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition: AttrTable.cc:409
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition: AttrTable.cc:606
virtual Attr_iter attr_end()
Definition: AttrTable.cc:718
virtual Attr_iter attr_begin()
Definition: AttrTable.cc:710
virtual string get_name() const
Get the name of this attribute table.
Definition: AttrTable.cc:237
virtual void erase()
Erase the attribute table.
Definition: AttrTable.cc:1035
virtual void print(FILE *out, string pad=" ", bool dereference=false)
Prints the attribute table.
Definition: AttrTable.cc:1242
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition: AttrTable.cc:230
virtual void dump(ostream &strm) const
dumps information about this object
Definition: AttrTable.cc:1509
AttrTable::Attr_iter var_begin()
Returns a reference to the attribute table for the first variable.
Definition: DAS.cc:146
virtual unsigned int get_size() const
Returns the number of attributes in the current attribute table.
Definition: DAS.cc:124
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:208
virtual void print(FILE *out, bool dereference=false)
Definition: DAS.cc:330
virtual void dump(ostream &strm) const
dumps information about this object
Definition: DAS.cc:368
AttrTable::Attr_iter var_end()
Definition: DAS.cc:157
virtual void parse(string fname)
Reads a DAS from the named file.
Definition: DAS.cc:231
AttrTable * get_table(AttrTable::Attr_iter &i)
Returns the referenced variable attribute table.
Definition: DAS.cc:178
virtual void erase()
erase all attributes in this DAS
Definition: DAS.cc:134
string get_name(AttrTable::Attr_iter &i)
Returns the name of the referenced variable attribute table.
Definition: DAS.cc:167
virtual string container_name() const
Returns the name of the current attribute container when multiple files used to build this DAS.
Definition: DAS.h:149
A class for error processing.
Definition: Error.h:91
A class for software fault reporting.
Definition: InternalErr.h:65
Pass parameters by reference to a parser.
Definition: parser.h:69