read game configuration data from .ini file

This commit is contained in:
Enno Rehling 2017-01-21 18:53:11 +01:00
parent 9c69963477
commit 23e0943baa
3 changed files with 36 additions and 0 deletions

View File

@ -713,6 +713,40 @@ bool config_changed(int *cache_key) {
return false;
}
#define MAXKEYS 16
void config_set_from(const dictionary *d)
{
int s, nsec = iniparser_getnsec(d);
for (s=0;s!=nsec;++s) {
char key[128];
const char *sec = iniparser_getsecname(d, s);
int k, nkeys = iniparser_getsecnkeys(d, sec);
const char *akeys[MAXKEYS];
const char ** keys = akeys;
size_t slen = strlen(sec);
assert(slen<sizeof(key));
memcpy(key, sec, slen);
key[slen] = ':';
if (nkeys>MAXKEYS) {
keys = malloc(sizeof(const char *) * nkeys);
}
iniparser_getseckeys(d, sec, keys);
for (k=0;k!=nkeys;++k) {
const char *val;
size_t klen = strlen(keys[k]);
assert(klen+slen+1<sizeof(key));
memcpy(key+slen+1, keys[k], klen+1);
val = iniparser_getstring(d, key, NULL);
if (val) {
config_set(key, val);
}
}
if (keys!=akeys) {
free(keys);
}
}
}
void config_set(const char *key, const char *value) {
++config_cache_key;
set_param(&configuration, key, value);

View File

@ -123,6 +123,7 @@ struct param;
void free_params(struct param **pp);
void config_set(const char *key, const char *value);
void config_set_from(const struct _dictionary_ *d);
const char *config_get(const char *key);
int config_get_int(const char *key, int def);
double config_get_flt(const char *key, double def);

View File

@ -83,6 +83,7 @@ static void parse_config(const char *filename)
if (d) {
load_inifile(d);
log_debug("reading from configuration file %s\n", filename);
config_set_from(d);
memdebug = iniparser_getint(d, "game:memcheck", memdebug);
#ifdef USE_CURSES