-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLineParser.hh
More file actions
182 lines (146 loc) · 3.54 KB
/
LineParser.hh
File metadata and controls
182 lines (146 loc) · 3.54 KB
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
#ifndef _LINEPARSER_HH_
#define _LINEPARSER_HH_
#include <stdarg.h>
#include <stdlib.h>
#include <exception>
#include <iostream>
#include <string>
using namespace std;
// TODO Move to util class?
static string buildMessage(const string msg, va_list ap)
throw()
{
size_t maxsize = 128;
char *buf = static_cast<char *>(malloc(maxsize));
size_t size = vsnprintf(buf, maxsize, msg.c_str(), ap);
if (size >= maxsize) {
buf = static_cast<char *>(realloc(buf, size));
vsnprintf(buf, maxsize, msg.c_str(), ap);
}
string s = string(buf);
return s;
}
// TODO Move to separate files
class ParseError : public exception {
private:
string msg;
public:
ParseError(const string msg, ...)
throw()
{
va_list ap;
va_start(ap, msg);
this->msg = buildMessage(msg, ap);
va_end(ap);
}
~ParseError()
throw() { }
const char *what() const
throw() { return msg.c_str(); }
};
class LineParser {
private:
const string line;
enum {
SKIP_INITIAL_SPACE, PARSE_KEY, SKIP_SPACE_AFTER_KEY, SKIP_EQUALS_SIGN,
SKIP_SPACE_AFTER_EQUALS_SIGN, PARSE_VALUE, SKIP_SPACE_AFTER_VALUE
} state;
size_t idx;
string key;
string val;
bool isvalidchar(int c)
throw()
{
return c == '.' || isalnum(c);
}
void process(size_t i, const char c)
throw(ParseError)
{
switch (state) {
case SKIP_INITIAL_SPACE:
if (isspace(c)) {
// NOP
} else if (isvalidchar(c)) {
state = PARSE_KEY;
idx = i;
} else {
throw ParseError("Invalid character '%c': pos %zu line '%s'", c, i, line.c_str());
}
break;
case PARSE_KEY:
if (isvalidchar(c)) {
// NOP
} else if (isspace(c)) {
state = SKIP_SPACE_AFTER_KEY;
key = line.substr(idx, i - idx);
} else if (c == '=') {
state = SKIP_EQUALS_SIGN;
key = line.substr(idx, i - idx);
} else {
throw ParseError("Invalid character '%c': pos %zu line '%s'", c, i, line.c_str());
}
break;
case SKIP_SPACE_AFTER_KEY:
if (isspace(c)) {
// NOP
} else if (c == '=') {
state = SKIP_EQUALS_SIGN;
} else {
throw ParseError("Invalid character '%c': pos %zu line '%s'", c, i, line.c_str());
}
break;
case SKIP_EQUALS_SIGN:
if (isspace(c)) {
state = SKIP_SPACE_AFTER_EQUALS_SIGN;
} else if (isvalidchar(c)) {
state = PARSE_VALUE;
idx = i;
} else {
throw ParseError("Invalid character '%c': pos %zu line '%s'", c, i, line.c_str());
}
break;
case SKIP_SPACE_AFTER_EQUALS_SIGN:
if (isspace(c)) {
// NOP
} else if (isvalidchar(c)) {
state = PARSE_VALUE;
idx = i;
} else {
throw ParseError("Invalid character '%c': pos %zu line '%s'", c, i, line.c_str());
}
break;
case PARSE_VALUE:
if (isvalidchar(c)) {
// NOP
} else if (isspace(c)) {
state = SKIP_SPACE_AFTER_VALUE;
val = line.substr(idx, i - idx);
} else {
throw ParseError("Invalid character '%c': pos %zu line '%s'", c, i, line.c_str());
}
break;
case SKIP_SPACE_AFTER_VALUE:
if (isspace(c)) {
// NOP
} else {
throw ParseError("Invalid character '%c': pos %zu line '%s'", c, i, line.c_str());
}
break;
default:
throw ParseError("Invalid state");
}
}
public:
LineParser(string line)
throw(ParseError)
: line(line), state(SKIP_INITIAL_SPACE), idx(0)
{
size_t i;
for (i = 0; i < line.length(); i++)
process(i, line[i]);
process(i, ' ');
}
const string getKey() { return key; }
const string getVal() { return val; }
};
#endif /* _LINEPARSER_HH_ */