/
ssuvariables.cpp
123 lines (101 loc) · 3.49 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
/**
* @file ssuvariables.cpp
* @copyright 2013 Jolla Ltd.
* @author Bernd Wachter <bwachter@lart.info>
* @date 2013
*/
#include <QStringList>
#include <QRegExp>
#include <QStringRef>
#include "ssuvariables.h"
#include "../constants.h"
SsuVariables::SsuVariables(): QObject() {
}
void SsuVariables::resolveSection(SsuSettings *settings, QString section, QHash<QString, QString> *storageHash){
QStringList repoVariables;
settings->beginGroup(section);
repoVariables = settings->allKeys();
foreach (const QString &key, repoVariables){
storageHash->insert(key, settings->value(key).toString());
}
settings->endGroup();
}
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.toAscii()){
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;
}