/
Structure.cc
608 lines (515 loc) · 15 KB
/
Structure.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// -*- 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 <jgallagher@opendap.org>
//
// 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 <jgallagher@gso.uri.edu>
// Implementation for the class Structure
//
// jhrg 9/14/94
//#define DODS_DEBUG
#include "config.h"
#include <sstream>
#include "Byte.h"
#include "Int16.h"
#include "UInt16.h"
#include "Int32.h"
#include "UInt32.h"
#include "Float32.h"
#include "Float64.h"
#include "Str.h"
#include "Url.h"
#include "Array.h"
#include "Structure.h"
#include "Sequence.h"
#include "Grid.h"
#include "DDS.h"
#include "ConstraintEvaluator.h"
#include "XDRStreamMarshaller.h"
#include "util.h"
#include "debug.h"
#include "InternalErr.h"
#include "escaping.h"
using std::cerr;
using std::endl;
namespace libdap {
#if 0
/** This method is protected so it's hidden from the whole world, but
* available to direct child classes. Because of that, we need a glue-routine
* here so children of Structure can specialize it.
*/
void
Structure::m_duplicate(const Structure &s)
{
Constructor::m_duplicate(s);
#if 0
Structure &cs = const_cast<Structure &>(s);
DBG(cerr << "Copying structure: " << name() << endl);
for (Vars_iter i = cs.d_vars.begin(); i != cs.d_vars.end(); i++) {
DBG(cerr << "Copying field: " << (*i)->name() << endl);
// Jose Garcia
// I think this assert here is part of a debugging
// process since it is going along with a DBG call
// I leave it here since it can be remove by defining NDEBUG.
// assert(*i);
BaseType *btp = (*i)->ptr_duplicate();
btp->set_parent(this);
d_vars.push_back(btp);
}
#endif
}
#endif
/** The Structure constructor requires only the name of the variable
to be created. The name may be omitted, which will create a
nameless variable. This may be adequate for some applications.
@param n A string containing the name of the variable to be
created.
*/
Structure::Structure(const string &n) : Constructor(n, dods_structure_c)
{}
/** The Structure server-side constructor requires the name of the variable
to be created and the dataset name from which this variable is being
created. Used on server-side handlers.
@param n A string containing the name of the variable to be
created.
@param d A string containing the name of the dataset from which this
variable is being created.
*/
Structure::Structure(const string &n, const string &d)
: Constructor(n, d, dods_structure_c)
{}
/** The Structure copy constructor. */
Structure::Structure(const Structure &rhs) : Constructor(rhs)
{
m_duplicate(rhs);
}
Structure::~Structure()
{
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
BaseType *btp = *i ;
delete btp ; btp = 0;
}
}
BaseType *
Structure::ptr_duplicate()
{
return new Structure(*this);
}
Structure &
Structure::operator=(const Structure &rhs)
{
if (this == &rhs)
return *this;
dynamic_cast<Constructor &>(*this) = rhs; // run Constructor=
m_duplicate(rhs);
return *this;
}
#if 0
int
Structure::element_count(bool leaves)
{
if (!leaves)
return d_vars.size();
else {
int i = 0;
for (Vars_iter j = d_vars.begin(); j != d_vars.end(); j++) {
i += (*j)->element_count(leaves);
}
return i;
}
}
#endif
bool
Structure::is_linear()
{
bool linear = true;
for (Vars_iter i = d_vars.begin(); linear && i != d_vars.end(); i++) {
if ((*i)->type() == dods_structure_c)
linear = linear && static_cast<Structure*>((*i))->is_linear();
else
linear = linear && (*i)->is_simple_type();
}
return linear;
}
#if 0
void
Structure::set_send_p(bool state)
{
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
(*i)->set_send_p(state);
}
BaseType::set_send_p(state);
}
void
Structure::set_read_p(bool state)
{
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
(*i)->set_read_p(state);
}
BaseType::set_read_p(state);
}
#endif
#if 0
/** Set the \e in_selection property for this variable and all of its
children.
@brief Set the \e in_selection property.
@param state Set the property value to \e state. */
void
Structure::set_in_selection(bool state)
{
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
(*i)->set_in_selection(state);
}
BaseType::set_in_selection(state);
}
#endif
/** @brief Traverse Structure, set Sequence leaf nodes. */
void
Structure::set_leaf_sequence(int level)
{
for (Vars_iter i = var_begin(); i != var_end(); i++) {
if ((*i)->type() == dods_sequence_c)
static_cast<Sequence&>(**i).set_leaf_sequence(++level);
else if ((*i)->type() == dods_structure_c)
static_cast<Structure&>(**i).set_leaf_sequence(level);
}
}
#if 0
/** Adds an element to a Structure.
@param bt A pointer to the DAP2 type variable to add to this Structure.
@param part Not used by this class, defaults to nil */
void
Structure::add_var(BaseType *bt, Part)
{
// Jose Garcia
// Passing and invalid pointer to an object is a developer's error.
if (!bt)
throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
if (bt->is_dap4_only_type())
throw InternalErr(__FILE__, __LINE__, "Attempt to add a DAP4 type to a DAP2 Structure.");
// Jose Garcia
// Now we add a copy of bt so the external user is able to destroy bt as
// he/she wishes. The policy is: "If it is allocated outside, it is
// deallocated outside, if it is allocated inside, it is deallocated
// inside"
BaseType *btp = bt->ptr_duplicate();
btp->set_parent(this);
d_vars.push_back(btp);
}
/** Adds an element to a Structure.
@param bt A pointer to the DAP2 type variable to add to this Structure.
@param part Not used by this class, defaults to nil */
void
Structure::add_var_nocopy(BaseType *bt, Part)
{
if (!bt)
throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
if (bt->is_dap4_only_type())
throw InternalErr(__FILE__, __LINE__, "Attempt to add a DAP4 type to a DAP2 Structure.");
bt->set_parent(this);
d_vars.push_back(bt);
}
/** Removed an element from a Structure.
@param n name of the variable to remove */
void
Structure::del_var(const string &n)
{
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
if ((*i)->name() == n) {
BaseType *bt = *i ;
d_vars.erase(i) ;
delete bt ; bt = 0;
return;
}
}
}
#endif
#if 0
/** @brief simple implementation of read that iterates through vars
* and calls read on them
*
* @return returns false to signify all has been read
*/
bool Structure::read()
{
if (!read_p()) {
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
(*i)->read();
}
set_read_p(true);
}
return false;
}
#endif
#if 0
// TODO Recode to use width(bool)
unsigned int
Structure::width()
{
unsigned int sz = 0;
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
sz += (*i)->width();
}
return sz;
}
/** This version of width simply returns the same thing as width() for simple
types and Arrays. For Structure it returns the total size if constrained
is false, or the size of the elements in the current projection if true.
@param constrained If true, return the size after applying a constraint.
@return The number of bytes used by the variable.
*/
unsigned int
Structure::width(bool constrained)
{
unsigned int sz = 0;
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
if (constrained) {
if ((*i)->send_p())
sz += (*i)->width(constrained);
}
else {
sz += (*i)->width(constrained);
}
}
return sz;
}
#endif
#if 0
void
Structure::intern_data(ConstraintEvaluator & eval, DDS & dds)
{
DBG(cerr << "Structure::intern_data: " << name() << endl);
if (!read_p())
read(); // read() throws Error and InternalErr
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
if ((*i)->send_p()) {
(*i)->intern_data(eval, dds);
}
}
}
bool
Structure::serialize(ConstraintEvaluator &eval, DDS &dds,
Marshaller &m, bool ce_eval)
{
dds.timeout_on();
if (!read_p())
read(); // read() throws Error and InternalErr
#if EVAL
if (ce_eval && !eval.eval_selection(dds, dataset()))
return true;
#endif
dds.timeout_off();
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
if ((*i)->send_p()) {
#ifdef CHECKSUMS
XDRStreamMarshaller *sm = dynamic_cast<XDRStreamMarshaller*>(&m);
if (sm && sm->checksums() && (*i)->type() != dods_structure_c && (*i)->type() != dods_grid_c)
sm->reset_checksum();
(*i)->serialize(eval, dds, m, false);
if (sm && sm->checksums() && (*i)->type() != dods_structure_c && (*i)->type() != dods_grid_c)
sm->get_checksum();
#else
(*i)->serialize(eval, dds, m, false);
#endif
}
}
return true;
}
bool
Structure::deserialize(UnMarshaller &um, DDS *dds, bool reuse)
{
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
(*i)->deserialize(um, dds, reuse);
}
return false;
}
#endif
#if 0
/** @brief Never call this
This method cannot be used to change values of a Structure since
the values of a Constructor type must be set using methods in
Constructor. See the Constructor::var_begin() and related
methods.
@todo Make this throw an exception
@return Returns the size of the structure. */
unsigned int
Structure::val2buf(void *, bool)
{
return sizeof(Structure);
}
/** @brief Never call this
@see val2buf()
@return Returns the size of the structure. */
unsigned int
Structure::buf2val(void **)
{
return sizeof(Structure);
}
#endif
#if 0
BaseType *
Structure::var(const string &name, bool exact_match, btp_stack *s)
{
string n = www2id(name);
if (exact_match)
return m_exact_match(n, s);
else
return m_leaf_match(n, s);
}
/** @deprecated See comment in BaseType */
BaseType *
Structure::var(const string &n, btp_stack &s)
{
string name = www2id(n);
BaseType *btp = m_exact_match(name, &s);
if (btp)
return btp;
return m_leaf_match(name, &s);
}
#endif
#if 0
// Private method to find a variable using the shorthand name. This
// should be moved to Constructor.
BaseType *
Structure::m_leaf_match(const string &name, btp_stack *s)
{
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
if ((*i)->name() == name) {
if (s) {
DBG(cerr << "Pushing " << this->name() << endl);
s->push(static_cast<BaseType *>(this));
}
return *i;
}
if ((*i)->is_constructor_type()) {
BaseType *btp = (*i)->var(name, false, s);
if (btp) {
if (s) {
DBG(cerr << "Pushing " << this->name() << endl);
s->push(static_cast<BaseType *>(this));
}
return btp;
}
}
}
return 0;
}
// Breadth-first search for NAME. If NAME contains one or more dots (.)
// TODO The btp_stack is not needed since there are 'back pointers' in
// BaseType.
BaseType *
Structure::m_exact_match(const string &name, btp_stack *s)
{
// Look for name at the top level first.
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
if ((*i)->name() == name) {
if (s)
s->push(static_cast<BaseType *>(this));
return *i;
}
}
// If it was not found using the simple search, look for a dot and
// search the hierarchy.
string::size_type dot_pos = name.find("."); // zero-based index of `.'
if (dot_pos != string::npos) {
string aggregate = name.substr(0, dot_pos);
string field = name.substr(dot_pos + 1);
BaseType *agg_ptr = var(aggregate);
if (agg_ptr) {
if (s)
s->push(static_cast<BaseType *>(this));
return agg_ptr->var(field, true, s); // recurse
}
else
return 0; // qualified names must be *fully* qualified
}
return 0;
}
#endif
// TODO Can these be removed and the versions in Constructor used instead?
// Yes.
#if 0
void
Structure::print_val(FILE *out, string space, bool print_decl_p)
{
ostringstream oss;
print_val(oss, space, print_decl_p);
fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
}
void
Structure::print_val(ostream &out, string space, bool print_decl_p)
{
if (print_decl_p) {
print_decl(out, space, false);
out << " = " ;
}
out << "{ " ;
for (Vars_citer i = d_vars.begin(); i != d_vars.end();
i++, (void)(i != d_vars.end() && out << ", ")) {
(*i)->print_val(out, "", false);
}
out << " }" ;
if (print_decl_p)
out << ";\n" ;
}
#endif
#if 0
bool
Structure::check_semantics(string &msg, bool all)
{
if (!BaseType::check_semantics(msg))
return false;
bool status = true;
if (!unique_names(d_vars, name(), type_name(), msg))
return false;
if (all) {
for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
//assert(*i);
if (!(*i)->check_semantics(msg, true)) {
status = false;
goto exit;
}
}
}
exit:
return status;
}
#endif
/** @brief dumps information about this object
*
* Displays the pointer value of this instance and information about this
* instance.
*
* @param strm C++ i/o stream to dump the information to
* @return void
*/
void
Structure::dump(ostream &strm) const
{
strm << DapIndent::LMarg << "Structure::dump - ("
<< (void *)this << ")" << endl ;
DapIndent::Indent() ;
Constructor::dump(strm) ;
DapIndent::UnIndent() ;
}
} // namespace libdap