libdap Updated for version 3.20.10
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4EnumDefs.cc
1// -*- mode: c++; c-basic-offset:4 -*-
2
3// This file is part of libdap, A C++ implementation of the OPeNDAP Data
4// Access Protocol.
5
6// Copyright (c) 2013 OPeNDAP, Inc.
7// Author: James Gallagher <jgallagher@opendap.org>
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24
25#include "config.h"
26
27#include "D4Group.h"
28#include "D4EnumDefs.h"
29
30#include <sstream>
31
32#include "dods-limits.h"
33#include "util.h"
34
35namespace libdap {
36
42bool
44{
45 switch (type()) {
46 case dods_int8_c:
47 return (value >= DODS_SCHAR_MIN && value <= DODS_SCHAR_MAX);
48 case dods_byte_c:
49 case dods_uint8_c:
50 return (value >= 0 && static_cast<unsigned long long>(value) <= DODS_UCHAR_MAX);
51 case dods_int16_c:
52 return (value >= DODS_SHRT_MIN && value <= DODS_SHRT_MAX);
53 case dods_uint16_c:
54 return (value >= 0 && static_cast<unsigned long long>(value) <= DODS_USHRT_MAX);
55 case dods_int32_c:
56 return (value >= DODS_INT_MIN && value <= DODS_INT_MAX);
57 case dods_uint32_c:
58 return (value >= 0 && static_cast<unsigned long long>(value) <= DODS_UINT_MAX);
59 case dods_int64_c:
60 return true; // This is always true: (value >= DODS_LLONG_MIN && value <= DODS_LLONG_MAX);
61 case dods_uint64_c:
62 return (value >= 0 /*Always true: && static_cast<unsigned long long>(value) <= DODS_ULLONG_MAX*/);
63 default:
64 return false;
65 }
66}
67
68#if 0
69// Note that in order for this to work the second argument must not be a reference.
70// jhrg 8/20/13
71static bool
72enum_def_name_eq(D4EnumDef *d, const string name)
73{
74 return d->name() == name;
75}
76#endif
77
78D4EnumDef *
79D4EnumDefs::find_enum_def(const string &name)
80{
81 auto d = find_if(d_enums.begin(), d_enums.end(),
82 [name](const D4EnumDef *def) { return name == def->name(); });
83
84 return (d != d_enums.end()) ? *d: nullptr;
85}
86
87void D4EnumDef::print_value(XMLWriter &xml, const D4EnumDef::tuple &tuple) const
88{
89 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*)"EnumConst") < 0)
90 throw InternalErr(__FILE__, __LINE__, "Could not write EnumConst element");
91
92 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*)tuple.label.c_str()) < 0)
93 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
94
95 ostringstream oss;
96 oss << tuple.value;
97 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "value", (const xmlChar*)oss.str().c_str()) < 0)
98 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for value");
99
100 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
101 throw InternalErr(__FILE__, __LINE__, "Could not end EnumConst element");
102}
103
104void D4EnumDef::print_dap4(XMLWriter &xml) const
105{
106 vector<D4EnumDef::tuple>::const_iterator i = d_tuples.begin();
107 while(i != d_tuples.end()) {
108 print_value(xml, *i++);
109 }
110}
111
112void D4EnumDefs::m_print_enum(XMLWriter &xml, D4EnumDef *e) const
113{
114 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*)"Enumeration") < 0)
115 throw InternalErr(__FILE__, __LINE__, "Could not write Enumeration element");
116
117 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*)e->name().c_str()) < 0)
118 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
119
120 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "basetype", (const xmlChar*)D4type_name(e->type()).c_str()) < 0)
121 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
122
123 // print each of e.values
124 e->print_dap4(xml);
125
126 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
127 throw InternalErr(__FILE__, __LINE__, "Could not end Enumeration element");
128}
129
130void D4EnumDefs::print_dap4(XMLWriter &xml, bool constrained) const
131{
132 D4EnumDefCIter i = d_enums.begin();
133 while (i != d_enums.end()) {
134 if (!constrained || parent()->find_first_var_that_uses_enumeration(*i))
135 m_print_enum(xml, *i);
136 ++i;
137 }
138}
139
140} /* namespace libdap */
bool is_valid_enum_value(long long value)
Definition D4EnumDefs.cc:43
top level DAP object to house generic methods
string D4type_name(Type t)
Returns the type of the class instance as a string. Supports all DAP4 types and not the DAP2-only typ...
Definition util.cc:697