-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig.cpp
More file actions
120 lines (90 loc) · 2.37 KB
/
Copy pathconfig.cpp
File metadata and controls
120 lines (90 loc) · 2.37 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
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
#include "config.h"
#include "tools.h"
#include <fstream>
#include <sstream>
Config::Config(){
m_loaded = false;
}
bool Config::load(std::string filename){
m_file = filename;
std::ifstream c_file(filename.c_str());
if(!c_file.good()){
return false;
}
std::string line;
while(std::getline(c_file, line)){
if(line.find(CONFIG_COMMENT_PRE) != std::string::npos){
line = trim_s(line.substr(0, line.find(CONFIG_COMMENT_PRE)));
}
if(line.empty()){
continue;
}
if(line.find(CONFIG_DELIMITER) == std::string::npos){
continue;
}
std::string lhs = trim_s(line.substr(0, line.find(CONFIG_DELIMITER)));
std::string rhs = trim_s(line.substr(line.find(CONFIG_DELIMITER) + std::string(CONFIG_DELIMITER).length()));
if(lhs.empty() || rhs.empty()){
continue;
}
m_values[lhs] = rhs;
}
c_file.close();
return m_loaded = true;
}
bool Config::save(){
std::ofstream c_file(m_file.c_str());
if(!c_file.good()){
return false;
}
c_file << CONFIG_FILE_DESCRIPTION;
for(valuesMap::iterator it = m_values.begin(); it != m_values.end(); ++it){
c_file << "\n" << it->first << " " << CONFIG_DELIMITER << " " << it->second;
}
c_file.close();
return true;
}
void Config::free(){
m_values.clear();
m_loaded = false;
}
std::string Config::getString(std::string key, std::string def /*= std::string()*/){
valuesMap::iterator it = m_values.find(key);
if(it != m_values.end()){
return it->second;
}
return def;
}
int Config::getInteger(std::string key, int def /*= 0*/){
valuesMap::iterator it = m_values.find(key);
if(it != m_values.end()){
return atoi(it->second.c_str());
}
return def;
}
bool Config::getBoolean(std::string key, bool def /*= false*/){
valuesMap::iterator it = m_values.find(key);
if(it != m_values.end()){
if(iequals(it->second, "yes")){
return true;
} else if(iequals(it->second, "no")){
return false;
}
}
return def;
}
void Config::setString(std::string key, std::string value){
m_values[key] = value;
}
void Config::setInteger(std::string key, int value){
std::stringstream ss;
ss << value;
m_values[key] = ss.str();
}
void Config::setBoolean(std::string key, bool value){
if(value){
m_values[key] = std::string("yes");
} else {
m_values[key] = std::string("no");
}
}