-
Notifications
You must be signed in to change notification settings - Fork 1
/
Config.cpp
53 lines (41 loc) · 1.26 KB
/
Config.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
/*
* configuration input subroutine.
*
* copyright@ Yimin Zhong. yzhong@math.utexas.edu. All Rights Reserved.
*
*/
#include "Config.h"
Config::Config() {
options.clear();
}
Config::~Config() {
options.clear();
}
void Config::parse(std::istream &cfgFile) {
for (std::string line; std::getline(cfgFile, line);) {
std::istringstream iss(line);
std::string id, eq, val;
bool error = false;
if (!(iss >> id)) { error = true; }
else if (id[0] == '#') { continue; }
else if (!(iss >> eq >> val >> std::ws) || eq != "=" || iss.get() != EOF) {
error = true;
}
if (line.size() == 0) error = false;
if (error) {
std::cout << "Incomplete configuration! Did you forget the space?" << std::endl;
} else {
/*
* eliminate empty string key
*/
if (line.size()) options[id] = val;
}
}
}
void Config::print() {
std::cout << std::setw(15) << "=============== OPTIONS ===============" << std::endl;
for (auto key:options) {
std::cout << std::setw(15) << key.first << " = " << key.second << std::endl;
}
std::cout << std::setw(15) << "=======================================" << std::endl;
}