7 static char *stripspace(char *s);
10 struct cfgopt *gliar_load_cfg(const char *fname)
14 struct cfgopt *optlist = 0;
15 struct cfgopt *opt = 0;
17 if(!(fp = fopen(fname, "r"))) {
21 while(fgets(buf, sizeof buf, fp)) {
22 char *line = stripspace(buf);
24 if(!*line || *line == '#') {
29 char *end = strrchr(line, ']');
31 fprintf(stderr, "invalid config %s: %s\n", fname, line);
42 if((opt = malloc(sizeof *opt))) {
43 if((opt->key = malloc(strlen(line) + 1))) {
44 strcpy(opt->key, line);
53 int prev_len = opt->val ? strlen(opt->val) : 0;
55 if(opt && (tmp = realloc(opt->val, prev_len + strlen(line) + 2))) {
58 strcat(opt->val, " ");
59 strcat(opt->val, line);
61 strcpy(opt->val, line);
76 const char *gliar_find_opt(struct cfgopt *list, const char *name)
83 if(strcmp(list->key, name) == 0) {
91 void gliar_print_opt(struct cfgopt *list)
95 printf("\"%s\" -> \"%s\"\n", list->key, list->val);
100 static char *stripspace(char *s)
102 char *end = s + strlen(s) - 1;
104 while(isspace(*s)) s++;
106 while(isspace(*end)) {