PolyConfig.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyConfig.h"
  20. #include "tinyxml.h"
  21. using namespace Polycode;
  22. Config::Config() {
  23. }
  24. Config::~Config() {
  25. }
  26. void Config::loadConfig(const String& configNamespace, const String& fileName) {
  27. TiXmlDocument doc(fileName.c_str());
  28. Logger::log("Loading config: %s\n", fileName.c_str());
  29. if(!doc.LoadFile()) {
  30. Logger::log("Error loading config file...\n");
  31. Logger::log("Error: %s\n", doc.ErrorDesc());
  32. return;
  33. }
  34. TiXmlElement *rootElement = doc.RootElement();
  35. TiXmlNode *pChild;
  36. ConfigEntry *entry;
  37. for(pChild = rootElement->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
  38. TiXmlElement *pChildElement = pChild->ToElement();
  39. if (!pChildElement) continue; // Skip comment nodes
  40. entry = getEntry(configNamespace, pChild->Value());
  41. entry->stringVal = pChildElement->GetText();
  42. entry->numVal = atof(pChildElement->GetText());
  43. entry->isString = true;
  44. entry->configNamespace = configNamespace;
  45. }
  46. }
  47. void Config::saveConfig(const String& configNamespace, const String& fileName) {
  48. TiXmlDocument doc;
  49. TiXmlElement* node;
  50. TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
  51. doc.LinkEndChild( decl );
  52. TiXmlElement * root = new TiXmlElement( "PolyConfig" );
  53. doc.LinkEndChild( root );
  54. for(int i=0; i < entries.size(); i++) {
  55. if(entries[i]->configNamespace == configNamespace) {
  56. node = new TiXmlElement(entries[i]->key.c_str());
  57. if(entries[i]->isString)
  58. node->LinkEndChild( new TiXmlText(entries[i]->stringVal.c_str()));
  59. else
  60. node->LinkEndChild( new TiXmlText(String::NumberToString(entries[i]->numVal).c_str()));
  61. root->LinkEndChild( node);
  62. }
  63. }
  64. doc.SaveFile(fileName.c_str());
  65. }
  66. ConfigEntry *Config::getEntry(const String& configNamespace, const String& key) {
  67. for(int i=0; i < entries.size(); i++) {
  68. ConfigEntry *entry = entries[i];
  69. if(entry->key == key && entry->configNamespace == configNamespace) {
  70. return entry;
  71. }
  72. }
  73. ConfigEntry *newEntry = new ConfigEntry();
  74. newEntry->key = key;
  75. newEntry->isString = false;
  76. newEntry->numVal = 0;
  77. newEntry->configNamespace = configNamespace;
  78. entries.push_back(newEntry);
  79. return newEntry;
  80. }
  81. void Config::setStringValue(const String& configNamespace, const String& key, const String& value) {
  82. getEntry(configNamespace, key)->stringVal = value;
  83. getEntry(configNamespace, key)->isString = true;
  84. }
  85. void Config::setNumericValue(const String& configNamespace, const String& key, Number value) {
  86. getEntry(configNamespace, key)->numVal = value;
  87. getEntry(configNamespace, key)->isString = false;
  88. }
  89. Number Config::getNumericValue(const String& configNamespace, const String& key) {
  90. return getEntry(configNamespace, key)->numVal;
  91. }
  92. const String& Config::getStringValue(const String& configNamespace, const String& key) {
  93. return getEntry(configNamespace, key)->stringVal;
  94. }
  95. void Config::setBoolValue(const String& configNamespace, const String& key, bool value) {
  96. getEntry(configNamespace, key)->stringVal = (!value ? "false" : "true");
  97. getEntry(configNamespace, key)->isString = true;
  98. }
  99. bool Config::getBoolValue(const String& configNamespace, const String& key) {
  100. const String& str = getEntry(configNamespace, key)->stringVal;
  101. if (str == "true" || str == "1") {
  102. return true;
  103. }
  104. return false;
  105. }