GRSISort "v4.1.1.0"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TXMLOdb.cxx
Go to the documentation of this file.
1#ifdef HAS_XML
2
3#include <stdexcept>
4
5#include "TXMLOdb.h"
6
7#include "TList.h"
8#include "TXMLAttr.h"
9
10std::array<char, 256> TXMLOdb::fTextBuffer;
11
12TXMLOdb::TXMLOdb(char* buffer, int size)
13 : fDoc(nullptr), fParser(new TDOMParser), fOdb(nullptr)
14{
15 /// Creator, tries to open buffer as input file and parse it, if that fails, parses size bytes of the buffer.
16
17 // trim empty new-line from end of buffer if needed
18 while(buffer[size - 1] == '\0') {
19 // std::cout << "Found null-character at " << size - 1 << ", decrementing size from " << size;
20 --size;
21 // std::cout << " to " << size << std::endl;
22 }
23 std::ifstream input;
24 input.open(buffer);
25 fParser->SetValidate(false);
26 if(input.is_open()) {
27 fParser->ParseFile(buffer);
28 } else {
29 std::string odb(buffer, size);
30 fParser->ParseBuffer(buffer, size);
31 }
32 input.close();
33 fDoc = fParser->GetXMLDocument();
34 if(fDoc == nullptr) {
35 throw std::runtime_error("XmlOdb::XmlOdb: Malformed ODB dump: cannot get XML document");
36 }
37 fOdb = fDoc->GetRootNode();
38 if(strcmp(fOdb->GetNodeName(), "odb") != 0) {
39 throw std::runtime_error("XmlOdb::XmlOdb: Malformed ODB dump: cannot find <odb> tag");
40 }
41}
42
44{
45 /// Default destructor, deletes the parser.
46 delete fParser;
47}
48
49TXMLNode* TXMLOdb::FindNode(const char* name, TXMLNode* node)
50{
51 /// Finds node with name "name". If a node is provided this node will be used as a starting point.
52 /// If the provided node is a null pointer fOdb is used instead. Returns a null pointer if the
53 /// search fails.
54 if(node == nullptr) {
55 if(fOdb == nullptr) {
56 return nullptr;
57 }
58 node = fOdb; //->GetChildren();
59 }
60 if(!node->HasChildren()) {
61 return nullptr;
62 }
63 node = node->GetChildren();
64 while(node != nullptr) {
65 std::string nodename = GetNodeName(node);
66 if(nodename == name) {
67 return node;
68 }
69 node = node->GetNextNode();
70 }
71
72 return nullptr;
73}
74
75TXMLNode* TXMLOdb::FindPath(const char* path, TXMLNode* node)
76{
77 /// Find path "path" under the provided node. If the node is a null pointer, fOdb is used instead.
78 if(node == nullptr) {
79 if(fOdb == nullptr) {
80 return nullptr;
81 }
82 node = fOdb; //->GetChildren();
83 }
84
85 std::string pathname = path;
86 std::vector<std::string> elems;
87 std::size_t last = 0;
88 std::size_t slash = pathname.find_first_of('/');
89 if(slash == 0) {
90 last = 1;
91 }
92 while(true) {
93 slash = pathname.find_first_of('/', last);
94 elems.push_back(pathname.substr(last, slash - last));
95 last = slash + 1;
96 if(slash == std::string::npos) {
97 break;
98 }
99 }
100
101 for(auto& elem : elems) {
102 node = FindNode(elem.c_str(), node);
103 if(node != nullptr) {
104 } else {
105 node = nullptr;
106 break;
107 }
108 }
109
110 return node;
111}
112
113const char* TXMLOdb::GetNodeName(TXMLNode* node)
114{
115 /// Returns the name of a node.
116 TList* list = node->GetAttributes();
117 if(list != nullptr) {
118 std::string buffer = (static_cast<TXMLAttr*>(list->At(0)))->GetValue();
119 strlcpy(fTextBuffer.data(), buffer.c_str(), fTextBuffer.size());
120 return fTextBuffer.data();
121 }
122 return "";
123}
124
125int TXMLOdb::ReadInt(const char* path, int, int defaultValue)
126{
127 /// tries to find the path "path", returns defaultValue if that fails, otherwise returns 0.
128 TXMLNode* node = FindPath(path);
129 if(node == nullptr) {
130 return defaultValue;
131 }
132 return 0;
133}
134
135std::vector<int> TXMLOdb::ReadIntArray(TXMLNode* node)
136{
137 /// Reads and returns an array of integers.
138
139 std::vector<int> temp;
140 if(node == nullptr) {
141 return temp;
142 }
143 if(!node->HasChildren()) {
144 return temp;
145 }
146 TList* list = node->GetAttributes();
147 if(list == nullptr) {
148 return temp;
149 }
150 TIter iter(list);
151 int size = 0;
152 while(auto* attr = static_cast<TXMLAttr*>(iter.Next())) {
153 if(strcmp(attr->GetName(), "num_values") == 0) {
154 size = atoi(attr->GetValue());
155 }
156 }
157 temp.assign(size, 0);
158 TXMLNode* child = node->GetChildren();
159 int counter = 0;
160 while(true) {
161 if(TList* index = child->GetAttributes()) {
162 int indexnum = atoi((static_cast<TXMLAttr*>(index->At(0)))->GetValue());
163 int value = atoi(child->GetText());
164 temp.at(indexnum) = value;
165 } else if(child->GetText() != nullptr) {
166 int indexnum = counter++;
167 temp.at(indexnum) = atoi(child->GetText());
168 }
169 child = child->GetNextNode();
170 if(child == nullptr) {
171 break;
172 }
173 }
174 return temp;
175}
176
177std::vector<std::string> TXMLOdb::ReadStringArray(TXMLNode* node)
178{
179 /// Reads and returns an array of strings.
180
181 std::vector<std::string> temp;
182 if(node == nullptr) {
183 return temp;
184 }
185 if(!node->HasChildren()) {
186 return temp;
187 }
188 TList* list = node->GetAttributes();
189 if(list == nullptr) {
190 return temp;
191 }
192 TIter iter(list);
193 int size = 0;
194 while(auto* attr = static_cast<TXMLAttr*>(iter.Next())) {
195 if(strcmp(attr->GetName(), "num_values") == 0) {
196 size = atoi(attr->GetValue());
197 }
198 }
199 temp.assign(size, "");
200 TXMLNode* child = node->GetChildren();
201 int counter = 0;
202 while(true) {
203 if(TList* index = child->GetAttributes()) {
204 int indexnum = atoi((static_cast<TXMLAttr*>(index->At(0)))->GetValue());
205 const char* value = child->GetText();
206
207 // Make sure we actually read a word
208 std::string value_str;
209 if(value == nullptr) {
210 value_str = "";
211 } else {
212 value_str = value;
213 }
214 temp.at(indexnum) = value_str;
215 } else if(child->GetText() != nullptr) {
216 int indexnum = counter++;
217 temp.at(indexnum).assign(child->GetText());
218 }
219 child = child->GetNextNode();
220 if(child == nullptr) {
221 break;
222 }
223 }
224 return temp;
225}
226
227std::vector<double> TXMLOdb::ReadDoubleArray(TXMLNode* node)
228{
229 /// Reads and returns an array of doubles.
230
231 std::vector<double> temp;
232 if(node == nullptr) {
233 return temp;
234 }
235 if(!node->HasChildren()) {
236 return temp;
237 }
238 TList* list = node->GetAttributes();
239 if(list == nullptr) {
240 return temp;
241 }
242 TIter iter(list);
243 int size = 0;
244 while(auto* attr = static_cast<TXMLAttr*>(iter.Next())) {
245 if(strcmp(attr->GetName(), "num_values") == 0) {
246 size = atoi(attr->GetValue());
247 }
248 }
249 temp.assign(size, 0.0);
250 TXMLNode* child = node->GetChildren();
251 int counter = 0;
252 while(true) {
253 if(TList* index = child->GetAttributes()) {
254 int indexnum = atoi((static_cast<TXMLAttr*>(index->At(0)))->GetValue());
255 double value = atof(child->GetText());
256 temp.at(indexnum) = value;
257 } else if(child->GetText() != nullptr) {
258 int indexnum = counter++;
259 temp.at(indexnum) = atof(child->GetText());
260 }
261 child = child->GetNextNode();
262 if(child == nullptr) {
263 break;
264 }
265 }
266 return temp;
267}
268#endif
std::vector< std::string > ReadStringArray(TXMLNode *node)
Definition TXMLOdb.cxx:177
TXMLDocument * fDoc
Definition TXMLOdb.h:40
TXMLOdb(char *buffer, int size=0)
Definition TXMLOdb.cxx:12
const char * GetNodeName(TXMLNode *)
Definition TXMLOdb.cxx:113
std::vector< double > ReadDoubleArray(TXMLNode *node)
Definition TXMLOdb.cxx:227
int ReadInt(const char *path, int index=0, int defaultValue=0xffffffff)
Definition TXMLOdb.cxx:125
TDOMParser * fParser
Definition TXMLOdb.h:41
TXMLNode * FindPath(const char *path, TXMLNode *node=nullptr)
Definition TXMLOdb.cxx:75
TXMLNode * FindNode(const char *name, TXMLNode *node=nullptr)
Definition TXMLOdb.cxx:49
std::vector< int > ReadIntArray(TXMLNode *node)
Definition TXMLOdb.cxx:135
static std::array< char, 256 > fTextBuffer
Definition TXMLOdb.h:54
virtual ~TXMLOdb()
Definition TXMLOdb.cxx:43
TXMLNode * fOdb
Definition TXMLOdb.h:42