added GPL license
[libgliar] / src / cfg.c
1 /*
2 libgliar - a library that can fake the OpenGL context info returned by
3 the glGet OpenGL calls
4
5 Copyright (C) 2013 Canonical Ltd
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 Author: Eleni Maria Stea <elene.mst@gmail.com>
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include "cfg.h"
28
29 static char *stripspace(char *s);
30
31
32 struct cfgopt *gliar_load_cfg(const char *fname)
33 {
34         FILE *fp;
35         char buf[512];
36         struct cfgopt *optlist = 0;
37         struct cfgopt *opt = 0;
38
39         if(!(fp = fopen(fname, "r"))) {
40                 return 0;
41         }
42
43         while(fgets(buf, sizeof buf, fp)) {
44                 char *line = stripspace(buf);
45
46                 if(!*line || *line == '#') {
47                         continue;
48                 }
49
50                 if(*line == '[') {
51                         char *end = strrchr(line, ']');
52                         if(!end) {
53                                 fprintf(stderr, "invalid config %s: %s\n", fname, line);
54                                 continue;
55                         }
56                         line++;
57                         *end = 0;
58
59                         if(opt) {
60                                 opt->next = optlist;
61                                 optlist = opt;
62                         }
63
64                         if((opt = malloc(sizeof *opt))) {
65                                 if((opt->key = malloc(strlen(line) + 1))) {
66                                         strcpy(opt->key, line);
67                                         opt->val = 0;
68                                 } else {
69                                         free(opt);
70                                         opt = 0;
71                                 }
72                         }
73                 } else {
74                         char *tmp;
75                         int prev_len = opt->val ? strlen(opt->val) : 0;
76
77                         if(opt && (tmp = realloc(opt->val, prev_len + strlen(line) + 2))) {
78                                 opt->val = tmp;
79                                 if(prev_len) {
80                                         strcat(opt->val, " ");
81                                         strcat(opt->val, line);
82                                 } else {
83                                         strcpy(opt->val, line);
84                                 }
85                         }
86                 }
87         }
88
89         if(opt) {
90                 opt->next = optlist;
91                 optlist = opt;
92         }
93
94         fclose(fp);
95         return optlist;
96 }
97
98 const char *gliar_find_opt(struct cfgopt *list, const char *name)
99 {
100         if(!list || !name) {
101                 return 0;
102         }
103
104         while(list) {
105                 if(strcmp(list->key, name) == 0) {
106                         return list->val;
107                 }
108                 list = list->next;
109         }
110         return 0;
111 }
112
113 void gliar_print_opt(struct cfgopt *list)
114 {
115         printf("OPTIONS\n");
116         while(list) {
117                 printf("\"%s\" -> \"%s\"\n", list->key, list->val);
118                 list = list->next;
119         }
120 }
121
122 static char *stripspace(char *s)
123 {
124         char *end = s + strlen(s) - 1;
125
126         while(isspace(*s)) s++;
127
128         while(isspace(*end)) {
129                 *end-- = 0;
130         }
131         return s;
132 }