forked from github/server
require that main.c declare any ini variables that we might expect.
This commit is contained in:
parent
4b88a5abc7
commit
ba3f3a17d3
|
@ -732,19 +732,8 @@ bool config_changed(int *cache_key) {
|
||||||
return false;
|
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
|
#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);
|
int s, nsec = iniparser_getnsec(d);
|
||||||
for (s=0;s!=nsec;++s) {
|
for (s=0;s!=nsec;++s) {
|
||||||
|
@ -753,7 +742,6 @@ void config_set_from(const dictionary *d)
|
||||||
int k, nkeys = iniparser_getsecnkeys(d, sec);
|
int k, nkeys = iniparser_getsecnkeys(d, sec);
|
||||||
const char *keys[MAXKEYS];
|
const char *keys[MAXKEYS];
|
||||||
size_t slen = strlen(sec);
|
size_t slen = strlen(sec);
|
||||||
bool check = strcmp(sec, "game") == 0;
|
|
||||||
|
|
||||||
assert(nkeys <= MAXKEYS);
|
assert(nkeys <= MAXKEYS);
|
||||||
assert(slen<sizeof(key));
|
assert(slen<sizeof(key));
|
||||||
|
@ -769,13 +757,14 @@ void config_set_from(const dictionary *d)
|
||||||
val = iniparser_getstring(d, keys[k], NULL);
|
val = iniparser_getstring(d, keys[k], NULL);
|
||||||
if (!orig) {
|
if (!orig) {
|
||||||
if (val) {
|
if (val) {
|
||||||
if (check) {
|
if (valid_keys) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; valid_keys[i]; ++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]) {
|
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);
|
config_set(key, val);
|
||||||
|
|
|
@ -126,7 +126,7 @@ extern "C" {
|
||||||
void free_params(struct param **pp);
|
void free_params(struct param **pp);
|
||||||
|
|
||||||
void config_set(const char *key, const char *value);
|
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);
|
const char *config_get(const char *key);
|
||||||
int config_get_int(const char *key, int def);
|
int config_get_int(const char *key, int def);
|
||||||
double config_get_flt(const char *key, double def);
|
double config_get_flt(const char *key, double def);
|
||||||
|
|
|
@ -242,7 +242,7 @@ static void test_config_inifile(CuTest *tc) {
|
||||||
dictionary_set(ini, "game", NULL);
|
dictionary_set(ini, "game", NULL);
|
||||||
iniparser_set(ini, "game:id", "42");
|
iniparser_set(ini, "game:id", "42");
|
||||||
iniparser_set(ini, "game:name", "Eressea");
|
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", config_get("game.name"));
|
||||||
CuAssertStrEquals(tc, "Eressea", game_name());
|
CuAssertStrEquals(tc, "Eressea", game_name());
|
||||||
CuAssertIntEquals(tc, 42, game_id());
|
CuAssertIntEquals(tc, 42, game_id());
|
||||||
|
|
50
src/main.c
50
src/main.c
|
@ -45,38 +45,50 @@ static const char *inifile = "eressea.ini";
|
||||||
static int memdebug = 0;
|
static int memdebug = 0;
|
||||||
static int verbosity = 1;
|
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;
|
const char *str;
|
||||||
|
|
||||||
assert(d);
|
str = config_get("game.base");
|
||||||
|
if (str) {
|
||||||
str = iniparser_getstring(d, "game:base", basedir);
|
|
||||||
if (str != basedir) {
|
|
||||||
set_basepath(str);
|
set_basepath(str);
|
||||||
}
|
}
|
||||||
str = iniparser_getstring(d, "game:report", reportdir);
|
str = config_get("game.report");
|
||||||
if (str != reportdir) {
|
if (str) {
|
||||||
set_reportpath(str);
|
set_reportpath(str);
|
||||||
}
|
}
|
||||||
str = iniparser_getstring(d, "game:data", datadir);
|
str = config_get("game.data");
|
||||||
if (str != datadir) {
|
if (str) {
|
||||||
set_datapath(str);
|
set_datapath(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
lomem = iniparser_getint(d, "game:lomem", lomem) ? 1 : 0;
|
lomem = config_get_int("game.lomem", lomem) ? 1 : 0;
|
||||||
verbosity = iniparser_getint(d, "game:verbose", 2);
|
verbosity = config_get_int("game.verbose", 2);
|
||||||
memdebug = iniparser_getint(d, "game:memcheck", memdebug);
|
memdebug = config_get_int("game.memcheck", memdebug);
|
||||||
#ifdef USE_CURSES
|
#ifdef USE_CURSES
|
||||||
/* only one value in the [editor] section */
|
/* only one value in the [editor] section */
|
||||||
force_color = iniparser_getint(d, "editor:color", force_color);
|
force_color = config_get_int("editor.color", force_color);
|
||||||
gm_codepage = iniparser_getint(d, "editor:codepage", gm_codepage);
|
gm_codepage = config_get_int("editor.codepage", gm_codepage);
|
||||||
#endif
|
#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)
|
static dictionary *parse_config(const char *filename)
|
||||||
{
|
{
|
||||||
char path[MAX_PATH];
|
char path[MAX_PATH];
|
||||||
|
@ -92,8 +104,8 @@ static dictionary *parse_config(const char *filename)
|
||||||
d = iniparser_load(filename);
|
d = iniparser_load(filename);
|
||||||
}
|
}
|
||||||
if (d) {
|
if (d) {
|
||||||
load_inifile(d);
|
config_set_from(d, valid_keys);
|
||||||
config_set_from(d);
|
load_inifile();
|
||||||
}
|
}
|
||||||
str = config_get("game.locales");
|
str = config_get("game.locales");
|
||||||
make_locales(str ? str : "de,en");
|
make_locales(str ? str : "de,en");
|
||||||
|
|
Loading…
Reference in New Issue