-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_parser.h
68 lines (60 loc) · 1.94 KB
/
config_parser.h
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
#pragma once
#ifdef __cplusplus // if c++
extern "C" {
#endif // end if c++
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "structures.h"
#include "dynamic_array.h"
#define __SAVE_VARIABLES_TO_FILE(to_be_saved, options_file, length){\
assert(options_file != NULL || !"File must not be NULL");\
for (size_t i = 0; i < length; i++){\
fprintf(options_file, "%s = %s\n", (char *)to_be_saved[i].a, (char *)to_be_saved[0].b);\
}\
}
__DARRAY(struct __pointer_pair*) __load_variables(FILE *options_file){
assert(options_file != NULL || !"File must not be NULL");
size_t length_of_file;
struct __pointer_pair *array_to_return = NULL;
DARRAY_MAKE(struct __pointer_pair, array_to_return);
DARRAY_RESIZE(struct __pointer_pair, array_to_return, 1024-1);
fseek(options_file, 0, SEEK_END);
length_of_file = ftell(options_file);
fseek(options_file, 0, SEEK_SET);
char *buff = (char*)malloc(sizeof(char) * length_of_file);
fread(buff, 1, length_of_file, options_file);
char *pos = buff, *end = buff + length_of_file;
while (pos < end){
char *arg, *val;
while ((*pos == ' ' || *pos == '\n' || *pos == '\t' || *pos == '\0') && pos < end) pos++;
if (*pos == '#'){
pos++;
while (*pos != '\n' && pos < end) pos++;
continue;
}
if (pos == end) break;
arg = pos;
while (*pos != '=' && *pos != '\n' && pos < end) pos++;
if (pos == end || *pos == '\n') {
*pos = '\0'; pos++;
val = NULL;
goto add;
}
val = pos+1;
while (*(pos-1) == ' ' && pos > arg) pos--;
if (pos == arg) arg = NULL;
else *pos = '\0';
while (*val == ' ' || *val == '\t' && val < end) val++;
pos = val;
while (*pos != '\n' && pos < end) pos++;
*pos = '\0'; pos++;
add:;
struct __pointer_pair v = {arg, val};
DARRAY_ADD(struct __pointer_pair, array_to_return, v);
}
return array_to_return;
}
#ifdef __cplusplus // if c++
}; // namespace arge parser
#endif // end if c++