ECF 1.7
Registry.cpp
1#include "ECF_base.h"
2
3
15bool Registry::registerEntry(std::string key, voidP x, enum ECF::type T, std::string description, uint index)
16{
17 ParamP a = (ParamP) new ECF::Param;
18 a->value= x;
19 a->T = T;
20 a->description = description;
21 a->bModified=false;
22
23 if(index > 0)
24 key = uint2str(index) + key;
25
26 map_iter it = parameters_.find(key);
27 if(it != parameters_.end())
28 return false;
29
30 parameters_.insert (std::pair < std::string, ParamP >(key, a));
31 return true;
32}
33
34
39bool Registry::isRegistered(std::string key)
40{
41 return parameters_.find(key) != parameters_.end();
42}
43
44
50bool Registry::modifyEntry(std::string key, voidP x, uint index)
51{
52 if(index > 0)
53 key = uint2str(index) + key;
54
55 map_iter it = parameters_.find(key);
56 if(it != parameters_.end()) {
57 it->second->value=x;
58 it->second->bModified=true;
59 return true;
60 }
61 else
62 return false;
63}
64
65
71voidP Registry::getEntry(std::string key, uint index)
72{
73 if(index > 0)
74 key = uint2str(index) + key;
75
76 map_iter iter = parameters_.find(key);
77
78 if (iter != parameters_.end())
79 return iter->second->value;
80
81 voidP p;
82 return p;
83}
84
85
92{
93 map_iter iter = parameters_.find(key);
94 if (iter != parameters_.end())
95 return iter->second->T;
96
97 return ECF::ERROR;
98}
99
100
106bool Registry::isModified(std::string key, uint index)
107{
108 if(index > 0)
109 key = uint2str(index) + key;
110
111 map_iter iter = parameters_.find(key);
112
113 if (iter != parameters_.end())
114 return iter->second->bModified;
115
116 return false;
117}
118
119
120
129bool Registry::readEntries(const XMLNode& node, std::string prefix, uint index)
130{
131/*
132 uint num = node.count(NODE_ENTRY);
133
134 //std::cout << node.get_child("Entry.<xmlattr>.key").data() << std::endl;
135
136 //print_tree(node);
137 //pt::ptree entry = node.get_child(NODE_ENTRY);
138 //pt::ptree::data_type& data = entry.data();
139 //std::cout << v.first << std::endl;
140 //std::cout << v.second.data() << std::endl;
141
142 //pt::ptree& attributes = node.get_child("<xmlattr>.key");
143 //BOOST_FOREACH(pt::ptree::value_type &v, attributes){
144 // std::cout << "First: " << v.first.data() << " Second: " << v.second.data() << std::endl;
145 //}
146
147 // iterate all nodes, ignore ones not <Entry>
148 BOOST_FOREACH(pt::ptree::value_type &v, node) {
149
150 if(v.first == "<xmlcomment>")
151 continue;
152
153 if(v.first != NODE_ENTRY) {
154 std::cout << "Warning: unknown entry! ('" << v.first << "')" << std::endl;
155 continue;
156 }
157
158 pt::ptree entry = v.second;
159 if(entry.count("<xmlattr>") == 0) {
160 std::cout << "Error: no attribute 'key' in current entry! (node '" << prefix << "')" << std::endl;
161 return false;
162 }
163
164 pt::ptree pkey = entry.get_child("<xmlattr>");
165 std::string key = pkey.get("key", "");
166
167 if(key == "") {
168 std::cout << "Error: no attribute 'key' in current entry! (node '" << prefix << "')" << std::endl;
169 return false;
170 }
171
172 key = (prefix.length() > 0 ? prefix + "." : "") + key;
173
174 if (index > 0)
175 key = uint2str(index) + key;
176
177 if (!isRegistered(key)) {
178 std::cerr << "Warning: key " << key << " not registered." << std::endl;
179 continue;
180 }
181
182 ECF::type t = getType(key);
183 voidP new_value;
184
185 std::string value = entry.get_value<std::string>("");
186
187 if(value == "") {
188 std::cout << "Error: no text in Entry node! (key: " << key << ")" << std::endl;
189 return false;
190 }
191
192 bool ok = true;
193 switch (t) {
194 case ECF::INT:
195 {
196 int *x = new int;
197 *x = str2int(value);
198 new_value = (voidP)x;
199
200 break;
201 }
202 case ECF::UINT:
203 {
204 uint *x = new uint;
205 *x = str2uint(value);
206 new_value = (voidP)x;
207
208 break;
209 }
210 case ECF::STRING:
211 {
212 std::string *s = new std::string(value);
213 new_value = (voidP)s;
214 break;
215 }
216 case ECF::FLOAT:
217 {
218 float *x = new float;
219 *x = str2flt(value);
220 new_value = (voidP)x;
221
222 break;
223 }
224 case ECF::DOUBLE:
225 {
226
227 double *x = new double;
228 *x = str2dbl(value);
229 new_value = (voidP)x;
230
231 break;
232 }
233 case ECF::CHAR:
234 {
235 char *x = new char;
236 *x = value[0];
237 new_value = (voidP)x;
238
239 break;
240 }
241 default:
242 ok = false;
243 break;
244 }
245
246 if (ok)
247 modifyEntry(key, new_value);
248 }
249*/
250
251
252 int num = node.nChildNode(NODE_ENTRY), iter = 0;
253
254 for (int i = 0; i < num; ++i) {
255 XMLNode child = node.getChildNode(NODE_ENTRY, &iter);
256 if(!child.getAttribute("key")) {
257 std::cerr << "Error: no attribute 'key' in current entry! (node '" << node.getName() << "')" << std::endl;
258 return false;
259 }
260
261 std::string key = (prefix.length() > 0 ? prefix + "." : "") + child.getAttribute("key");
262 if (child.getText() == NULL) {
263 std::cerr << "Error: no text in Entry node! (key: " << key << ")" << std::endl;
264 return false;
265 }
266 std::string value = child.getText();
267
268 if (index > 0)
269 key = uint2str(index) + key;
270
271 if (!isRegistered(key)) {
272 std::cerr << "Warning: key " << key << " not registered." << std::endl;
273 continue;
274 }
275
276 ECF::type t = getType(key);
277 voidP new_value;
278
279 bool ok = true;
280 switch (t) {
281 case ECF::INT:
282 {
283 int *x = new int;
284 *x = str2int(value);
285 new_value = (voidP)x;
286
287 break;
288 }
289 case ECF::UINT:
290 {
291 uint *x = new uint;
292 *x = str2uint(value);
293 new_value = (voidP)x;
294
295 break;
296 }
297 case ECF::STRING:
298 {
299 std::string *s = new std::string(value);
300 new_value = (voidP)s;
301 break;
302 }
303 case ECF::FLOAT:
304 {
305 float *x = new float;
306 *x = str2flt(value);
307 new_value = (voidP)x;
308
309 break;
310 }
311 case ECF::DOUBLE:
312 {
313
314 double *x = new double;
315 *x = str2dbl(value);
316 new_value = (voidP)x;
317
318 break;
319 }
320 case ECF::CHAR:
321 {
322 char *x = new char;
323 *x = value[0];
324 new_value = (voidP)x;
325
326 break;
327 }
328 default:
329 ok = false;
330 break;
331 }
332
333 if (ok)
334 modifyEntry(key, new_value);
335 }
336
337 return true;
338}
339
340
345void Registry::write(XMLNode& xRegistry)
346{
347 xRegistry = XMLNode::createXMLTopNode(NODE_REGISTRY);
348
349 for (map_iter it = parameters_.begin(); it != parameters_.end(); ++it) {
350
351 if(!it->second->bModified)
352 continue;
353
354 XMLNode xParam = XMLNode::createXMLTopNode(NODE_ENTRY);
355 std::string key = it->first;
356 voidP value = it->second->value;
357 ECF::type T = it->second->T;
358 std::stringstream ss;
359
360 switch (T) {
361 case ECF::INT:
362 ss << *((int *)value.get());
363 break;
364 case ECF::UINT:
365 ss << *((uint *)value.get());
366 break;
367 case ECF::STRING:
368 ss << *((std::string *)value.get());
369 break;
370 case ECF::FLOAT:
371 ss << *((float *)value.get());
372 break;
373 case ECF::DOUBLE:
374 ss << *((double *)value.get());
375 break;
376 case ECF::CHAR:
377 ss << *((char *)value.get());
378 break;
379 default:
380 break;
381 }
382 xParam.addAttribute("key", key.c_str());
383 xParam.addText(ss.str().c_str());
384 xRegistry.addChild(xParam);
385 }
386}
387
388
393void Registry::dumpEntries(XMLNode& xRegistry)
394{
395 xRegistry = XMLNode::createXMLTopNode(NODE_REGISTRY);
396
397 for (map_iter it = parameters_.begin(); it != parameters_.end(); ++it) {
398
399 XMLNode xParam = XMLNode::createXMLTopNode(NODE_ENTRY);
400 std::string key = it->first;
401 voidP value = it->second->value;
402 ECF::type T = it->second->T;
403 std::string desc = it->second->description;
404
405 std::stringstream ss;
406
407 switch (T) {
408 case ECF::INT:
409 ss << *((int *)value.get());
410 break;
411 case ECF::UINT:
412 ss << *((uint *)value.get());
413 break;
414 case ECF::STRING:
415 ss << *((std::string *)value.get());
416 break;
417 case ECF::FLOAT:
418 ss << *((float *)value.get());
419 break;
420 case ECF::DOUBLE:
421 ss << *((double *)value.get());
422 break;
423 case ECF::CHAR:
424 ss << *((char *)value.get());
425 break;
426 default:
427 break;
428 }
429 xParam.addAttribute("key", key.c_str());
430 xParam.addText(ss.str().c_str());
431 xParam.addAttribute("desc", desc.c_str());
432 xRegistry.addChild(xParam);
433
434 //if(desc != "")
435 // xRegistry.addClear(desc.c_str(), "<!-- ", " -->");
436 }
437}
438
bool isRegistered(std::string)
Definition Registry.cpp:39
voidP getEntry(std::string, uint index=0)
Definition Registry.cpp:71
void write(XMLNode &)
Definition Registry.cpp:345
ECF::type getType(std::string)
Definition Registry.cpp:91
bool modifyEntry(std::string, voidP, uint index=0)
Definition Registry.cpp:50
void dumpEntries(XMLNode &)
Definition Registry.cpp:393
bool readEntries(const XMLNode &node, std::string prefix="", uint index=0)
Definition Registry.cpp:129
bool registerEntry(std::string, voidP, enum ECF::type, std::string description="", uint index=0)
Definition Registry.cpp:15
bool isModified(std::string, uint index=0)
Definition Registry.cpp:106
type
Data types used for configuration file parameters.
Definition Registry.h:17
ECF parameter structure, as stored in the Registry.
Definition Registry.h:25