-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_parse_config.hpp
More file actions
331 lines (293 loc) · 8.79 KB
/
Copy pathcpp_parse_config.hpp
File metadata and controls
331 lines (293 loc) · 8.79 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
/*
* cpp_parse_config.h
*
* This is C++ one header file config file parser
* for work with simple key="value" per line styled
* config files.
* Parse config file and return std::unordered_map
* of paired std::string ("options"=>"value")
*
* See usage example at the end of file.
*
* Licensed under GNU General Public License v3
*
* Author: Kuzin Andrey <kuzinandrey@yandex.ru>
* (c) 2021
*/
#ifndef CPP_PARSE_CONFIG_H
#define CPP_PARSE_CONFIG_H
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
// some return error codes
#define CONFERR_NORET -1 // no valid pointer to return container
#define CONFERR_ERRFILE -2 // can't open config file
#define CONFERR_WRONGSYNTAX -3 // wrong syntax of config file
#define CONFERR_WRONGPARAM -4 // wrong parameter name
#define CONFERR_WRONGVALUE -5 // wrong parameter value
#ifndef CONF_PARAM_NAME_MAX_LEN
#define CONF_PARAM_NAME_MAX_LEN 30 // max length of parameter (buffer size)
#endif
#ifndef CONF_PARAM_VALUE_MAX_LEN
#define CONF_PARAM_VALUE_MAX_LEN 255 // max length of value (buffer size)
#endif
// Parse config file file_name and fill the unordered_map of strings "option"=>"value"
// return 0 on success or some error code
int parse_config(std::string file_name, std::unordered_map<std::string,std::string> *ret) {
if (!ret) return CONFERR_NORET;
std::ifstream fconf(file_name);
if (!fconf) return CONFERR_ERRFILE;
char param_name[CONF_PARAM_NAME_MAX_LEN];
char param_value[CONF_PARAM_VALUE_MAX_LEN];
int name_fill = 0;
int value_fill = 0;
char c;
int line = 1;
enum parse_mode {
parse_skip_space
, parse_skip_comment_line
, parse_param_name
, parse_skip_space_before_equal
, parse_skip_space_after_equal
, parse_value
, parse_line_end
, parse_value_in_single_quote
, parse_value_in_double_quote
};
parse_mode mode = parse_skip_space;
while (fconf.get(c)) {
if (c == EOF) {
if (mode == parse_value_in_single_quote || mode == parse_value_in_double_quote) {
param_value[value_fill] = 0;
ret->insert(std::make_pair(param_name,param_value));
}
break;
}
switch(mode) {
case parse_skip_space:
if (c == '#') { mode = parse_skip_comment_line; continue; }
if (c == '\n') { line++; continue; }
if (isalpha(c)) { // param name begin
param_name[0] = c;
name_fill = 1;
mode = parse_param_name;
continue;
}
if (!isspace(c)) {
std::cerr << "Error in " << file_name
<< ": param name can't start with not alpha char '"
<< c << "' on line " << line << std::endl;
fconf.close(); ret->clear();
return CONFERR_WRONGPARAM;
}
break; // parse_skip_space
case parse_skip_comment_line:
if (c == '\n') {
line++;
mode = parse_skip_space;
}
break; // parse_skip_comment_line
case parse_param_name:
if (isspace(c)) { // name end
if (c == '\n') line++;
param_name[name_fill] = 0;
name_fill++;
mode = parse_skip_space_before_equal;
continue;
}
if (c == '=') {
param_name[name_fill] = 0;
name_fill++;
mode = parse_skip_space_after_equal;
continue;
}
if (isalpha(c) || isdigit(c) || c == '_') {
param_name[name_fill] = c;
if (name_fill + 1 > CONF_PARAM_NAME_MAX_LEN - 1) {
std::cerr << "Error in " << file_name
<< ": param length is very big on " << line
<< " line" << std::endl;
fconf.close(); ret->clear();
return CONFERR_WRONGPARAM;
}
name_fill++;
continue;
} else {
std::cerr << "Error in " << file_name
<< ": wrong char in param name '" << c << "' on "
<< line << " line" << std::endl;
fconf.close(); ret->clear();
return CONFERR_WRONGPARAM;
}
break; // parse_param_name
case parse_skip_space_before_equal:
if (isspace(c)) {
if (c == '\n') line++;
continue;
}
if (c == '=') {
mode = parse_skip_space_after_equal;
}
break; // parse_skip_space_before_equal
case parse_skip_space_after_equal:
if (isspace(c)) { if (c == '\n') line++; continue; }
if (c == '\'') { mode = parse_value_in_single_quote; value_fill = 0; continue; }
if (c == '"') { mode = parse_value_in_double_quote; value_fill = 0; continue; }
if (c == '#') { // empty param value (comment line)
value_fill = 0;
param_value[value_fill] = 0;
ret->insert(std::make_pair(param_name,param_value));
mode = parse_skip_comment_line;
continue;
}
param_value[0] = c;
value_fill = 1;
mode = parse_value;
break; // parse_skip_space_after_equal
case parse_value:
if (isspace(c) || c == '#') {
mode = parse_line_end;
if (c == '#') mode = parse_skip_comment_line;
param_value[value_fill] = 0;
value_fill++;
ret->insert(std::make_pair(param_name,param_value));
if (c == '\n') { line++; mode = parse_skip_space; }
continue;
}
param_value[value_fill] = c;
if (value_fill + 1 > CONF_PARAM_VALUE_MAX_LEN - 1) {
std::cerr << "Error in " << file_name << ": value length is very big on " << line << " line" << std::endl;
fconf.close(); ret->clear();
return CONFERR_WRONGVALUE;
}
value_fill++;
break; // parse_value
case parse_line_end:
if (isspace(c)) {
if (c == '\n') { line++; mode = parse_skip_space; }
continue;
}
if (c == '#') {
mode = parse_skip_comment_line;
continue;
}
std::cerr << "Error in " << file_name
<< ": wrong char '" << c
<< "' on " << line << " line" << std::endl;
fconf.close(); ret->clear();
return CONFERR_WRONGSYNTAX;
break; // parse_line_end
case parse_value_in_single_quote:
if (c != '\'' ){
if (c == '\n') line++;
param_value[value_fill] = c;
if (value_fill + 1 > CONF_PARAM_VALUE_MAX_LEN - 1) {
std::cerr << "Error in " << file_name
<< ": value length is very big on"
<< line << " line" << std::endl;
fconf.close(); ret->clear();
return CONFERR_WRONGVALUE;
}
value_fill++;
continue;
}
param_value[value_fill] = 0;
value_fill++;
ret->insert(std::make_pair(param_name, param_value));
mode = parse_skip_space;
break; // parse_value_in_single_quote
case parse_value_in_double_quote:
if (c != '\"' ){
if (c == '\n') line++;
param_value[value_fill] = c;
if (value_fill + 1 > CONF_PARAM_VALUE_MAX_LEN - 1) {
std::cerr << "Error in " << file_name
<< ": value length is very big on "
<< line << " line" << std::endl;
fconf.close(); ret->clear();
return CONFERR_WRONGVALUE;
}
value_fill++;
continue;
}
param_value[value_fill] = 0;
value_fill++;
ret->insert(std::make_pair(param_name, param_value));
mode = parse_skip_space;
break; // parse_value_in_double_quote
} // switch
} // while read conf
return 0; // success
} // parse_config()
/*
#include <vector>
// Example of usage
int main() {
std::string config_file_name = "test.conf";
// define variables for store options from config file
// set default values for all options
std::string mysql_host = "127.0.0.1";
std::string mysql_user = "root";
std::string mysql_password = "strongrootpassword";
std::string mysql_database = "database";
// make list of known options and variables for program
struct config_options_st {
std::string name;
std::string *var_ptr;
};
std::vector<struct config_options_st> conf_params = {
{"host", &mysql_host}
,{"user", &mysql_user}
,{"password", &mysql_password}
,{"database", &mysql_database}
};
// result of parsing container
std::unordered_map<std::string, std::string> conf;
std::ofstream outconf(config_file_name);
if (outconf) {
outconf
<< "# this is config file example" << std::endl
<< "host=\"mysql.example.com\" # this is SQL host" << std::endl
<< "user = 'dba_admin'" << std::endl
<< "password = helloworld # test comment" << std::endl
<< "database=testdb123" << std::endl
// << "unknown = 'test unknown value' # uncomment this line for test" << std::endl
;
outconf.close();
} else {
std::cerr << "Can't create config file " << config_file_name << std::endl;
return -1;
}
// call parser
if (parse_config(config_file_name, &conf) == 0) {
for (auto c = conf.begin(); c != conf.end(); c++) {
std::cout << "param=" << c->first << " value=" << c->second << std::endl;
bool found = false;
for (auto i = conf_params.begin(); i != conf_params.end(); i++) {
if (i->name.compare(c->first) == 0) {
*i->var_ptr = c->second;
found = true; break;
};
}
if (!found) {
std::cerr << "Unknown parameter \"" << c->first << "\"" << std::endl;
return -1;
}
}
} else {
std::cerr << "Can't parse config file " << config_file_name << std::endl;
return -1;
}
conf.clear();
std::cout << std::endl
<< "MySQL config" << std::endl
<< "- host: " << mysql_host << std::endl
<< "- user: " << mysql_user << std::endl
<< "- password: " << mysql_password << std::endl
<< "- database: " << mysql_database << std::endl
;
return 0;
}
*/
#endif /* CPP_PARSE_CONFIG_H*/