configuration.cpp
Go to the documentation of this file.
1 
19 #include <cstdio>
20 #include <iostream>
21 #include <string>
22 #include <fstream>
23 
24 using namespace std;
25 
26 #include "log.h"
27 #include "configuration.h"
28 
30 {
31  setRootNode("Data");
32 }
33 
34 CConfiguration::CConfiguration(const char* rootNode)
35 {
36  setRootNode(rootNode);
37 }
38 
40 {
41  return doc;
42 }
43 
45 {
46 }
47 
49 {
50  doc.Clear();
51 }
52 
53 void CConfiguration::setRootNode(const char* node)
54 {
55  if (node != NULL)
56  {
57  sprintf(rootNode, "%s", node);
58  }
59  else
60  {
61  sprintf(rootNode, "%s", "");
62  }
63 }
64 
65 bool CConfiguration::load(const char* cfgFile)
66 {
67  bool okay = doc.LoadFile(cfgFile);
68 
69  return okay;
70 }
71 
72 bool CConfiguration::parse(const char* str)
73 {
74  doc.Parse(str);
75 
76  return true;
77 }
78 
79 
80 unsigned long CConfiguration::getUnsignedLong(const char* str, unsigned long def)
81 {
82  const char* value = getString(str, "");
83  if (strcmp(value, "") == 0)
84  return def;
85  else return strtoul(value, NULL, 10);
86 }
87 
88 unsigned long CConfiguration::getAttributeUnsignedLong(TiXmlElement* node, const char* str, unsigned long def)
89 {
90  const char* value = getAttributeString(node,str, "");
91  if (strcmp(value, "") == 0)
92  return def;
93  else return strtoul(value, NULL, 10);
94 }
95 
96 void CConfiguration::save(const char* cfgFile)
97 {
98  doc.SaveFile(cfgFile);
99 }
100 
101 TiXmlElement* CConfiguration::findNode(const char* name)
102 {
103  TiXmlElement* iter = doc.FirstChildElement( rootNode );
104 
105  return findNode(name, iter);
106 }
107 
108 TiXmlElement* CConfiguration::findNode(const char* name, TiXmlElement* start)
109 {
110  TiXmlElement* iter = start;
111 
112  if (name == NULL || strcasecmp(name, "") == 0)
113  return start;
114 
115  char *pch;
116  char sBuffer[1024];
117  strcpy(sBuffer, name);
118 
119  pch = strtok(sBuffer, ".");
120 
121  while (iter && (pch != NULL))
122  {
123  iter = iter->FirstChildElement(pch);
124  pch = strtok(NULL, ".");
125  }
126 
127  if ((pch == NULL) && (iter))
128  return iter;
129  else
130  return NULL;
131 }
132 
133 void tokenize(const std::string& str,
134  std::vector<std::string>& tokens,
135  const std::string& delimiters)
136 {
137  // Skip delimiters at beginning.
138  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
139  // Find first "non-delimiter".
140  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
141 
142  while (std::string::npos != pos || std::string::npos != lastPos)
143  {
144  // Found a token, add it to the vector.
145  tokens.push_back(str.substr(lastPos, pos - lastPos));
146  // Skip delimiters. Note the "not_of"
147  lastPos = str.find_first_not_of(delimiters, pos);
148  // Find next "non-delimiter"
149  pos = str.find_first_of(delimiters, lastPos);
150  }
151 }
152 
153 void CConfiguration::findNodes(const char* name, std::vector<TiXmlElement*> &result)
154 {
155  TiXmlElement* iter = doc.FirstChildElement( rootNode );
156 
157  findNodes(name, result, iter, 0);
158 }
159 
160 void CConfiguration::findNodes(const char* name, std::vector<TiXmlElement*> &result, TiXmlElement* start, unsigned int level)
161 {
162  std::string str = name;
163  std::vector<std::string> tokens;
164  tokenize(str, tokens, ".");
165 
166  if (level + 1 > tokens.size())
167  return;
168 
169  if (start == NULL)
170  return;
171 
172  TiXmlElement* iter = start->FirstChildElement();
173  while (iter != NULL)
174  {
175  if (strcasecmp(iter->Value(), tokens[level].c_str()) == 0)
176  {
177  if (level + 1 == tokens.size())
178  result.push_back(iter);
179  else
180  findNodes(name, result, iter, level + 1);
181  }
182  iter = iter->NextSiblingElement();
183  }
184 }
185 
186 void CConfiguration::findNodes(const char* name, std::vector<TiXmlElement*> &result, TiXmlElement* start)
187 {
188  findNodes(name, result, start, 0);
189  return;
190 
191  /*
192  TiXmlElement* iter = start;
193 
194  std::string str = name;
195  std::vector<std::string> tokens;
196  tokenize(str, tokens, ".");
197 
198  int index = 0;
199  while (iter != NULL && index < tokens.size())
200  {
201  iter = iter->FirstChildElement(tokens[index].c_str());
202  index++;
203  }
204 
205  if (iter == NULL)
206  return;
207 
208  while (iter != NULL)
209  {
210  result.push_back(iter);
211  iter = iter->NextSiblingElement(tokens[tokens.size()-1].c_str());
212  }
213  return;
214  */
215 }
216 
217 
218 
219 float CConfiguration::getAttributeFloat(TiXmlElement* node,const char* str, float def)
220 {
221  const char* value = getAttributeString(node,str, "");
222  if (strcmp(value, "") == 0)
223  return def;
224  else return atof(value);
225 }
226 
227 double CConfiguration::getAttributeDouble(TiXmlElement* node, const char* str, double def)
228 {
229 
230  const char* value = getAttributeString(node,str, "");
231  if (strcmp(value, "") == 0)
232  return def;
233  else return atof(value);
234 }
235 
236 
237 const char* CConfiguration::getAttributeString(TiXmlElement* node, const char* str, const char* def)
238 {
239  const char* tmpstr = node->Attribute(str);
240  if (tmpstr == NULL)
241  return def;
242  else return tmpstr;
243 }
244 
245 
246 int CConfiguration::getAttributeInteger(TiXmlElement* node, const char* str, int def)
247 {
248  const char* value = getAttributeString(node,str, "");
249  if (strcmp(value, "") == 0)
250  return def;
251  else return atoi(value);
252 }
253 
254 
255 bool CConfiguration::getAttributeBoolean(TiXmlElement* node, const char* str, bool def)
256 {
257  const char* value = getAttributeString(node,str,"");
258  if (strcmp(value, "") == 0)
259  return def;
260  else if (strcasecmp(value, "true") == 0)
261  return true;
262  else if (strcasecmp(value, "false") == 0)
263  return false;
264  else return def;
265 }
266 
267 
268 
269 
270 
271 
272 
273 void CConfiguration::setAttributeFloat(TiXmlElement* node, const char* str, float value)
274 {
275  char buffer[255];
276  sprintf(buffer, "%f", value);
277  node->SetAttribute(str, buffer);
278 }
279 
280 
281 void CConfiguration::setAttributeDouble(TiXmlElement* node, const char* str, double value)
282 {
283  char buffer[255];
284  sprintf(buffer, "%f", value);
285  node->SetAttribute(str, buffer);
286 }
287 
288 
289 void CConfiguration::setAttributeString(TiXmlElement* node, const char* str, const char* value)
290 {
291  node->SetAttribute(str, value);
292 }
293 
294 
295 void CConfiguration::setAttributeInteger(TiXmlElement* node, const char* str, int value)
296 {
297  char buffer[255];
298  sprintf(buffer, "%d", value);
299  node->SetAttribute(str, buffer);
300 }
301 
302 
303 void CConfiguration::setAttributeBoolean(TiXmlElement* node, const char* str, bool value)
304 {
305  if (value)
306  node->SetAttribute(str, "true");
307  else
308  node->SetAttribute(str, "false");
309 }
310 
311 
312 
313 void CConfiguration::setFloat(const char* str, float value)
314 {
315  char buffer[255];
316  sprintf(buffer, "%f", value);
317  setString(str, buffer);
318 }
319 
320 void CConfiguration::setDouble(const char* str, double value)
321 {
322  char buffer[255];
323  sprintf(buffer, "%f", value);
324  setString(str, buffer);
325 }
326 
327 void CConfiguration::setString(const char* str, const char* value)
328 {
329  TiXmlElement* result = findNode(str);
330 
331  if (result != NULL)
332  {
333  TiXmlText* text = result->FirstChild()->ToText();
334  text->SetValue(value);
335  }
336 }
337 
338 void CConfiguration::setInteger(const char* str, int value)
339 {
340  char buffer[255];
341  sprintf(buffer, "%d", value);
342  setString(str, buffer);
343 }
344 
345 void CConfiguration::setBoolean(const char* str, bool value)
346 {
347  if (value)
348  setString(str, "true");
349  else
350  setString(str, "false");
351 }
352 
353 std::string CConfiguration::getText(TiXmlElement* node)
354 {
355  if (node != NULL)
356  return node->GetText();
357 
358  return "";
359 }
360 
361 const char* CConfiguration::getString(const char* str, const char* def)
362 {
363  TiXmlElement* result = findNode(str);
364 
365  if (result != NULL && result->GetText() != NULL)
366  return (const char*)result->GetText();
367  else
368  return def;
369 }
370 
371 float CConfiguration::getFloat(const char* str, float def)
372 {
373  return (float) getDouble(str, (double)def);
374 }
375 
376 double CConfiguration::getDouble(const char* str, double def)
377 {
378  const char* value = getString(str, "");
379  if (strcmp(value, "") == 0)
380  return def;
381  else return atof(value);
382 }
383 
384 int CConfiguration::getInteger(const char* str, int def)
385 {
386  const char* value = getString(str, "");
387  if (strcmp(value, "") == 0)
388  return def;
389  else return atoi(value);
390 }
391 
392 bool CConfiguration::getBoolean(const char* str, bool def)
393 {
394  const char* value = getString(str, "");
395  if (strcmp(value, "") == 0)
396  return def;
397  else if (strcasecmp(value, "true") == 0)
398  return true;
399  else if (strcasecmp(value, "false") == 0)
400  return false;
401  else return def;
402 }
403 
404 
405 const char* CConfiguration::getString(const char* str, TiXmlElement* start, const char* def)
406 {
407  TiXmlElement* result = findNode(str, start);
408 
409  if (result != NULL && result->GetText() != NULL)
410  return (const char*)result->GetText();
411  else
412  return def;
413 }
414 
415 float CConfiguration::getFloat(const char* str, TiXmlElement* start, float def)
416 {
417  return (float) getDouble(str, start, (double)def);
418 }
419 
420 double CConfiguration::getDouble(const char* str, TiXmlElement* start, double def)
421 {
422  const char* value = getString(str, start, "");
423  if (strcmp(value, "") == 0)
424  return def;
425  else return atof(value);
426 }
427 
428 int CConfiguration::getInteger(const char* str, TiXmlElement* start, int def)
429 {
430  const char* value = getString(str, start,"");
431  if (strcmp(value, "") == 0)
432  return def;
433  else return atoi(value);
434 }
435 
436 bool CConfiguration::getBoolean(const char* str, TiXmlElement* start, bool def)
437 {
438  const char* value = getString(str, start,"");
439  if (strcmp(value, "") == 0)
440  return def;
441  else if (strcasecmp(value, "true") == 0)
442  return true;
443  else if (strcasecmp(value, "false") == 0)
444  return false;
445  else return def;
446 }
447 
448 TiXmlElement* CConfiguration::getNode(const char* name)
449 {
450  return new TiXmlElement( name );
451 }
452 
453 bool CConfiguration::addNode(TiXmlElement* element)
454 {
455  if (element == NULL)
456  return false;
457 
458  doc.LinkEndChild( element );
459  return true;
460 }
461 
462 bool CConfiguration::addNode(TiXmlElement * element, TiXmlElement* parent)
463 {
464  if (element == NULL)
465  return false;
466 
467  if (parent == NULL)
468  return addNode(element);
469 
470  parent->LinkEndChild( element );
471  return element;
472 }
473 
474 bool CConfiguration::insertNode(TiXmlElement* node, TiXmlElement* afterThis)
475 {
476  if (node == NULL)
477  return false;
478 
479  if (afterThis == NULL)
480  return false;
481 
482  doc.InsertAfterChild((TiXmlNode*)afterThis, *(TiXmlNode*)node);
483  return true;
484 }
485 
486 bool CConfiguration::insertNode(TiXmlElement* node, TiXmlElement* afterThis, TiXmlElement* parent)
487 {
488  if (node == NULL)
489  return false;
490 
491  if (afterThis == NULL)
492  return false;
493 
494  if (parent == NULL)
495  return insertNode(node, afterThis);
496 
497  parent->InsertAfterChild(afterThis, *node);
498  return true;
499 }
500 
501 bool CConfiguration::removeNode(TiXmlElement* element, TiXmlElement* parent)
502 {
503  if (element == NULL)
504  return false;
505 
506  if (parent == NULL)
507  return removeNode(element);
508 
509  return parent->RemoveChild(element);
510 }
511 
512 bool CConfiguration::removeNode(TiXmlElement* element)
513 {
514  if (element == NULL)
515  return false;
516 
517  return doc.RemoveChild(element);
518 }
519 
520 bool CConfiguration::removeAttribute(TiXmlElement* element, const char* name)
521 {
522  if (element == NULL)
523  return false;
524 
525  element->RemoveAttribute(name);
526  return true;
527 }
528 
TiXmlElement * getNode(const char *name)
void setString(const char *str, const char *value)
void setInteger(const char *str, int value)
static void setAttributeDouble(TiXmlElement *node, const char *str, double value)
static const char * getAttributeString(TiXmlElement *node, const char *str, const char *def="")
bool insertNode(TiXmlElement *node, TiXmlElement *afterThis)
void setFloat(const char *str, float value)
bool removeAttribute(TiXmlElement *element, const char *name)
unsigned long getUnsignedLong(const char *str, unsigned long def=0)
static void setAttributeBoolean(TiXmlElement *node, const char *str, bool value)
bool addNode(TiXmlElement *node)
float getFloat(const char *str, float def=0.0)
bool removeNode(TiXmlElement *element)
static float getAttributeFloat(TiXmlElement *node, const char *str, float def=0.0)
static unsigned long getAttributeUnsignedLong(TiXmlElement *node, const char *str, unsigned long def=0)
TiXmlElement * findNode(const char *name)
static void setAttributeString(TiXmlElement *node, const char *str, const char *value)
bool load(const char *cfgFile)
static std::string getText(TiXmlElement *node)
void setDouble(const char *str, double value)
static void setAttributeInteger(TiXmlElement *node, const char *str, int value)
bool getBoolean(const char *str, bool def=false)
bool parse(const char *str)
static void setAttributeFloat(TiXmlElement *node, const char *str, float value)
void tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters)
virtual ~CConfiguration()
void save(const char *cfgFile)
int getInteger(const char *str, int def=0)
double getDouble(const char *str, double def=0.0)
TiXmlDocument & getDocument()
void findNodes(const char *name, std::vector< TiXmlElement * > &result, TiXmlElement *start, unsigned int level)
void setRootNode(const char *node)
static bool getAttributeBoolean(TiXmlElement *node, const char *str, bool def=false)
static int getAttributeInteger(TiXmlElement *node, const char *str, int def=0)
const char * getString(const char *str, const char *def="")
static double getAttributeDouble(TiXmlElement *node, const char *str, double def=0.0)
void setBoolean(const char *str, bool value)


asr_kinematic_chain_optimizer
Author(s): Aumann Florian, Heller Florian, Jäkel Rainer, Wittenbeck Valerij
autogenerated on Mon Jun 10 2019 12:35:36