ini_file.cpp
8.5 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* CC/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fstream>
#include "tools.h"
#include "ini_file.h"
INIFile::~INIFile() {
_file_map.clear();
}
INIFile* INIFile::get_instance() {
static INIFile mgr;
return &mgr;
}
bool INIFile::load_file(const char* file_name, section_map_t& sections, bool casesensive)const {
IVR_DEBUG("load ini file(%s)", file_name == NULL ? "" : file_name);
std::auto_ptr<std::ifstream> tmpfs(new std::ifstream(file_name));
std::ifstream& fp = *tmpfs;
if (!fp) {
return false;
}
key_map_t keys;
string filename = file_name;
if (!casesensive) {
transform(filename.begin(), filename.end(), filename.begin(), ::tolower);
}
char szbuf[1024];
string line;
bool has_read = false;
while (!fp.eof() && !fp.fail()) {//用来搞定section
if (!has_read) {
fp.getline(szbuf, 1024);
if (szbuf[0] == '#' || szbuf[0] == ';' || szbuf[0] == '\0') {
continue;
}
line = szbuf;
ivr_tools_t::trim(line);
} else {
has_read = false;
}
if (line.size() == 0) {
continue;
}
uint32_t first_pos = line.find_first_not_of(' ');
if (line[first_pos] != '[') {
continue;
}
uint32_t last_pos = line.find_last_not_of(' ');
if (line[last_pos] != ']') {
continue;
}
if (last_pos - first_pos <= 1) {
continue;
}
string section = line.substr(first_pos + 1, last_pos - first_pos - 1);
ivr_tools_t::trim(section);
if (!casesensive) {
transform(section.begin(), section.end(), section.begin(), ::tolower);
}
keys.clear();
while (!fp.eof() && !fp.fail()) {//用来搞定section内部的key:value
fp.getline(szbuf, 1024);
if (szbuf[0] == '#' || szbuf[0] == ';' || szbuf[0] == '\0') {
continue;
}
line = szbuf;
ivr_tools_t::trim(line);
// if(!casesensive){
// transform(line.begin(), line.end(), line.begin(), ::tolower);
// }
if (line.size() == 0) {
continue;
}
first_pos = line.find_first_not_of(' ');
if (line[first_pos] == '=') {
continue;
}
if (line[first_pos] == '[') {
has_read = true;
break;
}
last_pos = line.find_last_not_of(' ');
if (last_pos - first_pos <= 1) {
continue;
}
int32_t pos0 = line.find_first_of('=');
if (pos0 == -1) {
continue;
}
int32_t pos = line.find_last_not_of(' ', pos0 - 1);
string key = line.substr(first_pos, pos - first_pos + 1);
if (!casesensive) {
transform(key.begin(), key.end(), key.begin(), ::tolower);
}
pos = line.find_first_not_of(' ', pos0 + 1);
string value;
if (pos == -1) {
value = "";
} else {
value = line.substr(pos);
}
ivr_tools_t::trim(key);
ivr_tools_t::trim(value);
keys[key] = value;
}
sections[section] = keys;
}
fp.close();
return true;
/*
if(sections.size()){
_file_map[filename]=sections;
}
*/
}
bool INIFile::load_file(const char* file_name, bool casesensive) {
unload_file(file_name, casesensive);
section_map_t sections;
if (load_file(file_name, sections, casesensive)) {
if (sections.size()) {
_file_map[file_name] = sections;
}
}
return _file_map.size();
}
void INIFile::unload_file(const char* file_name, bool casesensive) {
string filename = file_name;
if (!casesensive) {
transform(filename.begin(), filename.end(), filename.begin(), ::tolower);
}
if (_file_map.find(filename) == _file_map.end()) {
return;
}
section_map_t& sections = _file_map[filename];
section_map_t::iterator it = sections.begin();
for (; it != sections.end(); it++) {
it->second.clear();
}
sections.clear();
_file_map.erase(filename);
}
int32_t INIFile::get_int(
const char* section_name,
const char* key_name,
int default_value,
const section_map_t& sections,
bool casesensive)const {
int rt = default_value;
std::string tmp = section_name;
if (!casesensive) {
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
}
section_map_t::const_iterator it = sections.find(tmp);
if (it == sections.end()) {
return rt;
}
const key_map_t& keys = it->second;
tmp = key_name;
if (!casesensive) {
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
}
key_map_t::const_iterator it2 = keys.find(tmp);
if (it2 == keys.end()) {
return rt;
}
if (is_number(it2->second.c_str())) {
rt = atoi(it2->second.c_str());
}
return rt;
}
int32_t INIFile::get_int(
const char* section_name,
const char* key_name,
int default_value,
const char* file_name,
bool casesensive) {
int rt = default_value;
string tmp = file_name;
if (!casesensive) {
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
}
if (_file_map.find(tmp) == _file_map.end()) {
return rt;
}
section_map_t& sections = _file_map[tmp];
return get_int(section_name, key_name, default_value, sections, casesensive);
}
int32_t INIFile::get_string(
const char* section_name,
const char* key_name,
const char* default_value,
char* dest_buff,
uint32_t buff_size,
const section_map_t& sections,
bool casesensive) const {
std::string tmp = section_name;
if (!casesensive) {
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
}
section_map_t::const_iterator it = sections.find(tmp);
if (it == sections.end()) {
strcpy(dest_buff, default_value);
return 0;
}
const key_map_t& keys = it->second;
tmp = key_name;
if (!casesensive) {
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
}
key_map_t::const_iterator it2 = keys.find(tmp);
if (it2 == keys.end()) {
strcpy(dest_buff, default_value);
return 0;
}
const string& value = it2->second;
uint32_t len = value.length();
if (buff_size <= len) {
strcpy(dest_buff, value.substr(0, buff_size - 1).c_str());
len = buff_size - 1;
} else {
strcpy(dest_buff, value.c_str());
}
return len;
}
int32_t INIFile::get_string(
const char* section_name,
const char* key_name,
const char* default_value,
char* dest_buff,
uint32_t buff_size,
const char* file_name,
bool casesensive
) {
string tmp = file_name;
if (!casesensive) {
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
}
if (_file_map.find(tmp) == _file_map.end()) {
strcpy(dest_buff, default_value);
return 0;
}
section_map_t& sections = _file_map[tmp];
return get_string(section_name, key_name, default_value, dest_buff, buff_size, sections,
casesensive);
}
section_map_t INIFile::get_section_map(const char* file_name, bool casesensive) {
string tmp = file_name;
if (!casesensive) {
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
}
if (_file_map.find(tmp) != _file_map.end()) {
return _file_map[tmp];
}
return section_map_t();
}
bool INIFile::is_number(const char* str) const {
const char* pstr = str;
while (pstr && (*pstr)) {
if ((*pstr) >= '0' && (*pstr) <= '9') {
++pstr;
continue;
} else {
break;
}
}
return (pstr && 0 == (*pstr));
}