2021-12-05 17:54:36 +00:00
|
|
|
#include "dConfig.h"
|
2022-12-04 22:25:58 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
#include <sstream>
|
2022-12-04 22:25:58 +00:00
|
|
|
|
2022-11-27 11:59:59 +00:00
|
|
|
#include "BinaryPathFinder.h"
|
2022-12-04 22:25:58 +00:00
|
|
|
#include "GeneralUtils.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
dConfig::dConfig(const std::string& filepath) {
|
2022-12-04 22:25:58 +00:00
|
|
|
m_ConfigFilePath = filepath;
|
|
|
|
LoadConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
void dConfig::LoadConfig() {
|
|
|
|
std::ifstream in(BinaryPathFinder::GetBinaryDir() / m_ConfigFilePath);
|
2021-12-05 17:54:36 +00:00
|
|
|
if (!in.good()) return;
|
|
|
|
|
2022-12-04 22:25:58 +00:00
|
|
|
std::string line{};
|
2021-12-05 17:54:36 +00:00
|
|
|
while (std::getline(in, line)) {
|
2022-12-04 22:25:58 +00:00
|
|
|
if (!line.empty() && line.front() != '#') ProcessLine(line);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-10-29 23:17:35 +00:00
|
|
|
|
2022-11-27 11:59:59 +00:00
|
|
|
std::ifstream sharedConfig(BinaryPathFinder::GetBinaryDir() / "sharedconfig.ini", std::ios::in);
|
2022-10-29 23:17:35 +00:00
|
|
|
if (!sharedConfig.good()) return;
|
|
|
|
|
2022-12-04 22:25:58 +00:00
|
|
|
line.clear();
|
2022-10-29 23:17:35 +00:00
|
|
|
while (std::getline(sharedConfig, line)) {
|
2022-12-04 22:25:58 +00:00
|
|
|
if (!line.empty() && line.front() != '#') ProcessLine(line);
|
2022-10-29 23:17:35 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 22:25:58 +00:00
|
|
|
void dConfig::ReloadConfig() {
|
|
|
|
this->m_ConfigValues.clear();
|
|
|
|
LoadConfig();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& dConfig::GetValue(std::string key) {
|
2022-12-04 22:25:58 +00:00
|
|
|
return this->m_ConfigValues[key];
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dConfig::ProcessLine(const std::string& line) {
|
2022-12-04 22:25:58 +00:00
|
|
|
auto splitLine = GeneralUtils::SplitString(line, '=');
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-04 22:25:58 +00:00
|
|
|
if (splitLine.size() != 2) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//Make sure that on Linux, we remove special characters:
|
2022-12-04 22:25:58 +00:00
|
|
|
auto& key = splitLine.at(0);
|
|
|
|
auto& value = splitLine.at(1);
|
|
|
|
if (!value.empty() && value.at(value.size() - 1) == '\r') value.erase(value.size() - 1);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-04 22:25:58 +00:00
|
|
|
if (this->m_ConfigValues.find(key) != this->m_ConfigValues.end()) return;
|
2022-10-29 23:17:35 +00:00
|
|
|
|
2022-12-04 22:25:58 +00:00
|
|
|
this->m_ConfigValues.insert(std::make_pair(key, value));
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|