#include "CRSMConfig.hpp" #include 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& values = (dynamic_cast(&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(&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; }