ECF 1.5
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
128bool Registry::readEntries(const XMLNode& node, std::string prefix, uint index)
129{
130 int num = node.nChildNode(NODE_ENTRY), iter = 0;
131
132 for (int i = 0; i < num; ++i) {
133 XMLNode child = node.getChildNode(NODE_ENTRY, &iter);
134 if(!child.getAttribute("key")) {
135 std::cerr << "Error: no attribute 'key' in current entry! (node '" << node.getName() << "')" << std::endl;
136 return false;
137 }
138
139 std::string key = (prefix.length() > 0 ? prefix + "." : "") + child.getAttribute("key");
140 if (child.getText() == NULL) {
141 std::cerr << "Error: no text in Entry node! (key: " << key << ")" << std::endl;
142 return false;
143 }
144 std::string value = child.getText();
145
146 if (index > 0)
147 key = uint2str(index) + key;
148
149 if (!isRegistered(key)) {
150 std::cerr << "Warning: key " << key << " not registered." << std::endl;
151 continue;
152 }
153
154 ECF::type t = getType(key);
155 voidP new_value;
156
157 bool ok = true;
158 switch (t) {
159 case ECF::INT:
160 {
161 int *x = new int;
162 *x = str2int(value);
163 new_value = (voidP)x;
164
165 break;
166 }
167 case ECF::UINT:
168 {
169 uint *x = new uint;
170 *x = str2uint(value);
171 new_value = (voidP)x;
172
173 break;
174 }
175 case ECF::STRING:
176 {
177 std::string *s = new std::string(value);
178 new_value = (voidP)s;
179 break;
180 }
181 case ECF::FLOAT:
182 {
183 float *x = new float;
184 *x = str2flt(value);
185 new_value = (voidP)x;
186
187 break;
188 }
189 case ECF::DOUBLE:
190 {
191
192 double *x = new double;
193 *x = str2dbl(value);
194 new_value = (voidP)x;
195
196 break;
197 }
198 case ECF::CHAR:
199 {
200 char *x = new char;
201 *x = value[0];
202 new_value = (voidP)x;
203
204 break;
205 }
206 default:
207 ok = false;
208 break;
209 }
210
211 if (ok)
212 modifyEntry(key, new_value);
213 }
214
215 return true;
216}
217
218
223void Registry::write(XMLNode& xRegistry)
224{
225 xRegistry = XMLNode::createXMLTopNode(NODE_REGISTRY);
226
227 for (map_iter it = parameters_.begin(); it != parameters_.end(); ++it) {
228
229 if(!it->second->bModified)
230 continue;
231
232 XMLNode xParam = XMLNode::createXMLTopNode(NODE_ENTRY);
233 std::string key = it->first;
234 voidP value = it->second->value;
235 ECF::type T = it->second->T;
236 std::stringstream ss;
237
238 switch (T) {
239 case ECF::INT:
240 ss << *((int *)value.get());
241 break;
242 case ECF::UINT:
243 ss << *((uint *)value.get());
244 break;
245 case ECF::STRING:
246 ss << *((std::string *)value.get());
247 break;
248 case ECF::FLOAT:
249 ss << *((float *)value.get());
250 break;
251 case ECF::DOUBLE:
252 ss << *((double *)value.get());
253 break;
254 case ECF::CHAR:
255 ss << *((char *)value.get());
256 break;
257 default:
258 break;
259 }
260 xParam.addAttribute("key", key.c_str());
261 xParam.addText(ss.str().c_str());
262 xRegistry.addChild(xParam);
263 }
264}
265
266
271void Registry::dumpEntries(XMLNode& xRegistry)
272{
273 xRegistry = XMLNode::createXMLTopNode(NODE_REGISTRY);
274
275 for (map_iter it = parameters_.begin(); it != parameters_.end(); ++it) {
276
277 XMLNode xParam = XMLNode::createXMLTopNode(NODE_ENTRY);
278 std::string key = it->first;
279 voidP value = it->second->value;
280 ECF::type T = it->second->T;
281 std::string desc = it->second->description;
282
283 std::stringstream ss;
284
285 switch (T) {
286 case ECF::INT:
287 ss << *((int *)value.get());
288 break;
289 case ECF::UINT:
290 ss << *((uint *)value.get());
291 break;
292 case ECF::STRING:
293 ss << *((std::string *)value.get());
294 break;
295 case ECF::FLOAT:
296 ss << *((float *)value.get());
297 break;
298 case ECF::DOUBLE:
299 ss << *((double *)value.get());
300 break;
301 case ECF::CHAR:
302 ss << *((char *)value.get());
303 break;
304 default:
305 break;
306 }
307 xParam.addAttribute("key", key.c_str());
308 xParam.addText(ss.str().c_str());
309 xParam.addAttribute("desc", desc.c_str());
310 xRegistry.addChild(xParam);
311
312 //if(desc != "")
313 // xRegistry.addClear(desc.c_str(), "<!-- ", " -->");
314 }
315}
316
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:223
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:271
bool readEntries(const XMLNode &node, std::string prefix="", uint index=0)
Definition: Registry.cpp:128
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:16
Definition: nodes.h:92
ECF parameter structure, as stored in the Registry.
Definition: Registry.h:24