-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.cpp
More file actions
214 lines (170 loc) · 4.12 KB
/
Copy pathconfig.cpp
File metadata and controls
214 lines (170 loc) · 4.12 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "config.h"
#include "log.h"
#include <iostream>
#include <algorithm>
#include <functional>
#include <fstream>
#include <stdlib.h>
Config_parser::Config_parser()
{
get_process_name();
}
void Config_parser::parse_file()
{
std::ifstream fs;
//type_it it = parse_ret.end();
fs.open(config_file_name);
if(!fs.is_open())
{
LOG_ERR("config file:%s is not exist", config_file_name.c_str());
exit(1);
}
std::string line;
while(!fs.eof())
{
std::getline(fs, line);
if(is_space_line(line) || is_comment(line))
{
continue;
}
else if (is_segment(line))
{
MAP_LOCATION map_loc;
std::string segment;
segment = get_segment(line);
parse_ret[segment] = map_loc;
}
}
fs.clear();
fs.seekg(0, std::ios::beg);
std::string seg, key, value;
while(!fs.eof())
{
std::getline(fs, line);
if(is_space_line(line) || is_comment(line) )
{
continue;
}
if(is_segment(line))
{
seg = get_segment(line);
continue;
}
if(!get_key_and_value(line, key, value))
continue;
(parse_ret[seg])[key] = value;
}
}
std::string Config_parser::get_string(const std::string &seg, const std::string &key, std::string def)
{
std::string ret;
do
{
seg_it sg_it;
value_it va_it;
if((sg_it = parse_ret.find(seg)) == parse_ret.end())
{
LOG_ERR("Parse config file no segment: %s", seg.c_str());
ret = def;
break;
}
if((va_it = sg_it->second.find(key)) == sg_it->second.end())
{
LOG_ERR("Parse config file no key: %s", key.c_str());
ret = def;
break;
}
ret = va_it->second;
}while(0);
return ret;
}
uint32_t Config_parser::get_int(const std::string &seg, const std::string &key, uint32_t def)
{
uint32_t ret;
do
{
seg_it sg_it;
value_it va_it;
if((sg_it = parse_ret.find(seg)) == parse_ret.end())
{
LOG_ERR("Parse config file no segment: %s", seg.c_str());
ret = def;
break;
}
if((va_it = sg_it->second.find(key)) == sg_it->second.end())
{
LOG_ERR("Parse config file no key: %s", key.c_str());
ret = def;
break;
}
ret = atol(va_it->second.c_str());
}while(0);
return ret;
}
bool Config_parser::is_space_line(std::string line)
{
line.erase(0, line.find_first_not_of(" \t\f\v\n\r"));
return line.length() == 0?true:false;
}
void Config_parser::trim_space(std::string &dest_str)
{
dest_str.erase(0, dest_str.find_first_not_of(" \t\f\v\n\r"));
dest_str.erase(dest_str.find_last_not_of(" \t\f\v\n\r")+1);
}
bool Config_parser::is_segment(std::string line)
{
trim_space(line);
if((start_with(line, "[")) && (end_with(line, "]")) ) return true;
else return false;
}
bool Config_parser::is_comment(std::string line)
{
trim_space(line);
return start_with(line, "#");
}
bool Config_parser::get_key_and_value(std::string & line, std::string & key, std::string & value)
{
size_t pos = line.find('=');//找到每行的“=”号位置,之前是key之后是value
if(pos==std::string::npos) return false;
key = line.substr(0,pos);//取=号之前
trim_space(key);
value = line.substr(pos+1);//取=号之后
trim_space(value);
if(key.length() == 0) return false;
return true;
}
std::string Config_parser::get_segment( std::string& line)
{
line.erase(std::remove_if(line.begin(),line.end(),
std::bind2nd(std::equal_to<char>(), '[')),
line.end());
line.erase(std::remove_if(line.begin(),line.end(),
std::bind2nd(std::equal_to<char>(), ']')),
line.end());
return line;
}
bool Config_parser::start_with(const std::string& dst_str, const std::string& targ)
{
return dst_str.compare(0, targ.size(), targ) == 0;
}
bool Config_parser::end_with(const std::string& dst_str, const std::string& targ)
{
return dst_str.compare(dst_str.size() - targ.size(), targ.size(), targ) == 0 ;
}
void Config_parser::get_process_name()
{
char buf[1024] = { 0 };
char name[64] = { 0 };
int n;
n = readlink("/proc/self/exe" , buf , sizeof(buf));
buf[n] = '\0';
for(n=n-1;n>0;n--)
{
if(buf[n] == '/')
break;
}
strncpy(name, buf + n + 1 , strlen(buf+n) - 1);
config_file_name ="./";
config_file_name += name;
config_file_name += ".conf";
}