libdap  Updated for version 3.19.1
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 
68 using std::cerr;
69 using std::endl;
70 
71 namespace libdap {
72 
73 #if 0
74 
78 void
79 Structure::m_duplicate(const Structure &s)
80 {
81  Constructor::m_duplicate(s);
82 #if 0
83  Structure &cs = const_cast<Structure &>(s);
84 
85  DBG(cerr << "Copying structure: " << name() << endl);
86 
87  for (Vars_iter i = cs.d_vars.begin(); i != cs.d_vars.end(); i++) {
88  DBG(cerr << "Copying field: " << (*i)->name() << endl);
89  // Jose Garcia
90  // I think this assert here is part of a debugging
91  // process since it is going along with a DBG call
92  // I leave it here since it can be remove by defining NDEBUG.
93  // assert(*i);
94  BaseType *btp = (*i)->ptr_duplicate();
95  btp->set_parent(this);
96  d_vars.push_back(btp);
97  }
98 #endif
99 }
100 #endif
101 
109 Structure::Structure(const string &n) : Constructor(n, dods_structure_c)
110 {}
111 
121 Structure::Structure(const string &n, const string &d)
122  : Constructor(n, d, dods_structure_c)
123 {}
124 
127 {
128  DBG(cerr << "In Structure::copy_ctor for " << name() << endl);
129  //m_duplicate(rhs);
130 }
131 
132 Structure::~Structure()
133 {
134 }
135 
136 BaseType *
138 {
139  return new Structure(*this);
140 }
141 
151 void
153 {
154  DBG(cerr << __func__ <<"() - BEGIN" << endl;);
155  // Here we create a new Structure and then use it
156  // as the target container for the transformed versions of
157  // all the member variables by calling Constructor::transform_to_dap4() and
158  // passing our new target Structure in as the target container.
159  Structure *dest = new Structure(name());
160  DBG(cerr << __func__ <<"() - Calling Constructor::transform_to_dap4("<<
161  "'" << root->name() << "':" << (void*)root << ","
162  "'" << dest->name() << "':" << (void*)dest << ")"
163  << endl; );
164  Constructor::transform_to_dap4(root, dest);
165  container->add_var_nocopy(dest);
166  DBG(cerr << __func__ <<"() - Added new Structure '" << dest->name() << "' (" << (void*)dest <<
167  ") to the container '" << container->name() <<"'" << endl;);
168  DBG(cerr << __func__ <<"() - END"<< endl;);
169 }
170 
171 
178 vector<BaseType *> *
180 {
181  DBG(cerr << " " << __func__ << " BEGIN" << endl);
182  Structure *dest = new Structure(name());
183 
184  // convert the Structure's d4 attributes to a dap2 attribute table.
185  AttrTable *attrs = this->attributes()->get_AttrTable(name());
186  dest->set_is_dap4(false);
187 
188  vector<BaseType *> dropped_vars;
189  for (Structure::Vars_citer i = var_begin(), e = var_end(); i != e; ++i) {
190  vector<BaseType *> *new_vars = (*i)->transform_to_dap2(attrs);
191  if (new_vars) { // Might be un-mappable
192  // It's not so game on..
193  vector<BaseType*>::iterator vIter = new_vars->begin();
194  vector<BaseType*>::iterator end = new_vars->end();
195  for( ; vIter!=end ; vIter++ ){
196  BaseType *new_var = (*vIter);
197  new_var->set_parent(dest);
198  dest->add_var_nocopy(new_var);
199  (*vIter) = NULL;
200  }
201  delete new_vars;
202 
203  }
204  else {
205  // Got a NULL, so we are dropping this var.
206  dropped_vars.push_back(*i);
207  }
208  }
209 
210  AttrTable *dv_attr_table = make_dropped_vars_attr_table(&dropped_vars);
211  if(dv_attr_table){
212  DBG(cerr << " " << __func__ << "() - Adding "<< dv_attr_table->get_name() << " AttrTable" << endl);
213  attrs->append_container(dv_attr_table,dv_attr_table->get_name());
214  }
215  DBG(attrs->print(cerr,"",true););
216  // Since this does a copy we gotta delete the attrs when done
217  dest->set_attr_table(*attrs);
218  delete attrs;
219 
220  vector<BaseType *> *result = new vector<BaseType *>();
221  result->push_back(dest);
222  DBG(cerr << " " << __func__ << " END" << endl);
223  return result;
224 }
225 
226 
227 
228 
229 
230 Structure &
231 Structure::operator=(const Structure &rhs)
232 {
233  DBG(cerr << "Entering Structure::operator=" << endl);
234  if (this == &rhs)
235  return *this;
236 
237  dynamic_cast<Constructor &>(*this) = rhs; // run Constructor=
238 
239  //m_duplicate(rhs);
240 
241  DBG(cerr << "Exiting Structure::operator=" << endl);
242  return *this;
243 }
244 
245 #if 0
246 int
247 Structure::element_count(bool leaves)
248 {
249  if (!leaves)
250  return d_vars.size();
251  else {
252  int i = 0;
253  for (Vars_iter j = d_vars.begin(); j != d_vars.end(); j++) {
254  i += (*j)->element_count(leaves);
255  }
256  return i;
257  }
258 }
259 #endif
260 
261 bool
263 {
264  bool linear = true;
265  for (Vars_iter i = d_vars.begin(); linear && i != d_vars.end(); i++) {
266  if ((*i)->type() == dods_structure_c)
267  linear = linear && static_cast<Structure*>((*i))->is_linear();
268  else
269  linear = linear && (*i)->is_simple_type();
270  }
271 
272  return linear;
273 }
274 
275 #if 0
276 void
277 Structure::set_send_p(bool state)
278 {
279  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
280  (*i)->set_send_p(state);
281  }
282 
283  BaseType::set_send_p(state);
284 }
285 
286 void
287 Structure::set_read_p(bool state)
288 {
289  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
290  (*i)->set_read_p(state);
291  }
292 
293  BaseType::set_read_p(state);
294 }
295 #endif
296 #if 0
297 
302 void
303 Structure::set_in_selection(bool state)
304 {
305  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
306  (*i)->set_in_selection(state);
307  }
308 
310 }
311 #endif
312 
313 void
315 {
316  for (Vars_iter i = var_begin(); i != var_end(); i++) {
317  if ((*i)->type() == dods_sequence_c)
318  static_cast<Sequence&>(**i).set_leaf_sequence(++level);
319  else if ((*i)->type() == dods_structure_c)
320  static_cast<Structure&>(**i).set_leaf_sequence(level);
321  }
322 }
323 
324 #if 0
325 
329 void
331 {
332  // Jose Garcia
333  // Passing and invalid pointer to an object is a developer's error.
334  if (!bt)
335  throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
336 
337  if (bt->is_dap4_only_type())
338  throw InternalErr(__FILE__, __LINE__, "Attempt to add a DAP4 type to a DAP2 Structure.");
339 
340  // Jose Garcia
341  // Now we add a copy of bt so the external user is able to destroy bt as
342  // he/she wishes. The policy is: "If it is allocated outside, it is
343  // deallocated outside, if it is allocated inside, it is deallocated
344  // inside"
345  BaseType *btp = bt->ptr_duplicate();
346  btp->set_parent(this);
347  d_vars.push_back(btp);
348 }
349 
354 void
355 Structure::add_var_nocopy(BaseType *bt, Part)
356 {
357  if (!bt)
358  throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
359 
360  if (bt->is_dap4_only_type())
361  throw InternalErr(__FILE__, __LINE__, "Attempt to add a DAP4 type to a DAP2 Structure.");
362 
363  bt->set_parent(this);
364  d_vars.push_back(bt);
365 }
366 
367 
371 void
372 Structure::del_var(const string &n)
373 {
374  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
375  if ((*i)->name() == n) {
376  BaseType *bt = *i ;
377  d_vars.erase(i) ;
378  delete bt ; bt = 0;
379  return;
380  }
381  }
382 }
383 #endif
384 #if 0
385 
390 bool Structure::read()
391 {
392  if (!read_p()) {
393  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
394  (*i)->read();
395  }
396  set_read_p(true);
397  }
398 
399  return false;
400 }
401 #endif
402 #if 0
403 // TODO Recode to use width(bool)
404 unsigned int
406 {
407  unsigned int sz = 0;
408 
409  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
410  sz += (*i)->width();
411  }
412 
413  return sz;
414 }
415 
423 unsigned int
424 Structure::width(bool constrained)
425 {
426  unsigned int sz = 0;
427 
428  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
429  if (constrained) {
430  if ((*i)->send_p())
431  sz += (*i)->width(constrained);
432  }
433  else {
434  sz += (*i)->width(constrained);
435  }
436  }
437 
438  return sz;
439 }
440 #endif
441 
442 #if 0
443 void
444 Structure::intern_data(ConstraintEvaluator & eval, DDS & dds)
445 {
446  DBG(cerr << "Structure::intern_data: " << name() << endl);
447  if (!read_p())
448  read(); // read() throws Error and InternalErr
449 
450  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
451  if ((*i)->send_p()) {
452  (*i)->intern_data(eval, dds);
453  }
454  }
455 }
456 
457 bool
458 Structure::serialize(ConstraintEvaluator &eval, DDS &dds,
459  Marshaller &m, bool ce_eval)
460 {
461 #if USE_LOCAL_TIMEOUT_SCHEME
462  dds.timeout_on();
463 #endif
464  if (!read_p())
465  read(); // read() throws Error and InternalErr
466 
467 #if EVAL
468  if (ce_eval && !eval.eval_selection(dds, dataset()))
469  return true;
470 #endif
471 #if USE_LOCAL_TIMEOUT_SCHEME
472  dds.timeout_off();
473 #endif
474  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
475  if ((*i)->send_p()) {
476 #ifdef CHECKSUMS
477  XDRStreamMarshaller *sm = dynamic_cast<XDRStreamMarshaller*>(&m);
478  if (sm && sm->checksums() && (*i)->type() != dods_structure_c && (*i)->type() != dods_grid_c)
479  sm->reset_checksum();
480 
481  (*i)->serialize(eval, dds, m, false);
482 
483  if (sm && sm->checksums() && (*i)->type() != dods_structure_c && (*i)->type() != dods_grid_c)
484  sm->get_checksum();
485 #else
486  (*i)->serialize(eval, dds, m, false);
487 #endif
488  }
489  }
490 
491  return true;
492 }
493 
494 bool
495 Structure::deserialize(UnMarshaller &um, DDS *dds, bool reuse)
496 {
497  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
498  (*i)->deserialize(um, dds, reuse);
499  }
500 
501  return false;
502 }
503 #endif
504 #if 0
505 
514 unsigned int
515 Structure::val2buf(void *, bool)
516 {
517  return sizeof(Structure);
518 }
519 
523 unsigned int
524 Structure::buf2val(void **)
525 {
526  return sizeof(Structure);
527 }
528 #endif
529 
530 #if 0
531 BaseType *
532 Structure::var(const string &name, bool exact_match, btp_stack *s)
533 {
534  string n = www2id(name);
535 
536  if (exact_match)
537  return m_exact_match(n, s);
538  else
539  return m_leaf_match(n, s);
540 }
541 
543 BaseType *
544 Structure::var(const string &n, btp_stack &s)
545 {
546  string name = www2id(n);
547 
548  BaseType *btp = m_exact_match(name, &s);
549  if (btp)
550  return btp;
551 
552  return m_leaf_match(name, &s);
553 }
554 #endif
555 #if 0
556 // Private method to find a variable using the shorthand name. This
557 // should be moved to Constructor.
558 BaseType *
559 Structure::m_leaf_match(const string &name, btp_stack *s)
560 {
561  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
562  if ((*i)->name() == name) {
563  if (s) {
564  DBG(cerr << "Pushing " << this->name() << endl);
565  s->push(static_cast<BaseType *>(this));
566  }
567  return *i;
568  }
569  if ((*i)->is_constructor_type()) {
570  BaseType *btp = (*i)->var(name, false, s);
571  if (btp) {
572  if (s) {
573  DBG(cerr << "Pushing " << this->name() << endl);
574  s->push(static_cast<BaseType *>(this));
575  }
576  return btp;
577  }
578  }
579  }
580 
581  return 0;
582 }
583 
584 // Breadth-first search for NAME. If NAME contains one or more dots (.)
585 // TODO The btp_stack is not needed since there are 'back pointers' in
586 // BaseType.
587 BaseType *
588 Structure::m_exact_match(const string &name, btp_stack *s)
589 {
590  // Look for name at the top level first.
591  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
592  if ((*i)->name() == name) {
593  if (s)
594  s->push(static_cast<BaseType *>(this));
595 
596  return *i;
597  }
598  }
599 
600  // If it was not found using the simple search, look for a dot and
601  // search the hierarchy.
602  string::size_type dot_pos = name.find("."); // zero-based index of `.'
603  if (dot_pos != string::npos) {
604  string aggregate = name.substr(0, dot_pos);
605  string field = name.substr(dot_pos + 1);
606 
607  BaseType *agg_ptr = var(aggregate);
608  if (agg_ptr) {
609  if (s)
610  s->push(static_cast<BaseType *>(this));
611 
612  return agg_ptr->var(field, true, s); // recurse
613  }
614  else
615  return 0; // qualified names must be *fully* qualified
616  }
617 
618  return 0;
619 }
620 #endif
621 #if 0
622 void
623 Structure::print_val(FILE *out, string space, bool print_decl_p)
624 {
625  ostringstream oss;
626  print_val(oss, space, print_decl_p);
627  fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
628 }
629 
630 void
631 Structure::print_val(ostream &out, string space, bool print_decl_p)
632 {
633  if (print_decl_p) {
634  print_decl(out, space, false);
635  out << " = " ;
636  }
637 
638  out << "{ " ;
639  for (Vars_citer i = d_vars.begin(); i != d_vars.end();
640  i++, (void)(i != d_vars.end() && out << ", ")) {
641  (*i)->print_val(out, "", false);
642  }
643 
644  out << " }" ;
645 
646  if (print_decl_p)
647  out << ";\n" ;
648 }
649 #endif
650 
651 #if 0
652 bool
653 Structure::check_semantics(string &msg, bool all)
654 {
655  if (!BaseType::check_semantics(msg))
656  return false;
657 
658  bool status = true;
659 
660  if (!unique_names(d_vars, name(), type_name(), msg))
661  return false;
662 
663  if (all) {
664  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
665  //assert(*i);
666  if (!(*i)->check_semantics(msg, true)) {
667  status = false;
668  goto exit;
669  }
670  }
671  }
672 
673 exit:
674  return status;
675 }
676 #endif
677 
686 void
687 Structure::dump(ostream &strm) const
688 {
689  strm << DapIndent::LMarg << "Structure::dump - ("
690  << (void *)this << ")" << endl ;
691  DapIndent::Indent() ;
692  Constructor::dump(strm) ;
693  DapIndent::UnIndent() ;
694 }
695 
696 } // namespace libdap
697 
virtual unsigned int width(bool constrained=false) const
Definition: Constructor.cc:249
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:471
virtual string name() const
Returns the name of the class instance.
Definition: BaseType.cc:311
virtual void set_in_selection(bool state)
Set the in_selection property.
Definition: Constructor.cc:834
AttrTable * get_AttrTable(const std::string name)
copy attributes from DAP4 to DAP2
virtual BaseType * ptr_duplicate()
Definition: Structure.cc:137
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:152
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:237
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition: BaseType.cc:125
Structure(const string &n)
Definition: Structure.cc:109
virtual vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table)
DAP4 to DAP2 transform.
Definition: Structure.cc:179
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:1242
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:709
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:724
virtual void set_leaf_sequence(int level=1)
Traverse Structure, set Sequence leaf nodes.
Definition: Structure.cc:314
A class for software fault reporting.
Definition: InternalErr.h:64
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:559
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition: AttrTable.cc:409
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:507
virtual D4Attributes * attributes()
Definition: BaseType.cc:590
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
virtual unsigned int buf2val(void **)
Reads the class data.
Definition: Constructor.h:118
The basic data type for the DODS DAP types.
Definition: BaseType.h:117
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:370
virtual bool is_linear()
Check to see whether this variable can be printed simply.
Definition: Structure.cc:262
Vars_iter var_begin()
Definition: Constructor.cc:356
virtual void set_attr_table(const AttrTable &at)
Definition: BaseType.cc:581
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Structure.cc:687
Vars_iter var_end()
Definition: Constructor.cc:364
virtual void intern_data()
Read data into this variable.
Definition: Constructor.cc:556
virtual void del_var(const string &name)
Definition: Constructor.cc:448
virtual void set_send_p(bool state)
Definition: Constructor.cc:208
virtual string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:349
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:1200