Improve tokenizing of config file
This commit is contained in:
parent
2c2e4073f3
commit
fe3715e03c
@ -388,28 +388,38 @@ namespace util {
|
|||||||
cerr << "file \"" << filename << "\" does not exist!" << endl;
|
cerr << "file \"" << filename << "\" does not exist!" << endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
string line, key, token, buffer, value;
|
static string line, token[2];
|
||||||
int line_number = 0;
|
int line_number = 0;
|
||||||
while (getline(fin, line)) {
|
while (getline(fin, line)) {
|
||||||
++line_number;
|
++line_number;
|
||||||
istringstream is(line);
|
token[0].clear();
|
||||||
buffer.clear();
|
token[1].clear();
|
||||||
while (is >> token) {
|
int length = line.length();
|
||||||
if (token[0] == '#')
|
int equal_flag = 0;
|
||||||
|
for (int i = 0; i < length; ++i) {
|
||||||
|
if (line[i] == '#')
|
||||||
break;
|
break;
|
||||||
buffer += token;
|
if (line[i] == '=') {
|
||||||
|
if (++equal_flag > 1) {
|
||||||
|
cerr << "error in conf file " << filename << endl;
|
||||||
|
cerr << "multiple equal signs in line " << line_number << endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if (!isspace(line[i])) {
|
||||||
|
token[equal_flag].push_back(line[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (buffer.empty())
|
// ignore empty lines
|
||||||
|
if (equal_flag == 0 && token[0].length() == 0)
|
||||||
continue;
|
continue;
|
||||||
auto id = buffer.find('=');
|
if (equal_flag == 0) {
|
||||||
if (id == string::npos) {
|
|
||||||
cerr << "error in conf file: " << filename << endl;
|
cerr << "error in conf file: " << filename << endl;
|
||||||
cerr << "equal sign expected in line " << line_number << endl;
|
cerr << "equal sign expected in line " << line_number << endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
key = buffer.substr(0, id);
|
if (equal_flag == 1 && token[1].length() > 0) {
|
||||||
value = buffer.substr(id + 1);
|
conf[token[0]] = token[1];
|
||||||
conf[key] = value;
|
}
|
||||||
}
|
}
|
||||||
return conf;
|
return conf;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user