-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDAS.cc
351 lines (285 loc) · 9.14 KB
/
DAS.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <[email protected]>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
// (c) COPYRIGHT URI/MIT 1994-1999
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
// jhrg,jimg James Gallagher <[email protected]>
// Methods for the class DAS - a class used to parse the dataset attribute
// structure.
//
// jhrg 7/25/94
#include "config.h"
#include <cstdio>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef WIN32
#include <io.h>
#endif
#include <iostream>
#include <string>
#include "AttrTable.h"
#include "DAS.h"
#include "DapIndent.h"
#include "Error.h"
#include "InternalErr.h"
#include "debug.h"
#include "escaping.h"
#include "parser.h"
using std::cerr;
using std::endl;
// Glue routines declared in das.lex
extern void das_switch_to_buffer(void *new_buffer);
extern void das_delete_buffer(void *buffer);
extern void *das_buffer(FILE *fp);
// extern void dasrestart(FILE *yyin);
// extern int dasparse(void *arg); // defined in das.tab.c
extern int dasparse(libdap::parser_arg *arg); // defined in das.tab.c
namespace libdap {
void DAS::duplicate(const DAS &src) {
// If the container field is set, perform a deep copy
if (src.d_container)
d_container = new AttrTable(*src.d_container);
else
d_container = 0;
d_container_name = src.d_container_name;
d_attrs = src.d_attrs;
}
DAS &DAS::operator=(const DAS &rhs) {
if (this == &rhs)
return *this;
duplicate(rhs);
return *this;
}
/** @brief Sets the name of the current attribute container when multiple
* files used to build this DAS.
*
* @param cn container name
*/
void DAS::container_name(const string &cn) {
// We want to find a top level attribute table with the given name. So
// set d_container to null first so that we aren't searching some
// previous container
if (cn != d_container_name) {
d_container = 0;
if (!cn.empty()) {
d_container = get_table(cn);
if (!d_container) {
d_container = add_table(cn, new AttrTable);
}
}
d_container_name = cn;
}
}
/** @brief Returns the number of attributes in the current attribute table
*
* If the there is a container set, then return the number of variable
* attribute tables for the current container. If not set then return the
* number of current attribute tables in the outermost attribute table.
*/
unsigned int DAS::get_size() const {
if (d_container) {
return d_container->get_size();
}
return d_attrs.get_size();
}
/** @brief erase all attributes in this DAS
*/
void DAS::erase() {
if (d_container) {
d_container->erase();
} else {
d_attrs.erase();
}
}
/** @brief Returns a reference to the attribute table for the first variable.
*/
AttrTable::Attr_iter DAS::var_begin() {
if (d_container) {
return d_container->attr_begin();
}
return d_attrs.attr_begin();
}
/** Returns a reference to the end of the attribute table. Does not
* point to an attribute table.
*/
AttrTable::Attr_iter DAS::var_end() {
if (d_container) {
return d_container->attr_end();
}
return d_attrs.attr_end();
}
/** @brief Returns the name of the referenced variable attribute table.
*/
string DAS::get_name(AttrTable::Attr_iter &i) {
if (d_container) {
return d_container->get_name(i);
}
return d_attrs.get_name(i);
}
/** @brief Returns the referenced variable attribute table.
*/
AttrTable *DAS::get_table(AttrTable::Attr_iter &i) {
if (d_container) {
return d_container->get_attr_table(i);
}
return d_attrs.get_attr_table(i);
}
/** @brief Returns the variable attribute table with the given name.
*/
AttrTable *DAS::get_table(const string &name) {
if (d_container) {
return d_container->get_attr_table(name);
}
return d_attrs.get_attr_table(name);
}
//@}
/** @brief Adds an attribute table to the DAS.
@name add_table()
*/
//@{
/** @brief Adds a variable attribute table to the DAS or the current
* dataset container attribute table.
*/
AttrTable *DAS::add_table(const string &name, AttrTable *at) {
if (d_container) {
at->set_is_global_attribute(false);
return d_container->append_container(at, name);
}
return d_attrs.append_container(at, name);
}
//@}
/** @brief Reads a DAS in from an external source.
@name parse()
*/
//@{
/** @brief Reads a DAS from the named file.
Read attributes from a file. Throws Error if unable to open
the file, otherwise parses the file. */
void DAS::parse(string fname) {
FILE *in = fopen(fname.c_str(), "r");
if (!in) {
throw Error(cannot_read_file, "Could not open: " + fname);
}
parse(in);
int res = fclose(in);
if (res) {
DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl;);
}
}
/** @brief Read attributes from a file descriptor.
If the file descriptor cannot be fdopen'd, return false, otherwise
return the status of the mfunc parse.
\note Added call to dup() within fdopen so that once the FILE * is
closed the decriptor fd will not also be closed (instead the
duplicate descriptor will be closed). Thus further information can
be read from the descriptor fd.
*/
void DAS::parse(int fd) {
#ifdef WIN32
int new_fd = _dup(fd);
#else
int new_fd = dup(fd);
#endif
if (new_fd < 0)
throw InternalErr(__FILE__, __LINE__, "Could not access file.");
FILE *in = fdopen(new_fd, "r");
if (!in) {
throw InternalErr(__FILE__, __LINE__, "Could not access file.");
}
parse(in);
int res = fclose(in);
if (res) {
DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl;);
}
}
/** @brief Reads a DAS from an open file descriptor.
Read attributes from in (which defaults to stdin). If
dasrestart() fails, return false, otherwise return the status
of dasparse().
*/
void DAS::parse(FILE *in) {
if (!in) {
throw InternalErr(__FILE__, __LINE__, "Null input stream.");
}
void *buffer = das_buffer(in);
das_switch_to_buffer(buffer);
parser_arg arg(this);
// bool status = dasparse((void *) & arg) == 0;
bool status = dasparse(&arg) == 0;
das_delete_buffer(buffer);
// STATUS is the result of the parser function; if a recoverable error
// was found it will be true but arg.status() will be false.
if (!status || !arg.status()) { // Check parse result
if (arg.error())
throw *arg.error();
}
}
//@}
/** Creates an ASCII representation of a DAS on the given output
stream.
When an identifier contains a character that contains
characters that cannot be present in a URL (e.g., a space)
AttrTable::print replaces those characters with WWW
escape codes. 7/13/2001 jhrg
@param out output FILE on which to print the DAS
@param dereference If true, follow aliases. Default is false.
*/
void DAS::print(FILE *out, bool dereference) {
fprintf(out, "Attributes {\n");
d_attrs.print(out, " ", dereference);
fprintf(out, "}\n");
}
/** Creates an ASCII representation of a DAS on the given output
stream.
When an identifier contains a character that contains
characters that cannot be present in a URL (e.g., a space)
AttrTable::print replaces those characters with WWW
escape codes. 7/13/2001 jhrg
@param out output ostream on which to print the DAS
@param dereference If true, follow aliases. Default is false.
*/
void DAS::print(ostream &out, bool dereference) {
out << "Attributes {\n";
d_attrs.print(out, " ", dereference);
out << "}\n";
}
/** @brief dumps information about this object
*
* Displays the pointer value of this instance and then calls parent dump
*
* @param strm C++ i/o stream to dump the information to
* @return void
*/
void DAS::dump(ostream &strm) const {
strm << DapIndent::LMarg << "DAS::dump - (" << (void *)this << ")" << endl;
DapIndent::Indent();
if (d_container) {
strm << DapIndent::LMarg << "current container: " << d_container_name << endl;
} else {
strm << DapIndent::LMarg << "current container: NONE" << endl;
}
d_attrs.dump(strm);
DapIndent::UnIndent();
}
} // namespace libdap