-
Notifications
You must be signed in to change notification settings - Fork 7
/
configparser.h
128 lines (91 loc) · 3.17 KB
/
configparser.h
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
#ifndef CONFIGPARSER_H
#define CONFIGPARSER_H
#include <QString>
#include <QMap>
#include <QTextStream>
#include <QPair>
#include <QVector>
class ConfigToken {
public:
enum {INVALID = -1, EMPTY, COMMENT, EQUAL, BRACKET_LEFT, BRACKET_RIGHT, NEW_LINE, TAB, CHAR_LITERAL, DOT, SPACE};
ConfigToken() {
value = ' ';
type = INVALID;
}
ConfigToken(const ConfigToken& token) {
value = token.value;
type = token.type;
}
ConfigToken& operator=(const ConfigToken& token) {
if (this == &token)
return *this;
value = token.value;
type = token.type;
return *this;
}
char value;
int type;
};
class ConfigValue {
public:
QString variableName;
QString variableValue;
QString fileName;
ConfigValue() {
}
ConfigValue(const QString& varName, const QString& value, const QString& fileName = "") {
variableName = varName;
variableValue = value;
this->fileName = fileName;
}
ConfigValue(const ConfigValue& val) {
initialize(val);
}
ConfigValue& operator=(const ConfigValue& val) {
if (this == &val)
return *this;
initialize(val);
return *this;
}
void initialize(const ConfigValue& val) {
variableName = val.variableName;
variableValue = val.variableValue;
fileName = val.fileName;
}
};
class ConfigParser : public QObject {
Q_OBJECT
QMap<char, int> tokens;
QMap<QString, QVector<ConfigValue> > configValues;
QString currentClassName;
QString currentFolder;
QString currentFile;
QVector<QString> parsedFiles;
private:
void registerTokens();
void setVariable(const QString& variableName, const QString& variableValue);
signals:
void errorSignal(const QString&);
public:
ConfigParser();
int loadFile(const QString& folder, const QString& configFileName);
int processLine(QTextStream* stream, int currentLineNumber);
ConfigToken readToken(QTextStream* stream);
int parseDotExpression(QTextStream* stream, int lineNumber);
void errorMessage(const QString& errorMsg, int lineNumber);
QVector<ConfigValue> getValues(const QString& className) {
return configValues.value(className);
}
QMap<QString, QVector<ConfigValue> >* getAllValues() {
return &configValues;
}
bool hasConfigValue(const QString& className, const QString& variable) {
return getConfigValues(className, variable).size() > 0;
}
int getConfigValue(const QString& className, const QString& variable, ConfigValue& returnValue);
QVector<ConfigValue> getConfigValues(const QString& className, const QString& variable);
bool getBooleanConfigValue(const QString& className, const QString& variable, bool defaultValue = false);
int getIntConfigValue(const QString& className, const QString& variable, int defaultValue = 0);
QString getStringConfigValue(const QString& className, const QString& variable, const QString& defaultValue = "");
};
#endif // CONFIGPARSER_H