PCem
view src/config.c @ 151:55564c65aa15
Added configuration load/save options.
| author | TomW |
|---|---|
| date | Sat Aug 23 21:32:36 2014 +0100 |
| parents | f749363ad763 |
| children | d0d530adce12 |
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "config.h"
6 char config_file_default[256];
8 static char config_file[256];
10 typedef struct list_t
11 {
12 struct list_t *next;
13 } list_t;
15 static list_t config_head;
17 typedef struct section_t
18 {
19 struct list_t list;
21 char name[256];
23 struct list_t entry_head;
24 } section_t;
26 typedef struct entry_t
27 {
28 struct list_t list;
30 char name[256];
31 char data[256];
32 } entry_t;
34 #define list_add(new, head) \
35 { \
36 struct list_t *next = head; \
37 \
38 while (next->next) \
39 next = next->next; \
40 \
41 (next)->next = new; \
42 (new)->next = NULL; \
43 }
45 void config_dump()
46 {
47 section_t *current_section;
49 pclog("Config data :\n");
51 current_section = (section_t *)config_head.next;
53 while (current_section)
54 {
55 entry_t *current_entry;
57 pclog("[%s]\n", current_section->name);
59 current_entry = (entry_t *)current_section->entry_head.next;
61 while (current_entry)
62 {
63 pclog("%s = %s\n", current_entry->name, current_entry->data);
65 current_entry = (entry_t *)current_entry->list.next;
66 }
68 current_section = (section_t *)current_section->list.next;
69 }
70 }
72 void config_free()
73 {
74 section_t *current_section;
75 current_section = (section_t *)config_head.next;
77 while (current_section)
78 {
79 section_t *next_section = (section_t *)current_section->list.next;
80 entry_t *current_entry;
82 current_entry = (entry_t *)current_section->entry_head.next;
84 while (current_entry)
85 {
86 entry_t *next_entry = (entry_t *)current_entry->list.next;
88 free(current_entry);
89 current_entry = next_entry;
90 }
92 free(current_section);
93 current_section = next_section;
94 }
95 }
97 void config_load(char *fn)
98 {
99 FILE *f = fopen(fn, "rt");
100 section_t *current_section;
102 memset(&config_head, 0, sizeof(list_t));
104 current_section = malloc(sizeof(section_t));
105 memset(current_section, 0, sizeof(section_t));
106 list_add(¤t_section->list, &config_head);
108 if (!f)
109 return;
111 while (1)
112 {
113 int c;
114 char buffer[256];
116 fgets(buffer, 255, f);
117 if (feof(f)) break;
119 c = 0;
121 while (buffer[c] == ' ' && buffer[c])
122 c++;
124 if (!buffer[c]) continue;
126 if (buffer[c] == '#') /*Comment*/
127 continue;
129 if (buffer[c] == '[') /*Section*/
130 {
131 section_t *new_section;
132 char name[256];
133 int d = 0;
135 c++;
136 while (buffer[c] != ']' && buffer[c])
137 name[d++] = buffer[c++];
139 if (buffer[c] != ']')
140 continue;
141 name[d] = 0;
143 new_section = malloc(sizeof(section_t));
144 memset(new_section, 0, sizeof(section_t));
145 strncpy(new_section->name, name, 256);
146 list_add(&new_section->list, &config_head);
148 current_section = new_section;
150 // pclog("New section : %s %p\n", name, (void *)current_section);
151 }
152 else
153 {
154 entry_t *new_entry;
155 char name[256];
156 int d = 0, data_pos;
158 while (buffer[c] != '=' && buffer[c] != ' ' && buffer[c])
159 name[d++] = buffer[c++];
161 if (!buffer[c]) continue;
162 name[d] = 0;
164 while ((buffer[c] == '=' || buffer[c] == ' ') && buffer[c])
165 c++;
167 if (!buffer[c]) continue;
169 data_pos = c;
170 while (buffer[c])
171 {
172 if (buffer[c] == '\n')
173 buffer[c] = 0;
174 c++;
175 }
177 new_entry = malloc(sizeof(entry_t));
178 memset(new_entry, 0, sizeof(entry_t));
179 strncpy(new_entry->name, name, 256);
180 strncpy(new_entry->data, &buffer[data_pos], 256);
181 list_add(&new_entry->list, ¤t_section->entry_head);
183 // pclog("New data under section [%s] : %s = %s\n", current_section->name, new_entry->name, new_entry->data);
184 }
185 }
187 fclose(f);
189 config_dump();
190 }
194 void set_config_file(char *s)
195 {
196 strcpy(config_file, s);
197 }
199 void config_new()
200 {
201 FILE *f = fopen(config_file, "wt");
202 fclose(f);
203 }
205 static section_t *find_section(char *name)
206 {
207 section_t *current_section;
208 char blank[] = "";
210 current_section = (section_t *)config_head.next;
211 if (!name)
212 name = blank;
214 while (current_section)
215 {
216 if (!strncmp(current_section->name, name, 256))
217 return current_section;
219 current_section = (section_t *)current_section->list.next;
220 }
221 return NULL;
222 }
224 static entry_t *find_entry(section_t *section, char *name)
225 {
226 entry_t *current_entry;
228 current_entry = (entry_t *)section->entry_head.next;
230 while (current_entry)
231 {
232 if (!strncmp(current_entry->name, name, 256))
233 return current_entry;
235 current_entry = (entry_t *)current_entry->list.next;
236 }
237 return NULL;
238 }
240 static section_t *create_section(char *name)
241 {
242 section_t *new_section = malloc(sizeof(section_t));
244 memset(new_section, 0, sizeof(section_t));
245 strncpy(new_section->name, name, 256);
246 list_add(&new_section->list, &config_head);
248 return new_section;
249 }
251 static entry_t *create_entry(section_t *section, char *name)
252 {
253 entry_t *new_entry = malloc(sizeof(entry_t));
254 memset(new_entry, 0, sizeof(entry_t));
255 strncpy(new_entry->name, name, 256);
256 list_add(&new_entry->list, §ion->entry_head);
258 return new_entry;
259 }
261 int get_config_int(char *head, char *name, int def)
262 {
263 section_t *section;
264 entry_t *entry;
265 int value;
267 section = find_section(head);
269 if (!section)
270 return def;
272 entry = find_entry(section, name);
274 if (!entry)
275 return def;
277 sscanf(entry->data, "%i", &value);
279 return value;
280 }
282 char *get_config_string(char *head, char *name, char *def)
283 {
284 section_t *section;
285 entry_t *entry;
286 int value;
288 section = find_section(head);
290 if (!section)
291 return def;
293 entry = find_entry(section, name);
295 if (!entry)
296 return def;
298 return entry->data;
299 }
301 void set_config_int(char *head, char *name, int val)
302 {
303 section_t *section;
304 entry_t *entry;
306 section = find_section(head);
308 if (!section)
309 section = create_section(head);
311 entry = find_entry(section, name);
313 if (!entry)
314 entry = create_entry(section, name);
316 sprintf(entry->data, "%i", val);
317 }
319 void set_config_string(char *head, char *name, char *val)
320 {
321 section_t *section;
322 entry_t *entry;
324 section = find_section(head);
326 if (!section)
327 section = create_section(head);
329 entry = find_entry(section, name);
331 if (!entry)
332 entry = create_entry(section, name);
334 strncpy(entry->data, val, 256);
335 }
338 char *get_filename(char *s)
339 {
340 int c = strlen(s) - 1;
341 while (c > 0)
342 {
343 if (s[c] == '/' || s[c] == '\\')
344 return &s[c+1];
345 c--;
346 }
347 return s;
348 }
350 void append_filename(char *dest, char *s1, char *s2, int size)
351 {
352 sprintf(dest, "%s%s", s1, s2);
353 }
355 void put_backslash(char *s)
356 {
357 int c = strlen(s) - 1;
358 if (s[c] != '/' && s[c] != '\\')
359 s[c] = '/';
360 }
362 void config_save(char *fn)
363 {
364 FILE *f = fopen(fn, "wt");
365 section_t *current_section;
367 current_section = (section_t *)config_head.next;
369 while (current_section)
370 {
371 entry_t *current_entry;
373 if (current_section->name[0])
374 fprintf(f, "\n[%s]\n", current_section->name);
376 current_entry = (entry_t *)current_section->entry_head.next;
378 while (current_entry)
379 {
380 fprintf(f, "%s = %s\n", current_entry->name, current_entry->data);
382 current_entry = (entry_t *)current_entry->list.next;
383 }
385 current_section = (section_t *)current_section->list.next;
386 }
388 fclose(f);
389 }
