configuration.cpp
Go to the documentation of this file.
00001 
00019 #include <cstdio>
00020 #include <iostream>
00021 #include <string>
00022 #include <fstream>
00023 
00024 using namespace std;
00025 
00026 #include "log.h"
00027 #include "configuration.h"
00028 
00029 CConfiguration::CConfiguration()
00030 {
00031   setRootNode("Data");
00032 }
00033 
00034 CConfiguration::CConfiguration(const char* rootNode)
00035 {
00036   setRootNode(rootNode);
00037 }
00038 
00039 TiXmlDocument &CConfiguration::getDocument()
00040 {
00041   return doc;
00042 }
00043 
00044 CConfiguration::~CConfiguration()
00045 {
00046 }
00047 
00048 void CConfiguration::clear()
00049 {
00050     doc.Clear();
00051 }
00052 
00053 void CConfiguration::setRootNode(const char* node)
00054 {
00055      if (node != NULL)
00056      {
00057         sprintf(rootNode, "%s", node);
00058      }
00059      else
00060      {
00061         sprintf(rootNode, "%s", "");
00062      }
00063 }
00064 
00065 bool CConfiguration::load(const char* cfgFile)
00066 {
00067      bool okay = doc.LoadFile(cfgFile);
00068 
00069      return okay;
00070 }
00071 
00072 bool CConfiguration::parse(const char* str)
00073 {
00074     doc.Parse(str);
00075     
00076     return true;
00077 }
00078 
00079 
00080 unsigned long CConfiguration::getUnsignedLong(const char* str, unsigned long def)
00081 {
00082     const char* value = getString(str, "");
00083     if (strcmp(value, "") == 0)
00084         return def;
00085     else return strtoul(value, NULL, 10);
00086 }
00087 
00088 unsigned long CConfiguration::getAttributeUnsignedLong(TiXmlElement* node, const char* str, unsigned long def)
00089 {
00090     const char* value = getAttributeString(node,str, "");
00091     if (strcmp(value, "") == 0)
00092         return def;
00093     else return strtoul(value, NULL, 10);
00094 }
00095 
00096 void CConfiguration::save(const char* cfgFile)
00097 {
00098   doc.SaveFile(cfgFile);
00099 }
00100 
00101 TiXmlElement* CConfiguration::findNode(const char* name)
00102 {
00103     TiXmlElement* iter = doc.FirstChildElement( rootNode );
00104     
00105     return findNode(name, iter);
00106 }
00107 
00108 TiXmlElement* CConfiguration::findNode(const char* name, TiXmlElement* start)
00109 {
00110     TiXmlElement* iter = start;
00111     
00112     if (name == NULL || strcasecmp(name, "") == 0)
00113       return start;
00114 
00115     char *pch;
00116     char sBuffer[1024];
00117     strcpy(sBuffer, name);
00118     
00119     pch = strtok(sBuffer, ".");
00120     
00121     while (iter && (pch != NULL))
00122     {
00123         iter = iter->FirstChildElement(pch);
00124         pch = strtok(NULL, ".");
00125     }
00126 
00127     if ((pch == NULL) && (iter))
00128         return iter;
00129     else
00130         return NULL;
00131 }
00132 
00133 void tokenize(const std::string& str,
00134                       std::vector<std::string>& tokens,
00135                       const std::string& delimiters)
00136 {
00137     // Skip delimiters at beginning.
00138     std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00139     // Find first "non-delimiter".
00140     std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
00141 
00142     while (std::string::npos != pos || std::string::npos != lastPos)
00143     {
00144         // Found a token, add it to the vector.
00145         tokens.push_back(str.substr(lastPos, pos - lastPos));
00146         // Skip delimiters.  Note the "not_of"
00147         lastPos = str.find_first_not_of(delimiters, pos);
00148         // Find next "non-delimiter"
00149         pos = str.find_first_of(delimiters, lastPos);
00150     }
00151 }
00152 
00153 void CConfiguration::findNodes(const char* name, std::vector<TiXmlElement*> &result)
00154 {
00155     TiXmlElement* iter = doc.FirstChildElement( rootNode );
00156     
00157     findNodes(name, result, iter, 0);
00158 }
00159 
00160 void CConfiguration::findNodes(const char* name, std::vector<TiXmlElement*> &result, TiXmlElement* start, unsigned int level)
00161 {
00162   std::string str = name;
00163   std::vector<std::string> tokens;
00164   tokenize(str, tokens, ".");
00165 
00166   if (level + 1 > tokens.size())
00167     return;
00168 
00169   if (start == NULL)
00170     return;
00171 
00172   TiXmlElement* iter = start->FirstChildElement();
00173   while (iter != NULL)
00174     {
00175       if (strcasecmp(iter->Value(), tokens[level].c_str()) == 0)
00176     {
00177       if (level + 1 == tokens.size())
00178         result.push_back(iter);
00179       else
00180         findNodes(name, result, iter, level + 1);
00181     }
00182       iter = iter->NextSiblingElement();
00183     }
00184 }
00185 
00186 void CConfiguration::findNodes(const char* name, std::vector<TiXmlElement*> &result, TiXmlElement* start)
00187 {
00188   findNodes(name, result, start, 0);
00189   return;
00190 
00191   /*
00192   TiXmlElement* iter = start;
00193 
00194   std::string str = name;
00195   std::vector<std::string> tokens;
00196   tokenize(str, tokens, ".");
00197   
00198   int index = 0;
00199   while (iter != NULL && index < tokens.size())
00200     {
00201       iter = iter->FirstChildElement(tokens[index].c_str());
00202       index++;
00203     }
00204 
00205   if (iter == NULL)
00206     return;
00207 
00208   while (iter != NULL)
00209     {
00210       result.push_back(iter);
00211       iter = iter->NextSiblingElement(tokens[tokens.size()-1].c_str());
00212     }
00213   return;
00214   */
00215 }
00216 
00217 
00218 
00219 float  CConfiguration::getAttributeFloat(TiXmlElement* node,const  char* str, float def)
00220 {
00221     const char* value = getAttributeString(node,str, "");
00222     if (strcmp(value, "") == 0)
00223         return def;
00224     else return atof(value);
00225 }
00226 
00227 double CConfiguration::getAttributeDouble(TiXmlElement* node, const char* str, double def)
00228 {
00229     
00230     const char* value = getAttributeString(node,str, "");
00231     if (strcmp(value, "") == 0)
00232         return def;
00233     else return atof(value);
00234 }
00235 
00236 
00237 const char* CConfiguration::getAttributeString(TiXmlElement* node, const char* str, const char* def)
00238 {
00239     const char* tmpstr = node->Attribute(str);
00240     if (tmpstr == NULL)
00241        return def;
00242     else return tmpstr;
00243 }
00244 
00245 
00246 int    CConfiguration::getAttributeInteger(TiXmlElement* node, const char* str, int def)
00247 {
00248     const char* value = getAttributeString(node,str, "");
00249     if (strcmp(value, "") == 0)
00250         return def;
00251     else return atoi(value);
00252 }
00253 
00254 
00255 bool   CConfiguration::getAttributeBoolean(TiXmlElement* node, const char* str, bool def)
00256 {
00257      const char* value = getAttributeString(node,str,"");
00258      if (strcmp(value, "") == 0)
00259         return def;
00260      else if (strcasecmp(value, "true") == 0)
00261         return true;
00262      else if (strcasecmp(value, "false") == 0)
00263         return false;
00264      else return def;
00265 }
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00273 void   CConfiguration::setAttributeFloat(TiXmlElement* node, const char* str, float value)
00274 {
00275      char buffer[255];
00276      sprintf(buffer, "%f", value);
00277      node->SetAttribute(str, buffer);
00278 }
00279 
00280 
00281 void   CConfiguration::setAttributeDouble(TiXmlElement* node, const char* str, double value)
00282 {
00283      char buffer[255];
00284      sprintf(buffer, "%f", value);
00285      node->SetAttribute(str, buffer);
00286 }
00287 
00288 
00289 void   CConfiguration::setAttributeString(TiXmlElement* node, const char* str, const char* value)
00290 {       
00291      node->SetAttribute(str, value);
00292 }
00293 
00294 
00295 void   CConfiguration::setAttributeInteger(TiXmlElement* node, const char* str, int value)
00296 {
00297      char buffer[255];
00298      sprintf(buffer, "%d", value);
00299      node->SetAttribute(str, buffer);
00300 }
00301 
00302 
00303 void   CConfiguration::setAttributeBoolean(TiXmlElement* node, const char* str, bool value)
00304 {
00305      if (value)
00306      node->SetAttribute(str, "true");
00307      else
00308      node->SetAttribute(str, "false");
00309 }
00310 
00311 
00312 
00313 void CConfiguration::setFloat(const char* str, float value)
00314 {
00315      char buffer[255];
00316      sprintf(buffer, "%f", value);
00317      setString(str, buffer);
00318 }
00319 
00320 void CConfiguration::setDouble(const char* str, double value)
00321 {
00322      char buffer[255];
00323      sprintf(buffer, "%f", value);
00324      setString(str, buffer);
00325 }
00326 
00327 void CConfiguration::setString(const char* str, const char* value)
00328 {
00329     TiXmlElement* result = findNode(str);
00330     
00331     if (result != NULL)
00332     {
00333        TiXmlText* text = result->FirstChild()->ToText();
00334        text->SetValue(value);
00335     }
00336 }
00337 
00338 void CConfiguration::setInteger(const char* str, int value)
00339 {
00340      char buffer[255];
00341      sprintf(buffer, "%d", value);
00342      setString(str, buffer);
00343 }
00344 
00345 void CConfiguration::setBoolean(const char* str, bool value)
00346 {
00347      if (value)
00348         setString(str, "true");
00349      else
00350         setString(str, "false");
00351 }
00352 
00353 std::string CConfiguration::getText(TiXmlElement* node)
00354 {
00355   if (node != NULL)
00356     return node->GetText();
00357 
00358   return "";
00359 }
00360 
00361 const char* CConfiguration::getString(const char* str, const char* def)
00362 {
00363     TiXmlElement* result = findNode(str);
00364     
00365     if (result != NULL && result->GetText() != NULL)
00366        return (const char*)result->GetText();
00367     else
00368        return def;
00369 }
00370 
00371 float CConfiguration::getFloat(const char* str, float def)
00372 {
00373     return (float) getDouble(str, (double)def);
00374 }
00375 
00376 double CConfiguration::getDouble(const char* str, double def)
00377 {
00378     const char* value = getString(str, "");
00379     if (strcmp(value, "") == 0)
00380         return def;
00381     else return atof(value);
00382 }
00383 
00384 int CConfiguration::getInteger(const char* str, int def)
00385 {
00386     const char* value = getString(str, "");
00387     if (strcmp(value, "") == 0)
00388         return def;
00389     else return atoi(value);
00390 }
00391 
00392 bool CConfiguration::getBoolean(const char* str, bool def)
00393 {
00394      const char* value = getString(str, "");
00395      if (strcmp(value, "") == 0)
00396         return def;
00397      else if (strcasecmp(value, "true") == 0)
00398         return true;
00399      else if (strcasecmp(value, "false") == 0)
00400         return false;
00401      else return def;
00402 }
00403 
00404 
00405 const char* CConfiguration::getString(const char* str, TiXmlElement* start, const char* def)
00406 {
00407   TiXmlElement* result = findNode(str, start);
00408     
00409     if (result != NULL && result->GetText() != NULL)
00410        return (const char*)result->GetText();
00411     else
00412        return def;
00413 }
00414 
00415 float CConfiguration::getFloat(const char* str, TiXmlElement* start, float def)
00416 {
00417   return (float) getDouble(str, start, (double)def);
00418 }
00419 
00420 double CConfiguration::getDouble(const char* str, TiXmlElement* start, double def)
00421 {
00422   const char* value = getString(str, start, "");
00423     if (strcmp(value, "") == 0)
00424         return def;
00425     else return atof(value);
00426 }
00427 
00428 int CConfiguration::getInteger(const char* str, TiXmlElement* start, int def)
00429 {
00430   const char* value = getString(str, start,"");
00431     if (strcmp(value, "") == 0)
00432         return def;
00433     else return atoi(value);
00434 }
00435 
00436 bool CConfiguration::getBoolean(const char* str, TiXmlElement* start, bool def)
00437 {
00438   const char* value = getString(str, start,"");
00439      if (strcmp(value, "") == 0)
00440         return def;
00441      else if (strcasecmp(value, "true") == 0)
00442         return true;
00443      else if (strcasecmp(value, "false") == 0)
00444         return false;
00445      else return def;
00446 }
00447 
00448 TiXmlElement* CConfiguration::getNode(const char* name)
00449 {
00450   return new TiXmlElement( name );
00451 }
00452 
00453 bool CConfiguration::addNode(TiXmlElement* element)
00454 {
00455   if (element == NULL)
00456     return false;
00457 
00458   doc.LinkEndChild( element );
00459   return true;
00460 }
00461 
00462 bool CConfiguration::addNode(TiXmlElement * element, TiXmlElement* parent)
00463 { 
00464   if (element == NULL)
00465     return false;
00466 
00467   if (parent == NULL)
00468     return addNode(element);
00469 
00470   parent->LinkEndChild( element );
00471   return element;
00472 }
00473 
00474 bool CConfiguration::insertNode(TiXmlElement* node, TiXmlElement* afterThis)
00475 {
00476   if (node == NULL)
00477     return false;
00478 
00479   if (afterThis == NULL)
00480     return false;
00481 
00482   doc.InsertAfterChild((TiXmlNode*)afterThis, *(TiXmlNode*)node);
00483   return true;
00484 }
00485 
00486 bool CConfiguration::insertNode(TiXmlElement* node, TiXmlElement* afterThis, TiXmlElement* parent)
00487 {
00488   if (node == NULL)
00489     return false;
00490 
00491   if (afterThis == NULL)
00492     return false;
00493 
00494   if (parent == NULL)
00495     return insertNode(node, afterThis);
00496 
00497   parent->InsertAfterChild(afterThis, *node);
00498   return true;
00499 }
00500 
00501 bool CConfiguration::removeNode(TiXmlElement* element, TiXmlElement* parent)
00502 {
00503   if (element == NULL)
00504     return false;
00505 
00506   if (parent == NULL)
00507     return removeNode(element);
00508 
00509   return parent->RemoveChild(element);
00510 }
00511 
00512 bool CConfiguration::removeNode(TiXmlElement* element)
00513 {
00514   if (element == NULL)
00515     return false;
00516 
00517   return doc.RemoveChild(element);
00518 }
00519 
00520 bool CConfiguration::removeAttribute(TiXmlElement* element, const char* name)
00521 {
00522   if (element == NULL)
00523     return false;
00524 
00525   element->RemoveAttribute(name);
00526   return true;
00527 }
00528 


asr_kinematic_chain_optimizer
Author(s): Aumann Florian, Heller Florian, Jäkel Rainer, Wittenbeck Valerij
autogenerated on Sat Jun 8 2019 19:42:49