]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/config.c
Add configuration files for client and server
[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                 fscanf(f, "%s", key);
19
20                 bool found = false;
21                 for (size_t i = 0; i < num_entries; i++) {
22                         ConfigEntry *entry = &entries[i];
23                         if (strcmp(key, entry->key) == 0) {
24                                 bool valid = false;
25
26                                 switch (entry->type) {
27                                         case CT_STRING: {
28                                                 char value[BUFSIZ];
29
30                                                 if (! fscanf(f, "%s", value))
31                                                         break;
32
33                                                 valid = true;
34                                                 *(char **) entry->value = strdup(value);
35                                         } break;
36
37                                         case CT_INT:
38                                                 if (fscanf(f, "%d", (int *) entry->value))
39                                                         valid = true;
40
41                                                 break;
42
43                                         case CT_UINT:
44                                                 if (fscanf(f, "%u", (unsigned int *) entry->value))
45                                                         valid = true;
46
47                                                 break;
48
49                                         case CT_FLOAT:
50                                                 if (fscanf(f, "%lf", (double *) entry->value))
51                                                         valid = true;
52
53                                                 break;
54
55                                         case CT_BOOL: {
56                                                 char value[BUFSIZ];
57
58                                                 if (! fscanf(f, "%s", value))
59                                                         break;
60
61                                                 valid = true;
62
63                                                 if (strcmp(value, "on") == 0 || strcmp(value, "yes") == 0 || strcmp(value, "true") == 0)
64                                                         *(bool *) entry->value = true;
65                                                 else if (strcmp(value, "off") == 0 || strcmp(value, "no") == 0 || strcmp(value, "false") == 0)
66                                                         *(bool *) entry->value = false;
67                                                 else
68                                                         valid = false;
69
70                                         } break;
71                                 }
72
73                                 if (! valid)
74                                         fprintf(stderr, "Invalid value for setting %s in %s\n", key, path);
75
76                                 found = true;
77                                 break;
78                         }
79                 }
80
81                 if (! found)
82                         fprintf(stderr, "Unknown setting %s in %s\n", key, path);
83         }
84 }