TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
XML.h
Go to the documentation of this file.
1 
14 #ifndef G3D_XML_h
15 #define G3D_XML_h
16 
17 #include "G3D/platform.h"
18 #include "G3D/Table.h"
19 #include "G3D/Array.h"
20 #include "G3D/format.h"
21 #include <string>
22 
23 namespace G3D {
24 
25 class TextInput;
26 class TextOutput;
27 
65 class XML {
66 public:
67 
68  enum Type {VALUE, TAG};
69 
71 
72 private:
73 
75  std::string m_name;
76  std::string m_value;
77  AttributeTable m_attribute;
79 
80 public:
81 
82  XML() : m_type(VALUE) {}
83 
84  XML(const std::string& v) : m_type(VALUE), m_value(v) {}
85 
86  XML(const double& v) : m_type(VALUE), m_value(format("%f", v)) {}
87 
88  XML(float v) : m_type(VALUE), m_value(format("%f", v)) {}
89 
90  XML(int v) : m_type(VALUE), m_value(format("%d", v)) {}
91 
93  XML(Type tagType, const std::string& name, const AttributeTable& at, const Array<XML>& ch = Array<XML>()) : m_type(TAG), m_name(name), m_attribute(at), m_child(ch) {
94  (void)tagType;
95  debugAssert(tagType == TAG);
96  }
97 
99  XML(Type tagType, const std::string& name, const Array<XML>& ch = Array<XML>()) : m_type(TAG), m_name(name), m_child(ch) {
100  (void)tagType;
101  debugAssert(tagType == TAG);
102  }
103 
104  XML(TextInput& t);
105 
106  void serialize(TextOutput& t) const;
107 
108  void deserialize(TextInput& t);
109 
110  void load(const std::string& filename);
111 
112  void save(const std::string& filename) const;
113 
114  void parse(const std::string &s);
115 
116  void unparse(std::string& s) const;
117 
118  const AttributeTable& attributeTable() const {
119  return m_attribute;
120  }
121 
122  const Array<XML>& childArray() const {
123  return m_child;
124  }
125 
127  int numChildren() const {
128  return m_child.size();
129  }
130 
132  size_t numAttributes() const {
133  return m_attribute.size();
134  }
135 
138  const XML& operator[](int i) const {
139  return m_child[i];
140  }
141 
143  const XML& operator[](const std::string& k) const {
144  return m_attribute[k];
145  }
146 
147  bool containsAttribute(const std::string& k) const {
148  return m_attribute.containsKey(k);
149  }
150 
153  XML get(const std::string& k, const XML& defaultVal) const {
154  const XML* x = m_attribute.getPointer(k);
155  if (x) {
156  return *x;
157  } else {
158  return defaultVal;
159  }
160  }
161 
162  Type type() const {
163  return m_type;
164  }
165 
167  const std::string name() const {
168  return m_name;
169  }
170 
172  const std::string& string() const {
173  return m_value;
174  }
175 
177  double number() const;
178 
180  bool boolean() const;
181 
182  operator std::string() const {
183  return m_value;
184  }
185 
186  operator bool() const {
187  return boolean();
188  }
189 
190  operator double() const {
191  return number();
192  }
193 
194  operator float() const {
195  return float(number());
196  }
197 
198  operator int() const {
199  return iRound(number());
200  }
201 
202 }; // class XML
203 
204 } // namespace G3D
205 
206 #endif
void parse(const std::string &s)
Definition: XML.cpp:91
AttributeTable m_attribute
Definition: XML.h:77
void save(const std::string &filename) const
Definition: XML.cpp:47
const std::string & string() const
Definition: XML.h:172
Value * getPointer(const Key &key) const
Definition: Table.h:731
Table< std::string, XML > AttributeTable
Definition: XML.h:70
int numChildren() const
Definition: XML.h:127
const AttributeTable & attributeTable() const
Definition: XML.h:118
Type m_type
Definition: XML.h:74
Type
Definition: XML.h:68
const Array< XML > & childArray() const
Definition: XML.h:122
size_t size() const
Definition: Table.h:589
void unparse(std::string &s) const
Definition: XML.cpp:54
int iRound(double fValue)
Definition: g3dmath.h:226
std::string m_value
Definition: XML.h:76
Definition: AABox.h:25
Dynamic 1D array tuned for performance.
Definition: Array.h:95
const std::string name() const
Definition: XML.h:167
XML(const double &v)
Definition: XML.h:86
void load(const std::string &filename)
Definition: XML.cpp:36
Definition: XML.h:68
#define bool
Definition: CascPort.h:16
XML(float v)
Definition: XML.h:88
XML(int v)
Definition: XML.h:90
#define debugAssert(exp)
Definition: debugAssert.h:160
Definition: XML.h:68
Array< XML > m_child
Definition: XML.h:78
A simple tokenizer for parsing text files.
Definition: TextInput.h:223
Easy loading and saving of XML and HTML files.
Definition: XML.h:65
XML(Type tagType, const std::string &name, const Array< XML > &ch=Array< XML >())
Definition: XML.h:99
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS
int size() const
Definition: Array.h:430
Definition: TextOutput.h:60
bool containsAttribute(const std::string &k) const
Definition: XML.h:147
Type type() const
Definition: XML.h:162
void deserialize(TextInput &t)
Definition: XML.cpp:125
void serialize(TextOutput &t) const
Definition: XML.cpp:65
bool boolean() const
Definition: XML.cpp:31
std::string m_name
Definition: XML.h:75
double number() const
Definition: XML.cpp:26
const XML & operator[](int i) const
Definition: XML.h:138
XML()
Definition: XML.h:82
XML(Type tagType, const std::string &name, const AttributeTable &at, const Array< XML > &ch=Array< XML >())
Definition: XML.h:93
G3D::int16 x
Definition: Vector2int16.h:37
size_t numAttributes() const
Definition: XML.h:132
const XML & operator[](const std::string &k) const
Definition: XML.h:143
bool containsKey(const Key &key) const
Definition: Table.h:874
XML(const std::string &v)
Definition: XML.h:84