No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

configmanager.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "configmanager.h"
  24. #include "resourcemanager.h"
  25. #include <yaml-cpp/yaml.h>
  26. ConfigManager g_config;
  27. ConfigManager::ConfigManager()
  28. {
  29. }
  30. ConfigManager::~ConfigManager()
  31. {
  32. }
  33. bool ConfigManager::load(const std::string& fileName)
  34. {
  35. m_fileName = fileName;
  36. if(!g_resources.fileExists(fileName))
  37. return false;
  38. std::string fileContents = g_resources.loadTextFile(fileName);
  39. if(fileContents.size() == 0)
  40. return false;
  41. std::istringstream fin(fileContents);
  42. try {
  43. YAML::Parser parser(fin);
  44. YAML::Node doc;
  45. parser.GetNextDocument(doc);
  46. for(YAML::Iterator it = doc.begin(); it != doc.end(); ++it) {
  47. std::string key, value;
  48. it.first() >> key;
  49. it.second() >> value;
  50. m_confsMap[key] = value;
  51. }
  52. } catch (YAML::ParserException& e) {
  53. error("Malformed configuration file!");
  54. return false;
  55. }
  56. return true;
  57. }
  58. void ConfigManager::save()
  59. {
  60. if(!m_fileName.empty()) {
  61. YAML::Emitter out;
  62. out << m_confsMap;
  63. g_resources.saveFile(m_fileName, (const unsigned char*)out.c_str(), out.size());
  64. }
  65. }
  66. void ConfigManager::setValue(const std::string &key, const std::string &value)
  67. {
  68. m_confsMap[key] = value;
  69. }
  70. void ConfigManager::setValue(const std::string &key, const char *value)
  71. {
  72. m_confsMap[key] = value;
  73. }
  74. void ConfigManager::setValue(const std::string &key, int value)
  75. {
  76. setValue(key, boost::lexical_cast<std::string>(value));
  77. }
  78. void ConfigManager::setValue(const std::string &key, float value)
  79. {
  80. setValue(key, boost::lexical_cast<std::string>(value));
  81. }
  82. void ConfigManager::setValue(const std::string &key, bool value)
  83. {
  84. if(value)
  85. setValue(key,"true");
  86. else
  87. setValue(key,"false");
  88. }
  89. const std::string &ConfigManager::getString(const std::string &key)
  90. {
  91. std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
  92. if(iter == m_confsMap.end()) {
  93. warning("Config value %s not found", key.c_str());
  94. static std::string emptystr;
  95. return emptystr;
  96. }
  97. return iter->second;
  98. }
  99. float ConfigManager::getFloat(const std::string &key)
  100. {
  101. std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
  102. if(iter == m_confsMap.end()) {
  103. warning("Config value %s not found", key.c_str());
  104. return 0;
  105. }
  106. return boost::lexical_cast<float>(iter->second);
  107. }
  108. bool ConfigManager::getBoolean(const std::string &key)
  109. {
  110. std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
  111. if(iter == m_confsMap.end()) {
  112. warning("Config value %s not found", key.c_str());
  113. return 0;
  114. }
  115. return (iter->second == "true");
  116. }
  117. int ConfigManager::getInteger(const std::string &key)
  118. {
  119. std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
  120. if(iter == m_confsMap.end()) {
  121. warning("Config value %s not found", key.c_str());
  122. return 0;
  123. }
  124. return boost::lexical_cast<int>(iter->second);
  125. }