summaryrefslogtreecommitdiffstats
path: root/src/CRSMConfig.cpp
diff options
context:
space:
mode:
authorMarkus Mittendrein <git@maxmitti.tk>2015-10-10 20:17:56 +0200
committerMarkus Mittendrein <git@maxmitti.tk>2015-10-10 20:17:56 +0200
commitb5e22002ef491cdbae8ab04e96200f101ca6312b (patch)
tree1fdb9a45e71a4786414bfc5f6507de9f85b9ce17 /src/CRSMConfig.cpp
parentba5ed6186f6624e4f8cc6f73c52e597a4fa1d531 (diff)
downloadmanager-b5e22002ef491cdbae8ab04e96200f101ca6312b.tar.gz
manager-b5e22002ef491cdbae8ab04e96200f101ca6312b.zip
Split CRSMConfig and CRSMConfigBase into separate files
Diffstat (limited to 'src/CRSMConfig.cpp')
-rw-r--r--src/CRSMConfig.cpp202
1 files changed, 0 insertions, 202 deletions
diff --git a/src/CRSMConfig.cpp b/src/CRSMConfig.cpp
deleted file mode 100644
index 5ec0f9b..0000000
--- a/src/CRSMConfig.cpp
+++ /dev/null
@@ -1,202 +0,0 @@
-#include "CRSMConfig.hpp"
-
-#include <QFile>
-
-void CRSMConfigBase::addConfigValue(QString name, CRSMConfigValueBase* value)
-{
- configValues[name] = value;
-}
-
-void CRSMConfigBase::setConfigValue(const QString &name, const QString &value)
-{
- CRSMConfigValueBase& configValue = getConfigValue(name);
- configValue.setValue(value);
-}
-
-QString CRSMConfigBase::getConfigValueLine(const QString &name)
-{
- if(configValues.contains(name))
- {
- CRSMConfigValueBase& value = *configValues[name];
- switch(value.type())
- {
- case CRSMConfigValueType::Map:
- {
- QString ret;
- const QMap<QString, QString>& values = (dynamic_cast<CRSMConfigValueMap*>(&value))->getValues();
- foreach(const QString& mapKey, values.keys())
- {
- ret += name + "[" + mapKey + "]" + " = " + values.value(mapKey) + "\n";
- }
- return ret;
- break;
- }
- case CRSMConfigValueType::List:
- {
- QString ret;
- foreach(const QString& val, (dynamic_cast<CRSMConfigValueList*>(&value))->getValues())
- {
- ret += name + " += " + val + "\n";
- }
- return ret;
- break;
- }
- default:
- return name + " = " + value.value() + "\n";
- }
- }
- else
- {
- throw CRSMConfigException("Unknown config value \"" + name.toStdString() + "\"");
- }
-}
-
-void CRSMConfigBase::addConfigListEntry(const QString &name, const QString &value)
-{
- CRSMConfigValueBase& configValue = getConfigValue(name);
- if(configValue.type() != CRSMConfigValueType::List)
- {
- throw CRSMConfigException("Cannot add a list entry to a non-list-config value \"" + name.toStdString() + "\"");
- }
- ((CRSMConfigValueList*)&configValue)->append(value);
-}
-
-void CRSMConfigBase::removeConfigMapListEntry(const QString &name, const QString &value)
-{
- CRSMConfigValueBase& configValue = getConfigValue(name);
- if(configValue.type() == CRSMConfigValueType::List)
- {
- ((CRSMConfigValueList*)&configValue)->remove(value);
- }
- else if(configValue.type() == CRSMConfigValueType::Map)
- {
- ((CRSMConfigValueMap*)&configValue)->remove(value);
- }
- else
- {
- throw CRSMConfigException("Cannot remove a list entry from a value which is neither a list nor a map \"" + name.toStdString() + "\"");
- }
-}
-
-void CRSMConfigBase::setConfigMapValue(const QString &name, const QString &key, const QString &value)
-{
- CRSMConfigValueBase& configValue = getConfigValue(name);
- if(configValue.type() != CRSMConfigValueType::Map)
- {
- throw CRSMConfigException("Cannot set a map value on a non-map-config value \"" + name.toStdString() + "\"");
- }
- ((CRSMConfigValueMap*)&configValue)->setKeyValue(key, value);
-}
-
-CRSMConfigValueBase& CRSMConfigBase::getConfigValue(const QString& name)
-{
- if(!configValues.contains(name))
- {
- throw CRSMConfigException("Unknown config value \"" + name.toStdString() + "\"");
- }
- else
- {
- return *configValues[name];
- }
-}
-
-QString CRSMConfigBase::read(const QString &fileName, bool writeDefault)
-{
- 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();
- setConfigLine(line);
- }
- catch(CRSMConfigException e)
- {
- ret += fileName + ":" + QString::number(lineNr) + ": " + e.what() + "\n";
- }
- }
- }
- else
- {
- return fileName + ": The config-file could not be read.\n";
- }
-
- return ret;
-}
-
-bool CRSMConfigBase::write(const QString &fileName)
-{
- 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 CRSMConfigBase::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 CRSMConfigException("Syntax error: unkown syntax");
- }
-}
-
-void CRSMConfig::clear()
-{
- auto configVals = configValues;
- *this = CRSMConfig();
- configValues = configVals;
-}