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