PolyConfig.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * PolyConfig.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 4/15/09.
  6. * Copyright 2009 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolyConfig.h"
  10. using namespace Polycode;
  11. Config::Config() {
  12. }
  13. Config::~Config() {
  14. }
  15. void Config::loadConfig(string configNamespace, string fileName) {
  16. TiXmlDocument doc(fileName.c_str());
  17. doc.LoadFile();
  18. TiXmlElement *rootElement = doc.RootElement();
  19. TiXmlNode *pChild;
  20. ConfigEntry *entry;
  21. for(pChild = rootElement->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
  22. entry = getEntry(configNamespace, pChild->Value());
  23. entry->stringVal = pChild->ToElement()->GetText();
  24. entry->numVal = atof(pChild->ToElement()->GetText());
  25. entry->isString = true;
  26. entry->configNamespace = configNamespace;
  27. }
  28. }
  29. void Config::saveConfig(string configNamespace, string fileName) {
  30. TiXmlDocument doc;
  31. TiXmlElement* node;
  32. TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
  33. doc.LinkEndChild( decl );
  34. TiXmlElement * root = new TiXmlElement( "PolyConfig" );
  35. doc.LinkEndChild( root );
  36. for(int i=0; i < entries.size(); i++) {
  37. if(entries[i]->configNamespace == configNamespace) {
  38. node = new TiXmlElement(entries[i]->key.c_str());
  39. if(entries[i]->isString)
  40. node->LinkEndChild( new TiXmlText(entries[i]->stringVal.c_str()));
  41. else
  42. node->LinkEndChild( new TiXmlText(StringUtil::floatToString(entries[i]->numVal).c_str()));
  43. root->LinkEndChild( node);
  44. }
  45. }
  46. doc.SaveFile(fileName.c_str());
  47. }
  48. ConfigEntry *Config::getEntry(string configNamespace, string key) {
  49. for(int i=0; i < entries.size(); i++) {
  50. if(entries[i]->key == key && entries[i]->configNamespace == configNamespace) {
  51. return entries[i];
  52. }
  53. }
  54. ConfigEntry *newEntry = new ConfigEntry();
  55. newEntry->key = key;
  56. newEntry->isString = false;
  57. newEntry->numVal = 0;
  58. newEntry->configNamespace = configNamespace;
  59. entries.push_back(newEntry);
  60. return newEntry;
  61. }
  62. void Config::setStringValue(string configNamespace, string key, string value) {
  63. getEntry(configNamespace, key)->stringVal = value;
  64. getEntry(configNamespace, key)->isString = true;
  65. }
  66. void Config::setNumericValue(string configNamespace, string key, float value) {
  67. getEntry(configNamespace, key)->numVal = value;
  68. getEntry(configNamespace, key)->isString = false;
  69. }
  70. float Config::getNumericValue(string configNamespace, string key) {
  71. return getEntry(configNamespace, key)->numVal;
  72. }
  73. string Config::getStringValue(string configNamespace, string key) {
  74. return getEntry(configNamespace, key)->stringVal;
  75. }