wm pt1
[voxelands-alt.git] / src / core / config.c
blob4c7c41835c30f606d6da6807b049f5c7f8f2000d
1 /************************************************************************
2 * config.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
21 #include "nvp.h"
22 #include "crypto.h"
23 #include "file.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 static struct {
30 nvp_t *items;
31 nvp_t *files;
32 int isinit;
33 } config = {
34 NULL,
35 NULL,
39 typedef struct config_s {
40 char* default_value;
41 int (*setter)(char* v);
42 } config_t;
44 /* get the value of a config setting */
45 char* config_get(char* name)
47 nvp_t *n = nvp_get(&config.items,name);
48 if (!n)
49 return NULL;
51 if (!n->value)
52 return ((config_t*)n->data)->default_value;
54 return n->value;
57 /* get a config setting as an int value */
58 int config_get_int(char* name)
60 char* v = config_get(name);
61 if (v)
62 return strtol(v,NULL,10);
64 return 0;
67 /* get a config setting as a float value */
68 float config_get_float(char* name)
70 char* v = config_get(name);
71 if (v)
72 return strtof(v,NULL);
74 return 0.0;
77 /* get a config setting as a boolean value */
78 int config_get_bool(char* name)
80 char* v = config_get(name);
81 return parse_bool(v);
84 /* set the value of a config setting */
85 void config_set(char* name, char* value)
87 config_t *c;
88 nvp_t *n = nvp_get(&config.items,name);
90 if (!n) {
91 if (!value)
92 return;
94 c = malloc(sizeof(config_t));
95 c->default_value = NULL;
96 c->setter = NULL;
98 nvp_set(&config.items,name,value,c);
100 return;
103 if (n->value)
104 free(n->value);
105 n->value = NULL;
107 if (value)
108 n->value = strdup(value);
110 c = n->data;
111 if (c->setter) {
112 if (!n->value && c->default_value) {
113 c->setter(c->default_value);
114 }else{
115 c->setter(n->value);
120 /* set a config setting to an int value */
121 void config_set_int(char* name, int value)
123 char str[20];
124 sprintf(str,"%d",value);
125 config_set(name,str);
128 /* set a config setting to a float value */
129 void config_set_float(char* name, float value)
131 char str[50];
132 sprintf(str,"%f",value);
133 config_set(name,str);
136 /* set the default value of a config setting */
137 void config_set_default(char* name, char* value, int (*setter)(char* v))
139 config_t *c;
140 nvp_t *n = nvp_get(&config.items,name);
142 if (!n) {
143 if (!value && !setter)
144 return;
146 c = malloc(sizeof(config_t));
147 if (value) {
148 c->default_value = strdup(value);
149 }else{
150 c->default_value = NULL;
152 c->setter = setter;
154 nvp_set(&config.items,name,NULL,c);
156 if (setter)
157 setter(value);
159 return;
162 c = n->data;
164 if (c->default_value)
165 free(c->default_value);
166 c->default_value = NULL;
168 if (value)
169 c->default_value = strdup(value);
172 /* set the default of a config setting to an int value */
173 void config_set_default_int(char* name, int value, int (*setter)(char* v))
175 char str[20];
176 sprintf(str,"%d",value);
177 config_set_default(name,str,setter);
180 /* set the default of a config setting to a float value */
181 void config_set_default_float(char* name, float value, int (*setter)(char* v))
183 char str[50];
184 sprintf(str,"%f",value);
185 config_set_default(name,str,setter);
188 /* load a config file */
189 void config_load(char* file)
191 char buff[2048];
192 int s;
193 char* l;
195 file_t *f = file_load("config",file);
196 if (!f)
197 return;
199 while ((s = file_readline(f,buff,2048)) > -1) {
200 if (!s || buff[0] == '#')
201 continue;
202 l = trim(buff);
203 /* TODO: uncomment when command.c is written
204 if (l && l[0])
205 cmd_exec(l);*/
208 file_free(f);
211 /* initialise configuration, load config files and defaults, etc */
212 void config_init(int argc, char** argv)
214 int i;
215 nvp_t *n;
217 config_default_init();
219 /* add the default config file to the to-exec list */
220 nvp_set(&config.files,"default.cfg","true",NULL);
222 for (i=1; i<(argc-1); i++) {
223 if (!strcmp(argv[i],"exec")) {
224 i += 1;
225 nvp_set(&config.files,argv[i],"true",NULL);
226 }else if (!strcmp(argv[i],"ignore")) {
227 i += 1;
228 nvp_set(&config.files,argv[i],"false",NULL);
232 n = config.files;
233 while (n) {
234 if (n->value && !strcmp(n->value,"true"))
235 config_load(n->name);
236 n = n->next;
239 for (i=0; i<argc; i++) {
240 if (!strcmp(argv[i],"set") && i+2 < argc) {
241 config_set(argv[i+1],argv[i+2]);
242 i+=2;
243 }else if (!strcmp(argv[i],"unset") && i+1 < argc) {
244 i+=1;
245 config_set(argv[i],NULL);
246 }else if (!strcmp(argv[i],"exec")) {
247 i += 1;
248 }else if (!strcmp(argv[i],"ignore")) {
249 i += 1;
253 config.isinit = 1;
256 /* save the current config */
257 void config_save()
259 file_t *f;
260 nvp_t *n;
262 n = config.files;
263 while (n) {
264 if (n->value && !strcmp(n->value,"true"))
265 break;
266 n = n->next;
269 /* TODO: should probably force saving to somewhere, custom.cfg? */
270 if (!n)
271 return;
273 f = file_create("config",n->name);
274 if (!f)
275 return;
277 n = config.items;
278 while (n) {
279 if (n->value)
280 file_writef(f,"set %s %s\n",n->name,n->value);
281 n = n->next;
284 file_flush(f);
285 file_free(f);