libdap  Updated for version 3.20.6
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
75 
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  DBG(cerr << "Entering Structure::operator=" << endl);
251  if (this == &rhs)
252  return *this;
253 
254  dynamic_cast<Constructor &>(*this) = rhs; // run Constructor=
255 
256  //m_duplicate(rhs);
257 
258  DBG(cerr << "Exiting Structure::operator=" << endl);
259  return *this;
260 }
261 
262 #if 0
263 int
264 Structure::element_count(bool leaves)
265 {
266  if (!leaves)
267  return d_vars.size();
268  else {
269  int i = 0;
270  for (Vars_iter j = d_vars.begin(); j != d_vars.end(); j++) {
271  i += (*j)->element_count(leaves);
272  }
273  return i;
274  }
275 }
276 #endif
277 
278 bool
280 {
281  bool linear = true;
282  for (Vars_iter i = d_vars.begin(); linear && i != d_vars.end(); i++) {
283  if ((*i)->type() == dods_structure_c)
284  linear = linear && static_cast<Structure*>((*i))->is_linear();
285  else
286  linear = linear && (*i)->is_simple_type();
287  }
288 
289  return linear;
290 }
291 
292 #if 0
293 void
294 Structure::set_send_p(bool state)
295 {
296  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
297  (*i)->set_send_p(state);
298  }
299 
300  BaseType::set_send_p(state);
301 }
302 
303 void
304 Structure::set_read_p(bool state)
305 {
306  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
307  (*i)->set_read_p(state);
308  }
309 
310  BaseType::set_read_p(state);
311 }
312 #endif
313 #if 0
314 
319 void
320 Structure::set_in_selection(bool state)
321 {
322  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
323  (*i)->set_in_selection(state);
324  }
325 
327 }
328 #endif
329 
330 void
332 {
333  for (Vars_iter i = var_begin(); i != var_end(); i++) {
334  if ((*i)->type() == dods_sequence_c)
335  static_cast<Sequence&>(**i).set_leaf_sequence(++level);
336  else if ((*i)->type() == dods_structure_c)
337  static_cast<Structure&>(**i).set_leaf_sequence(level);
338  }
339 }
340 
341 #if 0
342 
346 void
348 {
349  // Jose Garcia
350  // Passing and invalid pointer to an object is a developer's error.
351  if (!bt)
352  throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
353 
354  if (bt->is_dap4_only_type())
355  throw InternalErr(__FILE__, __LINE__, "Attempt to add a DAP4 type to a DAP2 Structure.");
356 
357  // Jose Garcia
358  // Now we add a copy of bt so the external user is able to destroy bt as
359  // he/she wishes. The policy is: "If it is allocated outside, it is
360  // deallocated outside, if it is allocated inside, it is deallocated
361  // inside"
362  BaseType *btp = bt->ptr_duplicate();
363  btp->set_parent(this);
364  d_vars.push_back(btp);
365 }
366 
371 void
373 {
374  if (!bt)
375  throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
376 
377  if (bt->is_dap4_only_type())
378  throw InternalErr(__FILE__, __LINE__, "Attempt to add a DAP4 type to a DAP2 Structure.");
379 
380  bt->set_parent(this);
381  d_vars.push_back(bt);
382 }
383 
384 
388 void
389 Structure::del_var(const string &n)
390 {
391  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
392  if ((*i)->name() == n) {
393  BaseType *bt = *i ;
394  d_vars.erase(i) ;
395  delete bt ; bt = 0;
396  return;
397  }
398  }
399 }
400 #endif
401 #if 0
402 
407 bool Structure::read()
408 {
409  if (!read_p()) {
410  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
411  (*i)->read();
412  }
413  set_read_p(true);
414  }
415 
416  return false;
417 }
418 #endif
419 #if 0
420 // TODO Recode to use width(bool)
421 unsigned int
423 {
424  unsigned int sz = 0;
425 
426  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
427  sz += (*i)->width();
428  }
429 
430  return sz;
431 }
432 
440 unsigned int
441 Structure::width(bool constrained)
442 {
443  unsigned int sz = 0;
444 
445  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
446  if (constrained) {
447  if ((*i)->send_p())
448  sz += (*i)->width(constrained);
449  }
450  else {
451  sz += (*i)->width(constrained);
452  }
453  }
454 
455  return sz;
456 }
457 #endif
458 
459 #if 0
460 void
462 {
463  DBG(cerr << "Structure::intern_data: " << name() << endl);
464  if (!read_p())
465  read(); // read() throws Error and InternalErr
466 
467  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
468  if ((*i)->send_p()) {
469  (*i)->intern_data(eval, dds);
470  }
471  }
472 }
473 
474 bool
476  Marshaller &m, bool ce_eval)
477 {
478 #if USE_LOCAL_TIMEOUT_SCHEME
479  dds.timeout_on();
480 #endif
481  if (!read_p())
482  read(); // read() throws Error and InternalErr
483 
484 #if EVAL
485  if (ce_eval && !eval.eval_selection(dds, dataset()))
486  return true;
487 #endif
488 #if USE_LOCAL_TIMEOUT_SCHEME
489  dds.timeout_off();
490 #endif
491  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
492  if ((*i)->send_p()) {
493 #ifdef CHECKSUMS
494  XDRStreamMarshaller *sm = dynamic_cast<XDRStreamMarshaller*>(&m);
495  if (sm && sm->checksums() && (*i)->type() != dods_structure_c && (*i)->type() != dods_grid_c)
496  sm->reset_checksum();
497 
498  (*i)->serialize(eval, dds, m, false);
499 
500  if (sm && sm->checksums() && (*i)->type() != dods_structure_c && (*i)->type() != dods_grid_c)
501  sm->get_checksum();
502 #else
503  (*i)->serialize(eval, dds, m, false);
504 #endif
505  }
506  }
507 
508  return true;
509 }
510 
511 bool
512 Structure::deserialize(UnMarshaller &um, DDS *dds, bool reuse)
513 {
514  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
515  (*i)->deserialize(um, dds, reuse);
516  }
517 
518  return false;
519 }
520 #endif
521 #if 0
522 
531 unsigned int
532 Structure::val2buf(void *, bool)
533 {
534  return sizeof(Structure);
535 }
536 
540 unsigned int
541 Structure::buf2val(void **)
542 {
543  return sizeof(Structure);
544 }
545 #endif
546 
547 #if 0
548 BaseType *
549 Structure::var(const string &name, bool exact_match, btp_stack *s)
550 {
551  string n = www2id(name);
552 
553  if (exact_match)
554  return m_exact_match(n, s);
555  else
556  return m_leaf_match(n, s);
557 }
558 
560 BaseType *
561 Structure::var(const string &n, btp_stack &s)
562 {
563  string name = www2id(n);
564 
565  BaseType *btp = m_exact_match(name, &s);
566  if (btp)
567  return btp;
568 
569  return m_leaf_match(name, &s);
570 }
571 #endif
572 #if 0
573 // Private method to find a variable using the shorthand name. This
574 // should be moved to Constructor.
575 BaseType *
576 Structure::m_leaf_match(const string &name, btp_stack *s)
577 {
578  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
579  if ((*i)->name() == name) {
580  if (s) {
581  DBG(cerr << "Pushing " << this->name() << endl);
582  s->push(static_cast<BaseType *>(this));
583  }
584  return *i;
585  }
586  if ((*i)->is_constructor_type()) {
587  BaseType *btp = (*i)->var(name, false, s);
588  if (btp) {
589  if (s) {
590  DBG(cerr << "Pushing " << this->name() << endl);
591  s->push(static_cast<BaseType *>(this));
592  }
593  return btp;
594  }
595  }
596  }
597 
598  return 0;
599 }
600 
601 // Breadth-first search for NAME. If NAME contains one or more dots (.)
602 // TODO The btp_stack is not needed since there are 'back pointers' in
603 // BaseType.
604 BaseType *
605 Structure::m_exact_match(const string &name, btp_stack *s)
606 {
607  // Look for name at the top level first.
608  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
609  if ((*i)->name() == name) {
610  if (s)
611  s->push(static_cast<BaseType *>(this));
612 
613  return *i;
614  }
615  }
616 
617  // If it was not found using the simple search, look for a dot and
618  // search the hierarchy.
619  string::size_type dot_pos = name.find("."); // zero-based index of `.'
620  if (dot_pos != string::npos) {
621  string aggregate = name.substr(0, dot_pos);
622  string field = name.substr(dot_pos + 1);
623 
624  BaseType *agg_ptr = var(aggregate);
625  if (agg_ptr) {
626  if (s)
627  s->push(static_cast<BaseType *>(this));
628 
629  return agg_ptr->var(field, true, s); // recurse
630  }
631  else
632  return 0; // qualified names must be *fully* qualified
633  }
634 
635  return 0;
636 }
637 #endif
638 #if 0
639 void
640 Structure::print_val(FILE *out, string space, bool print_decl_p)
641 {
642  ostringstream oss;
643  print_val(oss, space, print_decl_p);
644  fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
645 }
646 
647 void
648 Structure::print_val(ostream &out, string space, bool print_decl_p)
649 {
650  if (print_decl_p) {
651  print_decl(out, space, false);
652  out << " = " ;
653  }
654 
655  out << "{ " ;
656  for (Vars_citer i = d_vars.begin(); i != d_vars.end();
657  i++, (void)(i != d_vars.end() && out << ", ")) {
658  (*i)->print_val(out, "", false);
659  }
660 
661  out << " }" ;
662 
663  if (print_decl_p)
664  out << ";\n" ;
665 }
666 #endif
667 
668 #if 0
669 bool
670 Structure::check_semantics(string &msg, bool all)
671 {
672  if (!BaseType::check_semantics(msg))
673  return false;
674 
675  bool status = true;
676 
677  if (!unique_names(d_vars, name(), type_name(), msg))
678  return false;
679 
680  if (all) {
681  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
682  //assert(*i);
683  if (!(*i)->check_semantics(msg, true)) {
684  status = false;
685  goto exit;
686  }
687  }
688  }
689 
690 exit:
691  return status;
692 }
693 #endif
694 
703 void
704 Structure::dump(ostream &strm) const
705 {
706  strm << DapIndent::LMarg << "Structure::dump - ("
707  << (void *)this << ")" << endl ;
708  DapIndent::Indent() ;
709  Constructor::dump(strm) ;
710  DapIndent::UnIndent() ;
711 }
712 
713 } // namespace libdap
714 
virtual unsigned int width(bool constrained=false) const
Definition: Constructor.cc:249
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:480
virtual string name() const
Returns the name of the class instance.
Definition: BaseType.cc:320
virtual void set_in_selection(bool state)
Set the in_selection property.
Definition: Constructor.cc:834
abstract base class used to unmarshall/deserialize dap data objects
Definition: UnMarshaller.h:54
virtual BaseType * ptr_duplicate()
Definition: Structure.cc:138
virtual BaseType * var(const string &name, bool exact_match=true, btp_stack *s=0)
btp_stack no longer needed; use back pointers (BaseType::get_parent())
Definition: Constructor.cc:267
Part
Names the parts of multi-section constructor data types.
Definition: Type.h:48
virtual void transform_to_dap4(D4Group *root, Constructor *container)
Definition: Structure.cc:153
Contains the attributes for a dataset.
Definition: AttrTable.h:142
virtual string get_name() const
Get the name of this attribute table.
Definition: AttrTable.cc:238
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition: BaseType.cc:126
Structure(const string &n)
Definition: Structure.cc:110
virtual vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table)
DAP4 to DAP2 transform.
Definition: Structure.cc:180
virtual void add_var_nocopy(BaseType *bt, Part part=nil)
Definition: Constructor.cc:432
virtual void print(FILE *out, string pad=" ", bool dereference=false)
Prints the attribute table.
Definition: AttrTable.cc:1243
Holds a structure (aggregate) type.
Definition: Structure.h:83
virtual void add_var(BaseType *bt, Part part=nil)
Definition: Constructor.cc:407
virtual void set_in_selection(bool state)
Definition: BaseType.cc:718
virtual int element_count(bool leaves=false)
Count the members of constructor types.
Definition: Constructor.cc:194
virtual void set_parent(BaseType *parent)
Definition: BaseType.cc:733
virtual void set_leaf_sequence(int level=1)
Traverse Structure, set Sequence leaf nodes.
Definition: Structure.cc:331
top level DAP object to house generic methods
Definition: AISConnect.cc:30
A class for software fault reporting.
Definition: InternalErr.h:64
void transform_attrs_to_dap2(AttrTable *d2_attr_table)
Copy the attributes from this D4Attributes object to a DAP2 AttrTable.
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=0)
Returns a pointer to a member of a constructor class.
Definition: BaseType.cc:758
virtual void transform_to_dap4(D4Group *root, Constructor *dest)
DAP2 to DAP4 transform.
Definition: Constructor.cc:141
virtual void set_send_p(bool state)
Definition: BaseType.cc:568
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition: AttrTable.cc:410
virtual bool serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval=true)
Move data to the net, then remove them from the object.
Definition: Constructor.cc:504
virtual bool read()
simple implementation of read that iterates through vars and calls read on them
Definition: Constructor.cc:476
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:516
virtual D4Attributes * attributes()
Definition: BaseType.cc:599
virtual bool check_semantics(string &msg, bool all=false)
Compare an object&#39;s current state with the semantics of its type.
Definition: Constructor.cc:792
virtual bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false)
Receive data from the net.
Definition: Constructor.cc:540
virtual void print_val(FILE *out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Definition: Constructor.cc:650
virtual unsigned int val2buf(void *, bool)
Loads class data.
Definition: Constructor.h:115
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Constructor.cc:913
virtual BaseType * ptr_duplicate()=0
string www2id(const string &in, const string &escape, const string &except)
Definition: escaping.cc:220
Evaluate a constraint expression.
virtual unsigned int buf2val(void **)
Reads the class data.
Definition: Constructor.h:118
virtual AttrTable & get_attr_table()
Definition: BaseType.cc:582
The basic data type for the DODS DAP types.
Definition: BaseType.h:117
abstract base class used to marshal/serialize dap data objects
Definition: Marshaller.h:50
virtual void print_decl(ostream &out, string space=" ", bool print_semi=true, bool constraint_info=false, bool constrained=false)
Print an ASCII representation of the variable structure.
Definition: Constructor.cc:626
virtual string type_name() const
Returns the type of the class instance as a string.
Definition: BaseType.cc:379
virtual bool is_linear()
Check to see whether this variable can be printed simply.
Definition: Structure.cc:279
Vars_iter var_begin()
Definition: Constructor.cc:356
virtual void set_attr_table(const AttrTable &at)
Definition: BaseType.cc:590
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Structure.cc:704
Vars_iter var_end()
Definition: Constructor.cc:364
bool eval_selection(DDS &dds, const std::string &dataset)
Evaluate a boolean-valued constraint expression. This is main method for the evaluator and is called ...
virtual void intern_data()
Read data into this variable.
Definition: Constructor.cc:556
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition: AttrTable.cc:231
virtual void set_name(const string &n)
Set the name of this attribute table.
Definition: AttrTable.cc:245
virtual void del_var(const string &name)
Definition: Constructor.cc:448
virtual void set_send_p(bool state)
Definition: Constructor.cc:208
Marshaller that knows how serialize dap data objects to a C++ iostream using XDR. ...
virtual string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:358
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: Constructor.cc:218
virtual bool check_semantics(string &msg, bool all=false)
Compare an object&#39;s current state with the semantics of its type.
Definition: BaseType.cc:1209