Guest User

Config.cpp - 4773n0x

a guest
Nov 22nd, 2016
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. #include "Config.h"
  2. #include <Windows.h>
  3.  
  4. struct ConfigKey
  5. {
  6.     ConfigKey() {}
  7.  
  8.     ConfigKey(std::string Name, std::shared_ptr<ConfigValue> Value)
  9.     {
  10.         this->Name = Name;
  11.         this->Value = std::move(Value);
  12.     }
  13.  
  14.     std::string Name;
  15.     std::shared_ptr<ConfigValue> Value;
  16. };
  17.  
  18. struct ConfigSection
  19. {
  20.     ConfigSection() {}
  21.  
  22.     ConfigSection(std::string Name)
  23.     {
  24.         this->Name = Name;
  25.     }
  26.  
  27.     std::string Name;
  28.     std::vector<std::unique_ptr<ConfigKey>> Keys;
  29. };
  30.  
  31. void CConfig::SetValue(std::string SectionName, std::string KeyName, std::unique_ptr<ConfigValue> Value)
  32. {
  33.     this->Sections.emplace_back(std::make_unique<ConfigSection>());
  34.  
  35.     bool FoundSection = false;
  36.     bool FoundKey = false;
  37.  
  38.     for (const auto& Section : this->Sections)
  39.     {
  40.         if (Section->Name == SectionName)
  41.         {
  42.             FoundSection = true;
  43.  
  44.             for (const auto& Key : Section->Keys)
  45.             {
  46.                 if (Key->Name == KeyName)
  47.                 {
  48.                     FoundKey = true;
  49.                     Key->Value = std::move(Value);
  50.                 }
  51.             }
  52.  
  53.             if (!FoundKey)
  54.             {
  55.                 auto Key = std::make_unique<ConfigKey>(KeyName, std::move(Value));
  56.                 Section->Keys.emplace_back(std::move(Key));
  57.             }
  58.         }
  59.     }
  60.  
  61.     if (!FoundSection)
  62.     {
  63.         auto Section = std::make_unique<ConfigSection>(SectionName);
  64.         auto Key = std::make_unique<ConfigKey>(KeyName, std::move(Value));
  65.  
  66.         Section->Keys.emplace_back(std::move(Key));
  67.         this->Sections.emplace_back(std::move(Section));
  68.     }
  69. }
  70.  
  71. std::shared_ptr<ConfigValue> CConfig::GetValue(const std::string& SectionName, const std::string& KeyName)
  72. {
  73.     bool FoundSection = false;
  74.     bool FoundKey = false;
  75.  
  76.     for (const auto& Section : this->Sections)
  77.     {
  78.         if (Section->Name == SectionName)
  79.         {
  80.             FoundSection = true;
  81.  
  82.             for (const auto& Key : Section->Keys)
  83.             {
  84.                 if (Key->Name == KeyName)
  85.                 {
  86.                     FoundKey = true;
  87.                     return Key->Value;
  88.                 }
  89.             }
  90.  
  91.             if (!FoundKey)
  92.                 std::runtime_error("Config key " + KeyName + " not found.");
  93.         }
  94.     }
  95.  
  96.     if (!FoundSection)
  97.         std::runtime_error("Config section " + SectionName + " not found.");
  98.  
  99.     return std::make_shared<ConfigValue>();
  100. }
  101.  
  102. bool CConfig::Save(std::string PathToINI)
  103. {
  104.     for (const auto& Section : this->Sections)
  105.     {
  106.         for (const auto& Key : Section->Keys)
  107.         {
  108.             switch (Key->Value->Type)
  109.             {
  110.             case CONFIG_INT:
  111.                 if (!WritePrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), std::to_string(Key->Value->Int).c_str(), PathToINI.c_str()))
  112.                     return false;
  113.                 break;
  114.             case CONFIG_FLOAT:
  115.                 if (!WritePrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), std::to_string(Key->Value->Float).c_str(), PathToINI.c_str()))
  116.                     return false;
  117.                 break;
  118.             case CONFIG_BOOL:
  119.                 if (!WritePrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), Key->Value->Bool ? "true" : "false", PathToINI.c_str()))
  120.                     return false;
  121.                 break;
  122.             case CONFIG_STRING:
  123.                 if (!WritePrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), Key->Value->String.c_str(), PathToINI.c_str()))
  124.                     return false;
  125.                 break;
  126.             default:
  127.                 break;
  128.             }
  129.         }
  130.     }
  131.  
  132.     return true;
  133. }
  134.  
  135. void CConfig::Load(std::string PathToINI)
  136. {
  137.     const int BufferSize = 64;
  138.     char Buffer[BufferSize];
  139.  
  140.     for (const auto& Section : this->Sections)
  141.     {
  142.         for (const auto& Key : Section->Keys)
  143.         {
  144.             switch (Key->Value->Type)
  145.             {
  146.             case CONFIG_INT:
  147.                 GetPrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), NULL, Buffer, BufferSize, PathToINI.c_str());
  148.                 Key->Value->Int = std::atoi(Buffer);
  149.                 break;
  150.             case CONFIG_FLOAT:
  151.                 GetPrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), NULL, Buffer, BufferSize, PathToINI.c_str());
  152.                 Key->Value->Float = static_cast<float>(std::atof(Buffer));
  153.                 break;
  154.             case CONFIG_BOOL:
  155.                 GetPrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), NULL, Buffer, BufferSize, PathToINI.c_str());
  156.                 Key->Value->Bool = std::string(Buffer).compare("true") == 0;
  157.                 break;
  158.             case CONFIG_STRING:
  159.                 GetPrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), NULL, Buffer, BufferSize, PathToINI.c_str());
  160.                 Key->Value->String = std::string(Buffer);
  161.                 break;
  162.             default:
  163.                 break;
  164.             }
  165.         }
  166.     }
  167. }
  168.  
  169. std::string CConfig::DumpValues()
  170. {
  171.     std::string Dump = "[CConfig::DumpValues]\n";
  172.  
  173.     for (const auto& Section : this->Sections)
  174.     {
  175.         for (const auto& Key : Section->Keys)
  176.         {
  177.             std::string PrintVal;
  178.  
  179.             switch (Key->Value->Type)
  180.             {
  181.             case CONFIG_INT:
  182.                 PrintVal = std::to_string(Key->Value->Int);
  183.                 break;
  184.             case CONFIG_FLOAT:
  185.                 PrintVal = std::to_string(Key->Value->Float);
  186.                 break;
  187.             case CONFIG_BOOL:
  188.                 PrintVal = Key->Value->Bool ? "true" : "false";
  189.                 break;
  190.             case CONFIG_STRING:
  191.                 PrintVal = Key->Value->String;
  192.                 break;
  193.             default:
  194.                 break;
  195.             }
  196.  
  197.             Dump.append(Section->Name + ", " + Key->Name + " = " + PrintVal + "\n");
  198.         }
  199.     }
  200.  
  201.     return Dump.append("\n");
  202. }
  203.  
  204. std::unique_ptr<CConfig> Config = std::make_unique<CConfig>();
Add Comment
Please, Sign In to add comment