/
ssuvariables.cpp
292 lines (242 loc) · 9.24 KB
/
ssuvariables.cpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* @file ssuvariables.cpp
* @copyright 2013 Jolla Ltd.
* @author Bernd Wachter <bwachter@lart.info>
* @date 2013
*/
#include <QStringList>
#include <QRegExp>
#include <QStringRef>
#include "ssuvariables_p.h"
#include "ssulog_p.h"
#include "../constants.h"
SsuVariables::SsuVariables(): QObject()
{
}
QString SsuVariables::defaultSection(SsuSettings *settings, QString section)
{
QStringList parts = section.split("-");
if (section.startsWith("var-"))
parts.insert(1, "default");
else
parts.replace(0, "default");
QString key = parts.join("-");
if (settings->childGroups().contains(key))
return key;
else
return "";
}
QString SsuVariables::resolveString(QString pattern, QHash<QString, QString> *variables, int recursionDepth)
{
if (recursionDepth >= SSU_MAX_RECURSION) {
return "maximum-recursion-level-reached";
}
QRegExp regex("%\\([^%]*\\)", Qt::CaseSensitive, QRegExp::RegExp2);
regex.setMinimal(true);
int pos = 0;
while ((pos = regex.indexIn(pattern, pos)) != -1) {
QString match = regex.cap(0);
if (match.contains(":")) {
// variable is special, resolve before replacing
QString variable = resolveVariable(match, variables);
pattern.replace(match, variable);
pos += variable.length();
} else {
// look up variable name in hashmap, and replace it with stored value,
// if found, or ""
QString variableName = match;
variableName.remove(0, 2);
variableName.chop(1);
if (variables->contains(variableName)) {
pattern.replace(match, variables->value(variableName));
pos += variables->value(variableName).length();
} else
pattern.replace(match, "");
}
}
// check if string still contains variables, and recurse
if (regex.indexIn(pattern, 0) != -1)
pattern = resolveString(pattern, variables, recursionDepth + 1);
return pattern;
}
QString SsuVariables::resolveVariable(QString variable, QHash<QString, QString> *variables)
{
QString variableValue = "";
if (variable.endsWith(")"))
variable.chop(1);
if (variable.startsWith("%("))
variable.remove(0, 2);
// hunt for your colon
int magic = variable.indexOf(":");
// seems you misplaced your colon
if (magic == -1) return variable;
QStringRef variableName(&variable, 0, magic);
QStringRef variableSub(&variable, magic + 2, variable.length() - magic - 2);
// Fill in variable value for later tests, if it exists
if (variables->contains(variableName.toString()))
variableValue = variables->value(variableName.toString());
// find the operator who's after your colon
QChar op;
if (variable.length() > magic + 1)
op = variable.at(magic + 1);
switch (op.toLatin1()) {
case '-':
// substitute default value if variable is empty
if (variableValue == "")
return variableSub.toString();
break;
case '+':
// substitute default value if variable is not empty
if (variableValue != "")
return variableSub.toString();
break;
case '=': {
// %(%(foo):=bar?foobar|baz)
// if foo == bar then return foobar, else baz
QString sub = variableSub.toString();
QString a = sub.left(sub.indexOf("?"));
QString b = sub.right(sub.length() - sub.indexOf("?") - 1);
if (b.indexOf("|") == -1)
return b;
if (variableName == a)
return b.left(b.indexOf("|"));
else
return b.right(b.length() - b.indexOf("|") - 1);
}
}
// no proper substitution found -> return default value
return variableValue;
}
void SsuVariables::setSettings(SsuSettings *settings)
{
m_settings = settings;
}
SsuSettings *SsuVariables::settings()
{
return m_settings;
}
/// @todo add override capability with an override-section in ssu.ini
QVariant SsuVariables::variable(QString section, const QString &key)
{
if (m_settings != NULL)
return variable(m_settings, section, key);
else
return QVariant();
}
QVariant SsuVariables::variable(SsuSettings *settings, QString section, const QString &key)
{
QVariant value;
value = readVariable(settings, section, key, 0);
// first check if the value is defined in the main section, and fall back
// to default sections
if (value.type() == QMetaType::UnknownType) {
QString dSection = defaultSection(settings, section);
if (!dSection.isEmpty())
value = readVariable(settings, dSection, key, 0, false);
}
return value;
}
void SsuVariables::variableSection(QString section, QHash<QString, QString> *storageHash)
{
if (m_settings != NULL)
variableSection(m_settings, section, storageHash);
}
void SsuVariables::variableSection(SsuSettings *settings, QString section, QHash<QString, QString> *storageHash)
{
QString dSection = defaultSection(settings, section);
if (dSection.isEmpty())
readSection(settings, section, storageHash, 0);
else {
readSection(settings, dSection, storageHash, 0);
readSection(settings, section, storageHash, 0, false);
}
}
// resolve a configuration section, recursively following all 'variables' sections.
// variables which exist in more than one section will get overwritten when discovered
// again
// the section itself gets evaluated at the end, again having a chance to overwrite variables
void SsuVariables::readSection(SsuSettings *settings, QString section,
QHash<QString, QString> *storageHash, int recursionDepth,
bool logOverride)
{
if (recursionDepth >= SSU_MAX_RECURSION) {
SsuLog::instance()->print(LOG_WARNING,
QString("Maximum recursion depth for resolving section %1 from %2")
.arg(section)
.arg(settings->fileName()));
return;
}
if (settings->contains(section + "/variables")) {
// child should log unless the parent is a default section
bool childLogOverride = true;
if (section.startsWith("default-") || section.startsWith("var-default-"))
childLogOverride = false;
QStringList sections = settings->value(section + "/variables").toStringList();
foreach (const QString §ion, sections) {
if (section.startsWith("var-"))
readSection(settings, section, storageHash, recursionDepth + 1, childLogOverride);
else
readSection(settings, "var-" + section, storageHash,
recursionDepth + 1, childLogOverride);
}
}
settings->beginGroup(section);
if (settings->group() != section)
return;
QStringList locals;
if (settings->contains("local"))
locals = settings->value("local").toStringList();
QStringList keys = settings->allKeys();
foreach (const QString &key, keys) {
// local variable
if (key.startsWith("_"))
continue;
if (locals.contains(key))
continue;
if (key == "variables" || key == "local")
continue;
if (storageHash->contains(key) && logOverride) {
SsuLog::instance()->print(LOG_DEBUG,
QString("Variable %1 overwritten from %2::%3")
.arg(key)
.arg(settings->fileName())
.arg(section));
}
storageHash->insert(key, settings->value(key).toString());
}
settings->endGroup();
}
QVariant SsuVariables::readVariable(SsuSettings *settings, QString section, const QString &key,
int recursionDepth, bool logOverride)
{
Q_UNUSED(logOverride)
QVariant value;
if (recursionDepth >= SSU_MAX_RECURSION) {
SsuLog::instance()->print(LOG_WARNING,
QString("Maximum recursion depth for resolving %1 from %2::%3")
.arg(key)
.arg(settings->fileName())
.arg(section));
return value;
}
// variables directly in the section take precedence
if (settings->contains(section + "/" + key)) {
return settings->value(section + "/" + key);
}
/// @todo add logging for overrides
if (settings->contains(section + "/variables")) {
// child should log unless the parent is a default section
bool childLogOverride = true;
if (section.startsWith("default-") || section.startsWith("var-default-"))
childLogOverride = false;
QStringList sections = settings->value(section + "/variables").toStringList();
foreach (const QString §ion, sections) {
if (section.startsWith("var-"))
value = readVariable(settings, section, key, recursionDepth + 1, childLogOverride);
else
value = readVariable(settings, "var-" + section, key,
recursionDepth + 1, childLogOverride);
}
}
return value;
}