libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
AttrTable.h
Go to the documentation of this file.
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#include <string>
38#include <vector>
39
40#ifndef _error_h
41#include "Error.h"
42#endif
43
44using std::string;
45using std::vector;
46
47#ifndef A_DapObj_h
48#include "DapObj.h"
49#endif
50
51#ifndef XMLWRITER_H_
52#include "XMLWriter.h"
53#endif
54
55namespace libdap {
56
103
104string AttrType_to_String(const AttrType at);
105AttrType String_to_AttrType(const string &s);
106
150class AttrTable : public DapObj {
151 // entry needs to be made public to make up for issues with this class'
152 // design. It should probably be moved to it's own class. 05/22/03 jhrg
153public:
158 struct entry {
159 string name;
161
164
165 bool is_global; // use this to mark non-container attributes. see below.
166
167 bool is_utf8_str = false;
168
169 // If type == Attr_container, use attributes to read the contained
170 // table, otherwise use attr to read the vector of values.
172 std::vector<string> *attr; // a vector of values. jhrg 12/5/94
173
175 : name(""), type(Attr_unknown), is_alias(false), aliased_to(""), is_global(true), attributes(0), attr(0) {}
176
177 entry(const entry &rhs)
178 : name(rhs.name), type(rhs.type), is_alias(rhs.is_alias), aliased_to(rhs.aliased_to),
179 is_global(rhs.is_global), attributes(0), attr(0) {
180 clone(rhs);
181 }
182
184 if (is_alias) // alias copies the pointers.
185 return;
186 if (type == Attr_container) {
187 delete attributes;
188 attributes = 0;
189 } else {
190 delete attr;
191 attr = 0;
192 }
193 }
194
195 virtual ~entry() { delete_entry(); }
196
197 void clone(const entry &rhs) {
198#if 0
199 name = rhs.name;
200 type = rhs.type;
201 is_alias = rhs.is_alias;
203 is_global = rhs.is_global;
204#endif
205 switch (rhs.type) {
206 case Attr_unknown:
207 break;
208 case Attr_container: {
209 if (rhs.is_alias)
211 else
212 attributes = new AttrTable(*rhs.attributes);
213 break;
214 }
215 default: {
216 if (rhs.is_alias)
217 attr = rhs.attr;
218 else
219 attr = new std::vector<string>(*rhs.attr);
220 break;
221 }
222 }
223 }
224
225 entry &operator=(const entry &rhs) {
226 if (this != &rhs) {
227 delete_entry();
228 clone(rhs);
229 }
230 return *this;
231 }
232
239 bool is_dap4_type(const std::string &path, std::vector<std::string> &inventory) const {
240 bool ima_d4_attr = false;
241 switch (type) {
242 case Attr_int8:
243 case Attr_int64:
244 case Attr_uint64:
245 ima_d4_attr = true;
246 break;
247 case Attr_container:
248 ima_d4_attr = attributes->has_dap4_types(path, inventory);
249 break;
250 default:
251 break;
252 }
253 return ima_d4_attr;
254 }
255 };
256
257 typedef std::vector<entry *>::const_iterator Attr_citer;
258 typedef std::vector<entry *>::iterator Attr_iter;
259
260private:
261 string d_name;
262 AttrTable *d_parent;
263 std::vector<entry *> attr_map;
264
265 // Use this to mark container attributes. Look at the methods
266 // is_global_attribute() and set_is_...., esp. at the versions that take
267 // an iterator. This code is tricky because it has to track both whole
268 // containers that are global and individual attributes that are 'global'
269 // relative to a constructor. That is, there are some attributes that are
270 // bound to a container and not any of the container's children.
271 bool d_is_global_attribute;
272
273 void delete_attr_table();
274
275 friend class AttrTableTest;
276
277protected:
278 void clone(const AttrTable &at);
279
280 void simple_print(FILE *out, string pad, Attr_iter i, bool dereference);
281 void simple_print(ostream &out, string pad, Attr_iter i, bool dereference);
282
283public:
284 AttrTable();
285 AttrTable(const AttrTable &rhs);
286 virtual ~AttrTable();
287 AttrTable &operator=(const AttrTable &rhs);
288
289 virtual void erase();
290
291 virtual unsigned int get_size() const;
292 virtual string get_name() const;
293 virtual void set_name(const string &n);
294
298 virtual AttrTable *get_parent() const { return d_parent; }
299
300 virtual bool is_global_attribute() const { return d_is_global_attribute; }
301 virtual void set_is_global_attribute(bool ga) { d_is_global_attribute = ga; }
302
303 virtual unsigned int append_attr(const string &name, const string &type, const string &value);
304 virtual unsigned int append_attr(const string &name, const string &type, vector<string> *values);
305 virtual unsigned int append_attr(const string &name, const string &type, const string &value, bool is_utf8_str);
306 virtual unsigned int append_attr(const string &name, const string &type, vector<string> *values, bool is_utf8_str);
307
308 virtual AttrTable *append_container(const string &name);
309 virtual AttrTable *append_container(AttrTable *at, const string &name);
310
311 virtual void find(const string &target, AttrTable **at, Attr_iter *iter);
312 virtual AttrTable *find_container(const string &target);
313 virtual AttrTable *recurrsive_find(const string &target, Attr_iter *location);
314
315 Attr_iter simple_find(const string &target);
316 AttrTable *simple_find_container(const string &target);
317
318 virtual AttrTable *get_attr_table(const string &name);
319 virtual string get_type(const string &name);
320 virtual AttrType get_attr_type(const string &name);
321 virtual unsigned int get_attr_num(const string &name);
322 virtual string get_attr(const string &name, unsigned int i = 0);
323 virtual vector<string> *get_attr_vector(const string &name);
324 virtual void del_attr(const string &name, int i = -1);
325
326 virtual Attr_iter attr_begin();
327 virtual Attr_iter attr_end();
328 virtual Attr_iter get_attr_iter(int i);
329 virtual string get_name(Attr_iter iter);
330 virtual bool is_container(Attr_iter iter);
331 virtual AttrTable *get_attr_table(Attr_iter iter);
332 virtual Attr_iter del_attr_table(Attr_iter iter);
333 virtual string get_type(Attr_iter iter);
334 virtual AttrType get_attr_type(Attr_iter iter);
335 virtual unsigned int get_attr_num(Attr_iter iter);
336 virtual string get_attr(Attr_iter iter, unsigned int i = 0);
337 virtual std::vector<string> *get_attr_vector(Attr_iter iter);
338 virtual bool is_global_attribute(Attr_iter iter);
339 virtual void set_is_global_attribute(Attr_iter iter, bool ga);
340
341 virtual void add_container_alias(const string &name, AttrTable *src);
342 virtual void add_value_alias(AttrTable *at, const string &name, const string &source);
343 virtual bool attr_alias(const string &alias, AttrTable *at, const string &name);
344 virtual bool attr_alias(const string &alias, const string &name);
345
346 bool has_dap4_types(const std::string &path, std::vector<std::string> &inventory) const;
347 bool is_dap4_type(const std::string &path, std::vector<std::string> &inventory) const;
348
349 virtual void print(FILE *out, string pad = " ", bool dereference = false);
350 virtual void print(ostream &out, string pad = " ", bool dereference = false);
351
352 virtual void print_xml(FILE *out, string pad = " ", bool constrained = false);
353 virtual void print_xml(ostream &out, string pad = " ", bool constrained = false);
354
355 void print_xml_writer(XMLWriter &xml);
356
357 void print_dap4(XMLWriter &xml);
358
359 virtual void dump(ostream &strm) const;
360};
361
362string remove_space_encoding(const string &s);
363string add_space_encoding(const string &s);
364
365} // namespace libdap
366
367#endif // _attrtable_h
Contains the attributes for a dataset.
Definition AttrTable.h:150
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition AttrTable.cc:517
bool is_dap4_type(const std::string &path, std::vector< std::string > &inventory) const
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:721
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:818
virtual void find(const string &target, AttrTable **at, Attr_iter *iter)
Definition AttrTable.cc:586
virtual void set_name(const string &n)
Set the name of this attribute table.
Definition AttrTable.cc:247
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition AttrTable.cc:699
void clone(const AttrTable &at)
Definition AttrTable.cc:182
AttrTable & operator=(const AttrTable &rhs)
Definition AttrTable.cc:224
virtual Attr_iter attr_end()
Definition AttrTable.cc:798
virtual void print_xml(FILE *out, string pad=" ", bool constrained=false)
virtual string get_attr(const string &name, unsigned int i=0)
Definition AttrTable.cc:918
AttrTable * simple_find_container(const string &target)
Definition AttrTable.cc:677
virtual AttrTable * get_parent() const
Definition AttrTable.h:298
virtual string get_type(const string &name)
Get the type name of an attribute within this attribute table.
Definition AttrTable.cc:702
virtual bool is_global_attribute() const
Definition AttrTable.h:300
virtual vector< string > * get_attr_vector(const string &name)
Get a vector-valued attribute.
Definition AttrTable.cc:738
virtual void add_value_alias(AttrTable *at, const string &name, const string &source)
Add an alias for an attribute.
Definition AttrTable.cc:997
virtual unsigned int append_attr(const string &name, const string &type, const string &value)
Add an attribute to the table.
Definition AttrTable.cc:308
virtual Attr_iter attr_begin()
Definition AttrTable.cc:793
virtual Attr_iter get_attr_iter(int i)
Definition AttrTable.cc:808
void print_dap4(XMLWriter &xml)
virtual void del_attr(const string &name, int i=-1)
Deletes an attribute.
Definition AttrTable.cc:759
virtual string get_name() const
Get the name of this attribute table.
Definition AttrTable.cc:243
virtual void erase()
Erase the attribute table.
virtual void set_is_global_attribute(bool ga)
Definition AttrTable.h:301
friend class AttrTableTest
Definition AttrTable.h:275
void print_xml_writer(XMLWriter &xml)
virtual Attr_iter del_attr_table(Attr_iter iter)
Definition AttrTable.cc:848
virtual ~AttrTable()
Definition AttrTable.cc:222
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.
Definition AttrTable.cc:963
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition AttrTable.cc:239
virtual void dump(ostream &strm) const
dumps information about this object
std::vector< entry * >::const_iterator Attr_citer
Definition AttrTable.h:257
virtual AttrTable * find_container(const string &target)
Find an attribute with a given name.
Definition AttrTable.cc:663
Attr_iter simple_find(const string &target)
Definition AttrTable.cc:640
std::vector< entry * >::iterator Attr_iter
Definition AttrTable.h:258
virtual AttrType get_attr_type(const string &name)
Get the type of an attribute.
Definition AttrTable.cc:709
virtual AttrTable * recurrsive_find(const string &target, Attr_iter *location)
Definition AttrTable.cc:614
libdap base object for common functionality of libdap objects
Definition DapObj.h:49
top level DAP object to house generic methods
Definition AISConnect.cc:30
string add_space_encoding(const string &s)
Definition AttrTable.cc:76
AttrType String_to_AttrType(const string &s)
Definition AttrTable.cc:136
string AttrType_to_String(const AttrType at)
Definition AttrTable.cc:93
string remove_space_encoding(const string &s)
Definition AttrTable.cc:61
@ Attr_float32
Definition AttrTable.h:86
@ Attr_container
Definition AttrTable.h:80
@ Attr_enum
Definition AttrTable.h:99
@ Attr_uint64
Definition AttrTable.h:97
@ Attr_string
Definition AttrTable.h:88
@ Attr_int8
Definition AttrTable.h:93
@ Attr_unknown
Definition AttrTable.h:79
@ Attr_int32
Definition AttrTable.h:84
@ Attr_uint32
Definition AttrTable.h:85
@ Attr_url
Definition AttrTable.h:89
@ Attr_other_xml
Definition AttrTable.h:90
@ Attr_uint8
Definition AttrTable.h:94
@ Attr_opaque
Definition AttrTable.h:100
@ Attr_uint16
Definition AttrTable.h:83
@ Attr_float64
Definition AttrTable.h:87
@ Attr_int64
Definition AttrTable.h:96
@ Attr_byte
Definition AttrTable.h:81
@ Attr_int16
Definition AttrTable.h:82
std::vector< string > * attr
Definition AttrTable.h:172
void clone(const entry &rhs)
Definition AttrTable.h:197
entry & operator=(const entry &rhs)
Definition AttrTable.h:225
bool is_dap4_type(const std::string &path, std::vector< std::string > &inventory) const
Definition AttrTable.h:239
entry(const entry &rhs)
Definition AttrTable.h:177