-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.cpp
127 lines (98 loc) · 2.62 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
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
#include "config.hpp"
#include <vector>
#include <fstream>
#include "regex/regex.hpp"
#include "logger.hpp"
#include "utils.hpp"
namespace remexec {
using namespace std;
using namespace sinlib;
unordered_map<int, string> Config::_names {
{Config::TASK_DIR, "TaskDir"},
{Config::TEMP_DIR, "TempDir"},
{Config::TASKCONF_DIR, "TaskConfDir"},
{Config::LISTEN_ADDRESS, "ListenAddress"},
{Config::LISTEN_PORT, "ListenPort"},
{Config::TASK_TIMEOUT, "TaskTimeout"},
};
unordered_map<int, string> Config::_defs {
{Config::TASK_DIR, "./tasks/"},
{Config::TEMP_DIR, "./tmp/"},
{Config::TASKCONF_DIR, "./taskconf/"},
{Config::LISTEN_ADDRESS, "127.0.0.1"},
{Config::LISTEN_PORT, "3500"},
{Config::TASK_TIMEOUT, "30000"},
};
Config::Config() : defaults(nullptr) {}
Config::Config(Config &defs) : defaults(&defs) {}
Config::~Config(){}
void Config::loadDefaultConfig(){
_vals = _defs;
}
bool Config::loadFromFile(string path){
ifstream conf(path);
if (!conf.good()){
return false;
}
string line;
while (getline(conf, line)){
// recognize '#' as start of comment only after whitespaces
// ('#' can be part of pathes)
line = regex_replace(line, regex("^\\s*#.*$|^\\s+|\\s+#.*$|\\s+$"), "");
if (line.empty())
continue;
vector<string> pars = regex_split(line, regex("\\s+"), 2);
if (pars.size() == 2){
EValue code = getParameterCode(pars[0]);
if (code == INVALID){
Log::warn("(", path, ") Skipped unknown parameter: ", pars[0]);
continue;
}
_vals[code] = pars[1];
Log::debug("(", path, ") Set param ", getParameterName(code), " to '", pars[1], "'");
} else {
Log::warn("(", path, ") Skipped broken config line: ", line);
}
}
return true;
}
bool Config::checkIsValid(){
if (!path_exists(getString(TASK_DIR) + "/")){
Log::error(getParameterName(TASK_DIR), " path not found: ", getString(TASK_DIR));
return false;
}
if (!path_exists(getString(TEMP_DIR) + "/")){
Log::error(getParameterName(TEMP_DIR), " path not found: ", getString(TEMP_DIR));
return false;
}
return true;
}
string Config::getString(EValue val){
auto it = _vals.find(val);
if (it == _vals.end()){
if (defaults){
return defaults->getString(val);
}
throw std::runtime_error("Invalid config value: " + std::to_string(val));
}
return it->second;
}
int Config::getInteger(EValue val){
return std::stoi(getString(val));
}
string Config::getParameterName(EValue v){
auto it = _names.find(v);
if (it == _names.end()){
return "";
}
return it->second;
}
Config::EValue Config::getParameterCode(string name){
for (auto &it : _names){
if (it.second == name){
return (EValue) it.first;
}
}
return (EValue) -1;
}
}