issue #633: parse FACTION/PARTEI in any language.

added a test since the first fix did not work.
This commit is contained in:
Enno Rehling 2017-01-23 10:47:49 +01:00
parent 23d1355fa1
commit e19f0ad381
5 changed files with 51 additions and 24 deletions

View File

@ -214,6 +214,27 @@ param_t findparam(const char *s, const struct locale * lang)
return result; return result;
} }
param_t findparam_block(const char *s, const struct locale *lang, bool any_locale)
{
param_t p;
if (!s || s[0] == '@') {
return NOPARAM;
}
p = findparam(s, lang);
if (any_locale && p==NOPARAM) {
const struct locale *loc;
for (loc=locales;loc;loc=nextlocale(loc)) {
if (loc!=lang) {
p = findparam(s, loc);
if (p==P_FACTION || p==P_GAMENAME) {
break;
}
}
}
}
return p;
}
param_t findparam_ex(const char *s, const struct locale * lang) param_t findparam_ex(const char *s, const struct locale * lang)
{ {
param_t result = findparam(s, lang); param_t result = findparam(s, lang);

View File

@ -45,6 +45,7 @@ extern "C" {
int findoption(const char *s, const struct locale *lang); int findoption(const char *s, const struct locale *lang);
param_t findparam(const char *s, const struct locale *lang); param_t findparam(const char *s, const struct locale *lang);
param_t findparam_block(const char *s, const struct locale *lang, bool any_locale);
param_t findparam_ex(const char *s, const struct locale * lang); param_t findparam_ex(const char *s, const struct locale * lang);
bool isparam(const char *s, const struct locale * lang, param_t param); bool isparam(const char *s, const struct locale * lang, param_t param);
param_t getparam(const struct locale *lang); param_t getparam(const struct locale *lang);

View File

@ -251,9 +251,33 @@ static void test_config_inifile(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_findparam(CuTest *tc) {
struct locale *en, *de;
test_setup();
en = get_or_create_locale("en");
locale_setstring(en, parameters[P_FACTION], "FACTION");
CuAssertIntEquals(tc, NOPARAM, findparam("FACTION", en));
init_parameters(en);
CuAssertIntEquals(tc, P_FACTION, findparam("FACTION", en));
de = get_or_create_locale("de");
locale_setstring(de, parameters[P_FACTION], "PARTEI");
CuAssertIntEquals(tc, NOPARAM, findparam("PARTEI", de));
init_parameters(de);
CuAssertIntEquals(tc, P_FACTION, findparam("PARTEI", de));
CuAssertIntEquals(tc, NOPARAM, findparam("HODOR", de));
CuAssertIntEquals(tc, NOPARAM, findparam("PARTEI", en));
CuAssertIntEquals(tc, NOPARAM, findparam_block("HODOR", de, false));
CuAssertIntEquals(tc, P_FACTION, findparam_block("PARTEI", de, true));
CuAssertIntEquals(tc, NOPARAM, findparam_block("PARTEI", en, false));
CuAssertIntEquals(tc, P_FACTION, findparam_block("PARTEI", en, true));
test_cleanup();
}
CuSuite *get_config_suite(void) CuSuite *get_config_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_findparam);
SUITE_ADD_TEST(suite, test_config_inifile); SUITE_ADD_TEST(suite, test_config_inifile);
SUITE_ADD_TEST(suite, test_config_cache); SUITE_ADD_TEST(suite, test_config_cache);
SUITE_ADD_TEST(suite, test_get_set_param); SUITE_ADD_TEST(suite, test_get_set_param);

View File

@ -252,26 +252,6 @@ static faction *factionorders(void)
return f; return f;
} }
static param_t next_param(const char *s, const struct locale *lang) {
param_t p;
if (!s || s[0] == '@') {
return NOPARAM;
}
p = findparam(s, lang);
if (p==NOPARAM) {
const struct locale *loc;
for (loc=locales;loc;loc=nextlocale(loc)) {
if (loc!=lang) {
p = findparam(s, lang);
if (p==P_FACTION || p==P_GAMENAME) {
break;
}
}
}
}
return p;
}
int readorders(const char *filename) int readorders(const char *filename)
{ {
FILE *F = NULL; FILE *F = NULL;
@ -299,7 +279,7 @@ int readorders(const char *filename)
const char *s; const char *s;
init_tokens_str(b); init_tokens_str(b);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
p = next_param(s, lang); p = findparam_block(s, lang, true);
switch (p) { switch (p) {
case P_GAMENAME: case P_GAMENAME:
case P_FACTION: case P_FACTION:

View File

@ -295,13 +295,14 @@ void init_translations(const struct locale *lang, int ut, const char * (*string_
// TODO: swap the name of s and key // TODO: swap the name of s and key
const char * s = string_cb(i); const char * s = string_cb(i);
if (s) { if (s) {
struct critbit_tree ** cb = (struct critbit_tree **)tokens;
const char * key = locale_string(lang, s, false); const char * key = locale_string(lang, s, false);
if (!key) key = s; if (key) {
struct critbit_tree ** cb = (struct critbit_tree **)tokens;
add_translation(cb, key, i); add_translation(cb, key, i);
} }
} }
} }
}
void *get_translation(const struct locale *lang, const char *str, int index) { void *get_translation(const struct locale *lang, const char *str, int index) {
void **tokens = get_translations(lang, index); void **tokens = get_translations(lang, index);