libdap  Updated for version 3.20.10
libdap4 is an implementation of OPeNDAP's DAP protocol.
Structure.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 // Implementation for the class Structure
33 //
34 // jhrg 9/14/94
35 
36 //#define DODS_DEBUG
37 
38 #include "config.h"
39 
40 #include <sstream>
41 
42 #include "Byte.h"
43 #include "Int16.h"
44 #include "UInt16.h"
45 #include "Int32.h"
46 #include "UInt32.h"
47 #include "Float32.h"
48 #include "Float64.h"
49 #include "Str.h"
50 #include "Url.h"
51 #include "Array.h"
52 #include "Structure.h"
53 #include "Sequence.h"
54 #include "Grid.h"
55 
56 #include "DDS.h"
57 #include "ConstraintEvaluator.h"
58 
59 #include "D4Attributes.h"
60 #include "D4Group.h"
61 
62 #include "XDRStreamMarshaller.h"
63 #include "util.h"
64 #include "debug.h"
65 #include "InternalErr.h"
66 #include "escaping.h"
67 #include "DapIndent.h"
68 
69 using std::cerr;
70 using std::endl;
71 
72 namespace libdap {
73 
74 #if 0
79 void
80 Structure::m_duplicate(const Structure &s)
81 {
82  Constructor::m_duplicate(s);
83 #if 0
84  Structure &cs = const_cast<Structure &>(s);
85 
86  DBG(cerr << "Copying structure: " << name() << endl);
87 
88  for (Vars_iter i = cs.d_vars.begin(); i != cs.d_vars.end(); i++) {
89  DBG(cerr << "Copying field: " << (*i)->name() << endl);
90  // Jose Garcia
91  // I think this assert here is part of a debugging
92  // process since it is going along with a DBG call
93  // I leave it here since it can be remove by defining NDEBUG.
94  // assert(*i);
95  BaseType *btp = (*i)->ptr_duplicate();
96  btp->set_parent(this);
97  d_vars.push_back(btp);
98  }
99 #endif
100 }
101 #endif
102 
110 Structure::Structure(const string &n) : Constructor(n, dods_structure_c)
111 {}
112 
122 Structure::Structure(const string &n, const string &d)
123  : Constructor(n, d, dods_structure_c)
124 {}
125 
128 {
129  DBG(cerr << "In Structure::copy_ctor for " << name() << endl);
130  //m_duplicate(rhs);
131 }
132 
133 Structure::~Structure()
134 {
135 }
136 
137 BaseType *
139 {
140  return new Structure(*this);
141 }
142 
152 void
154 {
155  DBG(cerr << __func__ <<"() - BEGIN" << endl;);
156  // Here we create a new Structure and then use it
157  // as the target container for the transformed versions of
158  // all the member variables by calling Constructor::transform_to_dap4() and
159  // passing our new target Structure in as the target container.
160  Structure *dest = new Structure(name());
161  DBG(cerr << __func__ <<"() - Calling Constructor::transform_to_dap4("<<
162  "'" << root->name() << "':" << (void*)root << ","
163  "'" << dest->name() << "':" << (void*)dest << ")"
164  << endl; );
165  Constructor::transform_to_dap4(root, dest);
166  container->add_var_nocopy(dest);
167  DBG(cerr << __func__ <<"() - Added new Structure '" << dest->name() << "' (" << (void*)dest <<
168  ") to the container '" << container->name() <<"'" << endl;);
169  DBG(cerr << __func__ <<"() - END"<< endl;);
170 }
171 
172 
179 vector<BaseType *> *
181 {
182  DBG(cerr << " " << __func__ << " BEGIN" << endl);
183  Structure *dest = new Structure(name());
184 
185  // convert the Structure's d4 attributes to a dap2 attribute table.
186 #if 0
187  AttrTable *attrs = this->attributes()->get_AttrTable(name());
188 #else
189  if (dest->get_attr_table().get_size() == 0) {
191  dest->get_attr_table().set_name(name());
192  }
193 #endif
194 
195  dest->set_is_dap4(false);
196 
197  vector<BaseType *> dropped_vars;
198  for (Structure::Vars_citer i = var_begin(), e = var_end(); i != e; ++i) {
199  vector<BaseType *> *new_vars = (*i)->transform_to_dap2(&dest->get_attr_table() /*attrs*/);
200  if (new_vars) { // Might be un-mappable
201  // It's not so game on..
202 #if 0
203  vector<BaseType*>::iterator vIter = new_vars->begin();
204  vector<BaseType*>::iterator end = new_vars->end();
205 #endif
206  for (vector<BaseType*>::iterator vi = new_vars->begin(), ve = new_vars->end(); vi != ve ; ++vi ) {
207  BaseType *new_var = (*vi);
208  new_var->set_parent(dest);
209  dest->add_var_nocopy(new_var);
210  (*vi) = NULL;
211  }
212  delete new_vars;
213 
214  }
215  else {
216  // Got a NULL, so we are dropping this var.
217  dropped_vars.push_back(*i);
218  }
219  }
220 
221  AttrTable *dv_attr_table = make_dropped_vars_attr_table(&dropped_vars);
222  if(dv_attr_table){
223  dest->get_attr_table().append_container(dv_attr_table,dv_attr_table->get_name());
224  }
225 
226  DBG(attrs->print(cerr,"",true););
227 
228 #if 0
229  // Since this does a copy we gotta delete the attrs when done
230  // FIXME This is a bug. jhrg 6/17/19
231  dest->set_attr_table(*attrs);
232  delete attrs;
233 #endif
234 
235  vector<BaseType *> *result = new vector<BaseType *>();
236  result->push_back(dest);
237 
238  DBG(cerr << " " << __func__ << " END" << endl);
239 
240  return result;
241 }
242 
243 
244 
245 
246 
247 Structure &
248 Structure::operator=(const Structure &rhs)
249 {
250  if (this == &rhs)
251  return *this;
252  Constructor::operator=(rhs);
253  return *this;
254 }
255 
256 bool
258 {
259  bool linear = true;
260  for (Vars_iter i = d_vars.begin(); linear && i != d_vars.end(); i++) {
261  if ((*i)->type() == dods_structure_c)
262  linear = linear && static_cast<Structure*>((*i))->is_linear();
263  else
264  linear = linear && (*i)->is_simple_type();
265  }
266 
267  return linear;
268 }
269 
270 #if 0
271 void
272 Structure::set_send_p(bool state)
273 {
274  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
275  (*i)->set_send_p(state);
276  }
277 
278  BaseType::set_send_p(state);
279 }
280 
281 void
282 Structure::set_read_p(bool state)
283 {
284  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
285  (*i)->set_read_p(state);
286  }
287 
288  BaseType::set_read_p(state);
289 }
290 #endif
291 #if 0
297 void
298 Structure::set_in_selection(bool state)
299 {
300  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
301  (*i)->set_in_selection(state);
302  }
303 
305 }
306 #endif
308 void
310 {
311  for (Vars_iter i = var_begin(); i != var_end(); i++) {
312  if ((*i)->type() == dods_sequence_c)
313  static_cast<Sequence&>(**i).set_leaf_sequence(++level);
314  else if ((*i)->type() == dods_structure_c)
315  static_cast<Structure&>(**i).set_leaf_sequence(level);
316  }
317 }
318 
319 #if 0
324 void
326 {
327  // Jose Garcia
328  // Passing and invalid pointer to an object is a developer's error.
329  if (!bt)
330  throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
331 
332  if (bt->is_dap4_only_type())
333  throw InternalErr(__FILE__, __LINE__, "Attempt to add a DAP4 type to a DAP2 Structure.");
334 
335  // Jose Garcia
336  // Now we add a copy of bt so the external user is able to destroy bt as
337  // he/she wishes. The policy is: "If it is allocated outside, it is
338  // deallocated outside, if it is allocated inside, it is deallocated
339  // inside"
340  BaseType *btp = bt->ptr_duplicate();
341  btp->set_parent(this);
342  d_vars.push_back(btp);
343 }
344 
349 void
350 Structure::add_var_nocopy(BaseType *bt, Part)
351 {
352  if (!bt)
353  throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
354 
355  if (bt->is_dap4_only_type())
356  throw InternalErr(__FILE__, __LINE__, "Attempt to add a DAP4 type to a DAP2 Structure.");
357 
358  bt->set_parent(this);
359  d_vars.push_back(bt);
360 }
361 
362 
366 void
367 Structure::del_var(const string &n)
368 {
369  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
370  if ((*i)->name() == n) {
371  BaseType *bt = *i ;
372  d_vars.erase(i) ;
373  delete bt ; bt = 0;
374  return;
375  }
376  }
377 }
378 #endif
379 #if 0
385 bool Structure::read()
386 {
387  if (!read_p()) {
388  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
389  (*i)->read();
390  }
391  set_read_p(true);
392  }
393 
394  return false;
395 }
396 #endif
397 #if 0
398 // TODO Recode to use width(bool)
399 unsigned int
401 {
402  unsigned int sz = 0;
403 
404  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
405  sz += (*i)->width();
406  }
407 
408  return sz;
409 }
410 
418 unsigned int
419 Structure::width(bool constrained)
420 {
421  unsigned int sz = 0;
422 
423  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
424  if (constrained) {
425  if ((*i)->send_p())
426  sz += (*i)->width(constrained);
427  }
428  else {
429  sz += (*i)->width(constrained);
430  }
431  }
432 
433  return sz;
434 }
435 #endif
436 
437 #if 0
438 void
439 Structure::intern_data(ConstraintEvaluator & eval, DDS & dds)
440 {
441  DBG(cerr << "Structure::intern_data: " << name() << endl);
442  if (!read_p())
443  read(); // read() throws Error and InternalErr
444 
445  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
446  if ((*i)->send_p()) {
447  (*i)->intern_data(eval, dds);
448  }
449  }
450 }
451 
452 bool
453 Structure::serialize(ConstraintEvaluator &eval, DDS &dds,
454  Marshaller &m, bool ce_eval)
455 {
456 #if USE_LOCAL_TIMEOUT_SCHEME
457  dds.timeout_on();
458 #endif
459  if (!read_p())
460  read(); // read() throws Error and InternalErr
461 
462 #if EVAL
463  if (ce_eval && !eval.eval_selection(dds, dataset()))
464  return true;
465 #endif
466 #if USE_LOCAL_TIMEOUT_SCHEME
467  dds.timeout_off();
468 #endif
469  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
470  if ((*i)->send_p()) {
471 #ifdef CHECKSUMS
472  XDRStreamMarshaller *sm = dynamic_cast<XDRStreamMarshaller*>(&m);
473  if (sm && sm->checksums() && (*i)->type() != dods_structure_c && (*i)->type() != dods_grid_c)
474  sm->reset_checksum();
475 
476  (*i)->serialize(eval, dds, m, false);
477 
478  if (sm && sm->checksums() && (*i)->type() != dods_structure_c && (*i)->type() != dods_grid_c)
479  sm->get_checksum();
480 #else
481  (*i)->serialize(eval, dds, m, false);
482 #endif
483  }
484  }
485 
486  return true;
487 }
488 
489 bool
490 Structure::deserialize(UnMarshaller &um, DDS *dds, bool reuse)
491 {
492  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
493  (*i)->deserialize(um, dds, reuse);
494  }
495 
496  return false;
497 }
498 #endif
499 #if 0
509 unsigned int
510 Structure::val2buf(void *, bool)
511 {
512  return sizeof(Structure);
513 }
514 
518 unsigned int
519 Structure::buf2val(void **)
520 {
521  return sizeof(Structure);
522 }
523 #endif
524 
525 #if 0
526 BaseType *
527 Structure::var(const string &name, bool exact_match, btp_stack *s)
528 {
529  string n = www2id(name);
530 
531  if (exact_match)
532  return m_exact_match(n, s);
533  else
534  return m_leaf_match(n, s);
535 }
536 
538 BaseType *
539 Structure::var(const string &n, btp_stack &s)
540 {
541  string name = www2id(n);
542 
543  BaseType *btp = m_exact_match(name, &s);
544  if (btp)
545  return btp;
546 
547  return m_leaf_match(name, &s);
548 }
549 #endif
550 #if 0
551 // Private method to find a variable using the shorthand name. This
552 // should be moved to Constructor.
553 BaseType *
554 Structure::m_leaf_match(const string &name, btp_stack *s)
555 {
556  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
557  if ((*i)->name() == name) {
558  if (s) {
559  DBG(cerr << "Pushing " << this->name() << endl);
560  s->push(static_cast<BaseType *>(this));
561  }
562  return *i;
563  }
564  if ((*i)->is_constructor_type()) {
565  BaseType *btp = (*i)->var(name, false, s);
566  if (btp) {
567  if (s) {
568  DBG(cerr << "Pushing " << this->name() << endl);
569  s->push(static_cast<BaseType *>(this));
570  }
571  return btp;
572  }
573  }
574  }
575 
576  return 0;
577 }
578 
579 // Breadth-first search for NAME. If NAME contains one or more dots (.)
580 // TODO The btp_stack is not needed since there are 'back pointers' in
581 // BaseType.
582 BaseType *
583 Structure::m_exact_match(const string &name, btp_stack *s)
584 {
585  // Look for name at the top level first.
586  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
587  if ((*i)->name() == name) {
588  if (s)
589  s->push(static_cast<BaseType *>(this));
590 
591  return *i;
592  }
593  }
594 
595  // If it was not found using the simple search, look for a dot and
596  // search the hierarchy.
597  string::size_type dot_pos = name.find("."); // zero-based index of `.'
598  if (dot_pos != string::npos) {
599  string aggregate = name.substr(0, dot_pos);
600  string field = name.substr(dot_pos + 1);
601 
602  BaseType *agg_ptr = var(aggregate);
603  if (agg_ptr) {
604  if (s)
605  s->push(static_cast<BaseType *>(this));
606 
607  return agg_ptr->var(field, true, s); // recurse
608  }
609  else
610  return 0; // qualified names must be *fully* qualified
611  }
612 
613  return 0;
614 }
615 #endif
616 #if 0
617 void
618 Structure::print_val(FILE *out, string space, bool print_decl_p)
619 {
620  ostringstream oss;
621  print_val(oss, space, print_decl_p);
622  fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
623 }
624 
625 void
626 Structure::print_val(ostream &out, string space, bool print_decl_p)
627 {
628  if (print_decl_p) {
629  print_decl(out, space, false);
630  out << " = " ;
631  }
632 
633  out << "{ " ;
634  for (Vars_citer i = d_vars.begin(); i != d_vars.end();
635  i++, (void)(i != d_vars.end() && out << ", ")) {
636  (*i)->print_val(out, "", false);
637  }
638 
639  out << " }" ;
640 
641  if (print_decl_p)
642  out << ";\n" ;
643 }
644 #endif
645 
646 #if 0
647 bool
648 Structure::check_semantics(string &msg, bool all)
649 {
650  if (!BaseType::check_semantics(msg))
651  return false;
652 
653  bool status = true;
654 
655  if (!unique_names(d_vars, name(), type_name(), msg))
656  return false;
657 
658  if (all) {
659  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
660  //assert(*i);
661  if (!(*i)->check_semantics(msg, true)) {
662  status = false;
663  goto exit;
664  }
665  }
666  }
667 
668 exit:
669  return status;
670 }
671 #endif
672 
681 void
682 Structure::dump(ostream &strm) const
683 {
684  strm << DapIndent::LMarg << "Structure::dump - ("
685  << (void *)this << ")" << endl ;
686  DapIndent::Indent() ;
687  Constructor::dump(strm) ;
688  DapIndent::UnIndent() ;
689 }
690 
691 } // namespace libdap
692 
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:410
virtual void set_name(const string &n)
Set the name of this attribute table.
Definition: AttrTable.cc:245
virtual string get_name() const
Get the name of this attribute table.
Definition: AttrTable.cc:238
virtual void print(FILE *out, string pad=" ", bool dereference=false)
Prints the attribute table.
Definition: AttrTable.cc:1243
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition: AttrTable.cc:231
The basic data type for the DODS DAP types.
Definition: BaseType.h:118
virtual BaseType * ptr_duplicate()=0
virtual string type_name() const
Returns the type of the class instance as a string.
Definition: BaseType.cc:375
virtual AttrTable & get_attr_table()
Definition: BaseType.cc:578
virtual string name() const
Returns the name of the class instance.
Definition: BaseType.cc:316
virtual void set_in_selection(bool state)
Definition: BaseType.cc:714
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:476
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:512
virtual string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:354
virtual void set_attr_table(const AttrTable &at)
Definition: BaseType.cc:586
virtual void set_parent(BaseType *parent)
Definition: BaseType.cc:729
virtual D4Attributes * attributes()
Definition: BaseType.cc:595
virtual void set_send_p(bool state)
Definition: BaseType.cc:564
virtual bool check_semantics(string &msg, bool all=false)
Compare an object's current state with the semantics of its type.
Definition: BaseType.cc:1205
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition: BaseType.cc:126
void transform_to_dap4(D4Group *root, Constructor *dest) override
DAP2 to DAP4 transform.
Definition: Constructor.cc:84
BaseType * var(const string &name, bool exact_match=true, btp_stack *s=nullptr) override
btp_stack no longer needed; use back pointers (BaseType::get_parent())
Definition: Constructor.cc:185
void add_var(BaseType *bt, Part part=nil) override
Definition: Constructor.cc:323
void print_decl(ostream &out, string space=" ", bool print_semi=true, bool constraint_info=false, bool constrained=false) override
Print an ASCII representation of the variable structure.
Definition: Constructor.cc:529
unsigned int buf2val(void **) override
Reads the class data.
Definition: Constructor.h:138
bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false) override
Receive data from the net.
Definition: Constructor.cc:452
void intern_data() override
Read data into this variable.
Definition: Constructor.cc:468
void set_read_p(bool state) override
Set the 'read_p' property for the Constructor and its members.
Definition: Constructor.cc:150
void print_val(FILE *out, string space="", bool print_decl_p=true) override
Prints the value of the variable.
Definition: Constructor.cc:553
void set_send_p(bool state) override
Definition: Constructor.cc:129
Vars_iter var_end()
Definition: Constructor.cc:280
bool read() override
Read the elements of Constructor marked for transmission.
Definition: Constructor.cc:393
void set_in_selection(bool state) override
Set the in_selection property.
Definition: Constructor.cc:694
bool serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval=true) override
Move data to the net, then remove them from the object.
Definition: Constructor.cc:421
void add_var_nocopy(BaseType *bt, Part part=nil) override
Definition: Constructor.cc:345
unsigned int width(bool constrained=false) const override
Definition: Constructor.cc:167
bool check_semantics(string &msg, bool all=false) override
Compare an object's current state with the semantics of its type.
Definition: Constructor.cc:654
Vars_iter var_begin()
Definition: Constructor.cc:272
unsigned int val2buf(void *, bool) override
Loads class data.
Definition: Constructor.h:135
virtual void del_var(const string &name)
Remove an element from a Constructor.
Definition: Constructor.cc:362
void dump(ostream &strm) const override
dumps information about this object
Definition: Constructor.cc:767
void transform_attrs_to_dap2(AttrTable *d2_attr_table)
Copy the attributes from this D4Attributes object to a DAP2 AttrTable.
A class for software fault reporting.
Definition: InternalErr.h:65
Holds a sequence.
Definition: Sequence.h:163
virtual void set_leaf_sequence(int lvl=1)
Mark the Sequence which holds the leaf elements.
Definition: Sequence.cc:1233
Holds a structure (aggregate) type.
Definition: Structure.h:84
Structure(const string &n)
Definition: Structure.cc:110
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Structure.cc:682
virtual void set_leaf_sequence(int level=1)
Traverse Structure, set Sequence leaf nodes.
Definition: Structure.cc:309
virtual vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table)
DAP4 to DAP2 transform.
Definition: Structure.cc:180
virtual bool is_linear()
Check to see whether this variable can be printed simply.
Definition: Structure.cc:257
virtual BaseType * ptr_duplicate()
Definition: Structure.cc:138
virtual void transform_to_dap4(D4Group *root, Constructor *container)
Definition: Structure.cc:153
top level DAP object to house generic methods
Definition: AlarmHandler.h:36
string www2id(const string &in, const string &escape, const string &except)
Definition: escaping.cc:220
Part
Names the parts of multi-section constructor data types.
Definition: Type.h:48