summaryrefslogtreecommitdiffstats
path: root/src/CRSMConfig.cpp
blob: 54b0e8f6a39e5d7ecf6e016a03d86a14dfa4ab09 (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
#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);
}

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::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))
    {
        static QRegExp valueExp(R"(^([^=]+)=(.*)$)");
        static QRegExp listExp(R"(^([^=]+)\+=(.*)$)");
        static QRegExp mapExp(R"(^([^\[]+)\[([^\]]+)\]\s*=\s*(.*)$)");
        QString line;
        for(size_t lineNr = 1; !config.atEnd(); ++lineNr)
        {
            try
            {
                line = config.readLine().trimmed();
                if(listExp.exactMatch(line))
                {
                    addConfigListEntry(listExp.cap(1).trimmed(), listExp.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());
                }
            }
            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())
        {
            CRSMConfigValueBase& value = *configValues[key];
            switch(value.type())
            {
                case CRSMConfigValueType::Map:
                {
                    const QMap<QString, QString> values = (dynamic_cast<CRSMConfigValueMap*>(&value))->getValues();
                    foreach(const QString& mapKey, values.keys())
                    {
                        config.write(QString(key + "[" + mapKey + "]" + " = " + values.value(mapKey) + "\n").toUtf8());
                    }
                    break;
                }
                case CRSMConfigValueType::List:
                    foreach(const QString& val, (dynamic_cast<CRSMConfigValueList*>(&value))->getValues())
                    {
                        config.write(QString(key + " += " + val + "\n").toUtf8());
                    }
                    break;
                default:
                    config.write(QString(key + " = " + value.value() + "\n").toUtf8());
            }
        }

        return true;
    }
    else
    {
        return false;
    }
}

void CRSMConfig::clear()
{
    auto configVals = configValues;
    *this = CRSMConfig();
    configValues = configVals;
}