Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Config.h"
- #include <Windows.h>
- struct ConfigKey
- {
- ConfigKey() {}
- ConfigKey(std::string Name, std::shared_ptr<ConfigValue> Value)
- {
- this->Name = Name;
- this->Value = std::move(Value);
- }
- std::string Name;
- std::shared_ptr<ConfigValue> Value;
- };
- struct ConfigSection
- {
- ConfigSection() {}
- ConfigSection(std::string Name)
- {
- this->Name = Name;
- }
- std::string Name;
- std::vector<std::unique_ptr<ConfigKey>> Keys;
- };
- void CConfig::SetValue(std::string SectionName, std::string KeyName, std::unique_ptr<ConfigValue> Value)
- {
- this->Sections.emplace_back(std::make_unique<ConfigSection>());
- bool FoundSection = false;
- bool FoundKey = false;
- for (const auto& Section : this->Sections)
- {
- if (Section->Name == SectionName)
- {
- FoundSection = true;
- for (const auto& Key : Section->Keys)
- {
- if (Key->Name == KeyName)
- {
- FoundKey = true;
- Key->Value = std::move(Value);
- }
- }
- if (!FoundKey)
- {
- auto Key = std::make_unique<ConfigKey>(KeyName, std::move(Value));
- Section->Keys.emplace_back(std::move(Key));
- }
- }
- }
- if (!FoundSection)
- {
- auto Section = std::make_unique<ConfigSection>(SectionName);
- auto Key = std::make_unique<ConfigKey>(KeyName, std::move(Value));
- Section->Keys.emplace_back(std::move(Key));
- this->Sections.emplace_back(std::move(Section));
- }
- }
- std::shared_ptr<ConfigValue> CConfig::GetValue(const std::string& SectionName, const std::string& KeyName)
- {
- bool FoundSection = false;
- bool FoundKey = false;
- for (const auto& Section : this->Sections)
- {
- if (Section->Name == SectionName)
- {
- FoundSection = true;
- for (const auto& Key : Section->Keys)
- {
- if (Key->Name == KeyName)
- {
- FoundKey = true;
- return Key->Value;
- }
- }
- if (!FoundKey)
- std::runtime_error("Config key " + KeyName + " not found.");
- }
- }
- if (!FoundSection)
- std::runtime_error("Config section " + SectionName + " not found.");
- return std::make_shared<ConfigValue>();
- }
- bool CConfig::Save(std::string PathToINI)
- {
- for (const auto& Section : this->Sections)
- {
- for (const auto& Key : Section->Keys)
- {
- switch (Key->Value->Type)
- {
- case CONFIG_INT:
- if (!WritePrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), std::to_string(Key->Value->Int).c_str(), PathToINI.c_str()))
- return false;
- break;
- case CONFIG_FLOAT:
- if (!WritePrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), std::to_string(Key->Value->Float).c_str(), PathToINI.c_str()))
- return false;
- break;
- case CONFIG_BOOL:
- if (!WritePrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), Key->Value->Bool ? "true" : "false", PathToINI.c_str()))
- return false;
- break;
- case CONFIG_STRING:
- if (!WritePrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), Key->Value->String.c_str(), PathToINI.c_str()))
- return false;
- break;
- default:
- break;
- }
- }
- }
- return true;
- }
- void CConfig::Load(std::string PathToINI)
- {
- const int BufferSize = 64;
- char Buffer[BufferSize];
- for (const auto& Section : this->Sections)
- {
- for (const auto& Key : Section->Keys)
- {
- switch (Key->Value->Type)
- {
- case CONFIG_INT:
- GetPrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), NULL, Buffer, BufferSize, PathToINI.c_str());
- Key->Value->Int = std::atoi(Buffer);
- break;
- case CONFIG_FLOAT:
- GetPrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), NULL, Buffer, BufferSize, PathToINI.c_str());
- Key->Value->Float = static_cast<float>(std::atof(Buffer));
- break;
- case CONFIG_BOOL:
- GetPrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), NULL, Buffer, BufferSize, PathToINI.c_str());
- Key->Value->Bool = std::string(Buffer).compare("true") == 0;
- break;
- case CONFIG_STRING:
- GetPrivateProfileStringA(Section->Name.c_str(), Key->Name.c_str(), NULL, Buffer, BufferSize, PathToINI.c_str());
- Key->Value->String = std::string(Buffer);
- break;
- default:
- break;
- }
- }
- }
- }
- std::string CConfig::DumpValues()
- {
- std::string Dump = "[CConfig::DumpValues]\n";
- for (const auto& Section : this->Sections)
- {
- for (const auto& Key : Section->Keys)
- {
- std::string PrintVal;
- switch (Key->Value->Type)
- {
- case CONFIG_INT:
- PrintVal = std::to_string(Key->Value->Int);
- break;
- case CONFIG_FLOAT:
- PrintVal = std::to_string(Key->Value->Float);
- break;
- case CONFIG_BOOL:
- PrintVal = Key->Value->Bool ? "true" : "false";
- break;
- case CONFIG_STRING:
- PrintVal = Key->Value->String;
- break;
- default:
- break;
- }
- Dump.append(Section->Name + ", " + Key->Name + " = " + PrintVal + "\n");
- }
- }
- return Dump.append("\n");
- }
- std::unique_ptr<CConfig> Config = std::make_unique<CConfig>();
Add Comment
Please, Sign In to add comment