libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
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 "AttrTable.h"
53#include "DAS.h"
54#include "DapIndent.h"
55#include "Error.h"
56#include "InternalErr.h"
57#include "debug.h"
58#include "escaping.h"
59#include "parser.h"
60
61using std::cerr;
62using std::endl;
63
64// Glue routines declared in das.lex
65extern void das_switch_to_buffer(void *new_buffer);
66extern void das_delete_buffer(void *buffer);
67extern void *das_buffer(FILE *fp);
68
69// extern void dasrestart(FILE *yyin);
70// extern int dasparse(void *arg); // defined in das.tab.c
71extern int dasparse(libdap::parser_arg *arg); // defined in das.tab.c
72
73namespace libdap {
74
75void DAS::duplicate(const DAS &src) {
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 if (this == &rhs)
88 return *this;
89 duplicate(rhs);
90 return *this;
91}
92
98void DAS::container_name(const string &cn) {
99 // We want to find a top level attribute table with the given name. So
100 // set d_container to null first so that we aren't searching some
101 // previous container
102 if (cn != d_container_name) {
103 d_container = 0;
104 if (!cn.empty()) {
105 d_container = get_table(cn);
106 if (!d_container) {
107 d_container = add_table(cn, new AttrTable);
108 }
109 }
110 d_container_name = cn;
111 }
112}
113
120unsigned int DAS::get_size() const {
121 if (d_container) {
122 return d_container->get_size();
123 }
124 return d_attrs.get_size();
125}
126
130 if (d_container) {
131 d_container->erase();
132 } else {
133 d_attrs.erase();
134 }
135}
136
140 if (d_container) {
141 return d_container->attr_begin();
142 }
143 return d_attrs.attr_begin();
144}
145
150 if (d_container) {
151 return d_container->attr_end();
152 }
153 return d_attrs.attr_end();
154}
155
159 if (d_container) {
160 return d_container->get_name(i);
161 }
162 return d_attrs.get_name(i);
163}
164
168 if (d_container) {
169 return d_container->get_attr_table(i);
170 }
171 return d_attrs.get_attr_table(i);
172}
173
176AttrTable *DAS::get_table(const string &name) {
177 if (d_container) {
178 return d_container->get_attr_table(name);
179 }
180 return d_attrs.get_attr_table(name);
181}
182
184
189
193AttrTable *DAS::add_table(const string &name, AttrTable *at) {
194 if (d_container) {
195 at->set_is_global_attribute(false);
196 return d_container->append_container(at, name);
197 }
198 return d_attrs.append_container(at, name);
199}
200
202
208
213void DAS::parse(string fname) {
214 FILE *in = fopen(fname.c_str(), "r");
215
216 if (!in) {
217 throw Error(cannot_read_file, "Could not open: " + fname);
218 }
219
220 parse(in);
221
222 int res = fclose(in);
223 if (res) {
224 DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl;);
225 }
226}
227
238void DAS::parse(int fd) {
239#ifdef WIN32
240 int new_fd = _dup(fd);
241#else
242 int new_fd = dup(fd);
243#endif
244
245 if (new_fd < 0)
246 throw InternalErr(__FILE__, __LINE__, "Could not access file.");
247 FILE *in = fdopen(new_fd, "r");
248
249 if (!in) {
250 throw InternalErr(__FILE__, __LINE__, "Could not access file.");
251 }
252
253 parse(in);
254
255 int res = fclose(in);
256 if (res) {
257 DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl;);
258 }
259}
260
267void DAS::parse(FILE *in) {
268 if (!in) {
269 throw InternalErr(__FILE__, __LINE__, "Null input stream.");
270 }
271
272 void *buffer = das_buffer(in);
273 das_switch_to_buffer(buffer);
274
275 parser_arg arg(this);
276
277 // bool status = dasparse((void *) & arg) == 0;
278 bool status = dasparse(&arg) == 0;
279
280 das_delete_buffer(buffer);
281
282 // STATUS is the result of the parser function; if a recoverable error
283 // was found it will be true but arg.status() will be false.
284 if (!status || !arg.status()) { // Check parse result
285 if (arg.error())
286 throw *arg.error();
287 }
288}
289
291
303
304void DAS::print(FILE *out, bool dereference) {
305 fprintf(out, "Attributes {\n");
306
307 d_attrs.print(out, " ", dereference);
308
309 fprintf(out, "}\n");
310}
311
323
324void DAS::print(ostream &out, bool dereference) {
325 out << "Attributes {\n";
326
327 d_attrs.print(out, " ", dereference);
328
329 out << "}\n";
330}
331
339void DAS::dump(ostream &strm) const {
340 strm << DapIndent::LMarg << "DAS::dump - (" << (void *)this << ")" << endl;
342 if (d_container) {
343 strm << DapIndent::LMarg << "current container: " << d_container_name << endl;
344 } else {
345 strm << DapIndent::LMarg << "current container: NONE" << endl;
346 }
347 d_attrs.dump(strm);
349}
350
351} // namespace libdap
int dasparse(libdap::parser_arg *arg)
void das_switch_to_buffer(void *new_buffer)
void * das_buffer(FILE *fp)
void das_delete_buffer(void *buffer)
#define cannot_read_file
(400)
Definition Error.h:68
Contains the attributes for a dataset.
Definition AttrTable.h:150
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition AttrTable.cc:517
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition AttrTable.cc:699
virtual void set_is_global_attribute(bool ga)
Definition AttrTable.h:301
std::vector< entry * >::iterator Attr_iter
Definition AttrTable.h:258
AttrTable::Attr_iter var_begin()
Returns a reference to the attribute table for the first variable.
Definition DAS.cc:139
DAS & operator=(const DAS &rhs)
Definition DAS.cc:86
virtual unsigned int get_size() const
Returns the number of attributes in the current attribute table.
Definition DAS.cc:120
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:193
virtual void print(FILE *out, bool dereference=false)
Definition DAS.cc:304
virtual void dump(ostream &strm) const
dumps information about this object
Definition DAS.cc:339
AttrTable::Attr_iter var_end()
Definition DAS.cc:149
virtual void parse(string fname)
Reads a DAS from the named file.
Definition DAS.cc:213
AttrTable * get_table(AttrTable::Attr_iter &i)
Returns the referenced variable attribute table.
Definition DAS.cc:167
virtual void erase()
erase all attributes in this DAS
Definition DAS.cc:129
string get_name(AttrTable::Attr_iter &i)
Returns the name of the referenced variable attribute table.
Definition DAS.cc:158
virtual string container_name() const
Returns the name of the current attribute container when multiple files used to build this DAS.
Definition DAS.h:146
static ostream & LMarg(ostream &strm)
Definition DapIndent.cc:61
static void Indent()
Definition DapIndent.cc:44
static void UnIndent()
Definition DapIndent.cc:46
A class for error processing.
Definition Error.h:92
A class for software fault reporting.
Definition InternalErr.h:61
#define DBG(x)
Definition debug.h:58
top level DAP object to house generic methods
Definition AISConnect.cc:30
Pass parameters by reference to a parser.
Definition parser.h:67
Error * error()
Definition parser.h:83