Revert "Optimize reading of config file"
This reverts commit 88b85d3941.
This commit is contained in:
parent
88b85d3941
commit
2c2e4073f3
@ -381,85 +381,36 @@ namespace util {
|
|||||||
return join("\\s+", arr, 4);
|
return join("\\s+", arr, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
const int MAX_LINE_LENGTH = 65536;
|
|
||||||
char line[MAX_LINE_LENGTH];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads config file and returns a map of configuration keys
|
|
||||||
*/
|
|
||||||
map<string, string> read_config_file(const char* filename) {
|
map<string, string> read_config_file(const char* filename) {
|
||||||
|
|
||||||
map<string, string> conf;
|
map<string, string> conf;
|
||||||
|
ifstream fin(filename);
|
||||||
// open config file for reading
|
if (!fin.is_open()) {
|
||||||
FILE *config_file = fopen(filename, "r");
|
|
||||||
if (config_file == NULL) {
|
|
||||||
cerr << "file \"" << filename << "\" does not exist!" << endl;
|
cerr << "file \"" << filename << "\" does not exist!" << endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
string line, key, token, buffer, value;
|
||||||
// parse each line in config
|
int line_number = 0;
|
||||||
for (int line_number = 0; fgets(line, MAX_LINE_LENGTH, config_file) != NULL; ++line_number) {
|
while (getline(fin, line)) {
|
||||||
|
++line_number;
|
||||||
int i, equal = -1;
|
istringstream is(line);
|
||||||
|
buffer.clear();
|
||||||
// find "=" delimiter
|
while (is >> token) {
|
||||||
for (i = 0; line[i]; ++i) {
|
if (token[0] == '#')
|
||||||
if (line[i] == '#') {
|
|
||||||
// found comment; make end of line
|
|
||||||
line[i] = '\0';
|
|
||||||
break;
|
break;
|
||||||
} else if (line[i] == '=') {
|
buffer += token;
|
||||||
line[i] = '\0';
|
|
||||||
if (equal == -1)
|
|
||||||
equal = i;
|
|
||||||
else {
|
|
||||||
cerr << "error in conf file: " << filename << endl;
|
|
||||||
cerr << "multiple equal signs found in line " << line_number << endl;
|
|
||||||
fclose(config_file);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (buffer.empty())
|
||||||
int length = i;
|
|
||||||
|
|
||||||
// trim end
|
|
||||||
while (length > 0 && isspace(line[length - 1]))
|
|
||||||
line[--length] = '\0';
|
|
||||||
|
|
||||||
// ignore empty lines
|
|
||||||
if (length == 0)
|
|
||||||
continue;
|
continue;
|
||||||
|
auto id = buffer.find('=');
|
||||||
// check if key is found
|
if (id == string::npos) {
|
||||||
if (equal == -1) {
|
|
||||||
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;
|
||||||
fclose(config_file);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
key = buffer.substr(0, id);
|
||||||
// trim key
|
value = buffer.substr(id + 1);
|
||||||
for (int j = equal - 1; j >= 0 && isspace(line[j]); --j)
|
conf[key] = value;
|
||||||
line[j] = '\0';
|
|
||||||
|
|
||||||
int key = 0;
|
|
||||||
while (isspace(line[key]))
|
|
||||||
++key;
|
|
||||||
|
|
||||||
// trim value
|
|
||||||
int value = equal + 1;
|
|
||||||
while (isspace(line[value]))
|
|
||||||
++value;
|
|
||||||
|
|
||||||
// set configuration keys
|
|
||||||
conf[line + key] = line + value;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// close config file
|
|
||||||
fclose(config_file);
|
|
||||||
return conf;
|
return conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user