forked from ismrmrd/siemens_to_ismrmrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConverterXml.h
287 lines (231 loc) · 8.15 KB
/
ConverterXml.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#ifndef CONVERTERXML_H
#define CONVERTERXML_H
#include <string>
#include <sstream>
#include <vector>
#include "tinyxml.h"
class ConverterXMLNode
{
public:
ConverterXMLNode(TiXmlNode* anchor)
: anchor_(anchor)
{ }
template<typename T> std::vector<T> get(std::string name);
ConverterXMLNode add(const std::string name) {
//We have to make a copy of this string, the strtok function will eat it up
char* tmp = new char[name.size()+1];
memcpy(tmp,name.c_str(),name.size());
tmp[name.size()] = 0;
TiXmlHandle h( anchor_ );
char* lname = strtok(tmp,".");
TiXmlElement* child = 0;
while (lname) {
char* next = strtok(NULL,"."); //Get next token
child = h.FirstChildElement(lname).ToElement();
if (!child) {
child = h.ToNode()->LinkEndChild(new TiXmlElement(lname))->ToElement();
} else if (!next) {
//We are on the last level, and we are finding the path, so there must tags with this name.
//Let's add a sibling.
child = child->Parent()->LinkEndChild(new TiXmlElement(lname))->ToElement();
}
lname = next;
h = TiXmlHandle(child);
}
delete [] tmp; tmp = 0x0;
return ConverterXMLNode(child);
}
template<typename T> ConverterXMLNode add(const std::string name, T contents) {
ConverterXMLNode n = add(name);
std::stringstream ss; ss << contents;
n.get_node()->LinkEndChild(new TiXmlText(ss.str().c_str()));
return n;
}
template<typename T> ConverterXMLNode add(std::string n, std::vector<T> c) {
ConverterXMLNode ret(NULL);
for (unsigned int i = 0; i < c.size(); i++) {
ret = add<T>(n,c[i]);
}
return ret;
}
template<typename T> T contents();
TiXmlNode* get_node() {
return anchor_;
}
TiXmlDocument* get_document() {
if (!anchor_) {
return 0;
}
return anchor_->GetDocument();
}
protected:
TiXmlNode* anchor_;
};
template<> inline std::string ConverterXMLNode::contents() {
std::string ret_val("");
TiXmlNode* e = anchor_->FirstChild();
if (e && (e->Type() == TiXmlNode::TINYXML_TEXT)) {
return std::string(e->Value());
}
return ret_val;
}
template<> inline long int ConverterXMLNode::contents() {
std::string s = contents<std::string>();
return atoi(s.c_str());
}
template<> inline double ConverterXMLNode::contents() {
std::string s = contents<std::string>();
return atof(s.c_str());
}
template<typename T> inline std::vector<T> contents(std::vector<ConverterXMLNode>& v)
{
std::vector<T> ret(v.size());
for (unsigned int i = 0; i < v.size(); i++) {
ret[i] = v[i].contents<T>();
}
return ret;
}
template<> inline std::vector<ConverterXMLNode> ConverterXMLNode::get<ConverterXMLNode>(std::string name) {
std::vector<ConverterXMLNode> ret;
//We have to make a copy of this string, the strtok function will eat it up
char* tmp = new char[name.size()+1];
memcpy(tmp,name.c_str(),name.size());
tmp[name.size()] = '\0';
TiXmlHandle h( anchor_ );
char* lname = strtok(tmp,".");
TiXmlElement* child;
while (lname && (child = h.FirstChildElement(lname).ToElement())) {
h = TiXmlHandle(child);
lname = strtok(NULL,"."); //Get next token
}
if (child) {
ret.push_back(ConverterXMLNode(child));
while ( (child = child->NextSiblingElement(child->Value())) ) {
ret.push_back(ConverterXMLNode(child));
}
}
delete [] tmp; tmp = 0x0;
return ret;
}
template<> inline std::vector<long> ConverterXMLNode::get<long>(std::string name) {
std::vector<ConverterXMLNode> vals = get<ConverterXMLNode>(name);
return ::contents<long>(vals);
}
template<> inline std::vector<double> ConverterXMLNode::get<double>(std::string name) {
std::vector<ConverterXMLNode> vals = get<ConverterXMLNode>(name);
return ::contents<double>(vals);
}
template<> inline std::vector<std::string> ConverterXMLNode::get< std::string >(std::string name) {
std::vector<ConverterXMLNode> vals = get<ConverterXMLNode>(name);
return ::contents<std::string>(vals);
}
// TODO BEGIN
// These few things may be obsolete
inline TiXmlElement* AddClassToXML(TiXmlNode* anchor, const char* section, const char* type, const char* class_name, const char* dll, int slot = 0, const char* name = 0)
{
TiXmlElement* pSectionElem = anchor->FirstChildElement(section);
if (!pSectionElem) {
pSectionElem = new TiXmlElement(section);
anchor->LinkEndChild(pSectionElem);
}
TiXmlElement* classElement = new TiXmlElement( type );
classElement->SetAttribute("class", class_name);
classElement->SetAttribute("dll", dll);
if (slot) {
classElement->SetAttribute("slot", slot);
}
if (name) {
classElement->SetAttribute("name", name);
}
pSectionElem->LinkEndChild(classElement);
return classElement;
}
inline TiXmlElement* AddWriterToXML(TiXmlNode* anchor, const char* class_name, const char* dll, int slot)
{
return AddClassToXML(anchor, "writers", "writer", class_name, dll, slot);
}
inline TiXmlElement* AddReaderToXML(TiXmlNode* anchor, const char* class_name, const char* dll, int slot)
{
return AddClassToXML(anchor, "readers", "reader", class_name, dll, slot);
}
inline TiXmlElement* AddConverterToXML(TiXmlNode* anchor, const char* name, const char* class_name, const char* dll)
{
return AddClassToXML(anchor, "stream", "gadget", class_name, dll,0,name);
}
// TODO END
template <class T> inline TiXmlElement* AddPropertyToXMLElement(TiXmlNode* anchor, const char* name, T value)
{
TiXmlElement* parameterElement = new TiXmlElement( "property" );
parameterElement->SetAttribute("name", name);
parameterElement->SetAttribute("value", value);
anchor->LinkEndChild(parameterElement);
return parameterElement;
}
template <class T> inline TiXmlElement* AddParameterToXML(TiXmlNode* anchor, const char* section,
const char* name, T value)
{
TiXmlElement* pSectionElem = anchor->FirstChildElement(section);
if (!pSectionElem) {
pSectionElem = new TiXmlElement(section);
anchor->LinkEndChild(pSectionElem);
}
TiXmlElement* parameterElement = new TiXmlElement( "parameter" );
parameterElement->SetAttribute("name", name);
parameterElement->SetAttribute("value", value);
pSectionElem->LinkEndChild(parameterElement);
return parameterElement;
}
inline TiXmlElement* AddDoubleParameterToXML(TiXmlNode* anchor, const char* section,
const char* name, double value)
{
TiXmlElement* pSectionElem = anchor->FirstChildElement(section);
if (!pSectionElem) {
pSectionElem = new TiXmlElement(section);
anchor->LinkEndChild(pSectionElem);
}
TiXmlElement* parameterElement = new TiXmlElement( "parameter" );
parameterElement->SetAttribute("name", name);
parameterElement->SetDoubleAttribute("value", value);
pSectionElem->LinkEndChild(parameterElement);
return parameterElement;
}
inline std::string GetStringParameterValueFromXML(TiXmlNode* anchor, const char* section,
const char* name)
{
TiXmlNode* child = 0;;
TiXmlNode* parent = anchor->FirstChildElement(section);
std::string ret("");
while( (child = parent->IterateChildren( child )) ) {
if ((child->Type() == TiXmlNode::TINYXML_ELEMENT) &&
//(ACE_OS::strncmp(child->ToElement()->Value(),"parameter",9) == 0))
(strncmp(child->ToElement()->Value(),"parameter",9) == 0))
{
//if ((ACE_OS::strncmp(child->ToElement()->Attribute("name"),name,100) == 0)) {
if ((strncmp(child->ToElement()->Attribute("name"),name,100) == 0)) {
ret = std::string(child->ToElement()->Attribute("value"));
break;
}
}
}
return ret;
}
inline int GetIntParameterValueFromXML(TiXmlNode* anchor, const char* section,
const char* name)
{
//return ACE_OS::atoi(GetStringParameterValueFromXML(anchor, section, name).c_str());
return atoi(GetStringParameterValueFromXML(anchor, section, name).c_str());
}
inline double GetDoubleParameterValueFromXML(TiXmlNode* anchor, const char* section,
const char* name)
{
//return ACE_OS::atof(GetStringParameterValueFromXML(anchor, section, name).c_str());
return atof(GetStringParameterValueFromXML(anchor, section, name).c_str());
}
inline std::string XmlToString(TiXmlNode& base)
{
TiXmlPrinter printer;
base.Accept( &printer );
std::string xmltext = printer.CStr();
return xmltext;
}
#endif //CONVERTERXML_H