aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/configuration.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/configuration/configuration.cpp')
-rw-r--r--lib/configuration/configuration.cpp68
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;