Improved config loading to read default options from one file and overrides from another and merge differences properly
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#define ConfigFile_h__
|
#define ConfigFile_h__
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
#include <boost/property_tree/ptree.hpp>
|
#include <boost/property_tree/ptree.hpp>
|
||||||
#include <boost/property_tree/ini_parser.hpp>
|
#include <boost/property_tree/ini_parser.hpp>
|
||||||
|
|
||||||
@@ -26,20 +27,23 @@ public:
|
|||||||
void SaveToDisk();
|
void SaveToDisk();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_Path;
|
boost::filesystem::path m_Path;
|
||||||
boost::property_tree::ptree m_Ptree;
|
boost::property_tree::ptree m_PTreeDefaults;
|
||||||
|
boost::property_tree::ptree m_PTreeOverrides;
|
||||||
|
boost::property_tree::ptree m_PTreeMerged;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T ConfigFile::GetValue(std::string key, T defaultValue)
|
T ConfigFile::GetValue(std::string key, T defaultValue)
|
||||||
{
|
{
|
||||||
return m_Ptree.get<T>(key, defaultValue);
|
return m_PTreeMerged.get<T>(key, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void ConfigFile::SetValue(std::string key, T value)
|
void ConfigFile::SetValue(std::string key, T value)
|
||||||
{
|
{
|
||||||
m_Ptree.put<T>(key, value);
|
m_PTreeOverrides.put<T>(key, value);
|
||||||
|
m_PTreeMerged.put<T>(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,14 +4,36 @@
|
|||||||
dd::ConfigFile::ConfigFile(std::string path)
|
dd::ConfigFile::ConfigFile(std::string path)
|
||||||
{
|
{
|
||||||
m_Path = path;
|
m_Path = path;
|
||||||
|
|
||||||
|
boost::filesystem::path defaultFile;
|
||||||
|
defaultFile = m_Path.parent_path() / ("Default" + m_Path.filename().string());
|
||||||
|
|
||||||
|
// Read defaults
|
||||||
|
if (boost::filesystem::exists(defaultFile)) {
|
||||||
try {
|
try {
|
||||||
boost::property_tree::ini_parser::read_ini(path, m_Ptree);
|
boost::property_tree::ini_parser::read_ini(defaultFile.string(), m_PTreeDefaults);
|
||||||
} catch (boost::property_tree::ptree_error& e) {
|
} catch (boost::property_tree::ptree_error& e) {
|
||||||
LOG_ERROR("Failed to load %s", path.c_str());
|
LOG_ERROR("Failed to parse \"%s\":\n%s", defaultFile.string().c_str(), e.what());
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
LOG_ERROR("Failed to find \"%s\"! Relying on hardcoded default values!", defaultFile.string().c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
m_PTreeMerged = m_PTreeDefaults;
|
||||||
|
|
||||||
|
// Read overrides
|
||||||
|
if (boost::filesystem::exists(m_Path)) {
|
||||||
|
try {
|
||||||
|
boost::property_tree::ini_parser::read_ini(m_Path.string(), m_PTreeOverrides);
|
||||||
|
for (auto& node : m_PTreeOverrides) {
|
||||||
|
m_PTreeMerged.put_child(node.first, node.second);
|
||||||
|
}
|
||||||
|
} catch (boost::property_tree::ptree_error& e) {
|
||||||
|
LOG_ERROR("Failed to parse \"%s\":\n%s", m_Path.filename().string().c_str(), e.what());
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
|
||||||
void dd::ConfigFile::SaveToDisk()
|
void dd::ConfigFile::SaveToDisk()
|
||||||
{
|
{
|
||||||
boost::property_tree::ini_parser::write_ini(m_Path, m_Ptree);
|
boost::property_tree::ini_parser::write_ini(m_Path.string(), m_PTreeOverrides);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user