1 /* Parse a configuration file into a struct using X-Macros */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include "../ini.h"
6
7 /* define the config struct type */
8 typedef struct {
9 #define CFG(s, n, default) char *s##_##n;
10 #include "config.def"
11 } config;
12
13 /* create one and fill in its default values */
14 config Config = {
15 #define CFG(s, n, default) default,
16 #include "config.def"
17 };
18
19 /* process a line of the INI file, storing valid values into config struct */
handler(void * user,const char * section,const char * name,const char * value)20 int handler(void *user, const char *section, const char *name,
21 const char *value)
22 {
23 config *cfg = (config *)user;
24
25 if (0) ;
26 #define CFG(s, n, default) else if (strcmp(section, #s)==0 && \
27 strcmp(name, #n)==0) cfg->s##_##n = strdup(value);
28 #include "config.def"
29
30 return 1;
31 }
32
33 /* print all the variables in the config, one per line */
dump_config(config * cfg)34 void dump_config(config *cfg)
35 {
36 #define CFG(s, n, default) printf("%s_%s = %s\n", #s, #n, cfg->s##_##n);
37 #include "config.def"
38 }
39
main(int argc,char * argv[])40 int main(int argc, char* argv[])
41 {
42 if (ini_parse("test.ini", handler, &Config) < 0)
43 printf("Can't load 'test.ini', using defaults\n");
44 dump_config(&Config);
45 return 0;
46 }
47