-
Notifications
You must be signed in to change notification settings - Fork 2
/
configparser.cpp
306 lines (227 loc) · 8.98 KB
/
configparser.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "configparser.h"
#include <QFile>
#include <QDebug>
#include <QStringList>
ConfigParser::ConfigParser() {
registerTokens();
}
void ConfigParser::registerTokens() {
//INVALID = -1, EMPTY, COMMENT, EQUAL, BRACKET_LEFT, BRACKET_RIGHT
tokens['#'] = ConfigToken::COMMENT;
tokens['='] = ConfigToken::EQUAL;
tokens['['] = ConfigToken::BRACKET_LEFT;
tokens[']'] = ConfigToken::BRACKET_RIGHT;
tokens['\n'] = ConfigToken::NEW_LINE;
//tokens['\t'] = ConfigToken::TAB;
tokens['.'] = ConfigToken::DOT;
//tokens[' '] = ConfigToken::SPACE;
}
int ConfigParser::loadFile(const QString& folder, const QString& configFileName) {
if (parsedFiles.contains(configFileName.toLower())) {
qDebug() << "error circular include for file: " << configFileName << " from: " << currentFile;
return -1;
} else
parsedFiles.append(configFileName.toLower());
QString oldFile = currentFile;
QString oldFolder = currentFolder;
//qDebug() << folder + "\\" + configFileName;
#ifdef Q_OS_WIN32
QFile fileObject(folder + "\\" + configFileName);
#else
QFile fileObject(folder + "/" + configFileName);
#endif
if (!fileObject.exists()) {
//qDebug() << configFileName << " doesnt exist";
errorMessage(configFileName + " doesnt exist", -1);
return -1;
}
if (!fileObject.open(QIODevice::ReadOnly | QIODevice::Text)) {
//qDebug() << configFileName << " failed to open";
errorMessage(configFileName + " failed to open", -1);
return -1;
}
currentFolder = folder;
currentFile = configFileName;
QTextStream in(&fileObject);
int lineNumber = 0;
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
QTextStream lineStream(&line);
processLine(&lineStream, ++lineNumber);
}
currentFolder = oldFolder;
currentFile = oldFile;
//qDebug() << configValues;
return 0;
}
void ConfigParser::errorMessage(const QString& errorMsg, int lineNumber) {
QString msg;
QTextStream stream(&msg);
if (lineNumber >= 0)
stream << currentFolder + "/" + currentFile << ":" << lineNumber << " error: " << errorMsg;
else
stream << currentFolder + "/" + currentFile << ": error: " << errorMsg;
qDebug() << msg;
emit errorSignal(msg);
}
int ConfigParser::parseDotExpression(QTextStream* stream, int lineNumber) {
//stream->
QString expression = stream->readAll();
if (!expression.contains(' ')) {
//qDebug() << "error in dot expression no space delimiter";
errorMessage("error in dot expression no space delimiter", lineNumber);
return -1;
}
QStringList values = expression.split(' ');
if (values.size() < 2) {
errorMessage("expected 2 values in expression: " + expression, lineNumber);
return -1;
}
if (values.at(0) != "include") {
errorMessage("unkown dot expression: " + values.at(0), lineNumber);
return -1;
}
QString includeFile = values.at(1);
if (includeFile.length() < 3) {
errorMessage("expected \"filename\" found instead:" + includeFile, lineNumber);
return -1;
}
QString fileName = includeFile.mid(1, includeFile.length() - 2);
qDebug() << "including: " << fileName;
return loadFile(currentFolder, fileName);
}
int ConfigParser::processLine(QTextStream* stream, int currentLineNumber) {
bool comment = false;
QString variableName;
QString variableValue;
bool classNameSet = false;
QString currentLiteral;
QTextStream currentLiteralStream(¤tLiteral);
while (!stream->atEnd()) {
ConfigToken token = readToken(stream);
//qDebug() << token.value << " " << token.type;
if (comment)
return 0;
switch (token.type) {
case ConfigToken::CHAR_LITERAL:
currentLiteralStream << token.value;
break;
case ConfigToken::EQUAL:
//qDebug() << "assignment";
if (currentLiteral.isEmpty()) {
errorMessage("no variable name before assignment operator!", currentLineNumber);
}
variableName = currentLiteral;
currentLiteral.clear();
break;
case ConfigToken::COMMENT:
//qDebug() << "comment";
comment = true;
break;
case ConfigToken::BRACKET_LEFT:
//qDebug() << "class definition start";
currentClassName.clear();
break;
case ConfigToken::BRACKET_RIGHT:
//qDebug() << "class definition end";
currentClassName = currentLiteral;
currentLiteral.clear();
classNameSet = true;
break;
case ConfigToken::DOT:
//qDebug() << "dot literal";
if (!variableName.isEmpty() || !variableValue.isEmpty())
currentLiteralStream << token.value;
else {
return parseDotExpression(stream, currentLineNumber);
}
break;
default:
//qDebug() << "unknown literal found in config:" << token.value;
errorMessage("unknown literal found in config:" + QString(token.value), currentLineNumber);
break;
}
if (stream->atEnd()) {
variableValue = currentLiteral;
if (variableName.isEmpty()) {
//qDebug() << "variable name is empty on assignment!";
if (!classNameSet)
errorMessage("variable name is empty on assignment!", currentLineNumber);
} else {
//qDebug() << "current class:" << currentClassName << " " << "variable:" << variableName << " set to: " << variableValue;
setVariable(variableName, variableValue);
}
}
}
return 0;
}
void ConfigParser::setVariable(const QString& variableName, const QString& variableValue) {
if (currentClassName.isEmpty()) {
qDebug() << "error assigning a variable to an empty class";
}
if (configValues.contains(currentClassName)) {
QVector<ConfigValue > classValues = configValues.value(currentClassName);
classValues.append(ConfigValue(variableName, variableValue, currentFile));
//ConfigValue(const QString& varName, const QString& value, const QString& fileName = "")
configValues.remove(currentClassName);
configValues.insert(currentClassName, classValues);
} else {
QVector<ConfigValue > classValues;
//classValues.append(QPair<QString, QString>(variableName, variableValue));
classValues.append(ConfigValue(variableName, variableValue, currentFile));
configValues.insert(currentClassName, classValues);
}
}
ConfigToken ConfigParser::readToken(QTextStream *stream) {
ConfigToken token;
*stream >> token.value;
token.type = tokens.value(token.value, ConfigToken::CHAR_LITERAL);
return token;
}
int ConfigParser::getConfigValue(const QString& className, const QString& variable, ConfigValue& returnValue) {
QVector<ConfigValue> values = getValues(className);
for (int i = values.size() - 1; i >= 0; --i) {
const ConfigValue val = values.at(i);
if (val.variableName.toLower() == variable.toLower()) {
returnValue = val;
return 0;
}
}
return -1;
}
bool ConfigParser::getBooleanConfigValue(const QString& className, const QString& variable, bool defaultValue) {
ConfigValue value;
int ret = getConfigValue(className, variable, value);
if (ret != 0)
return defaultValue;
if (value.variableValue == "0" || value.variableValue.toLower() == "false")
return false;
else
return true;
}
int ConfigParser::getIntConfigValue(const QString& className, const QString& variable, int defaultValue) {
ConfigValue value;
int ret = getConfigValue(className, variable, value);
if (ret != 0)
return defaultValue;
int result = value.variableValue.toInt();
return result;
}
QVector<ConfigValue> ConfigParser::getConfigValues(const QString& className, const QString& variable) {
QVector<ConfigValue> values = getValues(className);
QVector<ConfigValue> returnValues;
for (int i = values.size() - 1; i >= 0; --i) {
const ConfigValue val = values.at(i);
if (val.variableName.toLower() == variable.toLower()) {
returnValues.append(val);
}
}
return returnValues;
}
QString ConfigParser::getStringConfigValue(const QString& className, const QString& variable, const QString& defaultValue) {
ConfigValue value;
int ret = getConfigValue(className, variable, value);
if (ret != 0)
return defaultValue;
return value.variableValue;
}