From 74870600fa54a48d4c1ce4b19861a9b0ce027fee Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 22 Mar 2020 14:59:04 +0200 Subject: lib/configuration improvements Configuration changes: - Configuration::value return type is now [[nodiscard]] - Configuration::value is now a generic template that only works with the exact types of the underlying std::variant - Add Configuration::value 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) --- lib/configuration/configuration.cpp | 68 ++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 32 deletions(-) (limited to 'lib/configuration/configuration.cpp') 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 #include +#include #include #include #include @@ -36,20 +37,42 @@ Configuration::Configuration(std::initializer_list &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(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 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(key.c_str()).value() << "\n"; - else - out << key << "=" << obj.value(key.c_str()).value() << "\n"; + out << key << "=" << obj.value(key.c_str()).value() << "\n"; } return out; -- cgit v1.2.1