blob: 45209e4e3edd3afb9b6bb1dadb34aedf0b58fbda (
plain)
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
|
#include "config.hpp"
#include <cctype>
#include <fstream>
#include <iostream>
#include <optional>
#include <sstream>
#include <string>
namespace bot {
std::optional<Configuration> parse_configuration_from_file(
const std::string &file_path) {
std::ifstream ifs(file_path);
if (!ifs.is_open()) {
std::cerr << "*** Failed to open the configuration file: " << file_path
<< "!\n";
return std::nullopt;
}
Configuration cfg;
DatabaseConfiguration db_cfg;
std::string line;
while (std::getline(ifs, line, '\n')) {
std::istringstream iss(line);
std::string key;
std::string value;
std::getline(iss, key, '=');
std::getline(iss, value);
for (char &c : key) {
c = tolower(c);
}
if (key == "bot_username") {
cfg.bot_username = value;
} else if (key == "bot_password") {
cfg.bot_password = value;
} else if (key == "bot_client_id") {
cfg.bot_client_id = value;
} else if (key == "db_name") {
db_cfg.name = value;
} else if (key == "db_user") {
db_cfg.user = value;
} else if (key == "db_password") {
db_cfg.password = value;
} else if (key == "db_host") {
db_cfg.host = value;
} else if (key == "db_port") {
db_cfg.port = value;
}
}
cfg.database = db_cfg;
return cfg;
}
}
|