]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/config.c
Fix snapshot build error
[dragonblocks_alpha.git] / src / config.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdbool.h>
5 #include "config.h"
6
7 void config_read(char *path, ConfigEntry *entries, size_t num_entries)
8 {
9         FILE *f = fopen(path, "r");
10
11         if (! f)
12                 return;
13
14         printf("Reading config from %s\n", path);
15
16         while (! feof(f)) {
17                 char key[BUFSIZ];
18                 if (! fscanf(f, "%s", key))
19                         break;
20
21                 bool found = false;
22                 for (size_t i = 0; i < num_entries; i++) {
23                         ConfigEntry *entry = &entries[i];
24                         if (strcmp(key, entry->key) == 0) {
25                                 bool valid = false;
26
27                                 switch (entry->type) {
28                                         case CT_STRING: {
29                                                 char value[BUFSIZ];
30
31                                                 if (! fscanf(f, "%s", value))
32                                                         break;
33
34                                                 valid = true;
35                                                 *(char **) entry->value = strdup(value);
36                                         } break;
37
38                                         case CT_INT:
39                                                 if (fscanf(f, "%d", (int *) entry->value))
40                                                         valid = true;
41
42                                                 break;
43
44                                         case CT_UINT:
45                                                 if (fscanf(f, "%u", (unsigned int *) entry->value))
46                                                         valid = true;
47
48                                                 break;
49
50                                         case CT_FLOAT:
51                                                 if (fscanf(f, "%lf", (double *) entry->value))
52                                                         valid = true;
53
54                                                 break;
55
56                                         case CT_BOOL: {
57                                                 char value[BUFSIZ];
58
59                                                 if (! fscanf(f, "%s", value))
60                                                         break;
61
62                                                 valid = true;
63
64                                                 if (strcmp(value, "on") == 0 || strcmp(value, "yes") == 0 || strcmp(value, "true") == 0)
65                                                         *(bool *) entry->value = true;
66                                                 else if (strcmp(value, "off") == 0 || strcmp(value, "no") == 0 || strcmp(value, "false") == 0)
67                                                         *(bool *) entry->value = false;
68                                                 else
69                                                         valid = false;
70
71                                         } break;
72                                 }
73
74                                 if (! valid)
75                                         fprintf(stderr, "Invalid value for setting %s in %s\n", key, path);
76
77                                 found = true;
78                                 break;
79                         }
80                 }
81
82                 if (! found)
83                         fprintf(stderr, "Unknown setting %s in %s\n", key, path);
84         }
85 }