forked from OPENDAP/libdap4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttrTable.cc
1560 lines (1360 loc) · 52.6 KB
/
AttrTable.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 7/29/94
#include "config.h"
#include <cassert>
#include <sstream>
#include "AttrTable.h"
#include "util.h"
#include "escaping.h"
#include "DapIndent.h"
#include "debug.h"
// Should the www2id and id2www functions be used to encode attribute names?
// Probably not... jhrg 11/16/11
#define WWW_ENCODING 0
// See the note for del_attr_table(). That method now deletes the contained
// AttrTable.
#define NEW_DEL_ATTR_TABLE_BEHAVIOR 0
using std::cerr;
using std::string;
using std::endl;
using std::vector;
namespace libdap {
/** Remove %20 space encoding */
string remove_space_encoding(const string &s)
{
string::size_type pos = s.find("%20");
if (pos != string::npos) {
string n = s;
do {
n.replace(pos, 3, " ");
pos = n.find("%20");
} while (pos != string::npos);
return n;
}
else {
return s;
}
}
/** Add %20 space encoding. */
string add_space_encoding(const string &s)
{
string::size_type pos = s.find(" ");
if (pos != string::npos) {
string n = s;
do {
n.replace(pos, 1, "%20");
pos = n.find(" ");
} while (pos != string::npos);
return n;
}
else {
return s;
}
}
/** Convert an AttrType to it's string representation.
@param at The Attribute Type.
@return The type's string representation */
string AttrType_to_String(const AttrType at)
{
switch (at) {
case Attr_container:
return "Container";
case Attr_byte:
return "Byte";
case Attr_int16:
return "Int16";
case Attr_uint16:
return "UInt16";
case Attr_int32:
return "Int32";
case Attr_uint32:
return "UInt32";
case Attr_float32:
return "Float32";
case Attr_float64:
return "Float64";
case Attr_string:
return "String";
case Attr_url:
return "Url";
case Attr_other_xml:
return "OtherXML";
default:
return "";
}
}
AttrType String_to_AttrType(const string &s)
{
string s2 = s;
downcase(s2);
if (s2 == "container")
return Attr_container;
else if (s2 == "byte")
return Attr_byte;
else if (s2 == "int16")
return Attr_int16;
else if (s2 == "uint16")
return Attr_uint16;
else if (s2 == "int32")
return Attr_int32;
else if (s2 == "uint32")
return Attr_uint32;
else if (s2 == "float32")
return Attr_float32;
else if (s2 == "float64")
return Attr_float64;
else if (s2 == "string")
return Attr_string;
else if (s2 == "url")
return Attr_url;
else if (s2 == "otherxml")
return Attr_other_xml;
else
return Attr_unknown;
}
/** Clone the given attribute table in <tt>this</tt>.
Protected. */
void AttrTable::clone(const AttrTable &at)
{
d_name = at.d_name;
d_is_global_attribute = at.d_is_global_attribute;
// Set the parent to null (no parent, not in container)
// since using at.d_parent is semantically incorrect
// and potentially dangerous.
d_parent = 0;
Attr_citer i = at.attr_map.begin();
Attr_citer ie = at.attr_map.end();
for (; i != ie; ++i) {
// this deep-copies containers recursively
entry *e = new entry(*(*i));
attr_map.push_back(e);
// If the entry being added was a container,
// set its parent to this to maintain invariant.
if (e->type == Attr_container) {
assert(e->attributes);
e->attributes->d_parent = this;
}
}
}
/** @name Instance management functions */
//@{
AttrTable::AttrTable() :
DapObj(), d_name(""), d_parent(0), attr_map(), d_is_global_attribute(true)
{
}
AttrTable::AttrTable(const AttrTable &rhs) :
DapObj()
{
clone(rhs);
}
// Private
void AttrTable::delete_attr_table()
{
for (Attr_iter i = attr_map.begin(); i != attr_map.end(); ++i) {
delete *i;
}
attr_map.clear();
}
AttrTable::~AttrTable()
{
delete_attr_table();
}
AttrTable &
AttrTable::operator=(const AttrTable &rhs)
{
if (this != &rhs) {
delete_attr_table();
clone(rhs);
}
return *this;
}
//@}
/** Attributes that are containers count as one attribute, as do
attributes with both scalar and vector values.
@return The number of entries.
@brief Get the number of entries in this attribute table.
*/
unsigned int AttrTable::get_size() const
{
return attr_map.size();
}
/** @brief Get the name of this attribute table.
@return A string containing the name. */
string AttrTable::get_name() const
{
return d_name;
}
/** @brief Set the name of this attribute table.
@param n The new name of the attribute table. */
void AttrTable::set_name(const string &n)
{
#if WWW_ENCODING
d_name = www2id(n);
#else
d_name = remove_space_encoding(n);
#endif
}
#if 0
// This was taken from das.y and could be used here to make the 'dods_errors'
// attribute container like the parser used to. Then again, maybe this feature
// was just BS. jhrg (ticket 1469)
static void add_bad_attribute(AttrTable *attr, const string &type, const string &name, const string &value,
const string &msg) {
// First, if this bad value is already in a *_dods_errors container,
// then just add it. This can happen when the server side processes a DAS
// and then hands it off to a client which does the same.
// Make a new container. Call it <attr's name>_errors. If that container
// already exists, use it.
// Add the attribute.
// Add the error string to an attribute in the container called
// `<name_explanation.'.
if (attr->get_name().find("_dods_errors") != string::npos) {
attr->append_attr(name, type, value);
}
else {
// I think _dods_errors should be _dap_error. jhrg 11/16/11
string error_cont_name = attr->get_name() + "_dods_errors";
AttrTable *error_cont = attr->get_attr_table(error_cont_name);
if (!error_cont)
error_cont = attr->append_container(error_cont_name);
error_cont->append_attr(name, type, value);
#ifndef ATTR_STRING_QUOTE_FIX
error_cont->append_attr(name + "_dap_explanation", "String", "\"" + msg + "\"");
#else
error_cont->append_attr(name + "_dap_explanation", "String", msg);
#endif
}
}
#endif
/** If the given name already refers to an attribute, and the attribute has a
value, the given value is appended to the attribute vector. Calling this
function repeatedly is the way to append to an attribute vector.
The function throws an Error if the attribute is a container,
or if the type of the input value does not match the existing attribute's
type. Use <tt>append_container()</tt> to add container attributes.
This method performs a simple search for <tt>name</tt> in this attribute
table only; sub-tables are not searched and the dot notation is not
recognized.
@brief Add an attribute to the table.
@return Returns the length of the added attribute value.
@param name The name of the attribute to add or modify.
@param type The type of the attribute to add or modify.
@param value The value to add to the attribute table. */
unsigned int AttrTable::append_attr(const string &name, const string &type, const string &value)
{
DBG(cerr << "Entering AttrTable::append_attr" << endl);
#if WWW_ENCODING
string lname = www2id(name);
#else
string lname = remove_space_encoding(name);
#endif
Attr_iter iter = simple_find(lname);
// If the types don't match OR this attribute is a container, calling
// this mfunc is an error!
if (iter != attr_map.end() && ((*iter)->type != String_to_AttrType(type)))
throw Error(string("An attribute called `") + name + string("' already exists but is of a different type"));
if (iter != attr_map.end() && (get_type(iter) == "Container"))
throw Error(string("An attribute called `") + name + string("' already exists but is a container."));
if (iter != attr_map.end()) { // Must be a new attribute value; add it.
(*iter)->attr->push_back(value);
return (*iter)->attr->size();
}
else { // Must be a completely new attribute; add it
entry *e = new entry;
e->name = lname;
e->is_alias = false;
e->type = String_to_AttrType(type); // Record type using standard names.
e->attr = new vector<string> ;
e->attr->push_back(value);
attr_map.push_back(e);
return e->attr->size(); // return the length of the attr vector
}
}
/** This version of append_attr() takes a vector<string> of values.
If the given name already refers to an attribute, and the attribute has
values, append the new values to the existing ones.
The function throws an Error if the attribute is a container,
or if the type of the input value does not match the existing attribute's
type. Use <tt>append_container()</tt> to add container attributes.
This method performs a simple search for <tt>name</tt> in this attribute
table only; sub-tables are not searched and the dot notation is not
recognized.
@brief Add an attribute to the table.
@return Returns the length of the added attribute value.
@param name The name of the attribute to add or modify.
@param type The type of the attribute to add or modify.
@param values A vector of values. Note: The vector is COPIED, not stored. */
unsigned int AttrTable::append_attr(const string &name, const string &type, vector<string> *values)
{
DBG(cerr << "Entering AttrTable::append_attr(..., vector)" << endl);
#if WWW_ENCODING
string lname = www2id(name);
#else
string lname = remove_space_encoding(name);
#endif
Attr_iter iter = simple_find(lname);
// If the types don't match OR this attribute is a container, calling
// this mfunc is an error!
if (iter != attr_map.end() && ((*iter)->type != String_to_AttrType(type)))
throw Error(string("An attribute called `") + name + string("' already exists but is of a different type"));
if (iter != attr_map.end() && (get_type(iter) == "Container"))
throw Error(string("An attribute called `") + name + string("' already exists but is a container."));
if (iter != attr_map.end()) { // Must be new attribute values; add.
vector<string>::iterator i = values->begin();
while (i != values->end())
(*iter)->attr->push_back(*i++);
return (*iter)->attr->size();
}
else { // Must be a completely new attribute; add it
entry *e = new entry;
e->name = lname;
e->is_alias = false;
e->type = String_to_AttrType(type); // Record type using standard names.
e->attr = new vector<string> (*values);
attr_map.push_back(e);
return e->attr->size(); // return the length of the attr vector
}
}
/** Create and append an attribute container to this AttrTable. If this
attribute table already contains an attribute container called
<tt>name</tt> an exception is thrown. Return a pointer to the new container.
@brief Add a container to the attribute table.
@param name The name of the container to create.
@return A pointer to the new AttrTable object.
*/
AttrTable *
AttrTable::append_container(const string &name)
{
AttrTable *new_at = new AttrTable;
AttrTable *ret = NULL;
try {
ret = append_container(new_at, name);
} catch (Error &e) {
// an error occurred, attribute with that name already exists
delete new_at;
new_at = 0;
throw;
}
return ret;
}
/** Append a new attribute container to this attribute table. The new
container is <tt>at</tt> and its name is set to
<tt>name</tt>. If this attribute
table already contains an attribute container called
<tt>name</tt> an exception is thrown.
@note The value of \e name will override the name of \e at set using the
set_name() method.
@brief Add a container to the attribute table.
@param at A pointer to the new attribute table to append.
@param name The name of the new attribute table.
@return A pointer to the new AttrTable object.
*/
AttrTable *
AttrTable::append_container(AttrTable *at, const string &name)
{
#if WWW_ENCODING
string lname = www2id(name);
#else
string lname = remove_space_encoding(name);
#endif
if (simple_find(name) != attr_end())
throw Error("There already exists a container called '" + name + "' in this attribute table (" + at->get_name() + "). (1)");
DBG(cerr << "Setting appended attribute container name to: " << lname << endl);
at->set_name(lname);
entry *e = new entry;
e->name = lname;
e->is_alias = false;
e->type = Attr_container;
e->attributes = at;
attr_map.push_back(e);
at->d_parent = this;
return e->attributes;
}
/** Look for an attribute or an attribute container. If used to search
for an attribute container, this method returns the container's \e
parent using the value-result parameter \c at and a reference to the
container using the iterator value-result parameter \c iter. If used
to search for an attribute, the attribute's container is returned using
\c at; the attribute itself can be accessed using the iterator \c iter.
@param target The name (using dot notation) of the attribute or
container to find.
@param at A value-result used to return the attribute container in
which \c target was found. Null if \c target was not found.
@param iter The iterator which will reference the attribute found.
Can be used to access \c target from within \c at. References
dim_end() within \c at if the attribute or container does not exist. */
void AttrTable::find(const string &target, AttrTable **at, Attr_iter *iter)
{
string::size_type dotpos = target.rfind('.');
if (dotpos != string::npos) {
string container = target.substr(0, dotpos);
string field = target.substr(dotpos + 1);
*at = find_container(container);
if (*at) {
*iter = (*at)->simple_find(field);
}
else {
*iter = attr_map.end();
}
}
else {
*at = recurrsive_find(target, iter);
}
}
/** This method scans for attributes using recursion to look inside containers
even when the name of the attribute is not fully qualified. It starts
looking in itself and descends into its children depth first. It will find
attributes and attribute containers.
@param target Look for the attribute with this name.
@param location A value-result parameter. This returns an iterator to the
attribute within the returned AttrTable object
@return Returns a pointer to the AttrTable which holds \e target, or null
if \e target is not found. In the latter case, the value of \e location is
attr_end() for this AttrTable. */
AttrTable *
AttrTable::recurrsive_find(const string &target, Attr_iter *location)
{
Attr_iter i = attr_begin();
while (i != attr_end()) {
if (target == (*i)->name) {
*location = i;
return this;
}
else if ((*i)->type == Attr_container) {
AttrTable *at = (*i)->attributes->recurrsive_find(target, location);
if (at)
return at;
}
++i;
}
*location = i;
return 0;
}
// Made public for callers that want non-recursive find. [mjohnson 6 oct 09]
/** Look in this AttrTable for the attribute called \c name. If found return
an Attr_iter which references it, otherwise return the end iterator for
this AttrTable.
@param target The name of the attribute.
@return An Attr_iter which references \c target. */
AttrTable::Attr_iter AttrTable::simple_find(const string &target)
{
Attr_iter i;
for (i = attr_map.begin(); i != attr_map.end(); ++i) {
if (target == (*i)->name) {
break;
}
}
return i;
}
/** Look in this attribute table for an attribute container named
<tt>target</tt>. The search starts at this attribute table;
<tt>target</tt> should
use the dot notation to name containers held within children of this
attribute table.
To search the entire DAS object, make sure to invoke this method from
that object.
@brief Find an attribute with a given name.
@param target The attribute container to find.
@return A pointer to the attribute table or null if the container
cannot be found. */
AttrTable *
AttrTable::find_container(const string &target)
{
string::size_type dotpos = target.find('.');
if (dotpos != string::npos) {
string container = target.substr(0, dotpos);
string field = target.substr(dotpos + 1);
AttrTable *at = simple_find_container(container);
return (at) ? at->find_container(field) : 0;
}
else {
return simple_find_container(target);
}
}
// Made public for callers that want non-recursive find. [mjohnson 6 oct 09]
AttrTable *
AttrTable::simple_find_container(const string &target)
{
if (get_name() == target)
return this;
for (Attr_iter i = attr_map.begin(); i != attr_map.end(); ++i) {
if (is_container(i) && target == (*i)->name) {
return (*i)->attributes;
}
}
return 0;
}
/** Each of the following accessors get information using the name of an
attribute. They perform a simple search for the name in this
attribute table only; sub-tables are not searched and the dot
notation is not recognized.
@name Accessors using an attribute name */
//@{
/** @brief Get an attribute container. */
AttrTable *
AttrTable::get_attr_table(const string &name)
{
return find_container(name);
}
/** @brief Get the type name of an attribute within this attribute table. */
string AttrTable::get_type(const string &name)
{
Attr_iter p = simple_find(name);
return (p != attr_map.end()) ? get_type(p) : (string) "";
}
/** @brief Get the type of an attribute.
@return The <tt>AttrType</tt> value describing the attribute. */
AttrType AttrTable::get_attr_type(const string &name)
{
Attr_iter p = simple_find(name);
return (p != attr_map.end()) ? get_attr_type(p) : Attr_unknown;
}
/** If the indicated attribute is a container attribute, this function
returns the number of attributes in <i>its</i> attribute table. If the
indicated attribute is not a container, the method returns the number
of values for the attribute (1 for a scalar attribute, N for a vector
attribute value).
@brief Get the number of attributes in this container.
*/
unsigned int AttrTable::get_attr_num(const string &name)
{
Attr_iter iter = simple_find(name);
return (iter != attr_map.end()) ? get_attr_num(iter) : 0;
}
/** Get a pointer to the vector of values associated with the attribute
referenced by Pix <tt>p</tt> or named <tt>name</tt>.
Note that all values in an attribute table are stored as string data.
They may be converted to a more appropriate internal format by the
calling program.
@return If the indicated attribute is a container, this function
returns the null pointer. Otherwise returns a pointer to the
the attribute vector value.
@brief Get a vector-valued attribute.
*/
vector<string> *
AttrTable::get_attr_vector(const string &name)
{
Attr_iter p = simple_find(name);
return (p != attr_map.end()) ? get_attr_vector(p) : 0;
}
/** Delete the attribute named <tt>name</tt>. If <tt>i</tt> is given, and
the attribute has a vector value, delete the <tt>i</tt>$^th$
element of the vector.
You can use this function to delete container attributes, although
the <tt>i</tt> parameter has no meaning for that operation.
@brief Deletes an attribute.
@param name The name of the attribute to delete. This can be an
attribute of any type, including containers. However, this method
looks only in this attribute table and does not recognize the dot
notation.
@param i If the named attribute is a vector, and <tt>i</tt> is
non-negative, the i-th entry in the vector is deleted, and the
array is repacked. If <tt>i</tt> equals -1 (the default), the
entire attribute is deleted. */
void AttrTable::del_attr(const string &name, int i)
{
#if WWW_ENCODING
string lname = www2id(name);
#else
string lname = remove_space_encoding(name);
#endif
Attr_iter iter = simple_find(lname);
if (iter != attr_map.end()) {
if (i == -1) { // Delete the whole attribute
entry *e = *iter;
attr_map.erase(iter);
delete e;
e = 0;
}
else { // Delete one element from attribute array
// Don't try to delete elements from the vector of values if the
// map is a container!
if ((*iter)->type == Attr_container)
return;
vector<string> *sxp = (*iter)->attr;
assert(i >= 0 && i < (int) sxp->size());
sxp->erase(sxp->begin() + i); // rm the element
}
}
}
//@} Accessors using an attribute name
/** @name get information using an iterator */
//@{
/** Get an iterator to the first entry in this attribute table.
@return Attr_iter; references the end of the array if empty list. */
AttrTable::Attr_iter AttrTable::attr_begin()
{
return attr_map.begin();
}
/** Get an iterator to the end attribute table. Does not point to
the last attribute in the table
@return Attr_iter */
AttrTable::Attr_iter AttrTable::attr_end()
{
return attr_map.end();
}
/** Given an index \c i, return the \c Attr_iter to the corresponding
element. This method provides a way to use all the methods that take an
\c Attr_iter using a simple integer index. Use the get_attr_num() or
get_size() methods to determine how many items the AttrTable contains.
@param i The index
@return The corresponding Attr_iter
@see get_attr_num, get_size */
AttrTable::Attr_iter AttrTable::get_attr_iter(int i)
{
return attr_map.begin() + i;
}
/** Returns the name of the attribute referenced by \e iter. */
string AttrTable::get_name(Attr_iter iter)
{
assert(iter != attr_map.end());
return (*iter)->name;
}
/** Returns true if the attribute referenced by \e i is a container. */
bool AttrTable::is_container(Attr_iter i)
{
return (*i)->type == Attr_container;
}
/** Get the attribute container referenced by \e iter. If no
such container exists, then return a reference to the end of the
table.
@param iter Reference to a table contained by this object.
@return The child attribute table. */
AttrTable *
AttrTable::get_attr_table(Attr_iter iter)
{
assert(iter != attr_map.end());
return (*iter)->type == Attr_container ? (*iter)->attributes : 0;
}
/** Delete the iterator. Since AttrTable stores pointers to AttrTable
objects, the caller should be sure to delete the AttrTable itself.
The caller will gain control of the AttrTable* located at
get_attr_table(iter) prior to this call.
@note The original semantics of this methods were odd. The caller was
responsible for deleting the AttrTable, but if they did that before calling
this, then memory corruption would happen (because this code accesses a
field of the table). If the caller did not delete the table, memory leaked.
The only correct way to call the method was to grab the pointer, call this
and then delete the pointer. I added a call to delete the contained
AttrTable pointer, which changes the behavior of this, but probably in a
way that will fix leaks in existing code. This change can be reverted by
setting NEW_DEL_ATTR_TABLE_BEHAVIOR to false. jhrg 4/26/13
@note calling this method <b>invalidates</b> the iterator \e iter.
@param iter points to the entry to be deleted.
@return The Attr_iter for the element following \e iter */
AttrTable::Attr_iter AttrTable::del_attr_table(Attr_iter iter)
{
if ((*iter)->type != Attr_container)
return ++iter;
// the caller intends to delete/reuse the contained AttrTable,
// so zero it out so it doesn't get deleted before we delete the entry
// [mjohnson]
struct entry *e = *iter;
// container no longer has a parent.
if (e->attributes) {
e->attributes->d_parent = 0;
#if NEW_DEL_ATTR_TABLE_BEHAVIOR
delete e->attributes;
#endif
e->attributes = 0;
}
delete e;
return attr_map.erase(iter);
}
/** Get the type name of an attribute referenced by \e iter.
@param iter Reference to the Attribute.
@return A string with the name of this attribute datatype. */
string AttrTable::get_type(Attr_iter iter)
{
assert(iter != attr_map.end());
return AttrType_to_String((*iter)->type);
}
/** Get the type of the attribute referenced by \e iter.
@param iter
@return The datatype of this attribute in an instance of AttrType. */
AttrType AttrTable::get_attr_type(Attr_iter iter)
{
return (*iter)->type;
}
/** If the attribute referenced by \e iter is a container attribute, this
method returns the number of attributes in its attribute table.
If the indicated attribute is not a container, the method returns the
number of values for the attribute (1 for a scalar attribute, N for a
vector attribute value).
@param iter Reference to an attribute
@return The number of elements in the attribute. */
unsigned int AttrTable::get_attr_num(Attr_iter iter)
{
assert(iter != attr_map.end());
return ((*iter)->type == Attr_container) ? (*iter)->attributes->get_size() : (*iter)->attr->size();
}
/** Returns the value of an attribute. If the attribute has a vector
value, you can indicate which is the desired value with the index
argument, \e i. If the argument is omitted, the first value is
returned. If the attribute has only a single value, the index
argument is ignored. If \e i is greater than the number of
elements in the attribute, an error is produced.
All values in an attribute table are stored as string data. They may
be converted to a more appropriate internal format by the calling
program.
@param iter Reference to an attribute
@param i The attribute value index, zero-based. Default value: 0
@return If the indicated attribute is a container, this function
returns the string ``None''. If using a name to refer to the attribute
and the named attribute does not exist, return the empty string. */
string AttrTable::get_attr(Attr_iter iter, unsigned int i)
{
assert(iter != attr_map.end());
return (*iter)->type == Attr_container ? (string) "None" : (*(*iter)->attr)[i];
}
string AttrTable::get_attr(const string &name, unsigned int i)
{
Attr_iter p = simple_find(name);
return (p != attr_map.end()) ? get_attr(p, i) : (string) "";
}
/** Returns a pointer to the vector of values associated with the
attribute referenced by iterator \e iter.
Note that all values in an attribute table are stored as string data.
They may be converted to a more appropriate internal format by the
calling program.
@param iter Reference to the Attribute.
@return If the indicated attribute is a container, this function
returns the null pointer. Otherwise returns a pointer to the
the attribute vector value. */
vector<string> *
AttrTable::get_attr_vector(Attr_iter iter)
{
assert(iter != attr_map.end());
return (*iter)->type != Attr_container ? (*iter)->attr : 0;
}
bool AttrTable::is_global_attribute(Attr_iter iter)
{
assert(iter != attr_map.end());
if ((*iter)->type == Attr_container)
return (*iter)->attributes->is_global_attribute();
else
return (*iter)->is_global;
}
void AttrTable::set_is_global_attribute(Attr_iter iter, bool ga)
{
assert(iter != attr_map.end());
if ((*iter)->type == Attr_container)
(*iter)->attributes->set_is_global_attribute(ga);
else
(*iter)->is_global = ga;
}
//@} Accessors that use an iterator
// Alias an attribute table. The alias should be added to this object.
/** @brief Add an alias to a container held by this attribute table.
@param name The name of the alias. May <i>not</i> use dot notation.
@param src The existing attribute container to alias.
@exception Error if an attribute, container or alias called
<tt>name</tt> already exists in this attribute table. */
void AttrTable::add_container_alias(const string &name, AttrTable *src)
{
#if WWW_ENCODING
string lname = www2id(name);
#else
string lname = remove_space_encoding(name);
#endif
if (simple_find(lname) != attr_end())
throw Error(string("There already exists a container called `") + name + string("in this attribute table. (2)"));
entry *e = new entry;
e->name = lname;
e->is_alias = true;
e->aliased_to = src->get_name();
e->type = Attr_container;
e->attributes = src;
attr_map.push_back(e);
}
/** Assume \e source names an attribute value in some container. Add an alias
\e name for that value in this object.
@brief Add an alias for an attribute.
@param das
@param name The name of the alias. May <i>not</i> use dot notation.
@param source The name of the attribute to alias. May use dot
notation.
@exception Error if the attribute table already contains an
attribute, container or alias called <tt>name</tt> or if an
attribute called <tt>source</tt> does not exist. */
void AttrTable::add_value_alias(AttrTable *das, const string &name, const string &source)
{
#if WWW_ENCODING
string lname = www2id(name);
#else
string lname = remove_space_encoding(name);
#endif
#if WWW_ENCODING
string lsource = www2id(source);
#else
string lsource = remove_space_encoding(source);
#endif
// find the container that holds source and its (sources's) iterator
// within that container. Search at the uppermost level of the attribute
// object to find values defined `above' the current container.
AttrTable *at;
Attr_iter iter;
das->find(lsource, &at, &iter);
// If source is not found by looking at the topmost level, look in the
// current table (i.e., alias z x where x is in the current container
// won't be found by looking for `x' at the top level). See test case 26
// in das-testsuite.
if (!at || (iter == at->attr_end()) || !*iter) {
find(lsource, &at, &iter);
if (!at || (iter == at->attr_end()) || !*iter)
throw Error(string("Could not find the attribute `") + source + string("' in the attribute object."));
}
// If we've got a value to alias and it's being added at the top level of
// the DAS, that's an error.
if (at && !at->is_container(iter) && this == das)
throw Error(
string(
"A value cannot be aliased to the top level of the DAS;\nOnly containers may be present at that level of the DAS."));
if (simple_find(lname) != attr_end())
throw Error(string("There already exists a container called `") + name + string("in this attribute table. (3)"));
entry *e = new entry;
e->name = lname;
e->is_alias = true;
e->aliased_to = lsource;
e->type = get_attr_type(iter);
if (at && e->type == Attr_container)
e->attributes = at->get_attr_table(iter);
else
e->attr = (*iter)->attr;
attr_map.push_back(e);
}
// Deprecated
/** Once an alias is
inserted into an attribute table, reading the attributes for
<i>alias</i> will return those stored for <i>name</i>.