From c034e4ca79131928958617a886be681f5ac3ee3e Mon Sep 17 00:00:00 2001 From: NotAdam Date: Sun, 10 Jun 2018 16:28:03 +0000 Subject: [PATCH] new config class for ini files --- src/common/Config/ConfigMgr.cpp | 66 +++++++++++++++++++++++++++++++++ src/common/Config/ConfigMgr.h | 45 ++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/common/Config/ConfigMgr.cpp create mode 100644 src/common/Config/ConfigMgr.h diff --git a/src/common/Config/ConfigMgr.cpp b/src/common/Config/ConfigMgr.cpp new file mode 100644 index 00000000..1740ca54 --- /dev/null +++ b/src/common/Config/ConfigMgr.cpp @@ -0,0 +1,66 @@ +#include "ConfigMgr.h" + +#include +#include + +/** + * Loads an ini file and parses it + * @param configName the name of ini file relative to m_configFolderRoot to load alongside global.ini + * @return true if loading was successful + */ +bool Core::ConfigMgr::loadConfig( const std::string& configName ) +{ + std::stringstream configStream; + + // get global config + auto configDir = boost::filesystem::path( m_configFolderRoot ); + + if( !boost::filesystem::exists( configDir ) ) + { + return false; + } + + auto globalConfig = boost::filesystem::path( configDir / m_globalConfigFile ); + if( !boost::filesystem::exists( globalConfig ) ) + { + if( !copyDefaultConfig( globalConfig.filename().string() ) ) + return false; + } + + std::ifstream globalConfigFile( globalConfig.c_str() ); + configStream << globalConfigFile.rdbuf(); + + // add some newlines just in case there's no newline at the end of the global file + configStream << "\n\n"; + + // get local config + auto localConfig = boost::filesystem::path( configDir / configName ); + if( !boost::filesystem::exists( localConfig ) ) + { + if( !copyDefaultConfig( localConfig.filename().string() ) ) + return false; + } + std::ifstream configFile( localConfig.c_str() ); + configStream << configFile.rdbuf(); + + // parse the tree and we're fuckin done + boost::property_tree::read_ini( configStream, m_propTree ); + + return true; +} + +bool Core::ConfigMgr::copyDefaultConfig( const std::string& configName ) +{ + boost::filesystem::path configPath( m_configFolderRoot ); + configPath /= configName; + + if( !boost::filesystem::exists( configPath.c_str() + m_configDefaultSuffix ) ) + { + // no default file :( + return false; + } + + boost::filesystem::copy_file( configPath.c_str() + m_configDefaultSuffix, configPath ); + + return true; +} \ No newline at end of file diff --git a/src/common/Config/ConfigMgr.h b/src/common/Config/ConfigMgr.h new file mode 100644 index 00000000..5a9fab9c --- /dev/null +++ b/src/common/Config/ConfigMgr.h @@ -0,0 +1,45 @@ +#ifndef SAPPHIRE_CONFIGMGR_H +#define SAPPHIRE_CONFIGMGR_H + +#include + +namespace Core +{ + class ConfigMgr + { + public: + ConfigMgr() = default; + ~ConfigMgr() = default; + + bool loadConfig( const std::string& configName ); + + template< class T > + T getValue( const std::string& name, T defaultValue = T() ) + { + try + { + return m_propTree.get< T >( name ); + } + catch( ... ) + { + return defaultValue; + } + } + + template< class T > + void setValue( const std::string& name, T defaultValue = T() ) + { + m_propTree.put( name, defaultValue ); + } + + private: + bool copyDefaultConfig( const std::string& configName ); + + boost::property_tree::ptree m_propTree; + const std::string m_globalConfigFile = "global.ini"; + const std::string m_configFolderRoot = "./config/"; + const std::string m_configDefaultSuffix = ".default"; + }; +} + +#endif //SAPPHIRE_CONFIGMGR_H