Add support to reload the config (#868)

This commit is contained in:
David Markowitz
2022-12-04 14:25:58 -08:00
committed by GitHub
parent de3e53de6c
commit e8ba3357e8
11 changed files with 150 additions and 66 deletions

View File

@@ -1,61 +1,53 @@
#include "dConfig.h"
#include <sstream>
#include "BinaryPathFinder.h"
#include "GeneralUtils.h"
dConfig::dConfig(const std::string& filepath) {
m_EmptyString = "";
std::ifstream in(BinaryPathFinder::GetBinaryDir() / filepath);
m_ConfigFilePath = filepath;
LoadConfig();
}
void dConfig::LoadConfig() {
std::ifstream in(BinaryPathFinder::GetBinaryDir() / m_ConfigFilePath);
if (!in.good()) return;
std::string line;
std::string line{};
while (std::getline(in, line)) {
if (line.length() > 0) {
if (line[0] != '#') ProcessLine(line);
}
if (!line.empty() && line.front() != '#') ProcessLine(line);
}
std::ifstream sharedConfig(BinaryPathFinder::GetBinaryDir() / "sharedconfig.ini", std::ios::in);
if (!sharedConfig.good()) return;
line.clear();
while (std::getline(sharedConfig, line)) {
if (line.length() > 0) {
if (line[0] != '#') ProcessLine(line);
}
if (!line.empty() && line.front() != '#') ProcessLine(line);
}
}
dConfig::~dConfig(void) {
void dConfig::ReloadConfig() {
this->m_ConfigValues.clear();
LoadConfig();
}
const std::string& dConfig::GetValue(std::string key) {
for (size_t i = 0; i < m_Keys.size(); ++i) {
if (m_Keys[i] == key) return m_Values[i];
}
return m_EmptyString;
return this->m_ConfigValues[key];
}
void dConfig::ProcessLine(const std::string& line) {
std::stringstream ss(line);
std::string segment;
std::vector<std::string> seglist;
auto splitLine = GeneralUtils::SplitString(line, '=');
while (std::getline(ss, segment, '=')) {
seglist.push_back(segment);
}
if (seglist.size() != 2) return;
if (splitLine.size() != 2) return;
//Make sure that on Linux, we remove special characters:
if (!seglist[1].empty() && seglist[1][seglist[1].size() - 1] == '\r')
seglist[1].erase(seglist[1].size() - 1);
auto& key = splitLine.at(0);
auto& value = splitLine.at(1);
if (!value.empty() && value.at(value.size() - 1) == '\r') value.erase(value.size() - 1);
for (const auto& key : m_Keys) {
if (seglist[0] == key) {
return; // first loaded key is preferred due to loading shared config secondarily
}
}
if (this->m_ConfigValues.find(key) != this->m_ConfigValues.end()) return;
m_Keys.push_back(seglist[0]);
m_Values.push_back(seglist[1]);
this->m_ConfigValues.insert(std::make_pair(key, value));
}

View File

@@ -1,20 +1,34 @@
#pragma once
#include <fstream>
#include <map>
#include <string>
#include <vector>
class dConfig {
public:
dConfig(const std::string& filepath);
~dConfig(void);
/**
* Gets the specified key from the config. Returns an empty string if the value is not found.
*
* @param key Key to find
* @return The keys value in the config
*/
const std::string& GetValue(std::string key);
/**
* Loads the config from a file
*/
void LoadConfig();
/**
* Reloads the config file to reset values
*/
void ReloadConfig();
private:
void ProcessLine(const std::string& line);
private:
std::vector<std::string> m_Keys;
std::vector<std::string> m_Values;
std::string m_EmptyString;
std::map<std::string, std::string> m_ConfigValues;
std::string m_ConfigFilePath;
};