-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.cpp
198 lines (188 loc) · 5.25 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// Created by sin on 2020/8/26.
//
#include "config.h"
#include <fstream>
#include <cstdlib>
namespace rr
{
bool Config::IsSpace(char c)
{
if (' ' == c || '\t' == c)
return true;
return false;
}
bool Config::IsCommentChar(char c)
{
switch (c)
{
case '#':
return true;
default:
return false;
}
}
void Config::Trim(std::string &str)
{
if (str.empty())
{
return;
}
int i, start_pos, end_pos;
for (i = 0; i < str.size(); ++i)
{
if (!IsSpace(str[i]))
{
break;
}
}
if (i == str.size())
{
str = "";
return;
}
start_pos = i;
for (i = str.size() - 1; i >= 0; --i)
{
if (!IsSpace(str[i]))
{
break;
}
}
end_pos = i;
str = str.substr(start_pos, end_pos - start_pos + 1);
}
bool Config::AnalyseLine(const std::string &line, std::string §ion, std::string &key, std::string &value)
{
if (line.empty())
return false;
int start_pos = 0, end_pos = line.size() - 1, pos, s_startpos, s_endpos;
if ((pos = line.find("#")) != -1)
{
if (0 == pos)
{
return false;
}
end_pos = pos - 1;
}
if (((s_startpos = line.find("[")) != -1) && ((s_endpos = line.find("]"))) != -1)
{
section = line.substr(s_startpos + 1, s_endpos - 1);
return true;
}
std::string new_line = line.substr(start_pos, start_pos + 1 - end_pos);
if ((pos = new_line.find('=')) == -1)
return false;
key = new_line.substr(0, pos);
value = new_line.substr(pos + 1, end_pos + 1 - (pos + 1));
Trim(key);
if (key.empty())
{
return false;
}
Trim(value);
if ((pos = value.find("\r")) > 0)
{
value.replace(pos, 1, "");
}
if ((pos = value.find("\n")) > 0)
{
value.replace(pos, 1, "");
}
return true;
}
bool Config::ReadConfig(const std::string &filename)
{
settings_.clear();
std::ifstream infile(filename.c_str());
if (!infile)
{
return false;
}
std::string line, key, value, section;
std::map<std::string, std::string> k_v;
std::map<std::string, std::map<std::string, std::string>>::iterator it;
while (getline(infile, line))
{
if (AnalyseLine(line, section, key, value))
{
it = settings_.find(section);
if (it != settings_.end())
{
k_v[key] = value;
it->second = k_v;
}
else
{
k_v.clear();
settings_.insert(std::make_pair(section, k_v));
}
}
key.clear();
value.clear();
}
infile.close();
return true;
}
std::string Config::ReadString(const char *section, const char *item, const char *default_value)
{
std::string tmp_s(section);
std::string tmp_i(item);
std::string def(default_value);
std::map<std::string, std::string> k_v;
std::map<std::string, std::string>::iterator it_item;
std::map<std::string, std::map<std::string, std::string>>::iterator it;
it = settings_.find(tmp_s);
if (it == settings_.end())
{
return def;
}
k_v = it->second;
it_item = k_v.find(tmp_i);
if (it_item == k_v.end())
{
return def;
}
return it_item->second;
}
int Config::ReadInt(const char *section, const char *item, const int &default_value)
{
std::string tmp_s(section);
std::string tmp_i(item);
std::map<std::string, std::string> k_v;
std::map<std::string, std::string>::iterator it_item;
std::map<std::string, std::map<std::string, std::string>>::iterator it;
it = settings_.find(tmp_s);
if (it == settings_.end())
{
return default_value;
}
k_v = it->second;
it_item = k_v.find(tmp_i);
if (it_item == k_v.end())
{
return default_value;
}
return atoi(it_item->second.c_str());
}
float Config::ReadFloat(const char *section, const char *item, const float &default_value)
{
std::string tmp_s(section);
std::string tmp_i(item);
std::map<std::string, std::string> k_v;
std::map<std::string, std::string>::iterator it_item;
std::map<std::string, std::map<std::string, std::string>>::iterator it;
it = settings_.find(tmp_s);
if (it == settings_.end())
{
return default_value;
}
k_v = it->second;
it_item = k_v.find(tmp_i);
if (it_item == k_v.end())
{
return default_value;
}
return atof(it_item->second.c_str());
}
} // namespace rr