require that main.c declare any ini variables that we might expect.

This commit is contained in:
Enno Rehling 2017-03-12 21:03:38 +01:00
parent 4b88a5abc7
commit ba3f3a17d3
4 changed files with 38 additions and 37 deletions

View File

@ -732,19 +732,8 @@ bool config_changed(int *cache_key) {
return false;
}
static const char * valid_keys[] = {
"game.id",
"game.name",
"game.locale",
"game.verbose",
"game.email",
"game.mailcmd",
"game.sender",
NULL
};
#define MAXKEYS 16
void config_set_from(const dictionary *d)
void config_set_from(const dictionary *d, const char *valid_keys[])
{
int s, nsec = iniparser_getnsec(d);
for (s=0;s!=nsec;++s) {
@ -753,7 +742,6 @@ void config_set_from(const dictionary *d)
int k, nkeys = iniparser_getsecnkeys(d, sec);
const char *keys[MAXKEYS];
size_t slen = strlen(sec);
bool check = strcmp(sec, "game") == 0;
assert(nkeys <= MAXKEYS);
assert(slen<sizeof(key));
@ -769,13 +757,14 @@ void config_set_from(const dictionary *d)
val = iniparser_getstring(d, keys[k], NULL);
if (!orig) {
if (val) {
if (check) {
if (valid_keys) {
int i;
for (i = 0; valid_keys[i]; ++i) {
if (strcmp(key, valid_keys[i]) == 0) break;
size_t vlen = strlen(valid_keys[i]);
if (strncmp(key, valid_keys[i], vlen) == 0) break;
}
if (!valid_keys[i]) {
log_error("unknown key in ini-section %s: %s = %s", sec, key, val);
log_error("unknown key in ini-section %s: %s = %s", sec, key+slen+1, val);
}
}
config_set(key, val);

View File

@ -126,7 +126,7 @@ extern "C" {
void free_params(struct param **pp);
void config_set(const char *key, const char *value);
void config_set_from(const struct _dictionary_ *d);
void config_set_from(const struct _dictionary_ *d, const char *keys[]);
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

@ -242,7 +242,7 @@ static void test_config_inifile(CuTest *tc) {
dictionary_set(ini, "game", NULL);
iniparser_set(ini, "game:id", "42");
iniparser_set(ini, "game:name", "Eressea");
config_set_from(ini);
config_set_from(ini, NULL);
CuAssertStrEquals(tc, "Eressea", config_get("game.name"));
CuAssertStrEquals(tc, "Eressea", game_name());
CuAssertIntEquals(tc, 42, game_id());

View File

@ -45,38 +45,50 @@ static const char *inifile = "eressea.ini";
static int memdebug = 0;
static int verbosity = 1;
static void load_inifile(dictionary * d)
static void load_inifile(void)
{
const char *reportdir = reportpath();
const char *datadir = datapath();
const char *basedir = basepath();
const char *str;
assert(d);
str = iniparser_getstring(d, "game:base", basedir);
if (str != basedir) {
str = config_get("game.base");
if (str) {
set_basepath(str);
}
str = iniparser_getstring(d, "game:report", reportdir);
if (str != reportdir) {
str = config_get("game.report");
if (str) {
set_reportpath(str);
}
str = iniparser_getstring(d, "game:data", datadir);
if (str != datadir) {
str = config_get("game.data");
if (str) {
set_datapath(str);
}
lomem = iniparser_getint(d, "game:lomem", lomem) ? 1 : 0;
verbosity = iniparser_getint(d, "game:verbose", 2);
memdebug = iniparser_getint(d, "game:memcheck", memdebug);
lomem = config_get_int("game.lomem", lomem) ? 1 : 0;
verbosity = config_get_int("game.verbose", 2);
memdebug = config_get_int("game.memcheck", memdebug);
#ifdef USE_CURSES
/* only one value in the [editor] section */
force_color = iniparser_getint(d, "editor:color", force_color);
gm_codepage = iniparser_getint(d, "editor:codepage", gm_codepage);
force_color = config_get_int("editor.color", force_color);
gm_codepage = config_get_int("editor.codepage", gm_codepage);
#endif
}
static const char * valid_keys[] = {
"game.id",
"game.name",
"game.locale",
"game.verbose",
"game.report",
"game.lomem",
"game.memcheck",
"game.email",
"game.mailcmd",
"game.sender",
"editor.color",
"editor.codepage",
"lua.",
NULL
};
static dictionary *parse_config(const char *filename)
{
char path[MAX_PATH];
@ -92,8 +104,8 @@ static dictionary *parse_config(const char *filename)
d = iniparser_load(filename);
}
if (d) {
load_inifile(d);
config_set_from(d);
config_set_from(d, valid_keys);
load_inifile();
}
str = config_get("game.locales");
make_locales(str ? str : "de,en");