Config.cpp 726 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include "../include/Config.h"
  2. namespace Config {
  3. std::map<std::string, std::string> storage;
  4. };
  5. bool Config::init(const std::string &filename) {
  6. std::ifstream ifile(filename);
  7. std::string line;
  8. if (ifile.is_open()) {
  9. while (getline(ifile, line)) {
  10. std::stringstream ss(line);
  11. std::string segment;
  12. std::vector<std::string> v;
  13. while (getline(ss, segment, '=')) {
  14. v.push_back(segment);
  15. }
  16. if (v.size() != 2) {
  17. return false;
  18. }
  19. storage.insert(std::pair<std::string, std::string>(v.at(0), v.at(1)));
  20. }
  21. return true;
  22. }
  23. return false;
  24. }
  25. std::string Config::getValue(const std::string &key) {
  26. auto it = storage.find(key);
  27. if (it != storage.end()) {
  28. return it->second;
  29. }
  30. return "";
  31. }