libdap Updated for version 3.21.0
libdap4 is an implementation of OPeNDAP's DAP protocol.
AttrTable.h
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of libdap, A C++ implementation of the OPeNDAP Data
5// Access Protocol.
6
7// Copyright (c) 2002,2003 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26// (c) COPYRIGHT URI/MIT 1994-1999
27// Please read the full copyright statement in the file COPYRIGHT_URI.
28//
29// Authors:
30// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31
32// An AttrTable is a table of attributes (type-name-value tuples).
33
34#ifndef _attrtable_h
35#define _attrtable_h 1
36
37
38#include <string>
39#include <vector>
40
41#ifndef _error_h
42#include "Error.h"
43#endif
44
45using std::vector;
46using std::string;
47using std::vector;
48
49#ifndef A_DapObj_h
50#include "DapObj.h"
51#endif
52
53#ifndef XMLWRITER_H_
54#include "XMLWriter.h"
55#endif
56
57namespace libdap
58{
59
82 Attr_unknown,
83 Attr_container,
84 Attr_byte,
85 Attr_int16,
86 Attr_uint16,
87 Attr_int32,
88 Attr_uint32,
89 Attr_float32,
90 Attr_float64,
91 Attr_string,
92 Attr_url,
93 Attr_other_xml,
94
95 // Added for DAP4
96 Attr_int8,
97 Attr_uint8,
98
99 Attr_int64,
100 Attr_uint64,
101
102 Attr_enum,
103 Attr_opaque
104
105};
106
107string AttrType_to_String(const AttrType at);
108AttrType String_to_AttrType(const string &s);
109
153class AttrTable : public DapObj
154{
155 // entry needs to be made public to make up for issues with this class'
156 // design. It should probably be moved to it's own class. 05/22/03 jhrg
157public:
162 struct entry
163 {
164 string name;
165 AttrType type;
166
167 bool is_alias;
168 string aliased_to;
169
170 bool is_global; // use this to mark non-container attributes. see below.
171
172 bool is_utf8_str =false;
173
174 // If type == Attr_container, use attributes to read the contained
175 // table, otherwise use attr to read the vector of values.
176 AttrTable *attributes;
177 std::vector<string> *attr; // a vector of values. jhrg 12/5/94
178
179 entry(): name(""), type(Attr_unknown), is_alias(false),
180 aliased_to(""), is_global(true), attributes(0), attr(0) {}
181
182 entry(const entry &rhs): name(rhs.name), type(rhs.type), is_alias(rhs.is_alias),
183 aliased_to(rhs.aliased_to), is_global(rhs.is_global),attributes(0), attr(0)
184 {
185 clone(rhs);
186 }
187
188 void delete_entry()
189 {
190 if (is_alias) // alias copies the pointers.
191 return;
192 if (type == Attr_container) {
193 delete attributes; attributes = 0;
194 }
195 else {
196 delete attr; attr = 0;
197 }
198 }
199
200 virtual ~entry()
201 {
202 delete_entry();
203 }
204
205 void clone(const entry &rhs)
206 {
207#if 0
208 name = rhs.name;
209 type = rhs.type;
210 is_alias = rhs.is_alias;
211 aliased_to = rhs.aliased_to;
212 is_global = rhs.is_global;
213#endif
214 switch (rhs.type) {
215 case Attr_unknown:
216 break;
217 case Attr_container: {
218 if (rhs.is_alias)
219 attributes = rhs.attributes;
220 else
221 attributes = new AttrTable(*rhs.attributes);
222 break;
223 }
224 default: {
225 if (rhs.is_alias)
226 attr = rhs.attr;
227 else
228 attr = new std::vector<string>(*rhs.attr);
229 break;
230 }
231 }
232 }
233
234 entry &operator=(const entry &rhs)
235 {
236 if (this != &rhs) {
237 delete_entry();
238 clone(rhs);
239 }
240 return *this;
241 }
242
249 bool is_dap4_type(const std::string &path, std::vector<std::string> &inventory) const
250 {
251 bool ima_d4_attr = false;
252 switch(type){
253 case Attr_int8:
254 case Attr_int64:
255 case Attr_uint64:
256 ima_d4_attr=true;
257 break;
258 case Attr_container:
259 ima_d4_attr = attributes->has_dap4_types(path ,inventory);
260 break;
261 default:
262 break;
263 }
264 return ima_d4_attr;
265 }
266
267 };
268
269 typedef std::vector<entry *>::const_iterator Attr_citer ;
270 typedef std::vector<entry *>::iterator Attr_iter ;
271
272private:
273 string d_name;
274 AttrTable *d_parent;
275 std::vector<entry *> attr_map;
276
277 // Use this to mark container attributes. Look at the methods
278 // is_global_attribute() and set_is_...., esp. at the versions that take
279 // an iterator. This code is tricky because it has to track both whole
280 // containers that are global and individual attributes that are 'global'
281 // relative to a constructor. That is, there are some attributes that are
282 // bound to a container and not any of the container's children.
283 bool d_is_global_attribute;
284
285 void delete_attr_table();
286
287 friend class AttrTableTest;
288
289protected:
290 void clone(const AttrTable &at);
291
292 void simple_print(FILE *out, string pad, Attr_iter i,
293 bool dereference);
294 void simple_print(ostream &out, string pad, Attr_iter i,
295 bool dereference);
296
297public:
298 AttrTable();
299 AttrTable(const AttrTable &rhs);
300 virtual ~AttrTable();
301 AttrTable & operator=(const AttrTable &rhs);
302
303 virtual void erase();
304
305 virtual unsigned int get_size() const;
306 virtual string get_name() const;
307 virtual void set_name(const string &n);
308
312 virtual AttrTable *get_parent() const
313 {
314 return d_parent;
315 }
316
317 virtual bool is_global_attribute() const { return d_is_global_attribute; }
318 virtual void set_is_global_attribute(bool ga) { d_is_global_attribute = ga; }
319
320 virtual unsigned int append_attr(const string &name, const string &type,
321 const string &value);
322 virtual unsigned int append_attr(const string &name, const string &type,
323 vector<string> *values);
324 virtual unsigned int append_attr(const string &name, const string &type,
325 const string &value, bool is_utf8_str);
326 virtual unsigned int append_attr(const string &name, const string &type,
327 vector<string> *values, bool is_utf8_str);
328
329
330 virtual AttrTable *append_container(const string &name);
331 virtual AttrTable *append_container(AttrTable *at, const string &name);
332
333 virtual void find(const string &target, AttrTable **at, Attr_iter *iter);
334 virtual AttrTable *find_container(const string &target);
335 virtual AttrTable *recurrsive_find(const string &target,
336 Attr_iter *location);
337
338 Attr_iter simple_find(const string &target);
339 AttrTable *simple_find_container(const string &target);
340
341
342 virtual AttrTable *get_attr_table(const string &name);
343 virtual string get_type(const string &name);
344 virtual AttrType get_attr_type(const string &name);
345 virtual unsigned int get_attr_num(const string &name);
346 virtual string get_attr(const string &name, unsigned int i = 0);
347 virtual vector<string> *get_attr_vector(const string &name);
348 virtual void del_attr(const string &name, int i = -1);
349
350 virtual Attr_iter attr_begin();
351 virtual Attr_iter attr_end();
352 virtual Attr_iter get_attr_iter(int i);
353 virtual string get_name(Attr_iter iter);
354 virtual bool is_container(Attr_iter iter);
355 virtual AttrTable *get_attr_table(Attr_iter iter);
356 virtual Attr_iter del_attr_table(Attr_iter iter);
357 virtual string get_type(Attr_iter iter);
358 virtual AttrType get_attr_type(Attr_iter iter);
359 virtual unsigned int get_attr_num(Attr_iter iter);
360 virtual string get_attr(Attr_iter iter, unsigned int i = 0);
361 virtual std::vector<string> *get_attr_vector(Attr_iter iter);
362 virtual bool is_global_attribute(Attr_iter iter);
363 virtual void set_is_global_attribute(Attr_iter iter, bool ga);
364
365 virtual void add_container_alias(const string &name, AttrTable *src);
366 virtual void add_value_alias(AttrTable *at, const string &name,
367 const string &source);
368 virtual bool attr_alias(const string &alias,
369 AttrTable *at,
370 const string &name);
371 virtual bool attr_alias(const string &alias, const string &name);
372
373 bool has_dap4_types(const std::string &path, std::vector<std::string> &inventory) const;
374 bool is_dap4_type(const std::string &path, std::vector<std::string> &inventory) const;
375
376 virtual void print(FILE *out, string pad = " ",
377 bool dereference = false);
378 virtual void print(ostream &out, string pad = " ",
379 bool dereference = false);
380
381 virtual void print_xml(FILE *out, string pad = " ",
382 bool constrained = false);
383 virtual void print_xml(ostream &out, string pad = " ",
384 bool constrained = false);
385
386 void print_xml_writer(XMLWriter &xml);
387
388 void print_dap4(XMLWriter &xml);
389
390 virtual void dump(ostream &strm) const ;
391
392};
393
394
395
396string remove_space_encoding(const string &s);
397string add_space_encoding(const string &s);
398
399} // namespace libdap
400
401#endif // _attrtable_h
Contains the attributes for a dataset.
Definition AttrTable.h:154
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition AttrTable.cc:554
void simple_print(FILE *out, string pad, Attr_iter i, bool dereference)
virtual unsigned int get_attr_num(const string &name)
Get the number of attributes in this container.
Definition AttrTable.cc:778
bool has_dap4_types(const std::string &path, std::vector< std::string > &inventory) const
virtual bool attr_alias(const string &alias, AttrTable *at, const string &name)
Adds an alias to the set of attributes.
virtual bool is_container(Attr_iter iter)
Definition AttrTable.cc:890
virtual void find(const string &target, AttrTable **at, Attr_iter *iter)
Definition AttrTable.cc:625
virtual void set_name(const string &n)
Set the name of this attribute table.
Definition AttrTable.cc:273
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition AttrTable.cc:751
void clone(const AttrTable &at)
Definition AttrTable.cc:188
virtual Attr_iter attr_end()
Definition AttrTable.cc:863
virtual void print_xml(FILE *out, string pad=" ", bool constrained=false)
virtual AttrTable * get_parent() const
Definition AttrTable.h:312
virtual string get_type(const string &name)
Get the type name of an attribute within this attribute table.
Definition AttrTable.cc:757
virtual vector< string > * get_attr_vector(const string &name)
Get a vector-valued attribute.
Definition AttrTable.cc:797
virtual void add_value_alias(AttrTable *at, const string &name, const string &source)
Add an alias for an attribute.
virtual unsigned int append_attr(const string &name, const string &type, const string &value)
Add an attribute to the table.
Definition AttrTable.cc:335
virtual Attr_iter attr_begin()
Definition AttrTable.cc:855
virtual Attr_iter get_attr_iter(int i)
Definition AttrTable.cc:876
void print_dap4(XMLWriter &xml)
virtual void del_attr(const string &name, int i=-1)
Deletes an attribute.
Definition AttrTable.cc:819
virtual string get_name() const
Get the name of this attribute table.
Definition AttrTable.cc:266
virtual void erase()
Erase the attribute table.
void print_xml_writer(XMLWriter &xml)
virtual Attr_iter del_attr_table(Attr_iter iter)
Definition AttrTable.cc:925
virtual void print(FILE *out, string pad=" ", bool dereference=false)
Prints the attribute table.
virtual void add_container_alias(const string &name, AttrTable *src)
Add an alias to a container held by this attribute table.
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition AttrTable.cc:259
virtual void dump(ostream &strm) const
dumps information about this object
virtual AttrTable * find_container(const string &target)
Find an attribute with a given name.
Definition AttrTable.cc:710
Attr_iter simple_find(const string &target)
Definition AttrTable.cc:685
virtual AttrType get_attr_type(const string &name)
Get the type of an attribute.
Definition AttrTable.cc:765
virtual AttrTable * recurrsive_find(const string &target, Attr_iter *location)
Definition AttrTable.cc:657
libdap base object for common functionality of libdap objects
Definition DapObj.h:51
top level DAP object to house generic methods
string add_space_encoding(const string &s)
Definition AttrTable.cc:78
string AttrType_to_String(const AttrType at)
Definition AttrTable.cc:97
string remove_space_encoding(const string &s)
Definition AttrTable.cc:61
bool is_dap4_type(const std::string &path, std::vector< std::string > &inventory) const
Definition AttrTable.h:249