diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-03-22 14:59:04 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-03-22 14:59:04 +0200 |
commit | 74870600fa54a48d4c1ce4b19861a9b0ce027fee (patch) | |
tree | d79eb50c18f1f62e937a1d1fe2e7180c7c14d06a /lib/configuration/configuration.cpp | |
parent | Remove ProfileInterface (diff) | |
download | smolbote-74870600fa54a48d4c1ce4b19861a9b0ce027fee.tar.xz |
lib/configuration improvements
Configuration changes:
- Configuration::value return type is now [[nodiscard]]
- Configuration::value<T> is now a generic template that only works with
the exact types of the underlying std::variant
- Add Configuration::value<concept_value_t> for standard library types
compatible with the types of std::variant
- Add Configuration::shortcut<> placeholder, and QAction and
QKeySequence specializations as a convenient way to set up shortcuts
- Deprecate setShortcut
- Add Configuration::read_file convenience member that takes file path
as parameter
Format changes:
- Configuration files can now have sections, specified as [section
name]. Section names are prepended to keys. Section names cannot be
nested.
- Configuration files can now have @@include directives, causing another
file to be read as well. The included file is not treated as nested into
a section, and will overwrite values previously set.
Others:
- add some tests for libconfiguration. QAction/QKeySequence require a
QApplication be set up, so the test application may require running
xorg/wayland.
old coverage: lines: 15.6% (960 out of 6172) branches: 9.9% (1187 out of 12012)
new coverage: lines: 17.1% (1067 out of 6254) branches: 11.0% (1388 out of 12644)
Diffstat (limited to 'lib/configuration/configuration.cpp')
-rw-r--r-- | lib/configuration/configuration.cpp | 68 |
1 files changed, 36 insertions, 32 deletions
diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp index 34e84af..0fdd0c0 100644 --- a/lib/configuration/configuration.cpp +++ b/lib/configuration/configuration.cpp @@ -9,6 +9,7 @@ #include "configuration.h" #include <QStandardPaths> #include <algorithm> +#include <fstream> #include <iostream> #include <sstream> #include <stdexcept> @@ -36,20 +37,42 @@ Configuration::Configuration(std::initializer_list<std::pair<std::string, conf_v } } +void Configuration::read_file(const std::string &location) +{ + std::fstream fs(location, std::fstream::in); + if(fs.is_open()) { + read(fs); + fs.close(); + } +} + void Configuration::read(std::basic_istream<char> &input) { - std::string line, key, value; + std::string line, section, key, value; std::istringstream is_line; while(std::getline(input, line)) { + if(line.rfind("@@") == 0) { + if(line.rfind("@@include ") == 0) { + read_file(line.substr(10)); + } + continue; + } if(line[0] == '#' || line.length() == 0) continue; + if(line.front() == '[' && line.back() == ']') { + section = line.substr(1, line.length() - 2) + '/'; + continue; + } + is_line.clear(); is_line.str(line); - if(std::getline(is_line, key, '=')) { - is_line >> value; + const auto pos = line.find_first_of('='); + if(pos != std::string::npos) { + key = section + line.substr(0, pos); + value = line.substr(pos + 1); strip(key); strip(value); @@ -82,45 +105,26 @@ Configuration *Configuration::instance() return s_conf.get(); } -void setShortcut(QAction *action, const char *name) +std::ostream &operator<<(std::ostream &out, const Configuration &obj) { - if(!s_conf) - throw new std::runtime_error("Trying to set a shortcut, but no configuration has been set!"); + if(obj.use_global) { + if(!s_conf) { + throw new std::runtime_error("Trying to use default Configuration, but none has been set!"); + } - if(const auto shortcutText = s_conf->value<QString>(name)) { - const QString tooltip = action->toolTip(); - action->setShortcut(QKeySequence::fromString(shortcutText.value())); - action->setToolTip(QString("%1 (%2)").arg(tooltip, shortcutText.value())); + out << *s_conf; + return out; } -#ifdef QT_DEBUG - else { - std::cout << "fixme: setShortcut called for " << name << ", but no such value exists!" << std::endl; - } -#endif -} -std::ostream &operator<<(std::ostream &out, const Configuration &obj) -{ // unordered_map is, well, unordered, so grab the keys and sort them before printing them std::vector<std::string> keys; - - if(obj.use_global) { - if(!s_conf) - throw new std::runtime_error("Trying to use default Configuration, but none has been set!"); - - for(const auto &pair : *s_conf) - keys.emplace_back(pair.first); - } else { - for(const auto &pair : obj) - out << pair.first << "\n"; + for(const auto &pair : obj) { + keys.emplace_back(pair.first); } std::sort(keys.begin(), keys.end()); for(const auto &key : keys) { - if(obj.use_global) - out << key << "=" << s_conf->value<std::string>(key.c_str()).value() << "\n"; - else - out << key << "=" << obj.value<std::string>(key.c_str()).value() << "\n"; + out << key << "=" << obj.value<std::string>(key.c_str()).value() << "\n"; } return out; |