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