-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.h
75 lines (62 loc) · 2.22 KB
/
xml.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include <string>
#include <sstream>
#include <hash_map>
#include <rapidxml.hpp>
#include <rapidxml_iterators.hpp>
#include <rapidxml_utils.hpp>
typedef rapidxml::xml_document<> XMLDocument;
typedef rapidxml::xml_node<> XMLNode;
typedef rapidxml::xml_attribute<> XMLAttribute;
typedef rapidxml::node_iterator<char> XMLNodeIterator;
void raiseXMLException(const XMLNode& node, const std::string& message);
bool nodeNameIs(const XMLNode& node, const char* name);
bool nodeHasChild(const XMLNode& node, const char* name);
std::string getXMLTypeAttribute(const XMLNode& node);
std::string buildStringFromXMLNode(XMLNode& node);
bool buildBoolFromXMLNode(XMLNode& node);
float buildFloatFromXMLNode(XMLNode& node);
double buildDoubleFromXMLNode(XMLNode& node);
int buildIntFromXMLNode(XMLNode& node);
class XMLChildFinder
{
const XMLNode& m_node;
int m_childrenFound;
public:
XMLChildFinder(const XMLNode& node) : m_node(node), m_childrenFound(0) {}
~XMLChildFinder()
{
XMLNodeIterator it(const_cast<XMLNode*>(&m_node));
for ( ; it != XMLNodeIterator(); ++it) --m_childrenFound;
if (m_childrenFound < 0) raiseXMLException(m_node, "Unexpected children");
}
template <typename T> T required(const char* name, T (*func)(XMLNode&))
{
XMLNode* child = m_node.first_node(name);
if (!child) raiseXMLException(m_node, std::string("Missing child \"") + name + "\"");
m_childrenFound += 1;
return func(*child);
}
template <typename T> T optional(const char* name, T (*func)(XMLNode&))
{
XMLNode* child = m_node.first_node(name);
if (child) { m_childrenFound += 1; return func(*child); }
else { return T(); }
}
template <typename T> std::vector<T> requiredVector(const char* name, T (*func)(XMLNode&))
{
XMLNode* child = m_node.first_node(name);
if (!child) raiseXMLException(m_node, std::string("Missing child \"") + name + "\"");
m_childrenFound += 1;
std::vector<T> result;
XMLNodeIterator it(child);
for ( ; it != XMLNodeIterator(); ++it)
{
if (nodeNameIs(*it, "Item"))
result.push_back(func(*it));
else
raiseXMLException(*child, std::string("Unexpected child: ") + it->name());
}
return result;
}
};