-
Notifications
You must be signed in to change notification settings - Fork 1
/
Config.cpp
63 lines (56 loc) · 2.32 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
#include "Config.h"
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
Config::Config() {
const std::string configFileName = "config.txt";
std::ifstream configFileStream(configFileName);
std::string configKey;
std::string configValue;
while(std::getline(configFileStream, configKey, '=') && std::getline(configFileStream, configValue)) {
configMap.insert(std::make_pair(configKey, configValue));
}
}
std::string Config::getConfig(std::string configKey, std::string defaultValue) {
std::unordered_map<std::string, std::string>::iterator it = configMap.find(configKey);
if(!configMap.empty() && it == configMap.end()) {
return it->second;
}
return defaultValue;
}
int Config::getIntConfig(std::string configKey, int defaultValue) {
return getIntConfig(configKey, defaultValue, 0, INT_MAX);
}
int Config::getIntConfig(std::string configKey, int defaultValue, int min, int max) {
std::unordered_map<std::string, std::string>::iterator it = configMap.find(configKey);
if(!configMap.empty() && it != configMap.end()) {
try {
int output = stoi(it->second);
if(output >= min && output <= max)
return output;
} catch(const std::invalid_argument& e) {
std::cout << "ERROR: Invalid Configuration: " << configKey << "\n";
} catch(const std::out_of_range& e) {
std::cout << "ERROR: Configuration Out Of Range: " << configKey << "\n";
}
}
return defaultValue;
}
long Config::getLongConfig(std::string configKey, long defaultValue) {
return getLongConfig(configKey, defaultValue, 0, LONG_MAX);
}
long Config::getLongConfig(std::string configKey, long defaultValue, int min, int max) {
std::unordered_map<std::string, std::string>::iterator it = configMap.find(configKey);
if(!configMap.empty() && it != configMap.end()) {
try {
long output = stol(it->second);
return output;
} catch(const std::invalid_argument& e) {
std::cout << "ERROR: Invalid Configuration: " << configKey << "\n";
} catch(const std::out_of_range& e) {
std::cout << "ERROR: Configuration Out Of Range: " << configKey << "\n";
}
}
return defaultValue;
}