2 libgliar - a library that can fake the OpenGL context info returned by
5 Copyright (C) 2013 Canonical Ltd
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.
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.
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/>.
20 Author: Eleni Maria Stea <elene.mst@gmail.com>
29 static char *stripspace(char *s);
30 static void concat_values(struct cfgopt *opt);
32 struct cfgopt *gliar_load_cfg(const char *fname)
36 struct cfgopt *optlist = 0;
37 struct cfgopt *opt = 0;
39 if(!(fp = fopen(fname, "r"))) {
43 while(fgets(buf, sizeof buf, fp)) {
44 char *line = stripspace(buf);
46 if(!*line || *line == '#') {
51 char *end = strrchr(line, ']');
53 fprintf(stderr, "invalid config %s: %s\n", fname, line);
65 if((opt = malloc(sizeof *opt))) {
66 if((opt->key = malloc(strlen(line) + 1))) {
67 strcpy(opt->key, line);
71 opt->type = GLIAR_STRING;
78 int new_sz = opt->str_count + 1;
82 int num = strtol(line, &end, 10);
86 opt->type = GLIAR_NUMBER;
89 if(opt && (tmp = realloc(opt->str_val, new_sz * sizeof(char*)))) {
91 if((opt->str_val[new_sz - 1] = malloc(strlen(line) + 1))) {
92 strcpy(opt->str_val[new_sz -1], line);
93 opt->str_count = new_sz;
98 opt->type = GLIAR_STRING;
113 const struct cfgopt *gliar_find_opt(struct cfgopt *list, const char *name)
120 if(strcmp(list->key, name) == 0) {
128 void gliar_print_opt(struct cfgopt *list)
132 if(list->type == GLIAR_NUMBER) {
133 printf("\"%s\" -> %d\n", list->key, list->num_val);
137 for(i=0; i<list->str_count; i++) {
138 printf("\"%s\" -> \"%s\"\n", list->key, list->str_val[i]);
145 static char *stripspace(char *s)
147 char *end = s + strlen(s) - 1;
149 while(isspace(*s)) s++;
151 while(isspace(*end)) {
157 static void concat_values(struct cfgopt *opt)
160 int sz = opt->str_count - 1;
162 for(i=0; i<opt->str_count; i++) {
163 sz += strlen(opt->str_val[i]);
166 if(!(opt->conc_vals = malloc(sz + 1))) {
171 for(i=0; i<opt->str_count - 1; i++) {
172 strcat(opt->conc_vals, opt->str_val[i]);
173 strcat(opt->conc_vals, " ");
175 strcat(opt->conc_vals, opt->str_val[i]);