-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
76 lines (66 loc) · 1.8 KB
/
config.cpp
File metadata and controls
76 lines (66 loc) · 1.8 KB
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
#include "log/log.h"
#include "string_util/stringUtil.h"
#include "config.h"
USING_NAMESPACE(std)
NAMESPACE_SETUP(Util)
Config::Config(const string& filePath) {
assert(filePath != "");
mFilePath = filePath;
}
Config::~Config() {
}
void Config::LoadFile() {
ifstream ifs(mFilePath.c_str());
assert(ifs);
string line = "";
size_t lineNo = 0;
while (getline(ifs, line)) {
lineNo++;
StringUtil::Trim(line);
if (line == "" || StringUtil::StartsWith(line, "#")) {
continue;
}
vector<string> vecBuf;
if (!StringUtil::Split(line, vecBuf, "=") || vecBuf.size() != 2) {
LOG_ERROR("Line number:[%d] line:[%s] is illegal.", lineNo, line.c_str());
mMap.clear();
return;
}
string key = vecBuf[0];
string value = vecBuf[1];
StringUtil::Trim(key);
StringUtil::Trim(value);
mMap.insert(make_pair<string, string>(key, value));
}
}
bool Config::HasKey(const string& key) const{
return mMap.find(key) != mMap.end();
}
bool Config::GetValue(const string& key, string& value) const{
if (!HasKey(key)) {
return false;
}
map<string, string>::const_iterator it = mMap.find(key);
value = it->second;
return true;
}
bool Config::GetValue(const string& key, int& value) const{
string v = "";
if (!GetValue(key, v)) {
return false;
}
value = atoi(v.c_str());
return true;
}
const char* Config::operator[](const char* key) const{
string value = "";
if (NULL == key || !HasKey(string(key)) || !GetValue(string(key), value)) {
return NULL;
}
return value.c_str();
}
string Config::GetConfigInfo() const {
string configInfo = "";
return operator<<(configInfo, mMap);
}
NAMESPACE_END(Util)