AttrTable.h
// -*- 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>
// An AttrTable is a table of attributes (type-name-value tuples).
#ifndef _attrtable_h
#define _attrtable_h 1
#include <string>
#include <vector>
#ifndef _error_h
#include "Error.h"
#endif
using std::vector;
using std::string;
using std::vector;
#ifndef A_DapObj_h
#include "DapObj.h"
#endif
#ifndef XMLWRITER_H_
#include "XMLWriter.h"
#endif
namespace libdap
{
enum AttrType {
Attr_unknown,
Attr_container,
Attr_byte,
Attr_int16,
Attr_uint16,
Attr_int32,
Attr_uint32,
Attr_float32,
Attr_float64,
Attr_string,
Attr_url,
Attr_other_xml,
// Added for DAP4
Attr_int8,
Attr_uint8,
Attr_int64,
Attr_uint64,
Attr_enum,
Attr_opaque
};
string AttrType_to_String(const AttrType at);
AttrType String_to_AttrType(const string &s);
class AttrTable : public DapObj
{
// entry needs to be made public to make up for issues with this class'
// design. It should probably be moved to it's own class. 05/22/03 jhrg
public:
struct entry
{
string name;
AttrType type;
bool is_alias;
string aliased_to;
bool is_global; // use this to mark non-container attributes. see below.
bool is_utf8_str =false;
// If type == Attr_container, use attributes to read the contained
// table, otherwise use attr to read the vector of values.
AttrTable *attributes;
std::vector<string> *attr; // a vector of values. jhrg 12/5/94
entry(): name(""), type(Attr_unknown), is_alias(false),
aliased_to(""), is_global(true), attributes(0), attr(0) {}
entry(const entry &rhs): name(rhs.name), type(rhs.type), is_alias(rhs.is_alias),
aliased_to(rhs.aliased_to), is_global(rhs.is_global),attributes(0), attr(0)
{
clone(rhs);
}
void delete_entry()
{
if (is_alias) // alias copies the pointers.
return;
if (type == Attr_container) {
delete attributes; attributes = 0;
}
else {
delete attr; attr = 0;
}
}
virtual ~entry()
{
delete_entry();
}
void clone(const entry &rhs)
{
#if 0
name = rhs.name;
type = rhs.type;
is_alias = rhs.is_alias;
aliased_to = rhs.aliased_to;
is_global = rhs.is_global;
#endif
switch (rhs.type) {
case Attr_unknown:
break;
case Attr_container: {
if (rhs.is_alias)
attributes = rhs.attributes;
else
attributes = new AttrTable(*rhs.attributes);
break;
}
default: {
if (rhs.is_alias)
attr = rhs.attr;
else
attr = new std::vector<string>(*rhs.attr);
break;
}
}
}
entry &operator=(const entry &rhs)
{
if (this != &rhs) {
delete_entry();
clone(rhs);
}
return *this;
}
bool is_dap4_type(const std::string &path, std::vector<std::string> &inventory) const
{
bool ima_d4_attr = false;
switch(type){
case Attr_int8:
case Attr_int64:
case Attr_uint64:
ima_d4_attr=true;
break;
case Attr_container:
ima_d4_attr = attributes->has_dap4_types(path ,inventory);
break;
default:
break;
}
return ima_d4_attr;
}
};
typedef std::vector<entry *>::const_iterator Attr_citer ;
typedef std::vector<entry *>::iterator Attr_iter ;
private:
string d_name;
AttrTable *d_parent;
std::vector<entry *> attr_map;
// Use this to mark container attributes. Look at the methods
// is_global_attribute() and set_is_...., esp. at the versions that take
// an iterator. This code is tricky because it has to track both whole
// containers that are global and individual attributes that are 'global'
// relative to a constructor. That is, there are some attributes that are
// bound to a container and not any of the container's children.
bool d_is_global_attribute;
void delete_attr_table();
friend class AttrTableTest;
protected:
void clone(const AttrTable &at);
void simple_print(FILE *out, string pad, Attr_iter i,
bool dereference);
void simple_print(ostream &out, string pad, Attr_iter i,
bool dereference);
public:
AttrTable();
AttrTable(const AttrTable &rhs);
virtual ~AttrTable();
AttrTable & operator=(const AttrTable &rhs);
virtual void erase();
virtual unsigned int get_size() const;
virtual string get_name() const;
virtual void set_name(const string &n);
virtual AttrTable *get_parent() const
{
return d_parent;
}
virtual bool is_global_attribute() const { return d_is_global_attribute; }
virtual void set_is_global_attribute(bool ga) { d_is_global_attribute = ga; }
virtual unsigned int append_attr(const string &name, const string &type,
const string &value);
virtual unsigned int append_attr(const string &name, const string &type,
vector<string> *values);
virtual unsigned int append_attr(const string &name, const string &type,
const string &value, bool is_utf8_str);
virtual unsigned int append_attr(const string &name, const string &type,
vector<string> *values, bool is_utf8_str);
virtual AttrTable *append_container(const string &name);
virtual AttrTable *append_container(AttrTable *at, const string &name);
virtual void find(const string &target, AttrTable **at, Attr_iter *iter);
virtual AttrTable *find_container(const string &target);
virtual AttrTable *recurrsive_find(const string &target,
Attr_iter *location);
Attr_iter simple_find(const string &target);
AttrTable *simple_find_container(const string &target);
virtual AttrTable *get_attr_table(const string &name);
virtual string get_type(const string &name);
virtual AttrType get_attr_type(const string &name);
virtual unsigned int get_attr_num(const string &name);
virtual string get_attr(const string &name, unsigned int i = 0);
virtual vector<string> *get_attr_vector(const string &name);
virtual void del_attr(const string &name, int i = -1);
virtual Attr_iter attr_begin();
virtual Attr_iter attr_end();
virtual Attr_iter get_attr_iter(int i);
virtual string get_name(Attr_iter iter);
virtual bool is_container(Attr_iter iter);
virtual AttrTable *get_attr_table(Attr_iter iter);
virtual Attr_iter del_attr_table(Attr_iter iter);
virtual string get_type(Attr_iter iter);
virtual AttrType get_attr_type(Attr_iter iter);
virtual unsigned int get_attr_num(Attr_iter iter);
virtual string get_attr(Attr_iter iter, unsigned int i = 0);
virtual std::vector<string> *get_attr_vector(Attr_iter iter);
virtual bool is_global_attribute(Attr_iter iter);
virtual void set_is_global_attribute(Attr_iter iter, bool ga);
virtual void add_container_alias(const string &name, AttrTable *src);
virtual void add_value_alias(AttrTable *at, const string &name,
const string &source);
virtual bool attr_alias(const string &alias,
AttrTable *at,
const string &name);
virtual bool attr_alias(const string &alias, const string &name);
bool has_dap4_types(const std::string &path, std::vector<std::string> &inventory) const;
bool is_dap4_type(const std::string &path, std::vector<std::string> &inventory) const;
virtual void print(FILE *out, string pad = " ",
bool dereference = false);
virtual void print(ostream &out, string pad = " ",
bool dereference = false);
virtual void print_xml(FILE *out, string pad = " ",
bool constrained = false);
virtual void print_xml(ostream &out, string pad = " ",
bool constrained = false);
void print_xml_writer(XMLWriter &xml);
void print_dap4(XMLWriter &xml);
virtual void dump(ostream &strm) const ;
};
string remove_space_encoding(const string &s);
string add_space_encoding(const string &s);
} // namespace libdap
#endif // _attrtable_h
libdap::AttrTable
Contains the attributes for a dataset.
Definition AttrTable.h:154
libdap::AttrTable::append_container
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition AttrTable.cc:554
libdap::AttrTable::simple_print
void simple_print(FILE *out, string pad, Attr_iter i, bool dereference)
Definition AttrTable.cc:1278
libdap::AttrTable::get_attr_num
virtual unsigned int get_attr_num(const string &name)
Get the number of attributes in this container.
Definition AttrTable.cc:778
libdap::AttrTable::has_dap4_types
bool has_dap4_types(const std::string &path, std::vector< std::string > &inventory) const
Definition AttrTable.cc:1184
libdap::AttrTable::attr_alias
virtual bool attr_alias(const string &alias, AttrTable *at, const string &name)
Adds an alias to the set of attributes.
Definition AttrTable.cc:1159
libdap::AttrTable::is_container
virtual bool is_container(Attr_iter iter)
Definition AttrTable.cc:890
libdap::AttrTable::find
virtual void find(const string &target, AttrTable **at, Attr_iter *iter)
Definition AttrTable.cc:625
libdap::AttrTable::set_name
virtual void set_name(const string &n)
Set the name of this attribute table.
Definition AttrTable.cc:273
libdap::AttrTable::get_attr_table
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition AttrTable.cc:751
libdap::AttrTable::clone
void clone(const AttrTable &at)
Definition AttrTable.cc:188
libdap::AttrTable::attr_end
virtual Attr_iter attr_end()
Definition AttrTable.cc:863
libdap::AttrTable::print_xml
virtual void print_xml(FILE *out, string pad=" ", bool constrained=false)
Definition AttrTable.cc:1502
libdap::AttrTable::get_parent
virtual AttrTable * get_parent() const
Definition AttrTable.h:312
libdap::AttrTable::get_type
virtual string get_type(const string &name)
Get the type name of an attribute within this attribute table.
Definition AttrTable.cc:757
libdap::AttrTable::get_attr_vector
virtual vector< string > * get_attr_vector(const string &name)
Get a vector-valued attribute.
Definition AttrTable.cc:797
libdap::AttrTable::add_value_alias
virtual void add_value_alias(AttrTable *at, const string &name, const string &source)
Add an alias for an attribute.
Definition AttrTable.cc:1086
libdap::AttrTable::append_attr
virtual unsigned int append_attr(const string &name, const string &type, const string &value)
Add an attribute to the table.
Definition AttrTable.cc:335
libdap::AttrTable::attr_begin
virtual Attr_iter attr_begin()
Definition AttrTable.cc:855
libdap::AttrTable::get_attr_iter
virtual Attr_iter get_attr_iter(int i)
Definition AttrTable.cc:876
libdap::AttrTable::print_dap4
void print_dap4(XMLWriter &xml)
Definition AttrTable.cc:1703
libdap::AttrTable::del_attr
virtual void del_attr(const string &name, int i=-1)
Deletes an attribute.
Definition AttrTable.cc:819
libdap::AttrTable::get_name
virtual string get_name() const
Get the name of this attribute table.
Definition AttrTable.cc:266
libdap::AttrTable::erase
virtual void erase()
Erase the attribute table.
Definition AttrTable.cc:1202
libdap::AttrTable::print_xml_writer
void print_xml_writer(XMLWriter &xml)
Definition AttrTable.cc:1614
libdap::AttrTable::del_attr_table
virtual Attr_iter del_attr_table(Attr_iter iter)
Definition AttrTable.cc:925
libdap::AttrTable::print
virtual void print(FILE *out, string pad=" ", bool dereference=false)
Prints the attribute table.
Definition AttrTable.cc:1432
libdap::AttrTable::add_container_alias
virtual void add_container_alias(const string &name, AttrTable *src)
Add an alias to a container held by this attribute table.
Definition AttrTable.cc:1052
libdap::AttrTable::get_size
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition AttrTable.cc:259
libdap::AttrTable::dump
virtual void dump(ostream &strm) const
dumps information about this object
Definition AttrTable.cc:1715
libdap::AttrTable::find_container
virtual AttrTable * find_container(const string &target)
Find an attribute with a given name.
Definition AttrTable.cc:710
libdap::AttrTable::simple_find
Attr_iter simple_find(const string &target)
Definition AttrTable.cc:685
libdap::AttrTable::get_attr_type
virtual AttrType get_attr_type(const string &name)
Get the type of an attribute.
Definition AttrTable.cc:765
libdap::AttrTable::recurrsive_find
virtual AttrTable * recurrsive_find(const string &target, Attr_iter *location)
Definition AttrTable.cc:657
libdap::DapObj
libdap base object for common functionality of libdap objects
Definition DapObj.h:51
libdap
top level DAP object to house generic methods
Definition AlarmHandler.h:36
libdap::add_space_encoding
string add_space_encoding(const string &s)
Definition AttrTable.cc:78
libdap::AttrType_to_String
string AttrType_to_String(const AttrType at)
Definition AttrTable.cc:97
libdap::remove_space_encoding
string remove_space_encoding(const string &s)
Definition AttrTable.cc:61
libdap::AttrType
AttrType
Definition AttrTable.h:81
libdap::AttrTable::entry
Definition AttrTable.h:163
libdap::AttrTable::entry::is_dap4_type
bool is_dap4_type(const std::string &path, std::vector< std::string > &inventory) const
Definition AttrTable.h:249