-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconf.cpp
More file actions
267 lines (248 loc) · 7.81 KB
/
conf.cpp
File metadata and controls
267 lines (248 loc) · 7.81 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "conf.h"
#include <fstream>
#include <cctype>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/regex.hpp>
using namespace cpputil::program;
using namespace boost::filesystem;
using namespace std;
enum ParseState {
KEY,
VALUE,
COMMENT,
ESCAPING,
DEFAULT
};
static bool is_terminator(int c){
if(c == ':' || c == '='){
return true;
}
return isspace(c);
}
void Conf::parse(const string & filename, map< string, string > & options){
path filepath(filename);
filepath = system_complete(filepath);
if(is_symlink(symlink_status(filepath))){
//get target file path when filepath is a symlink
char buf[1024];
size_t len = readlink(filepath.string().c_str(), buf, sizeof(buf));
buf[len] = '\0';
filepath = path(string(buf));
}
path dir = filepath.parent_path();
std::ifstream file(filepath.string().c_str());
if(!file){
_messages.push_back("fail to open file: " + filename);
return;
}
const int32_t buf_len = 4096;
char buf[buf_len];
string token = "";
ParseState pre_state = DEFAULT;
ParseState state = DEFAULT;
string key;
int32_t line_num = 1;
int32_t colum_num = 0;
bool new_line = false;
while(!file.eof()){
file.read(buf, buf_len);
int32_t count = file.gcount();
for(int32_t i = 0; i < count; i++){
if(new_line){
line_num++;
colum_num = 0;
new_line = false;
}
colum_num++;
if(buf[i] == '\n'){
new_line = true;
}
if( state == ESCAPING ){//leave escaping state
if(buf[i] == '\n'){
//escaping for line continuation
state = pre_state;
}else{
token.push_back(buf[i]);
state = pre_state;
}
continue;
}
if(buf[i] == '\\'){//enter escaping state
pre_state = state;
state = ESCAPING;
continue;
}
if(buf[i] == '#' || buf[i] == ';'){//comment state
pre_state = state;
state = COMMENT;
continue;
}
if(buf[i] == '\n'){//meet line end
if(key.length() == 0 && token.length() && state == KEY){
key = token;
token = "";
}
boost::trim(token);
if(key.length() > 0){
if(key =="include"){
if(!boost::starts_with(token, "/")){
//an relative path, relative to current dir
path abspath = path(dir);
abspath = abspath / token;
token = abspath.string();
}
parse(token, options);
}else{
options[key] = token;
}
}
token = "";
key = "";
pre_state = VALUE;
state = DEFAULT;
continue;
}
if(state == COMMENT){
//ignore everything in COMMENT state
continue;
}
if(is_terminator(buf[i])){
if(state == KEY){//leave KEY state
key = token;
token = "";
pre_state = state;
state = DEFAULT;
}else if(state == VALUE){//leave VALUE state
token.push_back(buf[i]);
//pre_state = state;
//state = DEFAULT;
//if(key == "include"){//recursive parsing for include
// parse(token, options);
//}else{
// options[key] = token;
//}
//key = "";
//token = "";
}
continue;
}else{
if(state == DEFAULT){
//need to do state transition
if(pre_state == KEY){
pre_state = state;
state = VALUE;
}else if(pre_state == KEY){
//meet extra character after value, ignore it
continue;
}else{
state = KEY;
}
}
char c = buf[i];
if(state == KEY){
c = tolower(c);
}
token.push_back(c);
}
}
}
if(key.length() == 0 && token.length() > 0 && state == KEY){
key = token;
token = "";
}
if(key.length() > 0){
if(key =="include"){
if(!boost::starts_with(token, "/")){
//an relative path, relative to current dir
path abspath = path(dir);
abspath = abspath / token;
token = abspath.string();
}
parse(token, options);
}else{
options[key] = token;
}
}
file.close();
// options = cpputil::consul::translate_conf(options, filename);
}
string Conf::get(const string & key, const string & default_value) const {
string find_key;
find_key.reserve(key.length());
for(uint32_t i = 0; i < key.length(); i++){
find_key.push_back(tolower(key[i]));
}
map< string, string >::const_iterator itr = _options.find(find_key);
if(itr != _options.end()){
string val = itr->second;
return val;
}else{
return default_value;
}
}
// 仅供接口兼容使用
string Conf::get(const string & key, const string & default_value, bool resolve_ref) {
if (!resolve_ref) {
return get(key, default_value);
} else {
resolve_reference(key);
return get(key, default_value);
}
}
void Conf::resolve_reference(){
for(map<string, string>::iterator itr = _options.begin();
itr != _options.end();
itr++){
resolve_reference(itr->first);
}
}
void Conf::resolve_reference(const string & key) {
string find_key;
find_key.reserve(key.length());
for(uint32_t i = 0; i < key.length(); i++){
find_key.push_back(tolower(key[i]));
}
map< string, string >::iterator itr = _options.find(find_key);
if(itr != _options.end()){
string val = itr->second;
const boost::regex ref_regex = boost::regex("\\{\\{(.*?)\\}\\}",
boost::regex_constants::icase);
while(1){
boost::smatch what;
bool has_ref = boost::regex_search(val, what, ref_regex);
if(has_ref){
string ref_val = what.str(1);
boost::trim(ref_val);
if(ref_val != find_key){
val = what.prefix() + this->get(ref_val) + what.suffix();
}
}else{
break;
}
}
itr->second = val;
}
}
vector< string > Conf::get_values(const string & key, bool clean_empty) const {
string val = get(key);
vector< string > vals;
boost::split(vals, val, boost::is_any_of(","));
for(uint32_t i = 0; i < vals.size(); i++){
boost::trim(vals[i]);
}
if (clean_empty){
vector<string> res;
copy_if(vals.begin(), vals.end(), back_inserter(res), [](const string& x){return !x.empty();});
return res;
} else {
return vals;
}
}
map< string, string > Conf::get_all() const {
map< string, string > all_options = _options;
return all_options;
}