PCem

view src/config.c @ 154:d0d530adce12

Initial port to Linux (using Allegro). 64-bit fixes. Some changes to aid portability. A few other tweaks.
author TomW
date Thu Sep 04 21:07:24 2014 +0100
parents 55564c65aa15
children
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(&current_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, &current_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 config_new()
195 {
196 FILE *f = fopen(config_file, "wt");
197 fclose(f);
198 }
200 static section_t *find_section(char *name)
201 {
202 section_t *current_section;
203 char blank[] = "";
205 current_section = (section_t *)config_head.next;
206 if (!name)
207 name = blank;
209 while (current_section)
210 {
211 if (!strncmp(current_section->name, name, 256))
212 return current_section;
214 current_section = (section_t *)current_section->list.next;
215 }
216 return NULL;
217 }
219 static entry_t *find_entry(section_t *section, char *name)
220 {
221 entry_t *current_entry;
223 current_entry = (entry_t *)section->entry_head.next;
225 while (current_entry)
226 {
227 if (!strncmp(current_entry->name, name, 256))
228 return current_entry;
230 current_entry = (entry_t *)current_entry->list.next;
231 }
232 return NULL;
233 }
235 static section_t *create_section(char *name)
236 {
237 section_t *new_section = malloc(sizeof(section_t));
239 memset(new_section, 0, sizeof(section_t));
240 strncpy(new_section->name, name, 256);
241 list_add(&new_section->list, &config_head);
243 return new_section;
244 }
246 static entry_t *create_entry(section_t *section, char *name)
247 {
248 entry_t *new_entry = malloc(sizeof(entry_t));
249 memset(new_entry, 0, sizeof(entry_t));
250 strncpy(new_entry->name, name, 256);
251 list_add(&new_entry->list, &section->entry_head);
253 return new_entry;
254 }
256 int config_get_int(char *head, char *name, int def)
257 {
258 section_t *section;
259 entry_t *entry;
260 int value;
262 section = find_section(head);
264 if (!section)
265 return def;
267 entry = find_entry(section, name);
269 if (!entry)
270 return def;
272 sscanf(entry->data, "%i", &value);
274 return value;
275 }
277 char *config_get_string(char *head, char *name, char *def)
278 {
279 section_t *section;
280 entry_t *entry;
281 int value;
283 section = find_section(head);
285 if (!section)
286 return def;
288 entry = find_entry(section, name);
290 if (!entry)
291 return def;
293 return entry->data;
294 }
296 void config_set_int(char *head, char *name, int val)
297 {
298 section_t *section;
299 entry_t *entry;
301 section = find_section(head);
303 if (!section)
304 section = create_section(head);
306 entry = find_entry(section, name);
308 if (!entry)
309 entry = create_entry(section, name);
311 sprintf(entry->data, "%i", val);
312 }
314 void config_set_string(char *head, char *name, char *val)
315 {
316 section_t *section;
317 entry_t *entry;
319 section = find_section(head);
321 if (!section)
322 section = create_section(head);
324 entry = find_entry(section, name);
326 if (!entry)
327 entry = create_entry(section, name);
329 strncpy(entry->data, val, 256);
330 }
333 char *get_filename(char *s)
334 {
335 int c = strlen(s) - 1;
336 while (c > 0)
337 {
338 if (s[c] == '/' || s[c] == '\\')
339 return &s[c+1];
340 c--;
341 }
342 return s;
343 }
345 void append_filename(char *dest, char *s1, char *s2, int size)
346 {
347 sprintf(dest, "%s%s", s1, s2);
348 }
350 void put_backslash(char *s)
351 {
352 int c = strlen(s) - 1;
353 if (s[c] != '/' && s[c] != '\\')
354 s[c] = '/';
355 }
357 void config_save(char *fn)
358 {
359 FILE *f = fopen(fn, "wt");
360 section_t *current_section;
362 current_section = (section_t *)config_head.next;
364 while (current_section)
365 {
366 entry_t *current_entry;
368 if (current_section->name[0])
369 fprintf(f, "\n[%s]\n", current_section->name);
371 current_entry = (entry_t *)current_section->entry_head.next;
373 while (current_entry)
374 {
375 fprintf(f, "%s = %s\n", current_entry->name, current_entry->data);
377 current_entry = (entry_t *)current_entry->list.next;
378 }
380 current_section = (section_t *)current_section->list.next;
381 }
383 fclose(f);
384 }