libdap++  Updated for version 3.13.3
D4EnumDef.cc
Go to the documentation of this file.
1 /*
2  * D4EnumDef.cc
3  *
4  * Created on: Oct 9, 2012
5  * Author: jimg
6  */
7 
8 #include "D4EnumDef.h"
9 
10 #include <sstream>
11 
12 #include "util.h"
13 
14 namespace libdap {
15 
16 #if 0
17 string D4EnumDef::type_name() {
18  switch (d_type) {
19  case dods_int8_c:
20  return string("Int8");
21  case dods_uint8_c:
22  return string("UInt8");
23  case dods_byte_c:
24  return string("Byte");
25  case dods_int16_c:
26  return string("Int16");
27  case dods_uint16_c:
28  return string("UInt16");
29  case dods_int32_c:
30  return string("Int32");
31  case dods_uint32_c:
32  return string("UInt32");
33  case dods_int64_c:
34  return string("Int64");
35  case dods_uint64_c:
36  return string("UInt64");
37 
38  default:
39  throw InternalErr(__FILE__, __LINE__, "Invalid enumeration basetype.");
40  }
41 }
42 #endif
43 
44 void D4EnumDef::print_enum_const(XMLWriter *xml, const enum_val ev)
45 {
46  if (xmlTextWriterStartElement(xml->get_writer(), (const xmlChar*)"EnumConst") < 0)
47  throw InternalErr(__FILE__, __LINE__, "Could not write EnumConst element");
48 
49  if (xmlTextWriterWriteAttribute(xml->get_writer(), (const xmlChar*) "name", (const xmlChar*)ev.d_item.c_str()) < 0)
50  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
51 
52  ostringstream oss;
53  oss << ev.d_val;
54  if (xmlTextWriterWriteAttribute(xml->get_writer(), (const xmlChar*) "value", (const xmlChar*)oss.str().c_str()) < 0)
55  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for value");
56 
57  if (xmlTextWriterEndElement(xml->get_writer()) < 0)
58  throw InternalErr(__FILE__, __LINE__, "Could not end EnumConst element");
59 
60 }
61 
63 {
64  if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*)"Enumeration") < 0)
65  throw InternalErr(__FILE__, __LINE__, "Could not write Enumeration element");
66 
67  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*)d_name.c_str()) < 0)
68  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
69 
70  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "basetype", (const xmlChar*)type_name(d_type).c_str()) < 0)
71  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
72 
73  for_each(d_values.begin(), d_values.end(), bind1st(ptr_fun(print_enum_const), &xml));
74 
75  if (xmlTextWriterEndElement(xml.get_writer()) < 0)
76  throw InternalErr(__FILE__, __LINE__, "Could not end Enumeration element");
77 }
78 
79 } /* namespace libdap */
xmlTextWriterPtr get_writer()
Definition: XMLWriter.h:60
void print_xml_writer(XMLWriter &xml)
Definition: D4EnumDef.cc:62
A class for software fault reporting.
Definition: InternalErr.h:64
string type_name(Type t)
Returns the type of the class instance as a string.
Definition: util.cc:522