-
Notifications
You must be signed in to change notification settings - Fork 12
/
pfConsoleParser.cpp
104 lines (99 loc) · 3.25 KB
/
pfConsoleParser.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
#include <QMap>
#include <QFile>
#include <QString>
#include <QStringList>
#include "pfConsoleParser.h"
enum ParseState { COMMAND, ARGUMENT, STRING, COMMENT };
pfConsoleParser::pfConsoleParser(QFile& file) {
ParseState state = COMMAND;
QString cmd;
QString arg;
QStringList* args = new QStringList();
while(!file.atEnd()) {
char c = *file.read(1).data();
if(c == '\r')
continue;
switch(state) {
case COMMAND:
// reads the first "word" on the line
switch(c) {
case ' ':
if(cmd.length() > 0)
state = ARGUMENT;
break;
case '#':
state = COMMENT;
break;
case '\n':
if(cmd.length() > 0) {
data.insert(cmd, args);
cmd.clear();
args = new QStringList();
}
break;
default:
cmd.append(c);
break;
}
break;
case ARGUMENT:
// reads space separated "words"
switch(c) {
case '\n':
case '#':
case '"':
case ' ':
if(arg.length() > 0) {
args->append(arg);
arg.clear();
}
if(c == '\n') {
state = COMMAND;
data.insert(cmd, args);
cmd.clear();
args = new QStringList();
}else if(c == '#') {
state = COMMENT;
data.insert(cmd, args);
cmd.clear();
args = new QStringList();
}else if(c == '"') {
state = STRING;
}
break;
default:
arg.append(c);
break;
}
break;
case STRING:
// reads until a closing " or the end of the line
// for now I won't bother with escape sequences
if(c == '"' || c == '\n') {
args->append(arg);
arg.clear();
state = ARGUMENT;
}else{
arg.append(c);
}
case COMMENT:
// ignores all characters until a line break
if(c == '\n')
state = COMMAND;
break;
}
}
// when we hit the end of the file, clean up any final state
if(arg.length() > 0)
args->append(arg);
if(cmd.length() > 0)
data.insert(cmd, args);
else
delete args;
}
const QStringList pfConsoleParser::operator [](const QString& key) {
return *data[key];
}
const QStringList pfConsoleParser::keys() {
return data.keys();
}