forked from mist-devel/mist-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini_parser.c
433 lines (374 loc) · 13.2 KB
/
ini_parser.c
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// ini_parser.c
// 2015, [email protected]
//#define INI_PARSER_TEST
//// includes ////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "ini_parser.h"
#ifndef INI_PARSER_TEST
#include "debug.h"
#include "fat_compat.h"
#endif
//// defines ////
#define INI_EOT 4 // End-Of-Transmission
#define INI_BUF_SIZE 512
#define INI_LINE_SIZE 140
#define INI_SECTION_START '['
#define INI_SECTION_END ']'
#define INI_SECTION_INVALID_ID 0
//// macros ////
#define CHAR_IS_NUM(c) (((c) >= '0') && ((c) <= '9'))
#define CHAR_IS_ALPHA_LOWER(c) (((c) >= 'a') && ((c) <= 'z'))
#define CHAR_IS_ALPHA_UPPER(c) (((c) >= 'A') && ((c) <= 'Z'))
#define CHAR_IS_ALPHA(c) (CHAR_IS_ALPHA_LOWER(c) || CHAR_IS_ALPHA_UPPER(c))
#define CHAR_IS_ALPHANUM(c) (CHAR_IS_ALPHA_LOWER(c) || CHAR_IS_ALPHA_UPPER(c) || CHAR_IS_NUM(c))
#define CHAR_IS_SPECIAL(c) (((c) == '[') || ((c) == ']') || ((c) == '-') || ((c) == '_') || ((c) == ',') || ((c) == '=') || ((c) == '~'))
#define CHAR_IS_VALID(c) (CHAR_IS_ALPHANUM(c) || CHAR_IS_SPECIAL(c))
#define CHAR_IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\r') || ((c) == '\n'))
#define CHAR_IS_SPACE(c) (((c) == ' ') || ((c) == '\t'))
#define CHAR_IS_LINEEND(c) (((c) == '\n'))
#define CHAR_IS_COMMENT(c) (((c) == ';'))
#define CHAR_IS_QUOTE(c) (((c) == '"'))
#define CHAR_TO_UPPERCASE(c) ({ char _c = (c); if (CHAR_IS_ALPHA_LOWER(_c)) _c = _c - 'a' + 'A'; _c;})
#define CHAR_TO_LOWERCASE(c) ({ char _c = (c); if (CHAR_IS_ALPHA_UPPER(_c)) _c = _c - 'A' + 'a'; _c;})
//// debug func ////
#ifdef INI_PARSER_TEST
#define ini_parser_debugf(a, ...) fprintf(stderr, a "\n", __VA_ARGS__)
#endif
//// globals ////
#ifdef INI_PARSER_TEST
FILE* ini_fp = NULL;
char sector_buffer[INI_BUF_SIZE] = {0};
int ini_size=0;
#else
FIL ini_file;
#endif
static int ini_pt = 0;
//// ini_getch() ////
static char ini_getch()
{
UINT br;
if (!(ini_pt&0x1ff)) {
// reload buffer
#ifdef INI_PARSER_TEST
fread(sector_buffer, sizeof(char), INI_BUF_SIZE, ini_fp);
#else
f_read(&ini_file, sector_buffer, 512, &br);
#endif
}
#ifdef INI_PARSER_TEST
if (ini_pt >= ini_size) return 0;
#else
if (ini_pt >= f_size(&ini_file)) return 0;
#endif
else return sector_buffer[(ini_pt++)&0x1ff];
}
//// ini_putch() ////
static void ini_putch(char c)
{
UINT bw;
sector_buffer[ini_pt++] = c;
if (!(ini_pt%0x1ff)) {
// write buffer
ini_pt = 0;
#ifdef INI_PARSER_TEST
fwrite(sector_buffer, sizeof(char), INI_BUF_SIZE, ini_fp);
#else
f_write(&ini_file, sector_buffer, INI_BUF_SIZE, &bw);
#endif
}
}
//// ini_findch() ////
static char ini_findch(char c)
{
char t;
do {
t = ini_getch();
} while ((t != 0) && (t != c));
return t;
}
//// ini_getline() ////
static int ini_getline(char* line)
{
char c;
char ignore=0;
char literal=0;
int i=0;
while(1) {
c = ini_getch();
if ((!c) || CHAR_IS_LINEEND(c)) break;
else if (CHAR_IS_QUOTE(c) && !ignore) literal ^= 1;
else if (CHAR_IS_COMMENT(c) && !ignore && !literal) ignore++;
else if ((literal || (CHAR_IS_VALID(c) && !ignore)) && i<(INI_LINE_SIZE-1)) line[i++] = c;
}
line[i] = '\0';
return c==0 ? INI_EOT : literal ? 1 : 0;
}
//// ini_putline() ////
static void ini_putline(char* line)
{
int i=0;
while(i<(INI_LINE_SIZE-1)) {
if (!line[i]) break;
ini_putch(line[i++]);
}
}
//// ini_get_section() ////
static int ini_get_section(const ini_cfg_t* cfg, char* buf, const char* alter_section)
{
int i=0;
// get section start marker
if (buf[0] != INI_SECTION_START) {
return INI_SECTION_INVALID_ID;
} else buf++;
// get section stop marker
while (1) {
if (buf[i] == INI_SECTION_END) {
buf[i] = '\0';
break;
}
i++;
if (i >= INI_LINE_SIZE) {
return INI_SECTION_INVALID_ID;
}
}
// convert to uppercase
for (i=0; i<INI_LINE_SIZE; i++) {
if (!buf[i]) break;
else buf[i] = CHAR_TO_UPPERCASE(buf[i]);
}
// parse section
for (i=0; i<cfg->nsections; i++) {
if (!strcmp(buf, cfg->sections[i].name)) {
ini_parser_debugf("Got SECTION '%s' with ID %d", buf, cfg->sections[i].id);
return cfg->sections[i].id;
}
}
if(alter_section && !strcasecmp(buf, alter_section)) {
ini_parser_debugf("Got ALTER SECTION '%s' with ID %d", buf, cfg->sections[0].id);
return cfg->sections[0].id;
}
return INI_SECTION_INVALID_ID;
}
//// ini_get_var() ////
static void* ini_get_var(const ini_cfg_t* cfg, int cur_section, char* buf, int tag)
{
int i=0, j=0;
int var_id = -1;
// find var
while(1) {
if (buf[i] == '=') {
buf[i] = '\0';
break;
} else if (buf[i] == '\0') return (void*)0;
i++;
}
// convert to uppercase
for (j=0; j<=i; j++) {
if (!buf[j]) break;
else buf[j] = CHAR_TO_UPPERCASE(buf[j]);
}
// parse var
for (j=0; j<cfg->nvars; j++) {
if ((!strcmp(buf, cfg->vars[j].name)) && (cfg->vars[j].section_id == cur_section)) var_id = j;
}
// get data
if (var_id != -1) {
ini_parser_debugf("Got VAR '%s' with VALUE %s", buf, &(buf[i+1]));
i++;
switch (cfg->vars[var_id].type) {
case UINT8:
*(uint8_t*)(cfg->vars[var_id].var) = strtoul(&(buf[i]), NULL, 0);
if (*(uint8_t*)(cfg->vars[var_id].var) > cfg->vars[var_id].max) *(uint8_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].max;
if (*(uint8_t*)(cfg->vars[var_id].var) < cfg->vars[var_id].min) *(uint8_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].min;
break;
case INT8:
*(int8_t*)(cfg->vars[var_id].var) = strtol(&(buf[i]), NULL, 0);
if (*(int8_t*)(cfg->vars[var_id].var) > (int8_t)cfg->vars[var_id].max) *(int8_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].max;
if (*(int8_t*)(cfg->vars[var_id].var) < (int8_t)cfg->vars[var_id].min) *(int8_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].min;
break;
case UINT16:
*(uint16_t*)(cfg->vars[var_id].var) = strtoul(&(buf[i]), NULL, 0);
if (*(uint16_t*)(cfg->vars[var_id].var) > cfg->vars[var_id].max) *(uint16_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].max;
if (*(uint16_t*)(cfg->vars[var_id].var) < cfg->vars[var_id].min) *(uint16_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].min;
break;
case INT16:
*(int16_t*)(cfg->vars[var_id].var) = strtol(&(buf[i]), NULL, 0);
if (*(int16_t*)(cfg->vars[var_id].var) > (int16_t)cfg->vars[var_id].max) *(int16_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].max;
if (*(int16_t*)(cfg->vars[var_id].var) < (int16_t)cfg->vars[var_id].min) *(int16_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].min;
break;
case UINT32:
*(uint32_t*)(cfg->vars[var_id].var) = strtoul(&(buf[i]), NULL, 0);
if (*(uint32_t*)(cfg->vars[var_id].var) > (uint32_t)cfg->vars[var_id].max) *(uint32_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].max;
if (*(uint32_t*)(cfg->vars[var_id].var) < (uint32_t)cfg->vars[var_id].min) *(uint32_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].min;
break;
case INT32:
*(int32_t*)(cfg->vars[var_id].var) = strtol(&(buf[i]), NULL, 0);
if (*(int32_t*)(cfg->vars[var_id].var) > (int32_t)cfg->vars[var_id].max) *(int32_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].max;
if (*(int32_t*)(cfg->vars[var_id].var) < (int32_t)cfg->vars[var_id].min) *(int32_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].min;
break;
case UINT64:
*(uint64_t*)(cfg->vars[var_id].var) = strtoull(&(buf[i]), NULL, 0);
if (*(uint64_t*)(cfg->vars[var_id].var) > (uint64_t)cfg->vars[var_id].max) *(uint64_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].max;
if (*(uint64_t*)(cfg->vars[var_id].var) < (uint64_t)cfg->vars[var_id].min) *(uint64_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].min;
break;
case INT64:
*(int64_t*)(cfg->vars[var_id].var) = strtoll(&(buf[i]), NULL, 0);
if (*(int64_t*)(cfg->vars[var_id].var) > (int64_t)cfg->vars[var_id].max) *(int64_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].max;
if (*(int64_t*)(cfg->vars[var_id].var) < (int64_t)cfg->vars[var_id].min) *(int64_t*)(cfg->vars[var_id].var) = cfg->vars[var_id].min;
break;
#ifdef INI_ENABLE_FLOAT
case FLOAT:
*(float*)(cfg->vars[var_id].var) = strtof(&(buf[i]), NULL);
if (*(float*)(cfg->vars[var_id].var) > cfg->vars[var_id].max) *(float*)(cfg->vars[var_id].var) = cfg->vars[var_id].max;
if (*(float*)(cfg->vars[var_id].var) < cfg->vars[var_id].min) *(float*)(cfg->vars[var_id].var) = cfg->vars[var_id].min;
break;
#endif
case STRING:
strncpy((char*)(cfg->vars[var_id].var), &(buf[i]), cfg->vars[var_id].max);
break;
case CUSTOM_HANDLER:
((custom_handler_t*)(cfg->vars[var_id].var))(&(buf[i]), INI_LOAD, tag);
break;
}
return (void*)(&(cfg->vars[var_id].var));
}
return (void*)0;
}
//// ini_parse() ////
void ini_parse(const ini_cfg_t* cfg, const char *alter_section, int tag)
{
char line[INI_LINE_SIZE] = {0};
int section = INI_SECTION_INVALID_ID;
int line_status;
if (alter_section)
ini_parser_debugf("Start INI parser for core \"%s\".", alter_section);
// open ini file
#ifdef INI_PARSER_TEST
if ((ini_fp = fopen(cfg->filename, "rb")) == NULL) {
#else
if (f_open(&ini_file, cfg->filename, FA_READ) != FR_OK) {
#endif
ini_parser_debugf("Can't open file %s !", cfg->filename);
return;
}
#ifdef INI_PARSER_TEST
// save size
fseek(ini_fp, 0L, SEEK_END);
ini_size = ftell(ini_fp);
fseek(ini_fp, 0L, SEEK_SET);
#endif
#ifdef INI_PARSER_TEST
ini_parser_debugf("Opened file %s with size %d bytes.", cfg->filename, ini_size);
#else
ini_parser_debugf("Opened file %s with size %llu bytes.", cfg->filename, f_size(&ini_file));
#endif
ini_pt = 0;
// parse ini
while (1) {
// get line
line_status = ini_getline(line);
ini_parser_debugf("line(%d): \"%s\".", line_status, line);
// if valid line
if (line_status != 1) {
if (line[0] == INI_SECTION_START) {
// if first char in line is INI_SECTION_START, get section
section = ini_get_section(cfg, line, alter_section);
} else {
// otherwise this is a variable, get it
ini_get_var(cfg, section, line, tag);
}
}
// if end of file, stop
if (line_status == INI_EOT) break;
}
#ifdef INI_PARSER_TEST
// close file
fclose(ini_fp);
#else
f_close(&ini_file);
#endif
}
//// ini_save() ////
void ini_save(const ini_cfg_t* cfg, int tag)
{
int section, var;
char line[INI_LINE_SIZE] = {0};
char val[INI_LINE_SIZE] = {0};
ini_pt = 0;
// open ini file
#ifdef INI_PARSER_TEST
if ((ini_fp = fopen(cfg->filename, "wb")) == NULL) {
#else
if (f_open(&ini_file, cfg->filename, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) {
#endif
ini_parser_debugf("Can't open file %s !", cfg->filename);
return;
}
// loop over sections
for (section=0; section<cfg->nsections; section++) {
ini_parser_debugf("writing section %s ...", cfg->sections[section].name);
siprintf(line, "[%s]\n", cfg->sections[section].name);
ini_putline(line);
// loop over vars
for (var=0; var<cfg->nvars; var++) {
if (cfg->vars[var].section_id == cfg->sections[section].id) {
ini_parser_debugf("writing var %s", cfg->vars[var].name);
switch (cfg->vars[var].type) {
case UINT8:
siprintf(line, "%s=%u\n", cfg->vars[var].name, *(uint8_t*)(cfg->vars[var].var));
break;
case UINT16:
siprintf(line, "%s=%u\n", cfg->vars[var].name, *(uint16_t*)(cfg->vars[var].var));
break;
case UINT32:
siprintf(line, "%s=0x%x\n", cfg->vars[var].name, *(uint32_t*)(cfg->vars[var].var));
break;
case UINT64:
siprintf(line, "%s=0x%llx\n", cfg->vars[var].name, *(uint64_t*)(cfg->vars[var].var));
break;
case INT8:
siprintf(line, "%s=%d\n", cfg->vars[var].name, *(int8_t*)(cfg->vars[var].var));
break;
case INT16:
siprintf(line, "%s=%d\n", cfg->vars[var].name, *(int16_t*)(cfg->vars[var].var));
break;
case INT32:
siprintf(line, "%s=0x%x\n", cfg->vars[var].name, *(int32_t*)(cfg->vars[var].var));
break;
#ifdef INI_ENABLE_FLOAT
case FLOAT:
siprintf(line, "%s=%f\n", cfg->vars[var].name, *(float*)(cfg->vars[var].var));
break;
#endif
case STRING:
siprintf(line, "%s=\"%s\"\n", cfg->vars[var].name, (char*)(cfg->vars[var].var));
break;
case CUSTOM_HANDLER:
while (((custom_handler_t*)(cfg->vars[var].var))(val, INI_SAVE, tag)) {
siprintf(line, "%s=\"%s\"\n", cfg->vars[var].name, val);
ini_putline(line);
};
}
if (cfg->vars[var].type != CUSTOM_HANDLER)
ini_putline(line);
}
}
}
// in case the buffer is not written yet, write it now
if (ini_pt) {
#ifdef INI_PARSER_TEST
fwrite(sector_buffer, sizeof(char), ini_pt, ini_fp);
#else
UINT bw;
f_write(&ini_file, sector_buffer, ini_pt, &bw);
#endif
}
#ifdef INI_PARSER_TEST
fclose(ini_fp);
#else
f_close(&ini_file);
#endif
}