From f6f6860c0365409a0389681e65e5c46b56db1690 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Thu, 15 Oct 2015 15:49:11 +0200 Subject: Add setValue() and value() to map-specialization of ConfigValue for inline saving --- src/ConfigBase.hpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/ConfigBase.hpp b/src/ConfigBase.hpp index ae3c3de..7c0bcd5 100644 --- a/src/ConfigBase.hpp +++ b/src/ConfigBase.hpp @@ -206,9 +206,11 @@ class ConfigValue> : public ConfigValueMap { using MapType = QMap; MapType& config; + const QChar assignChar; + const QChar splitChar; public: - ConfigValue(MapType& config) : config(config) {} + ConfigValue(MapType& config, const QChar assignChar = ':', const QChar splitChar = ';') : config(config), assignChar(assignChar), splitChar(splitChar) {} virtual MapType& map() { return config; } virtual ConfigValueType::Type type() { return ConfigValueType::Type::Map; } @@ -234,6 +236,31 @@ public: { config.remove(ConfigValueBase::getValue(key)); } + + QString value() + { + QStringList ret; + foreach(const KeyType& key, config.keys()) + { + ret.append(Util::joinEscape({ConfigValueBase::getStringValue(key), ConfigValueBase::getStringValue(config.value(key))}, assignChar)); + } + return Util::joinEscape(ret, splitChar); + } + + void setValue(const QString& value) + { + config.clear(); + QStringList parts = Util::splitEscaped(value, splitChar); + foreach(const QString& part, parts) + { + QStringList parts = Util::splitEscaped(part, assignChar); + if(parts.length() != 2) + { + throw ConfigException("Cannot parse corrupt map key-value-pair"); + } + config[ConfigValueBase::getValue(parts.first())] = ConfigValueBase::getValue(parts.at(1)); + } + } }; template -- cgit v1.2.3-54-g00ecf