-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
413 lines (365 loc) · 12.9 KB
/
config.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
#include <stdlib.h>
#include <memory.h>
#include <stdio.h>
#include <errno.h>
#include "config.h"
#include "utils.h"
struct config* config_new() {
struct config* conf = malloc(sizeof(struct config));
return conf;
}
void config_load(struct config* conf, char* filename) {
FILE* file = fopen(filename, "r");
if(!file && errno == ENOENT)
file = fopen(filename, "w+");
if(!file) {
perror("Config file opening failed");
return;
}
char* buf = calloc(sizeof(char *), 1024);
while (fgets(buf, 1024, file) != NULL) {
if (strlen(buf) > 0) {
int state = 0;
char* ptr = buf - 1;
char* conf_path = calloc(sizeof(char), 768);
char* path_ptr = conf_path;
char* conf_value = calloc(sizeof(char), 256);
char* val_ptr = conf_value;
int conf_type = TYPE_STRING;
while (*(++ptr) != 0) {
if (state == 0 && *ptr != ' ')
state = 1;
if (state == 1 && *ptr == ' ')
state = 2;
if ((state == 1 || state == 2) && *ptr == '=') {
state = 3;
ptr++;
}
if (state == 1)
*(path_ptr++) = *(ptr);
if (state == 3 && *ptr != ' ')
state = 4;
if (state == 5 && (*ptr == '\r' || *ptr == '\n'))
break;
// It's a number!
if (state == 4 && ('0' <= *ptr) && (*ptr <= '9')) {
conf_type = TYPE_INT;
state = 5;
}
if (state == 4 && *ptr == 'l') {
conf_type = TYPE_LONG;
state = 5;
ptr++;
}
if (state == 5 && (conf_type == TYPE_INT || conf_type == TYPE_LONG)) {
if (('0' <= *ptr) && (*ptr <= '9'))
*(val_ptr++) = *ptr;
else {
state = -1; // Non-integer character when reading integer type
break; // No sense in parsing further if there's already error
}
}
// It's a string!
if (state == 4 && *ptr == '"') {
state = 5;
ptr++;
}
if (state == 5
&& conf_type == TYPE_STRING
&& ((*(ptr + 1) == '\0')
|| (*(ptr + 1) == '\r')
|| (*(ptr + 1) == '\n')
)) {
if (*ptr != '"')
state = -2; // String is not terminated with double-quote
break;
}
if (state == 5 && conf_type == TYPE_STRING)
*(val_ptr++) = *ptr;
if (state == 4 && *ptr != ' ') {
state = -3; // Unable to determine type
break;
}
}
if (state == 5) {
union config_data data;
data.as_ptr = NULL;
if (conf_type == TYPE_INT) {
data.as_int = malloc(sizeof(int));
*(data.as_int) = atoi(conf_value);
}
if (conf_type == TYPE_LONG) {
data.as_long = malloc(sizeof(long));
*(data.as_long) = atol(conf_value);
}
if (conf_type == TYPE_STRING) {
size_t len = strlen(conf_value);
data.as_string = calloc(sizeof(char), len + 1);
strcpy(data.as_string, conf_value);
}
config_value_set(conf, conf_path, conf_type, data);
} else
printf("C> Error %i parsing config entry: %s\n", -state, buf);
free(conf_value);
free(conf_path);
}
}
fclose(file);
free(buf);
}
int config_save(struct config* config, char* filename) {
FILE* file = fopen(filename, "w");
if (file == 0) {
perror("Config file opening failed");
return EOF;
}
int ret = config_save_section(config->data, file);
fclose(file);
return ret;
}
int config_save_section(struct config_block *block, FILE* file) {
while (block != NULL) {
char* section = config_value_path(block->parent);
for (int i = 0; i < CONFIG_BLOCK_SIZE; i++) {
struct config_value* val = block->data[i];
if (val != NULL) {
if (val->type != TYPE_SECTION) {
fputs(section, file);
if (block->parent != NULL)
fputc('.', file);
fputs(val->name, file);
fputs(" = ", file);
char* buf = calloc(sizeof(char), 32);
switch(val->type) {
case TYPE_INT:
fputs(itoa(*(val->data.as_int), buf, 10), file);
break;
case TYPE_LONG:
fputc('l', file);
fputs(ltoa(*(val->data.as_long), buf, 10), file);
break;
case TYPE_STRING:
fputc('"', file);
fputs(val->data.as_string, file);
fputc('"', file);
default:
break;
}
free(buf);
fputc('\n', file);
fflush(file);
} else
config_save_section(val->data.as_section, file);
}
}
free(section);
block = block->next;
}
return 0;
}
void config_free(struct config *config) {
config_section_free(config->data);
free(config);
}
void config_section_data_free(struct config_block* block) {
for (int i = 0; i < CONFIG_BLOCK_SIZE; i++) {
if (block->data[i] != NULL) {
if (block->data[i]->type != TYPE_SECTION) {
free(block->data[i]->data.as_ptr);
} else {
config_section_free(block->data[i]->data.as_section);
}
free(block->data[i]->name);
free(block->data[i]);
}
}
free(block->data);
}
void config_section_free(struct config_block* block) {
struct config_block* next = block;
while (next != NULL) {
struct config_block* freeing = next;
next = freeing->next;
config_section_data_free(freeing);
free(freeing);
}
}
struct config_value* config_value_add(struct config *config, char *path, int type, union config_data data) {
struct config_value* ret = NULL;
struct config_block** block = &(config->data);
char **word = config_explode_path(path);
int error = 0;
for(int i = 0; (i < 32) && (word[i] != 0); i++) {
if (error == 0) {
struct config_value *val = config_section_get_value(*block, word[i]);
if (val != NULL)
if (val->type != TYPE_SECTION) {
printf("C> Attempt to add children value to a non-section value\n");
ret = NULL; // WE HAVE SOME PROBLEMS PLEASE STAND BY?!
error = 1;
} else
block = (struct config_block **) &(val->data);
else
if (word[i + 1] == NULL)
ret = config_section_add_value(block, word[i], type, data);
else {
struct config_value *v = config_section_add_value(block, word[i], TYPE_SECTION, (union config_data) {NULL});
block = &(v->data.as_section);
}
}
free(word[i]);
}
free(word);
return ret;
}
struct config_value* config_value_set(struct config *config, char* path, int type, union config_data data) {
struct config_value* val = config_value_get(config, path);
if (val != 0) {
free(val->data.as_ptr);
if (type != TYPE_NULL)
val->type = type;
val->data = data;
} else
val = config_value_add(config, path, type, data);
if (val == 0) //Things went wrong.
return 0;
return val;
}
struct config_value* config_value_get(struct config *config, char *path) {
struct config_value* ret = 0;
struct config_block* block = config->data;
char **word = config_explode_path(path);
for(int i = 0; (i < 32) && (word[i] != 0); i++) {
struct config_value* val = config_section_get_value(block, word[i]);
if (val != NULL) {
ret = val;
if (val->type == TYPE_SECTION)
block = val->data.as_section;
} else
ret = NULL;
free(word[i]);
}
free(word);
char *valpath = config_value_path(ret);
if (strcmp(valpath, path) != 0)
ret = NULL;
free(valpath);
return ret;
}
struct config_value* config_value_get_soft(struct config *config, char *path, int type, union config_data failsafe) {
struct config_value* ret = config_value_get(config, path);
if (ret == 0)
ret = config_value_add(config, path, type, failsafe);
else {
if (ret->type != type) {
config_value_set(config, path, type, failsafe);
}
}
return ret;
}
int config_value_type(struct config_value* value) {
return value->type;
}
char** config_explode_path(char *path) {
char** word = calloc(sizeof(char *), 32);
char* ptr = path;
memset(word, 0, sizeof(char *) * 32);
word[0] = calloc(sizeof(char), 32);
memset(word[0], 0, sizeof(char) * 32);
int words = 0;
int w = 0;
while (*ptr != '\0') {
if (*ptr == '.') {
words++;
word[words] = calloc(sizeof(char), 32);
memset(word[words], 0, sizeof(char) * 32);
w = 0;
} else
word[words][w++] = *ptr;
ptr++;
}
return word;
}
char* config_value_path(struct config_value* value) {
char **path = calloc(sizeof(char *), 32);
int i = 0;
size_t l = 2;
while (value != 0) {
size_t len = strlen(value->name);
path[i] = calloc(sizeof(char *), len + 2);
memset(path[i], 0, len);
memcpy(path[i], value->name, len);
l += len + 1;
i += 1;
if (value->parent != 0)
value = value->parent->parent;
else
value = 0;
}
char *ret = calloc(sizeof(char), l);
memset(ret, 0, l);
for (i--; i >= 0; i--) {
strcat(ret, path[i]);
if (i != 0)
strcat(ret, ".");
free(path[i]);
}
free(path);
return ret;
}
struct config_value* config_section_add_value(struct config_block** start, char *name, int type, union config_data data) {
struct config_value *ret = 0;
struct config_block **next = start;
while (ret == 0) {
if (*next != 0) {
for (int i = 0; i < CONFIG_BLOCK_SIZE; i++) {
if ((*next)->data[i] == NULL) {
ret = malloc(sizeof(struct config_value));
ret->type = type;
ret->name = strdup(name);
ret->data = data;
ret->parent = (*next);
if (type == TYPE_SECTION && data.as_ptr == NULL) {
struct config_block* block = malloc(sizeof(struct config_block));
block->parent = ret;
block->next = NULL;
block->data = calloc(sizeof(struct config_value*), CONFIG_BLOCK_SIZE);
ret->data.as_section = block;
}
(*next)->data[i] = ret;
break;
}
}
next = &((*next)->next);
}
if (ret == NULL && *next == NULL) {
struct config_block* block = malloc(sizeof(struct config_block));
if (*start != NULL)
block->parent = (*start)->parent;
else //Root of config
block->parent = NULL;
block->next = NULL;
block->data = calloc(sizeof(struct config_value*), CONFIG_BLOCK_SIZE);
*next = block;
}
};
return ret;
}
struct config_value* config_section_get_value(struct config_block *block_ptr, char *singular) {
struct config_block* curr = block_ptr;
struct config_value* value = NULL;
while (curr != NULL) {
if ((value = config_block_get_value(curr, singular)) != NULL) {
return value;
}
curr = curr->next;
}
return value;
}
struct config_value* config_block_get_value(struct config_block *block, char *name) {
for (int i = 0; i < CONFIG_BLOCK_SIZE; i++) {
if (block->data[i] != NULL && strcmp(block->data[i]->name, name) == 0)
return block->data[i];
}
return 0;
}