summaryrefslogtreecommitdiffstats
path: root/ConfigBase.cpp
diff options
context:
space:
mode:
authorMarkus Mittendrein <git@maxmitti.tk>2016-02-16 15:18:14 +0100
committerMarkus Mittendrein <git@maxmitti.tk>2016-02-16 15:18:14 +0100
commit681f2be56c45e3945cd6cbc56ac14317a97ec9c1 (patch)
tree8c0a78a97ab82a22df6802f5c49e3e1077fb9161 /ConfigBase.cpp
downloadqt-config-681f2be56c45e3945cd6cbc56ac14317a97ec9c1.tar.gz
qt-config-681f2be56c45e3945cd6cbc56ac14317a97ec9c1.zip
Initial commit
Diffstat (limited to 'ConfigBase.cpp')
-rw-r--r--ConfigBase.cpp207
1 files changed, 207 insertions, 0 deletions
diff --git a/ConfigBase.cpp b/ConfigBase.cpp
new file mode 100644
index 0000000..ce4bcc0
--- /dev/null
+++ b/ConfigBase.cpp
@@ -0,0 +1,207 @@
+#include "CRSMConfig.hpp"
+
+#include <QFile>
+
+void ConfigBase::addConfigValue(QString name, ConfigValueBase* value)
+{
+ configValues[name] = value;
+}
+
+void ConfigBase::setConfigValue(const QString &name, const QString &value)
+{
+ ConfigValueBase& configValue = getConfigValue(name);
+ configValue.setValue(Util::unescape(value));
+}
+
+QString ConfigBase::getConfigValueLine(const QString &name)
+{
+ if(configValues.contains(name))
+ {
+ ConfigValueBase& value = *configValues[name];
+ switch(value.type())
+ {
+ case ConfigValueType::Map:
+ {
+ QString ret;
+ const QMap<QString, QString>& values = (dynamic_cast<ConfigValueMap*>(&value))->getValues();
+ foreach(const QString& mapKey, values.keys())
+ {
+ ret += name + "[" + Util::escape(mapKey, '\\', "]") + "]" + " = " + Util::escape(values.value(mapKey)) + "\n";
+ }
+ return ret;
+ break;
+ }
+ case ConfigValueType::List:
+ {
+ QString ret;
+ foreach(const QString& val, (dynamic_cast<ConfigValueList*>(&value))->getValues())
+ {
+ ret += name + " += " + Util::escape(val) + "\n";
+ }
+ return ret;
+ break;
+ }
+ default:
+ return name + " = " + Util::escape(value.value()) + "\n";
+ }
+ }
+ else
+ {
+ throw ConfigException("Unknown config value \"" + name.toStdString() + "\"");
+ }
+}
+
+void ConfigBase::addConfigListEntry(const QString &name, const QString &value)
+{
+ ConfigValueBase& configValue = getConfigValue(name);
+ if(configValue.type() != ConfigValueType::List)
+ {
+ throw ConfigException("Cannot add a list entry to a non-list-config value \"" + name.toStdString() + "\"");
+ }
+ ((ConfigValueList*)&configValue)->append(Util::unescape(value));
+}
+
+void ConfigBase::removeConfigMapListEntry(const QString &name, const QString &value)
+{
+ ConfigValueBase& configValue = getConfigValue(name);
+ if(configValue.type() == ConfigValueType::List)
+ {
+ ((ConfigValueList*)&configValue)->remove(Util::unescape(value));
+ }
+ else if(configValue.type() == ConfigValueType::Map)
+ {
+ ((ConfigValueMap*)&configValue)->remove(Util::unescape(value));
+ }
+ else
+ {
+ throw ConfigException("Cannot remove a list entry from a value which is neither a list nor a map \"" + name.toStdString() + "\"");
+ }
+}
+
+void ConfigBase::setConfigMapValue(const QString &name, const QString &key, const QString &value)
+{
+ ConfigValueBase& configValue = getConfigValue(name);
+ if(configValue.type() != ConfigValueType::Map)
+ {
+ throw ConfigException("Cannot set a map value on a non-map-config value \"" + name.toStdString() + "\"");
+ }
+ ((ConfigValueMap*)&configValue)->setKeyValue(Util::unescape(key), Util::unescape(value));
+}
+
+ConfigValueBase& ConfigBase::getConfigValue(const QString& name)
+{
+ if(!configValues.contains(name))
+ {
+ throw ConfigException("Unknown config value \"" + name.toStdString() + "\"");
+ }
+ else
+ {
+ return *configValues[name];
+ }
+}
+
+QString ConfigBase::read(const QString &fileName, bool writeDefault)
+{
+ curFileName = fileName;
+ QString ret = "";
+
+ QFile config(fileName);
+ if(!config.exists())
+ {
+ if(writeDefault)
+ {
+ if(write(fileName))
+ {
+ return fileName + ": The config-file did not exist, a new one with default values has been created.\n";
+ }
+ else
+ {
+ return fileName + ": The config-file did not exist, a new one could not be created.\n";
+ }
+ }
+ else
+ {
+ return ret;
+ }
+ }
+ else if(config.open(QIODevice::ReadOnly | QIODevice::Text))
+ {
+ QString line;
+ for(size_t lineNr = 1; !config.atEnd(); ++lineNr)
+ {
+ try
+ {
+ line = config.readLine().trimmed();
+ if(!line.isEmpty())
+ {
+ setConfigLine(line);
+ }
+ }
+ catch(ConfigException e)
+ {
+ ret += fileName + ":" + QString::number(lineNr) + ": " + e.what() + "\n";
+ }
+ }
+ }
+ else
+ {
+ return fileName + ": The config-file could not be read.\n";
+ }
+
+ return ret;
+}
+
+bool ConfigBase::write(QString fileName)
+{
+ if(fileName.isEmpty())
+ {
+ fileName = curFileName;
+ }
+ if(fileName.isEmpty())
+ {
+ return false;
+ }
+ QFile config(fileName);
+ if(config.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
+ {
+ foreach(const QString& key, configValues.keys())
+ {
+ config.write(getConfigValueLine(key).toUtf8());
+ }
+
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
+void ConfigBase::setConfigLine(const QString &line)
+{
+ static QRegExp valueExp(R"(^([^=]+)=(.*)$)");
+ static QRegExp listExp(R"(^([^=]+)\+=(.*)$)");
+ static QRegExp listMapRemoveExp(R"(^([^=]+)\-=(.*)$)");
+ static QRegExp mapExp(R"(^([^\[]+)\[((?:[^\]\\]|\\\]|\\\\)+)\]\s*=\s*(.*)$)");
+
+ if(listExp.exactMatch(line))
+ {
+ addConfigListEntry(listExp.cap(1).trimmed(), listExp.cap(2).trimmed());
+ }
+ else if(listMapRemoveExp.exactMatch(line))
+ {
+ removeConfigMapListEntry(listMapRemoveExp.cap(1).trimmed(), listMapRemoveExp.cap(2).trimmed());
+ }
+ else if(mapExp.exactMatch(line))
+ {
+ setConfigMapValue(mapExp.cap(1).trimmed(), mapExp.cap(2).trimmed(), mapExp.cap(3).trimmed());
+ }
+ else if(valueExp.exactMatch(line))
+ {
+ setConfigValue(valueExp.cap(1).trimmed(), valueExp.cap(2).trimmed());
+ }
+ else
+ {
+ throw ConfigException("Syntax error: unkown syntax");
+ }
+}