libdap  Updated for version 3.20.11
libdap4 is an implementation of OPeNDAP's DAP protocol.
Constructor.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 1995-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 #include "config.h"
33 
34 #include <string>
35 #include <sstream>
36 #include <algorithm>
37 
38 #include "crc.h"
39 
40 #include "Constructor.h"
41 #include "Grid.h"
42 
43 #include "DMR.h"
44 #include "XMLWriter.h"
45 #include "D4StreamMarshaller.h"
46 #include "D4StreamUnMarshaller.h"
47 #include "D4Group.h"
48 
49 #include "D4Attributes.h"
50 
51 #include "escaping.h"
52 #include "util.h"
53 #include "InternalErr.h"
54 #include "DapIndent.h"
55 
56 #include "debug.h"
57 
58 using namespace std;
59 
60 namespace libdap {
61 
62 // Private member functions
63 
64 void
65 Constructor::m_duplicate(const Constructor &c)
66 {
67  // Clear out any spurious vars in Constructor::d_vars
68  // Moved from Grid::m_duplicate. jhrg 4/3/13
69  d_vars.clear(); // [mjohnson 10 Sep 2009]
70 
71  for (auto var: c.d_vars) {
72  BaseType *btp = var->ptr_duplicate();
73  btp->set_parent(this);
74  d_vars.push_back(btp);
75  }
76 }
77 
78 // Public member functions
79 
80 // A public method, but just barely...
81 // TODO Understand what this method does. What is dest? Is it the parent-to-be
82 // of the variables in this Constructor? jhrg 4/25/22
83 void
84 Constructor::transform_to_dap4(D4Group *root, Constructor *dest)
85 {
86  for (Constructor::Vars_citer i = var_begin(), e = var_end(); i != e; ++i) {
87 
88  BaseType *d4_var = dest->var((*i)->name());
89  // Don't add duplicate variables. We have to make this check
90  // because some child variables may add arrays
91  // to the root object. For example, this happens in
92  // Grid with the Map Arrays - ndp - 05/08/17
93  if (!d4_var) {
94  (*i)->transform_to_dap4(root /*group*/, dest /*container*/);
95  }
96  }
97  dest->attributes()->transform_to_dap4(get_attr_table());
98  dest->set_is_dap4(true);
99 }
100 
101 string
102 Constructor::FQN() const
103 {
104  if (get_parent() == 0)
105  return name();
106  else if (get_parent()->type() == dods_group_c)
107  return get_parent()->FQN() + name();
108  else if (get_parent()->type() == dods_array_c)
109  return get_parent()->FQN();
110  else
111  return get_parent()->FQN() + "." + name();
112 }
113 
114 int
115 Constructor::element_count(bool leaves)
116 {
117  if (!leaves)
118  return d_vars.size();
119  else {
120  int i = 0;
121  for (auto var: d_vars) {
122  i += var->element_count(leaves);
123  }
124  return i;
125  }
126 }
127 
128 void
129 Constructor::set_send_p(bool state)
130 {
131  for (auto var: d_vars) {
132  var->set_send_p(state);
133  }
134 
135  BaseType::set_send_p(state);
136 }
137 
149 void
150 Constructor::set_read_p(bool state)
151 {
152  for (auto var: d_vars) {
153  var->set_read_p(state);
154  }
155 
156  BaseType::set_read_p(state);
157 }
158 
166 unsigned int
167 Constructor::width(bool constrained) const
168 {
169  unsigned int sz = 0;
170 
171  for (auto var: d_vars) {
172  if (constrained) {
173  if (var->send_p())
174  sz += var->width(constrained);
175  }
176  else {
177  sz += var->width(constrained);
178  }
179  }
180 
181  return sz;
182 }
183 
184 BaseType *
185 Constructor::var(const string &name, bool exact_match, btp_stack *s)
186 {
187  string n = www2id(name);
188 
189  if (exact_match)
190  return m_exact_match(n, s);
191  else
192  return m_leaf_match(n, s);
193 }
194 
196 BaseType *
197 Constructor::var(const string &n, btp_stack &s)
198 {
199  // This should probably be removed. The BES code should remove web encoding
200  // with the possible exception of spaces. jhrg 11/25/13
201  string name = www2id(n);
202 
203  BaseType *btp = m_exact_match(name, &s);
204  if (btp)
205  return btp;
206 
207  return m_leaf_match(name, &s);
208 }
209 
210 // Protected method
211 BaseType *
212 Constructor::m_leaf_match(const string &name, btp_stack *s)
213 {
214  for (auto var: d_vars) {
215  if (var->name() == name) {
216  if (s) {
217  s->push(static_cast<BaseType *>(this));
218  }
219  return var;
220  }
221  if (var->is_constructor_type()) {
222  BaseType *btp = var->var(name, false, s);
223  if (btp) {
224  if (s) {
225  s->push(static_cast<BaseType *>(this));
226  }
227  return btp;
228  }
229  }
230  }
231 
232  return nullptr;
233 }
234 
235 // Protected method
236 BaseType *
237 Constructor::m_exact_match(const string &name, btp_stack *s)
238 {
239  // Look for name at the top level first.
240  for (auto var: d_vars) {
241  if (var->name() == name) {
242  if (s)
243  s->push(static_cast<BaseType *>(this));
244 
245  return var;
246  }
247  }
248 
249  // If it was not found using the simple search, look for a dot and
250  // search the hierarchy.
251  string::size_type dot_pos = name.find("."); // zero-based index of `.'
252  if (dot_pos != string::npos) {
253  string aggregate = name.substr(0, dot_pos);
254  string field = name.substr(dot_pos + 1);
255 
256  BaseType *agg_ptr = var(aggregate);
257  if (agg_ptr) {
258  if (s)
259  s->push(static_cast<BaseType *>(this));
260 
261  return agg_ptr->var(field, true, s); // recurse
262  }
263  else
264  return nullptr; // qualified names must be *fully* qualified
265  }
266 
267  return nullptr;
268 }
269 
271 Constructor::Vars_iter
272 Constructor::var_begin()
273 {
274  return d_vars.begin();
275 }
276 
279 Constructor::Vars_iter
280 Constructor::var_end()
281 {
282  return d_vars.end();
283 }
284 
286 Constructor::Vars_riter
287 Constructor::var_rbegin()
288 {
289  return d_vars.rbegin();
290 }
291 
294 Constructor::Vars_riter
295 Constructor::var_rend()
296 {
297  return d_vars.rend();
298 }
299 
303 Constructor::Vars_iter
304 Constructor::get_vars_iter(int i)
305 {
306  return d_vars.begin() + i;
307 }
308 
312 BaseType *
313 Constructor::get_var_index(int i)
314 {
315  return *(d_vars.begin() + i);
316 }
317 
322 void
323 Constructor::add_var(BaseType *bt, Part)
324 {
325  // Jose Garcia
326  // Passing and invalid pointer to an object is a developer's error.
327  if (!bt)
328  throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
329 
330  // Jose Garcia
331  // Now we add a copy of bt so the external user is able to destroy bt as
332  // he/she wishes. The policy is: "If it is allocated outside, it is
333  // deallocated outside, if it is allocated inside, it is deallocated
334  // inside"
335  BaseType *btp = bt->ptr_duplicate();
336  btp->set_parent(this);
337  d_vars.push_back(btp);
338 }
339 
344 void
345 Constructor::add_var_nocopy(BaseType *bt, Part)
346 {
347  if (!bt)
348  throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
349 
350  bt->set_parent(this);
351  d_vars.push_back(bt);
352 }
353 
361 void
362 Constructor::del_var(const string &n)
363 {
364  auto to_remove = stable_partition(d_vars.begin(), d_vars.end(),
365  [n](BaseType* btp){ return btp->name() != n; });
366  for_each(to_remove, d_vars.end(), [](BaseType* btp){ delete btp; });
367  d_vars.erase(to_remove, d_vars.end());
368 }
369 
375 void
376 Constructor::del_var(Vars_iter i)
377 {
378  delete *i;
379  d_vars.erase(i);
380 }
381 
393 bool Constructor::read()
394 {
395  if (!read_p()) {
396  for (auto var: d_vars) {
397  if (var->send_p())
398  var->read();
399  }
400  // Set read_p for the Constructor
401  BaseType::set_read_p(true);
402  }
403 
404  return false;
405 }
406 
407 void
408 Constructor::intern_data(ConstraintEvaluator & eval, DDS & dds)
409 {
410  if (!read_p())
411  read(); // read() throws Error and InternalErr
412 
413  for (auto var: d_vars) {
414  if (var->send_p()) {
415  var->intern_data(eval, dds);
416  }
417  }
418 }
419 
420 bool
421 Constructor::serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval)
422 {
423  if (!read_p())
424  read(); // read() throws Error and InternalErr
425 
426  if (ce_eval && !eval.eval_selection(dds, dataset()))
427  return true;
428 
429  for (auto var: d_vars) {
430  if (var->send_p()) {
431 #ifdef CHECKSUMS
432  XDRStreamMarshaller *sm = dynamic_cast<XDRStreamMarshaller*>(&m);
433  if (sm && sm->checksums() && var->type() != dods_structure_c && var->type() != dods_grid_c)
434  sm->reset_checksum();
435 
436  var->serialize(eval, dds, m, false);
437 
438  if (sm && sm->checksums() && var->type() != dods_structure_c && var->type() != dods_grid_c)
439  sm->get_checksum();
440 #else
441  // (*i)->serialize(eval, dds, m, false);
442  // Only Sequence and Vector run the evaluator.
443  var->serialize(eval, dds, m, true);
444 #endif
445  }
446  }
447 
448  return true;
449 }
450 
451 bool
452 Constructor::deserialize(UnMarshaller &um, DDS *dds, bool reuse)
453 {
454  for (auto var: d_vars) {
455  var->deserialize(um, dds, reuse);
456  }
457 
458  return false;
459 }
460 
461 void
462 Constructor::compute_checksum(Crc32 &)
463 {
464  throw InternalErr(__FILE__, __LINE__, "Computing a checksum alone is not supported for Constructor types.");
465 }
466 
467 void
468 Constructor::intern_data()
469 {
470  for (auto var: d_vars) {
471  if (var->send_p()) {
472  var->intern_data(/*checksum, dmr, eval*/);
473  }
474  }
475 }
476 
488 void
489 Constructor::serialize(D4StreamMarshaller &m, DMR &dmr, bool filter)
490 {
491  // Not used for the same reason the equivalent code in D4Group::serialize()
492  // is not used. Fail for D4Sequence and general issues with memory use.
493  //
494  // Revisit this - I had to uncomment this to get the netcdf_handler code
495  // to work - it relies on having NCStructure::read() called. The D4Sequence
496  // ::serialize() method calls read_next_instance(). What seems to be happening
497  // is that this call to read gets the first set of values, but does not store
498  // them; the call to serialize then runs the D4Sequence::serialize() method that
499  // _does_ read all the sequence data and then serialize it. However, the first
500  // sequence instance is missing...
501  if (!read_p())
502  read(); // read() throws Error
503 
504  for (auto var: d_vars) {
505  if (var->send_p()) {
506  var->serialize(m, dmr, filter);
507  }
508  }
509 }
510 
511 void
512 Constructor::deserialize(D4StreamUnMarshaller &um, DMR &dmr)
513 {
514  for (auto var: d_vars) {
515  var->deserialize(um, dmr);
516  }
517 }
518 
519 void
520 Constructor::print_decl(FILE *out, string space, bool print_semi,
521  bool constraint_info, bool constrained)
522 {
523  ostringstream oss;
524  print_decl(oss, space, print_semi, constraint_info, constrained);
525  fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
526 }
527 
528 void
529 Constructor::print_decl(ostream &out, string space, bool print_semi,
530  bool constraint_info, bool constrained)
531 {
532  if (constrained && !send_p())
533  return;
534 
535  out << space << type_name() << " {\n" ;
536  for (auto var: d_vars) {
537  var->print_decl(out, space + " ", true, constraint_info, constrained);
538  }
539  out << space << "} " << id2www(name()) ;
540 
541  if (constraint_info) { // Used by test drivers only.
542  if (send_p())
543  out << ": Send True";
544  else
545  out << ": Send False";
546  }
547 
548  if (print_semi)
549  out << ";\n" ;
550 }
551 
552 void
553 Constructor::print_val(FILE *out, string space, bool print_decl_p)
554 {
555  ostringstream oss;
556  print_val(oss, space, print_decl_p);
557  fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
558 }
559 
560 void
561 Constructor::print_val(ostream &out, string space, bool print_decl_p)
562 {
563  if (print_decl_p) {
564  print_decl(out, space, false);
565  out << " = " ;
566  }
567 
568  out << "{ " ;
569  for (Vars_citer i = d_vars.begin(), e = d_vars.end(); i != e; i++, (void)(i != e && out << ", ")) {
570  (*i)->print_val(out, "", false);
571  }
572 
573  out << " }" ;
574 
575  if (print_decl_p)
576  out << ";\n" ;
577 }
578 
582 void
583 Constructor::print_xml(FILE *out, string space, bool constrained)
584 {
585  XMLWriter xml(space);
586  print_xml_writer(xml, constrained);
587  fwrite(xml.get_doc(), sizeof(char), xml.get_doc_size(), out);
588 }
589 
593 void
594 Constructor::print_xml(ostream &out, string space, bool constrained)
595 {
596  XMLWriter xml(space);
597  print_xml_writer(xml, constrained);
598  out << xml.get_doc();
599 }
600 
601 void
602 Constructor::print_xml_writer(XMLWriter &xml, bool constrained)
603 {
604  if (constrained && !send_p())
605  return;
606 
607  if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*)type_name().c_str()) < 0)
608  throw InternalErr(__FILE__, __LINE__, "Could not write " + type_name() + " element");
609 
610  if (!name().empty())
611  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*)name().c_str()) < 0)
612  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
613 
614  // DAP2 prints attributes first. For some reason we decided that DAP4 should
615  // print them second. No idea why... jhrg 8/15/14
616  if (!is_dap4() && get_attr_table().get_size() > 0)
617  get_attr_table().print_xml_writer(xml);
618 
619  if (!d_vars.empty())
620  for_each(d_vars.begin(), d_vars.end(),
621  [&xml, constrained](BaseType *btp) { btp->print_xml_writer(xml, constrained); });
622 
623  if (is_dap4())
624  attributes()->print_dap4(xml);
625 
626  if (xmlTextWriterEndElement(xml.get_writer()) < 0)
627  throw InternalErr(__FILE__, __LINE__, "Could not end " + type_name() + " element");
628 }
629 
630 void
631 Constructor::print_dap4(XMLWriter &xml, bool constrained)
632 {
633  if (constrained && !send_p())
634  return;
635 
636  if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*)type_name().c_str()) < 0)
637  throw InternalErr(__FILE__, __LINE__, "Could not write " + type_name() + " element");
638 
639  if (!name().empty())
640  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*)name().c_str()) < 0)
641  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
642 
643  if (!d_vars.empty())
644  for_each(d_vars.begin(), d_vars.end(),
645  [&xml, constrained](BaseType *btp) { btp->print_dap4(xml, constrained); });
646 
647  attributes()->print_dap4(xml);
648 
649  if (xmlTextWriterEndElement(xml.get_writer()) < 0)
650  throw InternalErr(__FILE__, __LINE__, "Could not end " + type_name() + " element");
651 }
652 
653 bool
654 Constructor::check_semantics(string &msg, bool all)
655 {
656  if (!BaseType::check_semantics(msg))
657  return false;
658 
659  if (!unique_names(d_vars, name(), type_name(), msg))
660  return false;
661 
662  if (all) {
663  for (auto var: d_vars) {
664  if (!var->check_semantics(msg, true)) {
665  return false;
666  }
667  }
668  }
669 
670  return true;
671 }
672 
682 bool
683 Constructor::is_linear()
684 {
685  return false;
686 }
687 
693 void
694 Constructor::set_in_selection(bool state)
695 {
696  for (auto var: d_vars) {
697  var->set_in_selection(state);
698  }
699 
700  BaseType::set_in_selection(state);
701 }
702 
703 void Constructor::transfer_attributes(AttrTable *at_container)
704 {
705  AttrTable *at = at_container->get_attr_table(name());
706 
707  if (at) {
708  BaseType::transfer_attributes(at_container);
709  for (auto var: d_vars) {
710  var->transfer_attributes(at);
711  }
712  }
713 }
714 
715 AttrTable *
716 Constructor::make_dropped_vars_attr_table(vector<BaseType *> *dropped_vars)
717 {
718  AttrTable *dv_table = nullptr;
719  if (!dropped_vars->empty()) {
720  dv_table = new AttrTable;
721  dv_table->set_name("dap4:dropped_members");
722 
723  vector<BaseType *>::iterator dvIter = dropped_vars->begin();
724  vector<BaseType *>::iterator dvEnd = dropped_vars->end();
725  unsigned int i = 0;
726  for (; dvIter != dvEnd; dvIter++, i++) {
727  BaseType *bt = (*dvIter);
728 
729  AttrTable *bt_attr_table = new AttrTable(bt->get_attr_table());
730  bt_attr_table->set_name(bt->name());
731  string type_name = bt->type_name();
732 
733  if (bt->is_vector_type()) {
734  Array *array = dynamic_cast <Array *>(bt);
735  if (array) { // This is always true - only an Array is_vector_type(). jhrg 4/25/22
736  type_name = array->prototype()->type_name();
737  Array::Dim_iter d_iter = array->dim_begin();
738  Array::Dim_iter end = array->dim_end();
739  for (; d_iter < end; d_iter++) {
740 
741  ostringstream dim_size;
742  dim_size << (*d_iter).size;
743  bt_attr_table->append_attr("array_dimensions", AttrType_to_String(Attr_uint32), dim_size.str());
744  }
745  }
746  }
747 
748  bt_attr_table->append_attr("dap4:type", "String", type_name);
749  dv_table->append_container(bt_attr_table, bt_attr_table->get_name());
750  // Clear entry now that we're done.
751  (*dvIter) = 0;
752  }
753  }
754 
755  return dv_table;
756 }
757 
766 void
767 Constructor::dump(ostream &strm) const {
768  strm << DapIndent::LMarg << "Constructor::dump - (" << (void *) this << ")" << endl;
769  DapIndent::Indent();
770  BaseType::dump(strm);
771  strm << DapIndent::LMarg << "vars: " << endl;
772  DapIndent::Indent();
773 
774  for (auto var: d_vars) {
775  var->dump(strm);
776  }
777 
778  DapIndent::UnIndent();
779  DapIndent::UnIndent();
780 }
781 
782 } // namespace libdap
783 
Definition: crc.h:77
A multidimensional array of identical data types.
Definition: Array.h:113
std::vector< dimension >::iterator Dim_iter
Definition: Array.h:206
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 AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition: AttrTable.cc:607
virtual unsigned int append_attr(const string &name, const string &type, const string &value)
Add an attribute to the table.
Definition: AttrTable.cc:307
virtual string get_name() const
Get the name of this attribute table.
Definition: AttrTable.cc:238
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_parent(BaseType *parent)
Definition: BaseType.cc:729
virtual bool is_vector_type() const
Returns true if the instance is a vector (i.e., array) type variable.
Definition: BaseType.cc:398
virtual D4Attributes * attributes()
Definition: BaseType.cc:595
virtual void transform_to_dap4(D4Group *root, Constructor *container)
DAP2 to DAP4 transform.
Definition: BaseType.cc:212
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=nullptr)
Returns a pointer to a member of a constructor class.
Definition: BaseType.cc:754
Evaluate a constraint expression.
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 ...
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 transform_to_dap4(AttrTable &at)
copy attributes from DAP2 to DAP4
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4's receiv...
Read data from the stream made by D4StreamMarshaller.
A class for software fault reporting.
Definition: InternalErr.h:65
abstract base class used to marshal/serialize dap data objects
Definition: Marshaller.h:50
abstract base class used to unmarshall/deserialize dap data objects
Definition: UnMarshaller.h:55
Marshaller that knows how serialize dap data objects to a C++ iostream using XDR.
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
string type_name(Type t)
Definition: util.cc:763
string AttrType_to_String(const AttrType at)
Definition: AttrTable.cc:97
Part
Names the parts of multi-section constructor data types.
Definition: Type.h:48
string id2www(string in, const string &allowable)
Definition: escaping.cc:153