1 /************************************************************************
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 ************************************************************************/
41 typedef struct config_s
{
43 int (*setter
)(char* v
);
46 /* get the value of a config setting */
47 char* config_get(char* name
)
49 nvp_t
*n
= nvp_get(&config
.items
,name
);
54 return ((config_t
*)n
->data
)->default_value
;
59 /* get a config setting as an int value */
60 int config_get_int(char* name
)
62 char* v
= config_get(name
);
64 return strtol(v
,NULL
,10);
69 /* get a config setting as a float value */
70 float config_get_float(char* name
)
72 char* v
= config_get(name
);
74 return strtof(v
,NULL
);
79 /* get a config setting as a boolean value */
80 int config_get_bool(char* name
)
82 char* v
= config_get(name
);
86 /* set the value of a config setting */
87 void config_set(char* name
, char* value
)
95 n
= nvp_get(&config
.items
,name
);
101 c
= malloc(sizeof(config_t
));
102 c
->default_value
= NULL
;
105 nvp_set(&config
.items
,name
,value
,c
);
115 n
->value
= strdup(value
);
119 if (!n
->value
&& c
->default_value
) {
120 c
->setter(c
->default_value
);
127 /* set a config setting from a command */
128 int config_set_command(array_t
*args
)
135 n
= array_get_string(args
,0);
136 v
= array_join(args
," ",1);
142 /* set a config setting to an int value */
143 void config_set_int(char* name
, int value
)
146 sprintf(str
,"%d",value
);
147 config_set(name
,str
);
150 /* set a config setting to a float value */
151 void config_set_float(char* name
, float value
)
154 sprintf(str
,"%f",value
);
155 config_set(name
,str
);
158 /* set the default value of a config setting */
159 void config_set_default(char* name
, char* value
, int (*setter
)(char* v
))
162 nvp_t
*n
= nvp_get(&config
.items
,name
);
165 if (!value
&& !setter
)
168 c
= malloc(sizeof(config_t
));
170 c
->default_value
= strdup(value
);
172 c
->default_value
= NULL
;
176 nvp_set(&config
.items
,name
,NULL
,c
);
186 if (c
->default_value
)
187 free(c
->default_value
);
188 c
->default_value
= NULL
;
191 c
->default_value
= strdup(value
);
194 /* set the default of a config setting to an int value */
195 void config_set_default_int(char* name
, int value
, int (*setter
)(char* v
))
198 sprintf(str
,"%d",value
);
199 config_set_default(name
,str
,setter
);
202 /* set the default of a config setting to a float value */
203 void config_set_default_float(char* name
, float value
, int (*setter
)(char* v
))
206 sprintf(str
,"%f",value
);
207 config_set_default(name
,str
,setter
);
210 /* load a config file */
211 void config_load(char* file
)
217 file_t
*f
= file_load("config",file
);
221 while ((s
= file_readline(f
,buff
,2048)) > -1) {
222 if (!s
|| buff
[0] == '#')
232 /* load a config file from a command */
233 int config_load_command(array_t
*args
)
239 f
= array_get_string(args
,0);
240 nvp_set(&config
.files
,f
,"true",NULL
);
246 /* set the ignore flag for a config file from a command */
247 int config_ignore_command(array_t
*args
)
253 f
= array_get_string(args
,0);
255 nvp_set(&config
.files
,f
,"false",NULL
);
260 /* initialise configuration, load config files and defaults, etc */
261 void config_init(int argc
, char** argv
)
266 config_default_init();
268 /* add the default config file to the to-exec list */
269 nvp_set(&config
.files
,"default.cfg","true",NULL
);
271 for (i
=1; i
<(argc
-1); i
++) {
272 if (!strcmp(argv
[i
],"exec")) {
274 nvp_set(&config
.files
,argv
[i
],"true",NULL
);
275 }else if (!strcmp(argv
[i
],"ignore")) {
277 nvp_set(&config
.files
,argv
[i
],"false",NULL
);
283 if (n
->value
&& !strcmp(n
->value
,"true"))
284 config_load(n
->name
);
288 for (i
=0; i
<argc
; i
++) {
289 if (!strcmp(argv
[i
],"set") && i
+2 < argc
) {
290 config_set(argv
[i
+1],argv
[i
+2]);
292 }else if (!strcmp(argv
[i
],"unset") && i
+1 < argc
) {
294 config_set(argv
[i
],NULL
);
295 }else if (!strcmp(argv
[i
],"exec")) {
297 }else if (!strcmp(argv
[i
],"ignore")) {
305 /* save the current config */
306 void config_save(char* section
, char* type
, char* file
)
311 if (!type
&& !file
) {
314 if (n
->value
&& !strcmp(n
->value
,"true"))
319 /* TODO: should probably force saving to somewhere, custom.cfg? */
323 f
= file_create("config",n
->name
);
325 f
= file_create(type
,file
);
334 if (!section
|| strstr(n
->name
,section
) == n
->name
)
335 file_writef(f
,"set %s %s\n",n
->name
,n
->value
);