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 ************************************************************************/
39 typedef struct config_s
{
41 int (*setter
)(char* v
);
44 /* get the value of a config setting */
45 char* config_get(char* name
)
47 nvp_t
*n
= nvp_get(&config
.items
,name
);
52 return ((config_t
*)n
->data
)->default_value
;
57 /* get a config setting as an int value */
58 int config_get_int(char* name
)
60 char* v
= config_get(name
);
62 return strtol(v
,NULL
,10);
67 /* get a config setting as a float value */
68 float config_get_float(char* name
)
70 char* v
= config_get(name
);
72 return strtof(v
,NULL
);
77 /* get a config setting as a boolean value */
78 int config_get_bool(char* name
)
80 char* v
= config_get(name
);
84 /* set the value of a config setting */
85 void config_set(char* name
, char* value
)
88 nvp_t
*n
= nvp_get(&config
.items
,name
);
94 c
= malloc(sizeof(config_t
));
95 c
->default_value
= NULL
;
98 nvp_set(&config
.items
,name
,value
,c
);
108 n
->value
= strdup(value
);
112 if (!n
->value
&& c
->default_value
) {
113 c
->setter(c
->default_value
);
120 /* set a config setting to an int value */
121 void config_set_int(char* name
, int value
)
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
)
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
))
140 nvp_t
*n
= nvp_get(&config
.items
,name
);
143 if (!value
&& !setter
)
146 c
= malloc(sizeof(config_t
));
148 c
->default_value
= strdup(value
);
150 c
->default_value
= NULL
;
154 nvp_set(&config
.items
,name
,NULL
,c
);
164 if (c
->default_value
)
165 free(c
->default_value
);
166 c
->default_value
= NULL
;
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
))
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
))
184 sprintf(str
,"%f",value
);
185 config_set_default(name
,str
,setter
);
188 /* load a config file */
189 void config_load(char* file
)
195 file_t
*f
= file_load("config",file
);
199 while ((s
= file_readline(f
,buff
,2048)) > -1) {
200 if (!s
|| buff
[0] == '#')
203 /* TODO: uncomment when command.c is written
211 /* initialise configuration, load config files and defaults, etc */
212 void config_init(int argc
, char** argv
)
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")) {
225 nvp_set(&config
.files
,argv
[i
],"true",NULL
);
226 }else if (!strcmp(argv
[i
],"ignore")) {
228 nvp_set(&config
.files
,argv
[i
],"false",NULL
);
234 if (n
->value
&& !strcmp(n
->value
,"true"))
235 config_load(n
->name
);
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]);
243 }else if (!strcmp(argv
[i
],"unset") && i
+1 < argc
) {
245 config_set(argv
[i
],NULL
);
246 }else if (!strcmp(argv
[i
],"exec")) {
248 }else if (!strcmp(argv
[i
],"ignore")) {
256 /* save the current config */
264 if (n
->value
&& !strcmp(n
->value
,"true"))
269 /* TODO: should probably force saving to somewhere, custom.cfg? */
273 f
= file_create("config",n
->name
);
280 file_writef(f
,"set %s %s\n",n
->name
,n
->value
);