-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathparser.h
48 lines (41 loc) · 1.24 KB
/
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
#define CONFIG_ARG_MAX_BYTES 128
typedef struct config_option config_option;
typedef config_option* config_option_t;
struct config_option {
config_option_t prev;
char key[CONFIG_ARG_MAX_BYTES];
char value[CONFIG_ARG_MAX_BYTES];
};
config_option_t read_config_file(char* path) {
FILE* fp;
if ((fp = fopen(path, "r+")) == NULL) {
perror("fopen()");
return NULL;
}
config_option_t last_co_addr = NULL;
while(1) {
config_option_t co = NULL;
if ((co = calloc(1, sizeof(config_option))) == NULL)
continue;
memset(co, 0, sizeof(config_option));
co->prev = last_co_addr;
if (fscanf(fp, "%s = %s", &co->key[0], &co->value[0]) != 2) {
if (feof(fp)) {
break;
}
if (co->key[0] == '#') {
while (fgetc(fp) != '\n') {
// Do nothing (to move the cursor to the end of the line).
}
free(co);
continue;
}
perror("fscanf()");
free(co);
continue;
}
//printf("Key: %s\nValue: %s\n", co->key, co->value);
last_co_addr = co;
}
return last_co_addr;
}