/* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: git://neueland.iserlohn-fortress.net/smolbote.git * * SPDX-License-Identifier: GPL-3.0 */ #include "configuration.h" #include #include using namespace libconfig; Configuration::Configuration() { m_userCfg = new Config(); #ifdef C_LIKE_CONFIG m_userCfg->setOptions(Config::OptionSemicolonSeparators | Config::OptionOpenBraceOnSeparateLine); #endif // fsync after writing and before closing m_userCfg->setOption(Config::OptionFsync, true); // make sure the cfgPath is empty m_userCfgPath = std::string(); m_defaultCfg = new Config(); } Configuration::~Configuration() { delete m_userCfg; delete m_defaultCfg; } bool Configuration::readUserConfiguration(const std::string &path) { m_userCfgPath = path; try { m_userCfg->readFile(path.c_str()); } catch (FileIOException) { return false; } catch (ParseException) { return false; } return true; } bool Configuration::parse(const std::string &contents) { try { m_userCfg->readString(contents); } catch (ParseException) { return false; } return true; } bool Configuration::writeUserConfiguration(const std::string &path) { m_userCfgPath = path; try { m_userCfg->writeFile(m_userCfgPath.c_str()); } catch (FileIOException) { return false; } return true; } bool Configuration::writeIfNeeded() { try { m_userCfg->writeFile(m_userCfgPath.c_str()); } catch (FileIOException) { return false; } return true; } bool Configuration::parseDefaultConfiguration(const std::string &contents) { try { m_defaultCfg->readString(contents); } catch (ParseException) { return false; } return true; } std::vector Configuration::childrenSettings(const char* name) { std::vector groupNames; const Setting &root = m_userCfg->lookup(name); for(const Setting &setting : root) { if(setting.getType() != Setting::TypeGroup) { groupNames.push_back(setting.getName()); } } return groupNames; } std::vector Configuration::childrenGroups(const char* name) { std::vector groupNames; const Setting &root = m_userCfg->lookup(name); for(const Setting &setting : root) { if(setting.getType() == Setting::TypeGroup) { groupNames.push_back(setting.getName()); } } return groupNames; } void Configuration::resetValue(const char *path) { if(m_userCfg->exists(path)) { Setting &v = m_userCfg->lookup(path); switch (v.getType()) { case Setting::TypeNone: break; case Setting::TypeInt: v = static_cast(m_defaultCfg->lookup(path)); break; case Setting::TypeInt64: v = static_cast(m_defaultCfg->lookup(path)); break; case Setting::TypeFloat: v = static_cast(m_defaultCfg->lookup(path)); break; case Setting::TypeString: v = static_cast(m_defaultCfg->lookup(path)); break; case Setting::TypeBoolean: v = static_cast(m_defaultCfg->lookup(path)); break; case Setting::TypeGroup: break; case Setting::TypeArray: break; case Setting::TypeList: break; } } } template std::optional Configuration::value(const char* path) const { // if setting doesn't exist, give back a nullopt if(!m_userCfg->exists(path)) { return std::nullopt; } // setting was found const Setting &setting = m_userCfg->lookup(path); std::optional r; // cast depending on type // type checks are done during compile time switch (setting.getType()) { case Setting::TypeNone: r = std::nullopt; break; case Setting::TypeInt: // int, unsigned int, long, unsigned long if constexpr(std::is_same_v) { r = std::optional(std::to_string(static_cast(setting))); } else if constexpr(std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v) { r = std::optional(static_cast(setting)); } else { r = std::nullopt; } break; case Setting::TypeInt64: // int, unsigned int; long long, unsigned long long if constexpr(std::is_same_v) { r = std::optional(std::to_string(static_cast(setting))); } else if constexpr(std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v) { r = std::optional(static_cast(setting)); } else { r = std::nullopt; } break; case Setting::TypeFloat: // float, double if constexpr(std::is_same_v) { r = std::optional(std::to_string(static_cast(setting))); } else if constexpr(std::is_same_v || std::is_same_v) { r = std::optional(static_cast(setting)); } else { r = std::nullopt; } break; case Setting::TypeString: // const char*, std::string if constexpr(std::is_same::value) { r = std::optional(static_cast(setting)); } else { r = std::nullopt; } break; case Setting::TypeBoolean: // bool if constexpr(std::is_same::value) { r = std::optional(static_cast(setting) ? "true" : "false"); } else if constexpr(std::is_same::value) { r = std::optional(static_cast(setting)); } else { r = std::nullopt; } break; case Setting::TypeGroup: r = std::nullopt; break; case Setting::TypeArray: r = std::nullopt; break; case Setting::TypeList: r = std::nullopt; break; } return r; } // tell the compiler to export these functions, otherwise you get linking errors template std::optional Configuration::value(const char* path) const; template std::optional Configuration::value(const char* path) const; template std::optional Configuration::value(const char* path) const; template std::optional Configuration::value(const char* path) const; template std::optional Configuration::value(const char* path) const; template std::optional Configuration::value(const char* path) const; template std::optional Configuration::value(const char* path) const; template std::optional Configuration::value(const char* path) const; template std::optional Configuration::value(const char* path) const; template std::optional Configuration::value(const char* path) const; template bool Configuration::setValue(std::string path, const T &val) { if(m_userCfg->exists(path)) { Setting &setting = m_userCfg->lookup(path); // compiler complained about operator= not taking unsinged ints, longs and long longs if constexpr(std::is_unsigned_v && !std::is_same_v) { setting = static_cast>(val); } else if constexpr(std::is_same_v) { switch (setting.getType()) { case Setting::TypeNone: break; case Setting::TypeInt: case Setting::TypeInt64: setting = std::stoi(static_cast(val)); break; case Setting::TypeFloat: setting = std::stod(static_cast(val)); break; case Setting::TypeString: setting = static_cast(val).c_str(); break; case Setting::TypeBoolean: if(static_cast(val) == "true") { setting = true; } else if (static_cast(val) == "false") { setting = false; } break; case Setting::TypeGroup: break; case Setting::TypeArray: break; case Setting::TypeList: break; } } else { setting = val; } changed = true; return true; } // the entry doesn't exist return false; } template bool Configuration::setValue(std::string path, const int &val); template bool Configuration::setValue(std::string path, const unsigned int &val); template bool Configuration::setValue(std::string path, const long &val); template bool Configuration::setValue(std::string path, const unsigned long &val); template bool Configuration::setValue(std::string path, const long long &val); template bool Configuration::setValue(std::string path, const unsigned long long &val); template bool Configuration::setValue(std::string path, const float &val); template bool Configuration::setValue(std::string path, const double &val); template bool Configuration::setValue(std::string path, const bool &val); template bool Configuration::setValue(std::string path, const std::string &val); std::string &patchHome(std::string &path, const std::string &home) { const size_t location = path.find('~'); if(location != std::string::npos) { return path.replace(location, 1, home); } return path; }