summaryrefslogtreecommitdiffstats
path: root/src/ConfigBase.cpp
blob: 317ecec9f9696862d91508a2db5f746441ba02b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#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();
                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");
    }
}