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 
35 namespace libdap {
36 
37 /** Test if a particular value is legal for a given type. In a D4EnumDef,
38  * all values are actually stored in a long long, but the different
39  * enumerations can specify different types like Byte, Int32, ..., and this
40  * method is used to test that the values match those types.
41  */
42 bool
is_valid_enum_value(long long value)43 D4EnumDef::is_valid_enum_value(long long value)
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 // Note that in order for this to work the second argument must not be a reference.
69 // jhrg 8/20/13
70 static bool
enum_def_name_eq(D4EnumDef * d,const string name)71 enum_def_name_eq(D4EnumDef *d, const string name)
72 {
73     return d->name() == name;
74 }
75 
76 D4EnumDef *
find_enum_def(const string & name)77 D4EnumDefs::find_enum_def(const string &name)
78 {
79     D4EnumDefIter d = find_if(d_enums.begin(), d_enums.end(), bind2nd(ptr_fun(enum_def_name_eq), name));
80     return (d != d_enums.end()) ? *d: 0;
81 }
82 
print_value(XMLWriter & xml,const D4EnumDef::tuple & tuple) const83 void D4EnumDef::print_value(XMLWriter &xml, const D4EnumDef::tuple &tuple) const
84 {
85     if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*)"EnumConst") < 0)
86         throw InternalErr(__FILE__, __LINE__, "Could not write EnumConst element");
87 
88     if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*)tuple.label.c_str()) < 0)
89         throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
90 
91     ostringstream oss;
92     oss << tuple.value;
93     if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "value", (const xmlChar*)oss.str().c_str()) < 0)
94         throw InternalErr(__FILE__, __LINE__, "Could not write attribute for value");
95 
96     if (xmlTextWriterEndElement(xml.get_writer()) < 0)
97         throw InternalErr(__FILE__, __LINE__, "Could not end EnumConst element");
98 }
99 
print_dap4(XMLWriter & xml) const100 void D4EnumDef::print_dap4(XMLWriter &xml) const
101 {
102     vector<D4EnumDef::tuple>::const_iterator i = d_tuples.begin();
103     while(i != d_tuples.end()) {
104         print_value(xml, *i++);
105     }
106 }
107 
m_print_enum(XMLWriter & xml,D4EnumDef * e) const108 void D4EnumDefs::m_print_enum(XMLWriter &xml, D4EnumDef *e) const
109 {
110     if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*)"Enumeration") < 0)
111         throw InternalErr(__FILE__, __LINE__, "Could not write Enumeration element");
112 
113     if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*)e->name().c_str()) < 0)
114         throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
115 
116     if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "basetype", (const xmlChar*)D4type_name(e->type()).c_str()) < 0)
117         throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
118 
119     // print each of e.values
120     e->print_dap4(xml);
121 
122     if (xmlTextWriterEndElement(xml.get_writer()) < 0)
123         throw InternalErr(__FILE__, __LINE__, "Could not end Enumeration element");
124 }
125 
print_dap4(XMLWriter & xml,bool constrained) const126 void D4EnumDefs::print_dap4(XMLWriter &xml, bool constrained) const
127 {
128     D4EnumDefCIter i = d_enums.begin();
129     while (i != d_enums.end()) {
130         if (!constrained || parent()->find_first_var_that_uses_enumeration(*i))
131             m_print_enum(xml, *i);
132         ++i;
133     }
134 }
135 
136 } /* namespace libdap */
137