-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDDS.cc
1441 lines (1210 loc) · 50.1 KB
/
DDS.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
// (c) COPYRIGHT URI/MIT 1994-1999
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
//
// jhrg 9/7/94
#include "config.h"
#include <climits>
#include <cmath>
#include <cstdio>
#ifdef WIN32
#include <fstream>
#include <io.h>
#include <process.h>
#else
#include <sys/wait.h>
#include <unistd.h> // for alarm and dup
#endif
#include <algorithm>
#include <functional>
#include <iostream>
#include <memory>
#include <sstream>
#include "Array.h"
#include "Byte.h"
#include "Float32.h"
#include "Float64.h"
#include "Grid.h"
#include "Int16.h"
#include "Int32.h"
#include "Sequence.h"
#include "Str.h"
#include "Structure.h"
#include "UInt16.h"
#include "UInt32.h"
#include "Url.h"
#include "Clause.h"
#include "DAS.h"
#include "DapIndent.h"
#include "Error.h"
#include "InternalErr.h"
#include "debug.h"
#include "escaping.h"
#include "parser.h"
#include "util.h"
/**
* DapXmlNamespaces
*
* @todo Replace all usages of the following variable with calls to DapXmlNamespaces
*/
const string c_xml_xsi = "http://www.w3.org/2001/XMLSchema-instance";
const string c_xml_namespace = "http://www.w3.org/XML/1998/namespace";
const string grddl_transformation_dap32 = "http://xml.opendap.org/transforms/ddxToRdfTriples.xsl";
const string c_default_dap20_schema_location = "http://xml.opendap.org/dap/dap2.xsd";
const string c_default_dap32_schema_location = "http://xml.opendap.org/dap/dap3.2.xsd";
const string c_default_dap40_schema_location = "http://xml.opendap.org/dap/dap4.0.xsd";
const string c_dap20_namespace = "http://xml.opendap.org/ns/DAP2";
const string c_dap32_namespace = "http://xml.opendap.org/ns/DAP/3.2#";
const string c_dap40_namespace = "http://xml.opendap.org/ns/DAP/4.0#";
const string c_dap_20_n_sl = c_dap20_namespace + " " + c_default_dap20_schema_location;
const string c_dap_32_n_sl = c_dap32_namespace + " " + c_default_dap32_schema_location;
const string c_dap_40_n_sl = c_dap40_namespace + " " + c_default_dap40_schema_location;
/// Name given to a container for orphaned top-level attributes.
/// These can show up when a DAS is built from a DMR because DAP4
/// supports attributes at the top level that are not in any container.
const string TOP_LEVEL_ATTRS_CONTAINER_NAME = "DAP4_GLOBAL";
using namespace std;
int ddsparse(libdap::parser_arg *arg);
// Glue for the DDS parser defined in dds.lex
void dds_switch_to_buffer(void *new_buffer);
void dds_delete_buffer(void *buffer);
void *dds_buffer(FILE *fp);
namespace libdap {
void DDS::duplicate(const DDS &dds) {
DBG(cerr << "Entering DDS::duplicate... " << endl);
d_factory = dds.d_factory;
d_name = dds.d_name;
d_filename = dds.d_filename;
d_container_name = dds.d_container_name;
d_container = dds.d_container;
d_dap_major = dds.d_dap_major;
d_dap_minor = dds.d_dap_minor;
d_dap_version = dds.d_dap_version; // String version of the protocol
d_request_xml_base = dds.d_request_xml_base;
d_namespace = dds.d_namespace;
d_attr = dds.d_attr;
DDS &dds_tmp = const_cast<DDS &>(dds);
// copy the things pointed to by the list, not just the pointers
for (Vars_iter i = dds_tmp.var_begin(); i != dds_tmp.var_end(); i++) {
add_var(*i); // add_var() dups the BaseType.
}
d_timeout = dds.d_timeout;
d_max_response_size_kb = dds.d_max_response_size_kb;
}
/**
* Make a DDS which uses the given BaseTypeFactory to create variables.
*
* @note The default DAP version is 3.2 - this is really DAP2 with a handful
* of enhancements that our WCS software relies on.
*
* @param factory The BaseTypeFactory to use when creating instances of
* DAP variables. The caller must ensure the factory's lifetime is at least
* that of the DDS instance.
* @param name The name of the DDS - usually derived from the name of the
* pathname or table name of the dataset.
*/
DDS::DDS(BaseTypeFactory *factory, const string &name)
: d_factory(factory), d_name(name), d_container_name(""), d_container(0), d_request_xml_base(""), d_timeout(0),
/*d_keywords(),*/ d_max_response_size_kb(0) {
DBG(cerr << "Building a DDS for the default version (2.0)" << endl);
// This method sets a number of values, including those returned by
// get_protocol_major(), ..., get_namespace().
set_dap_version("2.0");
}
/**
* Make a DDS with the DAP protocol set to a specific value. This method
* provides an easy way to build DDS objects for use in a server or client
* that will process DAP4, for example. It's roughly equivalent to calling
* set_dap_version() after making an instance using
* DDS::DDS(BaseTypeFactory *, const string &).
*
* @param factory The BaseTypeFactory to use when creating instances of
* DAP variables. The caller must ensure the factory's lifetime is at least
* that of the DDS instance.
* @param name The name of the DDS - usually derived from the name of the
* pathname or table name of the dataset.
* @param version The DAP version to support. This sets the DAP version, as
* well as a number of other dependent constants.
*/
DDS::DDS(BaseTypeFactory *factory, const string &name, const string &version)
: d_factory(factory), d_name(name), d_container_name(""), d_container(0), d_request_xml_base(""), d_timeout(0),
/*d_keywords(),*/ d_max_response_size_kb(0) {
DBG(cerr << "Building a DDS for version: " << version << endl);
// This method sets a number of values, including those returned by
// get_protocol_major(), ..., get_namespace().
set_dap_version(version);
}
/** The DDS copy constructor. */
DDS::DDS(const DDS &rhs) : DapObj() {
DBG(cerr << "Entering DDS(const DDS &rhs) ..." << endl);
duplicate(rhs);
DBG(cerr << " bye." << endl);
}
DDS::~DDS() {
// delete all the variables in this DDS
for (Vars_iter i = vars.begin(); i != vars.end(); i++) {
BaseType *btp = *i;
delete btp;
btp = 0;
}
}
DDS &DDS::operator=(const DDS &rhs) {
if (this == &rhs)
return *this;
duplicate(rhs);
return *this;
}
/**
* This is the main method used to transfer attributes from a DAS object into a
* DDS. This uses the BaseType::transfer_attributes() method and the various
* implementations found here (in the constructors classes) and in handlers.
*
* This method uses a deep copy to transfer the attributes, so it is safe to
* delete the source DAS object passed to this method once it is done.
*
* @note To accommodate oddly built DAS objects produced by various handlers,
* specialize the methods there.
*
* @param das Transfer (copy) attributes from this DAS object.
*/
void DDS::transfer_attributes(DAS *das) {
// If there is a container set in the DDS then check the container from
// the DAS. If they are not the same container, then throw an exception
// (should be working on the same container). If the container does not
// exist in the DAS, then throw an exception
if (d_container && das->container_name() != d_container_name)
throw InternalErr(__FILE__, __LINE__,
"Error transferring attributes: working on a container in dds, but not das");
// Give each variable a chance to claim its attributes.
AttrTable *top = das->get_top_level_attributes();
for (DDS::Vars_iter i = var_begin(), e = var_end(); i != e; i++) {
(*i)->transfer_attributes(top);
}
// Now we transfer all the attributes still marked as global to the
// global container in the DDS.
for (AttrTable::Attr_iter i = top->attr_begin(), e = top->attr_end(); i != e; ++i) {
if ((*i)->type == Attr_container && (*i)->attributes->is_global_attribute()) {
// copy the source container so that the DAS passed in can be
// deleted after calling this method.
AttrTable *at = new AttrTable(*(*i)->attributes);
d_attr.append_container(at, at->get_name());
}
}
}
/** Get and set the dataset's d_name. This is the d_name of the dataset
itself, and is not to be confused with the d_name of the file or
disk on which it is stored.
@d_name Dataset Name Accessors */
//@{
/** Returns the dataset's d_name. */
string DDS::get_dataset_name() const { return d_name; }
/** Sets the dataset d_name. */
void DDS::set_dataset_name(const string &n) { d_name = n; }
//@}
/** Get the attribute table for the global attributes. */
AttrTable &DDS::get_attr_table() { return d_attr; }
/** Get and set the dataset's filename. This is the physical
location on a disk where the dataset exists. The dataset d_name
is simply a title.
@d_name File Name Accessor
@see Dataset Name Accessors */
//@{
/** Gets the dataset file d_name. */
string DDS::filename() const { return d_filename; }
/** Set the dataset's filename. */
void DDS::filename(const string &fn) { d_filename = fn; }
//@}
/**
* @deprecated
*/
void DDS::set_dap_major(int p) {
d_dap_major = p;
// This works because regardless of the order set_dap_major and set_dap_minor
// are called, once they both are called, the value in the string is
// correct. I protect against negative numbers because that would be
// nonsensical.
if (d_dap_minor >= 0) {
ostringstream oss;
oss << d_dap_major << "." << d_dap_minor;
d_dap_version = oss.str();
}
}
/**
* @deprecated
*/
void DDS::set_dap_minor(int p) {
d_dap_minor = p;
if (d_dap_major >= 0) {
ostringstream oss;
oss << d_dap_major << "." << d_dap_minor;
d_dap_version = oss.str();
}
}
/**
* Given the DAP protocol version, parse that string and set the DDS fields.
*
* @param v The version string.
*/
void DDS::set_dap_version(const string &v /* = "2.0" */) {
istringstream iss(v);
int major = -1, minor = -1;
char dot;
if (!iss.eof() && !iss.fail())
iss >> major;
if (!iss.eof() && !iss.fail())
iss >> dot;
if (!iss.eof() && !iss.fail())
iss >> minor;
if (major == -1 || minor == -1 or dot != '.')
throw InternalErr(__FILE__, __LINE__, "Could not parse dap version. Value given: " + v);
d_dap_version = v;
d_dap_major = major;
d_dap_minor = minor;
// Now set the related XML constants. These might be overwritten if
// the DDS instance is being built from a document parse, but if it's
// being constructed by a server the code to generate the XML document
// needs these values to match the DAP version information.
switch (d_dap_major) {
case 2:
d_namespace = c_dap20_namespace;
break;
case 3:
d_namespace = c_dap32_namespace;
break;
case 4:
d_namespace = c_dap40_namespace;
break;
default:
throw InternalErr(__FILE__, __LINE__, "Unknown DAP version.");
}
}
/** Old way to set the DAP version.
*
* @note Don't use this - two interfaces to set the version number is overkill
*
* @param d The protocol version requested by the client, as a double.
* @deprecated
*/
void DDS::set_dap_version(double d) {
int major = floor(d);
int minor = (d - major) * 10;
DBG(cerr << "Major: " << major << ", Minor: " << minor << endl);
ostringstream oss;
oss << major << "." << minor;
set_dap_version(oss.str());
}
/** Get and set the current container. If there are multiple files being
used to build this DDS, using a container will set a virtual structure
for the current container.
@d_name Container Name Accessor
@see Dataset Name Accessors */
//@{
/** Gets the dataset file d_name. */
string DDS::container_name() { return d_container_name; }
/** Set the current container d_name and get or create a structure for that
* d_name. */
void DDS::container_name(const string &cn) {
// we want to search the DDS for the top level structure with the given
// d_name. Set the container to null so that we don't search some previous
// container.
d_container = 0;
if (!cn.empty()) {
d_container = dynamic_cast<Structure *>(var(cn));
if (!d_container) {
// create a structure for this container. Calling add_var
// while_container is null will add the new structure to DDS and
// not some sub structure. Adding the new structure makes a copy
// of it. So after adding it, go get it and set d_container.
Structure *s = new Structure(cn);
add_var(s);
delete s;
s = 0;
d_container = dynamic_cast<Structure *>(var(cn));
}
}
d_container_name = cn;
}
/** Get the current container structure. */
Structure *DDS::container() { return d_container; }
//@}
/** Get the size of a response. This method looks at the variables in the DDS
* a computes the number of bytes in the response.
*
* @note This version of the method does a poor job with Sequences. A better
* implementation would look at row-constraint-based limitations and use them
* for size computations. If a row-constraint is missing, return an error.
*
* @param constrained Should the size of the whole DDS be used or should the
* current constraint be taken into account?
* @deprecated "Use DDS::get_request_size_kb()"
*/
//[[deprecated("Use DDS::get_request_size_kb()")]]
int DDS::get_request_size(bool constrained) {
int w = 0;
for (Vars_iter i = vars.begin(); i != vars.end(); i++) {
if (constrained) {
if ((*i)->send_p())
w += (*i)->width(constrained);
} else {
w += (*i)->width(constrained);
}
}
return w;
}
/**
* @brief Get the estimated size of a response in kilobytes.
* This method looks at the variables in the DDS and computes
* the number of bytes in the response.
*
* @note This version of the method does a poor job with Sequences. A better
* implementation would look at row-constraint-based limitations and use them
* for size computations. If a row-constraint is missing, return an error.
*
* @param constrained Should the size of the whole DDS be used or should the
* current constraint be taken into account?
*/
uint64_t DDS::get_request_size_kb(bool constrained) {
uint64_t req_size = 0;
for (auto &btp : vars) {
if (constrained) {
if (btp->send_p())
req_size += btp->width_ll(constrained);
} else {
req_size += btp->width_ll(constrained);
}
}
return req_size / 1024;
}
/** @brief Adds a copy of the variable to the DDS.
Using the ptr_duplicate() method, perform a deep copy on the variable
\e bt and adds the result to this DDS.
@note The copy will not copy data values.
@param bt Source variable. */
void DDS::add_var(BaseType *bt) {
if (!bt)
throw InternalErr(__FILE__, __LINE__, "Trying to add a BaseType object with a NULL pointer.");
DBG2(cerr << "In DDS::add_var(), bt's address is: " << bt << endl);
BaseType *btp = bt->ptr_duplicate();
DBG2(cerr << "In DDS::add_var(), btp's address is: " << btp << endl);
if (d_container) {
// Mem leak fix [mjohnson nov 2009]
// Structure::add_var() creates ANOTHER copy.
d_container->add_var(bt);
// So we need to delete btp or else it leaks
delete btp;
btp = 0;
} else {
vars.push_back(btp);
}
}
/** @brief Adds the variable to the DDS.
@param bt Source variable. */
void DDS::add_var_nocopy(BaseType *bt) {
if (!bt)
throw InternalErr(__FILE__, __LINE__, "Trying to add a BaseType object with a NULL pointer.");
DBG2(cerr << "In DDS::add_var(), bt's address is: " << bt << endl);
if (d_container) {
d_container->add_var_nocopy(bt);
} else {
vars.push_back(bt);
}
}
/** Remove the named variable from the DDS. This method is not smart about
looking up names. The variable must exist at the top level of the DDS and
must match \e exactly the d_name given.
@note Invalidates any iterators that reference the contents of the DDS.
@param n The d_name of the variable to remove. */
void DDS::del_var(const string &n) {
if (d_container) {
d_container->del_var(n);
return;
}
for (Vars_iter i = vars.begin(); i != vars.end(); i++) {
if ((*i)->name() == n) {
BaseType *bt = *i;
vars.erase(i);
delete bt;
bt = 0;
return;
}
}
}
/** Remove the variable referenced by the iterator and free its storage.
@note Invalidates any iterators that reference the contents of the DDS.
@param i The Vars_iter which refers to the variable. */
void DDS::del_var(Vars_iter i) {
if (i != vars.end()) {
BaseType *bt = *i;
vars.erase(i);
delete bt;
bt = 0;
}
}
/** Remove the variables referenced by the range of iterators and free their
storage.
@note Invalidates any iterators that reference the contents of the DDS.
@param i1 The start of the range.
@param i2 The end of the range. */
void DDS::del_var(Vars_iter i1, Vars_iter i2) {
for (Vars_iter i_tmp = i1; i_tmp != i2; i_tmp++) {
BaseType *bt = *i_tmp;
delete bt;
bt = 0;
}
vars.erase(i1, i2);
}
/** Search for for variable <i>n</i> as above but record all
compound type variables which ultimately contain <i>n</i> on
<i>s</i>. This stack can then be used to mark the contained
compound-type variables as part of the current projection.
@return A BaseType pointer to the variable <i>n</i> or 0 if <i>n</i>
could not be found. */
BaseType *DDS::var(const string &n, BaseType::btp_stack &s) { return var(n, &s); }
/** @brief Find the variable with the given d_name.
Returns a pointer to the named variable. If the d_name contains one or
more field separators then the function looks for a variable whose
name matches exactly. If the d_name contains no field separators then
the function looks first in the top level and then in all subsequent
levels and returns the first occurrence found. In general, this
function searches constructor types in the order in which they appear
in the DDS, but there is no requirement that it do so.
@note If a dataset contains two constructor types which have field names
that are the same (say point.x and pair.x) you should use fully qualified
names to get each of those variables.
@param n The name of the variable to find.
@param s If given, this value-result parameter holds the path to the
returned BaseType. Thus, this method can return the FQN for the variable
\e n.
@return A BaseType pointer to the variable or null if not found. */
BaseType *DDS::var(const string &n, BaseType::btp_stack *s) {
string name = www2id(n);
if (d_container)
return d_container->var(name, false, s);
BaseType *v = exact_match(name, s);
if (v)
return v;
return leaf_match(name, s);
}
BaseType *DDS::leaf_match(const string &n, BaseType::btp_stack *s) {
DBG(cerr << "DDS::leaf_match: Looking for " << n << endl);
for (Vars_iter i = vars.begin(); i != vars.end(); i++) {
BaseType *btp = *i;
DBG(cerr << "DDS::leaf_match: Looking for " << n << " in: " << btp->name() << endl);
// Look for the d_name in the dataset's top-level
if (btp->name() == n) {
DBG(cerr << "Found " << n << " in: " << btp->name() << endl);
return btp;
}
if (btp->is_constructor_type()) {
BaseType *found = btp->var(n, false, s);
if (found) {
DBG(cerr << "Found " << n << " in: " << btp->name() << endl);
return found;
}
}
#if STRUCTURE_ARRAY_SYNTAX_OLD
if (btp->is_vector_type() && btp->var()->is_constructor_type()) {
s->push(btp);
BaseType *found = btp->var()->var(n, false, s);
if (found) {
DBG(cerr << "Found " << n << " in: " << btp->var()->d_name() << endl);
return found;
}
}
#endif
}
return 0; // It is not here.
}
BaseType *DDS::exact_match(const string &name, BaseType::btp_stack *s) {
for (Vars_iter i = vars.begin(); i != vars.end(); i++) {
BaseType *btp = *i;
DBG2(cerr << "Looking for " << d_name << " in: " << btp << endl);
// Look for the d_name in the current ctor type or the top level
if (btp->name() == name) {
DBG2(cerr << "Found " << d_name << " in: " << btp << endl);
return btp;
}
}
string::size_type dot_pos = name.find(".");
if (dot_pos != string::npos) {
string aggregate = name.substr(0, dot_pos);
string field = name.substr(dot_pos + 1);
BaseType *agg_ptr = var(aggregate, s);
if (agg_ptr) {
DBG2(cerr << "Descending into " << agg_ptr->name() << endl);
return agg_ptr->var(field, true, s);
} else
return 0; // qualified names must be *fully* qualified
}
return 0; // It is not here.
}
/** Insert a copy of the BaseType before the position given.
* @param i The iterator that marks the position
* @param ptr The BaseType object to copy and insert
*/
void DDS::insert_var(Vars_iter i, BaseType *ptr) { vars.insert(i, ptr->ptr_duplicate()); }
/** Insert the BaseType before the position given.
* @note Does not copy the BaseType object - that caller must not
* free the inserted object's pointer. This object will, however,
* delete the pointer when it is deleted.
* @param i The iterator that marks the position
* @param ptr The BaseType object to insert
*/
void DDS::insert_var_nocopy(Vars_iter i, BaseType *ptr) { vars.insert(i, ptr); }
/** @brief Returns the number of variables in the DDS. */
int DDS::num_var() { return vars.size(); }
void DDS::timeout_on() {
#if USE_LOCAL_TIMEOUT_SCHEME
#ifndef WIN32
alarm(d_timeout);
#endif
#endif
}
void DDS::timeout_off() {
#if USE_LOCAL_TIMEOUT_SCHEME
#ifndef WIN32
// Old behavior commented out. I think it is an error to change the value
// of d_timeout. The way this will likely be used is to set the timeout
// value once and then 'turn on' or turn off' that timeout as the situation
// dictates. The initeded use for the DDS timeout is so that timeouts for
// data responses will include the CPU resources needed to build the response
// but not the time spent transmitting the response. This may change when
// more parallelism is added to the server... These methods are called from
// BESDapResponseBuilder in bes/dap. jhrg 12/22/15
// d_timeout = alarm(0);
alarm(0);
#endif
#endif
}
void DDS::set_timeout(int) {
#if USE_LOCAL_TIMEOUT_SCHEME
// Has no effect under win32
d_timeout = t;
#endif
}
int DDS::get_timeout() {
#if USE_LOCAL_TIMEOUT_SCHEME
// Has to effect under win32
return d_timeout;
#endif
return 0;
}
/** @brief Traverse DDS, set Sequence leaf nodes. */
void DDS::tag_nested_sequences() {
for (Vars_iter i = vars.begin(); i != vars.end(); i++) {
if ((*i)->type() == dods_sequence_c)
dynamic_cast<Sequence &>(**i).set_leaf_sequence();
else if ((*i)->type() == dods_structure_c)
dynamic_cast<Structure &>(**i).set_leaf_sequence();
}
}
/** @brief Parse a DDS from a file with the given d_name. */
void DDS::parse(string fname) {
FILE *in = fopen(fname.c_str(), "r");
if (!in) {
throw Error(cannot_read_file, "Could not open: " + fname);
}
try {
parse(in);
fclose(in);
} catch (Error &e) {
fclose(in);
throw;
}
}
/** @brief Parse a DDS from a file indicated by the input file descriptor. */
void DDS::parse(int fd) {
#ifdef WIN32
int new_fd = _dup(fd);
#else
int new_fd = dup(fd);
#endif
if (new_fd < 0)
throw InternalErr(__FILE__, __LINE__, "Could not access file.");
FILE *in = fdopen(new_fd, "r");
if (!in) {
throw InternalErr(__FILE__, __LINE__, "Could not access file.");
}
try {
parse(in);
fclose(in);
} catch (Error &e) {
fclose(in);
throw;
}
}
/** @brief Parse a DDS from a file indicated by the input file descriptor.
Read the persistent representation of a DDS from the FILE *in, parse it
and create a matching binary object.
@param in Read the persistent DDS from this FILE*.
@exception InternalErr Thrown if \c in is null
@exception Error Thrown if the parse fails. */
void DDS::parse(FILE *in) {
if (!in) {
throw InternalErr(__FILE__, __LINE__, "Null input stream.");
}
void *buffer = dds_buffer(in);
dds_switch_to_buffer(buffer);
parser_arg arg(this);
bool status = ddsparse(&arg) == 0;
dds_delete_buffer(buffer);
DBG2(cout << "Status from parser: " << status << endl);
// STATUS is the result of the parser function; if a recoverable error
// was found it will be true but arg.status() will be false.
if (!status || !arg.status()) { // Check parse result
if (arg.error())
throw *arg.error();
}
}
/** @brief Print the entire DDS to the specified file. */
void DDS::print(FILE *out) {
ostringstream oss;
print(oss);
fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
}
/** @brief Print the entire DDS to the specified ostream. */
void DDS::print(ostream &out) {
out << "Dataset {\n";
for (Vars_citer i = vars.begin(); i != vars.end(); i++) {
(*i)->print_decl(out);
}
out << "} " << id2www(d_name) << ";\n";
return;
}
/**
* Does this AttrTable have descendants that are scalar or vector attributes?
*
* @param a The AttrTable
* @return true if the table contains a scalar- or vector-valued attribute,
* otherwise false.
*/
bool has_dap2_attributes(AttrTable &a) {
for (AttrTable::Attr_iter i = a.attr_begin(), e = a.attr_end(); i != e; ++i) {
if (a.get_attr_type(i) != Attr_container) {
return true;
} else if (has_dap2_attributes(*a.get_attr_table(i))) {
return true;
}
}
return false;
}
/**
* Does this variable, or any of its descendants, have attributes?
*
* @param btp The variable
* @return True if any of the variable's descendants have attributes,
* otherwise false.
*/
bool has_dap2_attributes(BaseType *btp) {
if (btp->get_attr_table().get_size() && has_dap2_attributes(btp->get_attr_table())) {
return true;
}
Constructor *cons = dynamic_cast<Constructor *>(btp);
if (cons) {
Grid *grid = dynamic_cast<Grid *>(btp);
if (grid) {
return has_dap2_attributes(grid->get_array());
} else {
for (Constructor::Vars_iter i = cons->var_begin(), e = cons->var_end(); i != e; i++) {
if (has_dap2_attributes(*i))
return true;
}
}
}
return false;
}
/**
* Print the DAP2 DAS object using attribute information recorded
* this DDS object.
*
* @note Uses default indenting of four spaces and does not follow
* (now deprecated) attribute aliases.
*
* @param out Write the DAS here.
*/
static string four_spaces = " ";
void print_var_das(ostream &out, BaseType *bt, string indent = "") {
if (!has_dap2_attributes(bt))
return;
AttrTable attr_table = bt->get_attr_table();
out << indent << add_space_encoding(bt->name()) << " {" << endl;
Constructor *cnstrctr = dynamic_cast<Constructor *>(bt);
if (cnstrctr) {
Grid *grid = dynamic_cast<Grid *>(bt);
if (grid) {
Array *gridArray = grid->get_array();
AttrTable arrayAT = gridArray->get_attr_table();
if (has_dap2_attributes(gridArray))
gridArray->get_attr_table().print(out, indent + four_spaces);
#if 0
// I dropped this because we don't want the MAP vectors showing up in the DAS
// as children of a Grid (aka flatten the Grid bro) - ndp 5/25/18
for (Grid::Map_iter mIter = grid->map_begin();
mIter != grid->map_end(); ++mIter) {
BaseType *currentMap = *mIter;
if (has_dap2_attributes(currentMap))
print_var_das(out, currentMap, indent + four_spaces);
}
#endif
} else {
attr_table.print(out, indent + four_spaces);
Constructor::Vars_iter i = cnstrctr->var_begin();
Constructor::Vars_iter e = cnstrctr->var_end();
for (; i != e; i++) {
// Only call print_var_das() if there really are attributes.
// This is made complicated because while there might be none
// for a particular var (*i), that var might be a ctor and its
// descendant might have an attribute. jhrg 3/18/18
if (has_dap2_attributes(*i))
print_var_das(out, *i, indent + four_spaces);
}
}
} else {
attr_table.print(out, indent + four_spaces);
}
out << indent << "}" << endl;
}
/**
* @brief write the DAS response given the attribute information in the DDS
*
* This method provides the same DAS response as DAS::print(), but does so
* using the AttrTables bound to the variables in this DDS object.
*
* @param out Write the DAS response to this stream
*/
void DDS::print_das(ostream &out) {
unique_ptr<DAS> das(get_das());
das->print(out);
}
/**
* @brief Get a DAS object
*
* Returns a new DAS that contains all of the Dataset attributes. This includes all Variable
* attributes as well as Global attributes. The caller is responsible for deleting the
* returned object.
*
* @return A newly allocated DAS object
*/
DAS *DDS::get_das() {
DAS *das = new DAS();
get_das(das);
return das;
}
/**
* Get a suitable name for new container for top-level attributes
* @param das Look in this DAS to find an unused name
* @return The name
*/
static string get_unique_top_level_global_container_name(DAS *das) {
// It's virtually certain that the TOP_LEVE... name will be unique. If so,
// return the name. The code tests for a table to see if the name _should not_ be used.
AttrTable *table = das->get_table(TOP_LEVEL_ATTRS_CONTAINER_NAME);
if (!table)
return TOP_LEVEL_ATTRS_CONTAINER_NAME;
// ... but the default name might already be used
unsigned int i = 0;
string name;
ostringstream oss;
while (table) {
oss.str(""); // reset to empty for the next suffix
oss << "_" << ++i;
if (!(i < UINT_MAX))
throw InternalErr(__FILE__, __LINE__, "Cannot add top-level attributes to the DAS");
name = TOP_LEVEL_ATTRS_CONTAINER_NAME + oss.str();
table = das->get_table(name);
}
return name;
}
/**
* @brief Recursive helper function for Building DAS entries for
* Constructor types.
* @param at Add Constructor content to this Attribute table
* @param bt A pointer to a BaseType which may be an
* instance of Constructor.
*/
void fillConstructorAttrTable(AttrTable *at, BaseType *bt) {
Constructor *cons = dynamic_cast<Constructor *>(bt);
if (cons) {