From f4025c0ebcddffbb1b826cd666b94d9140a56663 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 25 Jan 2018 19:20:30 +0100 Subject: Configuration class rework - castToString is now a free function - setFromString split away from setValue - moved Configuration to src --- lib/settings/configuration.cpp | 143 ----------------------------------------- 1 file changed, 143 deletions(-) delete mode 100644 lib/settings/configuration.cpp (limited to 'lib/settings/configuration.cpp') diff --git a/lib/settings/configuration.cpp b/lib/settings/configuration.cpp deleted file mode 100644 index 10c284c..0000000 --- a/lib/settings/configuration.cpp +++ /dev/null @@ -1,143 +0,0 @@ -/* - * 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: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "configuration.h" -#include -#include -#include - -using namespace libconfig; - -Configuration::Configuration(const std::string &path, const std::string &home) -{ - m_userCfg = new Config(); - // prettier output - m_userCfg->setOption(Config::OptionSemicolonSeparators, true); - m_userCfg->setOption(Config::OptionColonAssignmentForGroups, false); - m_userCfg->setOption(Config::OptionColonAssignmentForNonGroups, false); - m_userCfg->setOption(Config::OptionOpenBraceOnSeparateLine, false); - m_userCfg->setOption(Config::OptionFsync, true); - - m_userCfgPath = path; - m_homePath = home; -} - -Configuration::~Configuration() -{ - delete m_userCfg; -} - -bool Configuration::read(const QString &path) -{ - QFile conf(path); - - if(!conf.open(QIODevice::ReadOnly)) { - return false; - } - - try { - m_userCfg->readString(conf.readAll().toStdString()); - conf.close(); - } catch(const ParseException &e) { - return false; - } - - return true; -} - -bool Configuration::writeIfNeeded(const std::string &path) -{ - if(!path.empty()) - m_userCfgPath = path; - - if(!changed) - return true; - - try { - m_userCfg->writeFile(m_userCfgPath.c_str()); - } catch(const FileIOException &e) { - return false; - } - - changed = 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.emplace_back(setting.getName()); - //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.emplace_back(setting.getName()); - //groupNames.push_back(setting.getName()); - } - } - return groupNames; -} - -std::string Configuration::castToString(const libconfig::Setting &v) const -{ - // cast depending on type - // type checks are done during compile time - switch(v.getType()) { - case Setting::TypeNone: - return std::string(); - - case Setting::TypeInt: - // int, unsigned int, long, unsigned long - return std::to_string(static_cast(v)); - - case Setting::TypeInt64: - // int, unsigned int; long long, unsigned long long - return std::to_string(static_cast(v)); - - case Setting::TypeFloat: - // float, double - return std::to_string(static_cast(v)); - - case Setting::TypeString: - // const char*, std::string - return std::string(static_cast(v)); - - case Setting::TypeBoolean: - // bool - return std::string(static_cast(v) ? "true" : "false"); - - case Setting::TypeGroup: - case Setting::TypeArray: - case Setting::TypeList: - return std::string(); - } -} - -std::string patchHome(const std::string &path, const std::string &home) -{ - std::string r = path; - const size_t location = path.find('~'); - if(location != std::string::npos) { - return r.replace(location, 1, home); - } - return r; -} -- cgit v1.2.1