aboutsummaryrefslogtreecommitdiff
path: root/lib/settings
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-01-25 19:20:30 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-01-25 19:20:30 +0100
commitf4025c0ebcddffbb1b826cd666b94d9140a56663 (patch)
tree94d9990af2a41db4237154de1377151dd8997a74 /lib/settings
parentConfiguration class rework (diff)
downloadsmolbote-f4025c0ebcddffbb1b826cd666b94d9140a56663.tar.xz
Configuration class rework
- castToString is now a free function - setFromString split away from setValue - moved Configuration to src
Diffstat (limited to 'lib/settings')
-rw-r--r--lib/settings/CMakeLists.txt2
-rw-r--r--lib/settings/configuration.cpp143
-rw-r--r--lib/settings/configuration.h117
-rw-r--r--lib/settings/settingsdialog.cpp2
4 files changed, 1 insertions, 263 deletions
diff --git a/lib/settings/CMakeLists.txt b/lib/settings/CMakeLists.txt
index 26d8525..20c78ae 100644
--- a/lib/settings/CMakeLists.txt
+++ b/lib/settings/CMakeLists.txt
@@ -1,8 +1,6 @@
cmake_minimum_required(VERSION 3.1.0)
add_library(configuration
- configuration.cpp
- configuration.h
settingsdialog.cpp
settingsdialog.h
settingsdialog.ui)
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 <QtCore/QStandardPaths>
-#include <QFile>
-#include <sstream>
-
-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<std::string> Configuration::childrenSettings(const char *name)
-{
- std::vector<std::string> 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<std::string> Configuration::childrenGroups(const char *name)
-{
- std::vector<std::string> 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<int32_t>(v));
-
- case Setting::TypeInt64:
- // int, unsigned int; long long, unsigned long long
- return std::to_string(static_cast<int64_t>(v));
-
- case Setting::TypeFloat:
- // float, double
- return std::to_string(static_cast<double>(v));
-
- case Setting::TypeString:
- // const char*, std::string
- return std::string(static_cast<const char *>(v));
-
- case Setting::TypeBoolean:
- // bool
- return std::string(static_cast<bool>(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;
-}
diff --git a/lib/settings/configuration.h b/lib/settings/configuration.h
deleted file mode 100644
index 82e0c58..0000000
--- a/lib/settings/configuration.h
+++ /dev/null
@@ -1,117 +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
- */
-
-#ifndef CONFIGURATION_H
-#define CONFIGURATION_H
-
-#include <libconfig.h++>
-#include <optional>
-#include <string>
-#include <vector>
-#include <QString>
-
-class Configuration
-{
-public:
- explicit Configuration(const std::string &path, const std::string &home);
- ~Configuration();
-
- bool read(const QString &path);
- bool writeIfNeeded(const std::string &path = std::string());
-
- std::vector<std::string> childrenSettings(const char *name = "");
- std::vector<std::string> childrenGroups(const char *name = "");
-
- template <typename T>
- std::optional<T> value(const char *path) const
- {
- // if setting doesn't exist, give back a nullopt
- if(!m_userCfg->exists(path)) {
- return std::nullopt;
- }
-
- const libconfig::Setting &v = m_userCfg->lookup(path);
- if constexpr(std::is_same_v<T, std::string>) {
- std::string r = castToString(v);
-
- // check if it's a path
- if(r.front() == '~') {
- r.replace(0, 1, m_homePath);
- }
-
- return std::optional<std::string>(r);
- } else
- return std::optional<T>(static_cast<T>(v));
- }
-
- template <typename T>
- bool setValue(std::string path, const T &val)
- {
- if(!m_userCfg->exists(path)) {
- return false;
- }
-
- libconfig::Setting &setting = m_userCfg->lookup(path);
- // compiler complained about operator= not taking unsinged ints, longs and long longs
- if constexpr(std::is_unsigned_v<T> && !std::is_same_v<T, bool>) {
- setting = static_cast<typename std::make_signed_t<T>>(val);
- } else if constexpr(std::is_same_v<T, std::string>) {
- switch(setting.getType()) {
- case libconfig::Setting::TypeNone:
- break;
-
- case libconfig::Setting::TypeInt:
- case libconfig::Setting::TypeInt64:
- setting = std::stoi(static_cast<std::string>(val));
- break;
-
- case libconfig::Setting::TypeFloat:
- setting = std::stod(static_cast<std::string>(val));
- break;
-
- case libconfig::Setting::TypeString:
- setting = static_cast<std::string>(val).c_str();
- break;
-
- case libconfig::Setting::TypeBoolean:
- if(static_cast<std::string>(val) == "true") {
- setting = true;
- } else if(static_cast<std::string>(val) == "false") {
- setting = false;
- }
- break;
-
- case libconfig::Setting::TypeGroup:
- break;
- case libconfig::Setting::TypeArray:
- break;
- case libconfig::Setting::TypeList:
- break;
- }
-
- } else {
- setting = val;
- }
-
- changed = true;
- return true;
- }
-
-private:
- std::string castToString(const libconfig::Setting &v) const;
-
- bool changed = false;
- std::string m_homePath;
- std::string m_userCfgPath;
- libconfig::Config *m_userCfg;
-};
-
-// replace ~ with home
-std::string patchHome(const std::string &path, const std::string &home);
-
-#endif // CONFIGURATION_H
diff --git a/lib/settings/settingsdialog.cpp b/lib/settings/settingsdialog.cpp
index 36b7d2c..bd0aa49 100644
--- a/lib/settings/settingsdialog.cpp
+++ b/lib/settings/settingsdialog.cpp
@@ -7,7 +7,7 @@
*/
#include "settingsdialog.h"
-#include "configuration.h"
+#include "../../src/configuration.h"
#include "ui_settingsdialog.h"
#include <QFormLayout>
#include <QGroupBox>