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