-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfiguration.cpp
85 lines (69 loc) · 1.64 KB
/
Configuration.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
#include "stdafx.h"
#include "Configuration.h"
#include <fstream>
#include <algorithm>
#include <locale>
#include <cctype>
// damn, I'm so lazy
// trim from start
static inline void ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
}
// trim from end
static inline void rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}
// trim from both ends
static inline void trim(std::string &s)
{
ltrim(s);
rtrim(s);
}
Configuration::Configuration()
{
}
Configuration::~Configuration()
{
}
bool Configuration::load(const char *filename)
{
std::ifstream is;
is.open(filename, std::ios_base::in);
if (is.fail())
return false;
std::string line;
while (!is.eof())
{
std::getline(is, line);
trim(line);
if (line.empty() || line.at(0) == '#')
continue;
std::string::size_type idx = line.find('=');
if (idx != std::string::npos)
{
std::string key = line.substr(0, idx);
trim(key);
std::string value = line.substr(idx + 1);
ltrim(value);
m_config[key] = value;
}
}
is.close();
return true;
}
std::string Configuration::getString(const std::string &key, const std::string &defaultValue)
{
std::map<std::string, std::string>::iterator it = m_config.find(key);
if (it == m_config.end())
return defaultValue;
return it->second;
}
int Configuration::getInteger(const std::string &key, int defaultValue)
{
std::map<std::string, std::string>::iterator it = m_config.find(key);
if (it == m_config.end())
return defaultValue;
return atoi(it->second.c_str());
}