-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlRep.h
147 lines (137 loc) · 4.24 KB
/
xmlRep.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef XML_REP_H
#define XML_REP_H
#include <string>
#include <sstream>
#include <vector>
struct attrpair {
std::string name;
std::string value;
attrpair() { ; }
attrpair(const std::string& n, const std::string& v) {
name = n;
value = v;
}
attrpair(const attrpair& ap) {
name = ap.name;
value = ap.value;
}
};
// Should probably hide the children data structure behind operator[] or operator() and
// return only references to an xmlNode
struct xmlNode {
public:
int isSelfClosing;
std::string name;
std::vector<attrpair> attributes;
int valInline; // one for being inline, zero for putting the value on the next line
std::string value;
std::vector<xmlNode> children;
int getChildIndex(const std::string& name, int strict = 0) const;
int getAttributeIndex(const std::string& name, int strict = 0) const;
// note this is somewhat dangerous. Will die if an attribute with that name is not found
// but, if the conversion to the specified type using a stringstream is not good, you will get garbage
template<typename T>
void getAttribute(const std::string& name, T& result) const {
std::string value = attributes[getAttributeIndex(name, 1)].value;
std::stringstream ss(value);
ss >> result;
}
template<typename T>
void getAttribute(const char* name, T& result) const {
std::string sname(name);
return getAttribute(sname, result);
}
int getNumAttributes() const { return attributes.size(); }
int getNumChildren() const { return children.size(); }
template<typename T>
void getValue(T& result) const {
std::stringstream ss(value);
ss >> result;
}
template<typename T, typename TT> void addAttribute(const T& n, const TT& v) {
std::stringstream ss1;
ss1 << n;
std::stringstream ss2;
ss2 << v;
attrpair ap(ss1.str(), ss2.str());
attributes.push_back(ap);
}
template<typename T> void addYesNoAttribute(const T& n, int i) {
std::stringstream ss;
ss << n;
attrpair ap;
ap.name = ss.str();
if (i == 0) {
ap.value = "no";
} else {
ap.value = "yes";
}
attributes.push_back(ap);
}
template<typename T> void setValue(const T& v) {
std::stringstream ss;
ss << v;
value = ss.str();
}
xmlNode& addChild(const xmlNode& nd) {
children.push_back(nd);
return children.back();
}
xmlNode& addChild() {
xmlNode nd;
children.push_back(nd);
return children.back();
}
template<typename T, typename TT> void addParameterChild(const T& n, const TT& v) {
xmlNode nd;
nd.name = "parameter";
nd.addAttribute("name", n);
std::stringstream ss;
ss << v;
nd.value = ss.str();
children.push_back(nd);
}
template<typename T> void addYesNoParameterChild(const T& n, int v) {
xmlNode nd;
nd.name = "parameter";
nd.addAttribute("name", n);
if (v == 0) {
nd.value = "no";
} else {
nd.value = "yes";
}
addChild(nd);
}
xmlNode() { ; }
xmlNode(std::stringstream& ss);
xmlNode(const xmlNode& c);
xmlNode& operator=(const xmlNode& c);
void write(std::ostream& os, int indentLevel = 0) const;
std::string getString(int indentLevel = 0) const;
#ifdef TIMER
static double getNextTagTimer;
static double getNextTagGroupTimer;
#endif
private:
std::string getInStr(int is) const {
std::stringstream ss;
for (int i = 0; i < is; i++) {
ss << " ";
}
return ss.str();
}
void stripcomments(std::stringstream& ss) const;
std::string stripXmlDeclaration(std::stringstream& ss) const;
int checkSelfClosing(const std::string& tagstr) const;
int isEndingTag(const std::string& tagstr) const;
std::string trimWhitespace(const std::string& str) const;
size_t getPosNextLiveChar(const std::string& str, char c) const;
std::string getNextTag(std::stringstream& ss, int removeFromSS = 0) const;
std::string getNextTagGroup(std::stringstream& ss, int removeFromSS = 0) const;
std::string getTagName(const std::string& tagstr) const;
void getNextKeyVal(std::string& contentstr, std::string& key, std::string& val) const;
void handleTagString(const std::string& tagstr, xmlNode& node) const;
int numSubGroups(const std::string& tagGroupStr) const;
std::string getTextElement(const std::string& tagGroupStr) const;
};
#endif