-
Notifications
You must be signed in to change notification settings - Fork 29
/
Config.cpp
101 lines (80 loc) · 2.11 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
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
#include "Config.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using std::string;
using std::ifstream;
using std::stringstream;
using namespace Procon;
namespace {
template<class T>
using Store = Config::Store<T>;
enum class ConfigType {
Boolean,
Integer,
Float,
Double,
String
};
using ConfigInt = int32_t;
const std::unordered_map<char, ConfigType> typemap{
{ 'b', ConfigType::Boolean },
{ 's', ConfigType::String },
{ 'f', ConfigType::Float },
{ 'd', ConfigType::Double },
{ 'i', ConfigType::Integer }
};
template<class T>
void readConfig(const string& line) {
stringstream s{ line };
stringstream name;
T value;
try {
while (s.peek() != ' ' && s.peek() != '=') // Read up until space or equal sign
name << static_cast<char>(s.get());
while (s.peek() == ' ' || s.peek() == '=') // Skip everything that's a space or equal sign
s.get();
s >> value;
}
catch (const std::runtime_error &e) {
stringstream error;
error << "Error parsing config line : \n" << line << "\nException: " << e.what();
throw ConfigError(error.str());
}
Config::store(name.str(), value);
}
}; // namespace
void Config::readConfigFile(const string& filename) {
ifstream file;
file.open(filename);
if (!file.good()) {
string error{ "Unable to open config file " };
error += filename;
throw ConfigError(error);
}
string line;
while (std::getline(file, line)) {
if (line.size() == 0) continue;
auto type = typemap.find(line[0]);
if (type == typemap.end()) continue;
switch (type->second) {
case ConfigType::Boolean:
readConfig<bool>(line);
break;
case ConfigType::Double:
readConfig<double>(line);
break;
case ConfigType::Float:
readConfig<float>(line);
break;
case ConfigType::Integer:
readConfig<ConfigInt>(line);
break;
case ConfigType::String:
readConfig<string>(line);
break;
} // switch (type->second)
} // while (std::getline(file, line))
}// readConfigFile()
ConfigError::ConfigError(const string& what) : std::runtime_error(what) {}
ConfigError::ConfigError(const char* what) : std::runtime_error(what) {}