PolyConfig.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * PolyConfig.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 4/15/09.
  6. * Copyright 2009 Ivan Safrin. 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. Logger::log("Loading config: %s\n", fileName.c_str());
  18. if(!doc.LoadFile()) {
  19. Logger::log("Error loading config file...\n");
  20. Logger::log("Error: %s\n", doc.ErrorDesc());
  21. return;
  22. }
  23. TiXmlElement *rootElement = doc.RootElement();
  24. TiXmlNode *pChild;
  25. ConfigEntry *entry;
  26. for(pChild = rootElement->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
  27. entry = getEntry(configNamespace, pChild->Value());
  28. entry->stringVal = pChild->ToElement()->GetText();
  29. entry->numVal = atof(pChild->ToElement()->GetText());
  30. entry->isString = true;
  31. entry->configNamespace = configNamespace;
  32. }
  33. }
  34. void Config::saveConfig(String configNamespace, String fileName) {
  35. TiXmlDocument doc;
  36. TiXmlElement* node;
  37. TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
  38. doc.LinkEndChild( decl );
  39. TiXmlElement * root = new TiXmlElement( "PolyConfig" );
  40. doc.LinkEndChild( root );
  41. for(int i=0; i < entries.size(); i++) {
  42. if(entries[i]->configNamespace == configNamespace) {
  43. node = new TiXmlElement(entries[i]->key.c_str());
  44. if(entries[i]->isString)
  45. node->LinkEndChild( new TiXmlText(entries[i]->stringVal.c_str()));
  46. else
  47. node->LinkEndChild( new TiXmlText(String::NumberToString(entries[i]->numVal).c_str()));
  48. root->LinkEndChild( node);
  49. }
  50. }
  51. doc.SaveFile(fileName.c_str());
  52. }
  53. ConfigEntry *Config::getEntry(String configNamespace, String key) {
  54. for(int i=0; i < entries.size(); i++) {
  55. ConfigEntry *entry = entries[i];
  56. if(entry->key == key && entry->configNamespace == configNamespace) {
  57. return entry;
  58. }
  59. }
  60. ConfigEntry *newEntry = new ConfigEntry();
  61. newEntry->key = key;
  62. newEntry->isString = false;
  63. newEntry->numVal = 0;
  64. newEntry->configNamespace = configNamespace;
  65. entries.push_back(newEntry);
  66. return newEntry;
  67. }
  68. void Config::setStringValue(String configNamespace, String key, String value) {
  69. getEntry(configNamespace, key)->stringVal = value;
  70. getEntry(configNamespace, key)->isString = true;
  71. }
  72. void Config::setNumericValue(String configNamespace, String key, Number value) {
  73. getEntry(configNamespace, key)->numVal = value;
  74. getEntry(configNamespace, key)->isString = false;
  75. }
  76. Number Config::getNumericValue(String configNamespace, String key) {
  77. return getEntry(configNamespace, key)->numVal;
  78. }
  79. String Config::getStringValue(String configNamespace, String key) {
  80. return getEntry(configNamespace, key)->stringVal;
  81. }