-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
164 lines (131 loc) · 2.63 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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "mcc.h"
#define MAXCONFIGLEN 64
struct configitem
{
char setting[MAXCONFIGLEN];
char value[MAXCONFIGLEN];
struct configitem *next;
};
static struct
{
const char *filename;
struct configitem *head;
} s_config;
static void config_free(void)
{
struct configitem *prev;
struct configitem *curr = s_config.head;
s_config.head = NULL;
while (curr != NULL)
{
prev = curr;
curr = curr->next;
free(prev);
}
}
void config_set_string(const char *key, const char *value)
{
struct configitem *item;
struct configitem **itemp = &s_config.head;
while (*itemp != NULL)
{
if (strcasecmp((*itemp)->setting, key) == 0)
{
item = *itemp;
if (value == NULL)
{
*itemp = item->next;
free(item);
}
else
{
strncpy(item->value, value, sizeof item->value);
}
return;
}
itemp = &(*itemp)->next;
}
if (value == NULL) return;
*itemp = malloc(sizeof *item);
item = *itemp;
strncpy(item->setting, key, sizeof item->setting);
strncpy(item->value, value, sizeof item->value);
item->next = NULL;
}
static void config_rehash(void)
{
FILE *f = fopen(s_config.filename, "r");
if (f == NULL) return;
config_free();
while (!feof(f))
{
char buf[256];
if (fgets(buf, sizeof buf, f) <= 0) break;
/* Ignore comments */
if (*buf == '#') continue;
char *n = strchr(buf, '=');
if (n == NULL) continue;
*n++ = '\0';
char *eol = strchr(n, '\n');
if (eol != NULL) *eol = '\0';
config_set_string(buf, n);
}
fclose(f);
}
void config_write(void)
{
FILE *f = fopen(s_config.filename, "w");
if (f == NULL) return;
struct configitem *curr;
for (curr = s_config.head; curr != NULL; curr = curr->next)
{
fprintf(f, "%s=%s\n", curr->setting, curr->value);
}
fclose(f);
}
void config_init(const char *filename)
{
s_config.filename = filename;
s_config.head = NULL;
config_rehash();
}
void config_deinit(void)
{
config_write();
config_free();
}
void config_set_int(const char *setting, int value)
{
char c[MAXCONFIGLEN];
snprintf(c, sizeof c, "%d", value);
config_set_string(setting, c);
}
bool config_get_string(const char *setting, char **value)
{
struct configitem *curr;
for (curr = s_config.head; curr != NULL; curr = curr->next)
{
if (strcasecmp(curr->setting, setting) == 0)
{
*value = curr->value;
return true;
}
}
*value = NULL;
return false;
}
bool config_get_int(const char *setting, int *value)
{
char *c, *endptr;
int v;
if (!config_get_string(setting, &c)) return false;
v = strtol(c, &endptr, 10);
if (*endptr != '\0') return false;
*value = v;
return true;
}