-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
123 lines (110 loc) · 3.83 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
#include "Config.h"
#include "Common.h"
Config::Config() :
serverPort(5300),
connectionCount(2),
logLevel(notice)
{
}
Config::Config(std::ifstream& configStream) :
Config()
{
ReadConfigFile(configStream);
}
void Config::ReadConfigFile(std::ifstream& configStream)
{
std::string line;
while (getline(configStream, line))
{
size_t pos = line.find_first_not_of(" \t\r\n");
if (pos != std::string::npos) {
if (line[pos] == '#' || line[pos] == '\0') {
continue;
}
}
size_t delim_pos = line.find_first_of(" \t=", pos);
std::string option_name;
if (delim_pos != std::string::npos) {
option_name = line.substr(pos, delim_pos - pos);
}
else {
option_name = line;
}
std::transform(option_name.begin(), option_name.end(), option_name.begin(), ::toupper);
size_t value_pos = line.find_first_not_of(" \t=", delim_pos);
std::string option_value;
if (value_pos != std::string::npos) {
option_value = line.substr(value_pos);
size_t comment_pos = option_value.find_first_of(" \t#");
if (comment_pos != std::string::npos)
option_value = option_value.substr(0, comment_pos);
}
if (option_name == serverPortParamName) {
serverPort = ParseULongValue(option_name, option_value);
}
else if (option_name == logDirParamName) {
logDir = option_value;
}
else if (option_name == connectStringParamName) {
connectString = option_value;
}
else if (option_name == connectionCountParamName) {
connectionCount = ParseULongValue(option_name, option_value);
}
else if (option_name == logLevelParamName) {
if (option_value == "error") {
logLevel = error;
}
else if (option_value == "notice") {
logLevel = notice;
}
else if (option_value == "debug") {
logLevel = debug;
}
else {
throw std::runtime_error("Wrong value passed for " + option_name + ".");
}
}
else if (option_name == kafkaBrokerParamName) {
kafkaBroker = option_value;
}
else if (option_name == kafkaTopicSmsParamName) {
kafkaTopicSms = option_value;
}
else if (option_name == kafkaTopicCallsParamName) {
kafkaTopicCalls = option_value;
}
else if (!option_name.empty()){
throw std::runtime_error("Unknown parameter " + option_name + " found");
}
}
}
unsigned long Config::ParseULongValue(const std::string& name, const std::string& value)
{
try {
return std::stoul(value);
}
catch(const std::invalid_argument&) {
throw std::runtime_error("Wrong value given for numeric config parameter " + name);
}
}
void Config::ValidateParams()
{
if (connectString.empty()) {
throw std::runtime_error(connectStringParamName + " parameter is not set.");
}
if (!(connectionCount >= minConnCount && connectionCount <= maxConnCount)) {
throw std::runtime_error(connectionCountParamName + " must have value from " +
std::to_string(minConnCount) + " to " + std::to_string(maxConnCount));
}
}
std::string Config::DumpAllSettings()
{
return serverPortParamName + ": " + std::to_string(serverPort) + crlf +
logDirParamName + ": " + logDir + crlf +
connectionCountParamName + ": " + std::to_string(connectionCount) + crlf +
kafkaBrokerParamName + ": " + kafkaBroker + crlf +
kafkaTopicSmsParamName + ": " + kafkaTopicSms + crlf +
kafkaTopicCallsParamName + ": " + kafkaTopicCalls + crlf +
logLevelParamName + ": " + (logLevel == error ? "error" : (logLevel == debug ? "debug" : "notice")) + crlf;
}