4773n0x

Config.cpp

Jun 8th, 2017
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include "Config.h"
  2. #include <exception>
  3.  
  4. Config::Config(std::string file_path)
  5. {
  6.     this->file_path = file_path;
  7. }
  8.  
  9. void Config::SetupValue(std::string name, std::string& value)
  10. {
  11.     strings.emplace_back(std::make_unique<Value<std::string>>(name, &value));
  12. }
  13.  
  14. void Config::SetupValue(std::string name, int& value)
  15. {
  16.     ints.emplace_back(std::make_unique<Value<int>>(name, &value));
  17. }
  18.  
  19. void Config::SetupValue(std::string name, bool& value)
  20. {
  21.     bools.emplace_back(std::make_unique<Value<bool>>(name, &value));
  22. }
  23.  
  24. void Config::SetupValue(std::string name, float& value)
  25. {
  26.     floats.emplace_back(std::make_unique<Value<float>>(name, &value));
  27. }
  28.  
  29. void Config::SetupValue(std::string name, double& value)
  30. {
  31.     doubles.emplace_back(std::make_unique<Value<double>>(name, &value));
  32. }
  33.  
  34. void Config::Save()
  35. {
  36.     std::ofstream file(file_path);
  37.  
  38.     for (const auto& i : strings)
  39.         file << i->name << delimiter << *i->value << std::endl;
  40.  
  41.     for (const auto& i : ints)
  42.         file << i->name << delimiter << *i->value << std::endl;
  43.  
  44.     for (const auto& i : bools)
  45.         file << i->name << delimiter << (*i->value ? "true" : "false") << std::endl;
  46.  
  47.     for (const auto& i : floats)
  48.         file << i->name << delimiter << *i->value << std::endl;
  49.  
  50.     for (const auto& i : doubles)
  51.         file << i->name << delimiter << *i->value << std::endl;
  52.  
  53.     file.close();
  54. }
  55.  
  56. void Config::Load()
  57. {
  58.     std::ifstream file(file_path);
  59.     const auto pairs = FileToPairs(file);
  60.  
  61.     if (!pairs.empty())
  62.     {
  63.         int lines_parsed = 0;
  64.  
  65.         for (const auto& i : strings)
  66.         {
  67.             i->name = pairs[lines_parsed].first;
  68.             *i->value = pairs[lines_parsed].second;
  69.  
  70.             lines_parsed++;
  71.         }
  72.  
  73.         for (const auto& i : ints)
  74.         {
  75.             i->name = pairs[lines_parsed].first;
  76.             *i->value = std::stoi(pairs[lines_parsed].second);
  77.  
  78.             lines_parsed++;
  79.         }
  80.  
  81.         for (const auto& i : bools)
  82.         {
  83.             i->name = pairs[lines_parsed].first;
  84.             const std::string value = pairs[lines_parsed].second;
  85.  
  86.             if (value == "true")
  87.                 *i->value = true;
  88.             else if (value == "false")
  89.                 *i->value = false;
  90.             else
  91.                 throw std::runtime_error("Config boolean value must be true or false");
  92.  
  93.             lines_parsed++;
  94.         }
  95.  
  96.         for (const auto& i : floats)
  97.         {
  98.             i->name = pairs[lines_parsed].first;
  99.             *i->value = std::stof(pairs[lines_parsed].second);
  100.  
  101.             lines_parsed++;
  102.         }
  103.  
  104.         for (const auto& i : doubles)
  105.         {
  106.             i->name = pairs[lines_parsed].first;
  107.             *i->value = std::stod(pairs[lines_parsed].second);
  108.  
  109.             lines_parsed++;
  110.         }
  111.     }
  112.  
  113.     file.close();
  114. }
  115.  
  116. std::vector<std::pair<std::string, std::string>> Config::FileToPairs(std::ifstream& file)
  117. {
  118.     std::vector<std::pair<std::string, std::string>> pairs;
  119.  
  120.     for (std::string i; std::getline(file, i);)
  121.     {
  122.         const size_t delimiter_pos = i.find(delimiter);
  123.  
  124.         if (delimiter_pos != std::string::npos)
  125.         {
  126.             const std::string name = i.substr(0, delimiter_pos);
  127.             const std::string value = i.substr(delimiter_pos + delimiter.length());
  128.  
  129.             pairs.emplace_back(name, value);
  130.         }
  131.     }
  132.  
  133.     return pairs;
  134. }
Add Comment
Please, Sign In to add comment