KindConfig.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <iostream>
  2. #include "Exception.h"
  3. #include "KindConfig.h"
  4. using namespace std;
  5. KindConfig::KindConfig(const std::string& fn)
  6. {
  7. // settings.clear();
  8. addFile(fn);
  9. }
  10. KindConfig::KindConfig(std::istream& is)
  11. {
  12. // settings.clear();
  13. addFile(is);
  14. }
  15. void KindConfig::addFile(std::istream& is)
  16. {
  17. string line;
  18. string lastkey;
  19. while (getline(is, line))
  20. {
  21. line = trim(line);
  22. if (!line.empty() && line[0] != '#')
  23. {
  24. string key, value;
  25. string del;
  26. split(line, key, del, value);
  27. if (key.empty())
  28. key = lastkey;
  29. if (key.empty())
  30. throw Exception("read config", "empty key");
  31. if (del == "=")
  32. settings[key].clear();
  33. settings[key].push_back(value);
  34. lastkey = key;
  35. }
  36. }
  37. }
  38. void KindConfig::addFile(const std::string& fn)
  39. {
  40. ifstream is(fn);
  41. if (!is.good())
  42. throw Exception("read config", string("Cannot open ") + fn + " for reading");
  43. addFile(is);
  44. }
  45. bool KindConfig::hasKey(std::string key) const
  46. {
  47. key = trim(key);
  48. auto it = settings.find(key);
  49. return it != settings.end();
  50. }
  51. std::string KindConfig::getString(std::string key) const
  52. {
  53. key = trim(key);
  54. auto it = settings.find(key);
  55. if (it == settings.end())
  56. throw Exception("get config key", std::string("No key \"") + key + "\"");
  57. if (it->second.size() != 1)
  58. throw Exception("get config key", std::string("Key \"") + key + "\" is no single value");
  59. return it->second[0];
  60. }
  61. Strings KindConfig::getStrings(std::string key) const
  62. {
  63. key = trim(key);
  64. auto it = settings.find(key);
  65. if (it == settings.end())
  66. throw Exception("get config key", std::string("No key \"") + key + "\"");
  67. return it->second;
  68. }
  69. bool KindConfig::getBool(std::string key) const
  70. {
  71. key = trim(key);
  72. auto it = settings.find(key);
  73. if (it == settings.end())
  74. return false;
  75. if (it->second.size() != 1)
  76. throw Exception("get config key", std::string("Key \"") + key + "\" is no single value");
  77. std::string val = it->second[0];
  78. if (val.empty() || val == "true")
  79. return true;
  80. if (val != "false")
  81. throw Exception("get boolean key " + key, std::string("Invalid value \"") + val + "\"");
  82. return false;
  83. }
  84. void KindConfig::print() const
  85. {
  86. for (auto it = settings.begin(); it != settings.end(); ++it)
  87. {
  88. cout << it->first << ": ";
  89. if (it->second.size() > 1)
  90. for (string v : it->second)
  91. cout << v << "|";
  92. else if (!it->second.empty())
  93. cout << it->second[0];
  94. cout << endl;
  95. }
  96. }
  97. void KindConfig::split(const string& line,
  98. string& key,
  99. string& del,
  100. string& value)
  101. {
  102. unsigned int i = 0;
  103. key.clear();
  104. del.clear();
  105. value.clear();
  106. while (i < line.size() && line[i] != '+' && line[i] != '=')
  107. {
  108. key += line[i];
  109. ++i;
  110. }
  111. key = trim(key);
  112. while (i < line.size() && (line[i] == '+' || line[i] == '='))
  113. {
  114. del += line[i];
  115. ++i;
  116. }
  117. if (del != "=" && del != "+=")
  118. throw Exception("config", string("wrong delimiter ") + del + " in " + line);
  119. while (i < line.size())
  120. {
  121. value += line[i];
  122. ++i;
  123. }
  124. value = trim(value);
  125. }