fix keyword translations, they use critbit, not tokens

This commit is contained in:
Enno Rehling 2014-06-16 10:32:23 -07:00
parent 6a20594fd3
commit 645a7fcde6
4 changed files with 56 additions and 51 deletions

View file

@ -110,7 +110,7 @@ typedef struct translation {
static translation *translation_table[TRANSMAXHASH]; static translation *translation_table[TRANSMAXHASH];
static translation *junkyard; static translation *junkyard;
static const char *add_translation(const char *key, const char *value) static const char *translate(const char *key, const char *value)
{ {
int kk = ((key[0] << 5) + key[0]) % TRANSMAXHASH; int kk = ((key[0] << 5) + key[0]) % TRANSMAXHASH;
translation *t = translation_table[kk]; translation *t = translation_table[kk];
@ -170,7 +170,7 @@ static void print_items(FILE * F, item * items, const struct locale *lang)
const char *ic = resourcename(itm->type->rtype, 0); const char *ic = resourcename(itm->type->rtype, 0);
if (itm == items) if (itm == items)
fputs("GEGENSTAENDE\n", F); fputs("GEGENSTAENDE\n", F);
fprintf(F, "%d;%s\n", in, add_translation(ic, LOC(lang, ic))); fprintf(F, "%d;%s\n", in, translate(ic, LOC(lang, ic)));
} }
} }
@ -265,7 +265,7 @@ cr_output_curses(FILE * F, const faction * viewer, const void *obj, objtype_t ty
header = 1; header = 1;
fputs("EFFECTS\n", F); fputs("EFFECTS\n", F);
} }
fprintf(F, "\"%d %s\"\n", data->value, add_translation(key, fprintf(F, "\"%d %s\"\n", data->value, translate(key,
locale_string(default_locale, key))); locale_string(default_locale, key)));
} }
} }
@ -323,7 +323,7 @@ static int cr_resource(variant var, char *buffer, const void *userdata)
if (r) { if (r) {
const char *key = resourcename(r, 0); const char *key = resourcename(r, 0);
sprintf(buffer, "\"%s\"", sprintf(buffer, "\"%s\"",
add_translation(key, locale_string(report->locale, key))); translate(key, locale_string(report->locale, key)));
return 0; return 0;
} }
return -1; return -1;
@ -335,7 +335,7 @@ static int cr_race(variant var, char *buffer, const void *userdata)
const struct race *rc = (const race *)var.v; const struct race *rc = (const race *)var.v;
const char *key = rc_name(rc, 0); const char *key = rc_name(rc, 0);
sprintf(buffer, "\"%s\"", sprintf(buffer, "\"%s\"",
add_translation(key, locale_string(report->locale, key))); translate(key, locale_string(report->locale, key)));
return 0; return 0;
} }
@ -355,7 +355,7 @@ static int cr_skill(variant var, char *buffer, const void *userdata)
skill_t sk = (skill_t) var.i; skill_t sk = (skill_t) var.i;
if (sk != NOSKILL) if (sk != NOSKILL)
sprintf(buffer, "\"%s\"", sprintf(buffer, "\"%s\"",
add_translation(mkname("skill", skillnames[sk]), skillname(sk, translate(mkname("skill", skillnames[sk]), skillname(sk,
report->locale))); report->locale)));
else else
strcpy(buffer, "\"\""); strcpy(buffer, "\"\"");
@ -397,7 +397,7 @@ static int cr_resources(variant var, char *buffer, const void *userdata)
if (rlist != NULL) { if (rlist != NULL) {
const char *name = resourcename(rlist->type, rlist->number != 1); const char *name = resourcename(rlist->type, rlist->number != 1);
wp += wp +=
sprintf(wp, "\"%d %s", rlist->number, add_translation(name, LOC(f->locale, sprintf(wp, "\"%d %s", rlist->number, translate(name, LOC(f->locale,
name))); name)));
for (;;) { for (;;) {
rlist = rlist->next; rlist = rlist->next;
@ -405,7 +405,7 @@ static int cr_resources(variant var, char *buffer, const void *userdata)
break; break;
name = resourcename(rlist->type, rlist->number != 1); name = resourcename(rlist->type, rlist->number != 1);
wp += wp +=
sprintf(wp, ", %d %s", rlist->number, add_translation(name, sprintf(wp, ", %d %s", rlist->number, translate(name,
LOC(f->locale, name))); LOC(f->locale, name)));
} }
strcat(wp, "\""); strcat(wp, "\"");
@ -598,14 +598,14 @@ cr_output_building(FILE * F, building * b, const unit * owner, int fno,
report_building(b, &bname, &billusion); report_building(b, &bname, &billusion);
if (billusion) { if (billusion) {
fprintf(F, "\"%s\";Typ\n", add_translation(billusion, LOC(f->locale, fprintf(F, "\"%s\";Typ\n", translate(billusion, LOC(f->locale,
billusion))); billusion)));
if (owner && owner->faction == f) { if (owner && owner->faction == f) {
fprintf(F, "\"%s\";wahrerTyp\n", add_translation(bname, LOC(f->locale, fprintf(F, "\"%s\";wahrerTyp\n", translate(bname, LOC(f->locale,
bname))); bname)));
} }
} else { } else {
fprintf(F, "\"%s\";Typ\n", add_translation(bname, LOC(f->locale, bname))); fprintf(F, "\"%s\";Typ\n", translate(bname, LOC(f->locale, bname)));
} }
fprintf(F, "\"%s\";Name\n", b->name); fprintf(F, "\"%s\";Name\n", b->name);
if (b->display && b->display[0]) if (b->display && b->display[0])
@ -634,7 +634,7 @@ cr_output_ship(FILE * F, const ship * sh, const unit * u, int fcaptain,
fprintf(F, "\"%s\";Name\n", sh->name); fprintf(F, "\"%s\";Name\n", sh->name);
if (sh->display && sh->display[0]) if (sh->display && sh->display[0])
fprintf(F, "\"%s\";Beschr\n", sh->display); fprintf(F, "\"%s\";Beschr\n", sh->display);
fprintf(F, "\"%s\";Typ\n", add_translation(sh->type->_name, fprintf(F, "\"%s\";Typ\n", translate(sh->type->_name,
locale_string(f->locale, sh->type->_name))); locale_string(f->locale, sh->type->_name)));
fprintf(F, "%d;Groesse\n", sh->size); fprintf(F, "%d;Groesse\n", sh->size);
if (sh->damage) { if (sh->damage) {
@ -697,7 +697,7 @@ static void cr_output_spells(FILE * F, const unit * u, int maxlevel)
spellbook_entry * sbe = (spellbook_entry *)ql_get(ql, qi); spellbook_entry * sbe = (spellbook_entry *)ql_get(ql, qi);
if (sbe->level <= maxlevel) { if (sbe->level <= maxlevel) {
spell * sp = sbe->sp; spell * sp = sbe->sp;
const char *name = add_translation(mkname("spell", sp->sname), spell_name(sp, f->locale)); const char *name = translate(mkname("spell", sp->sname), spell_name(sp, f->locale));
if (!header) { if (!header) {
fputs("SPRUECHE\n", F); fputs("SPRUECHE\n", F);
header = 1; header = 1;
@ -802,7 +802,7 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
} }
if (prefix) { if (prefix) {
prefix = mkname("prefix", prefix); prefix = mkname("prefix", prefix);
fprintf(F, "\"%s\";typprefix\n", add_translation(prefix, LOC(f->locale, fprintf(F, "\"%s\";typprefix\n", translate(prefix, LOC(f->locale,
prefix))); prefix)));
} }
} }
@ -819,19 +819,19 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) { if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) {
const char *zRace = rc_name(u_race(u), 1); const char *zRace = rc_name(u_race(u), 1);
fprintf(F, "\"%s\";wahrerTyp\n", fprintf(F, "\"%s\";wahrerTyp\n",
add_translation(zRace, locale_string(f->locale, zRace))); translate(zRace, locale_string(f->locale, zRace)));
} }
} else { } else {
const race *irace = u_irace(u); const race *irace = u_irace(u);
const char *zRace = rc_name(irace, 1); const char *zRace = rc_name(irace, 1);
fprintf(F, "\"%s\";Typ\n", fprintf(F, "\"%s\";Typ\n",
add_translation(zRace, locale_string(f->locale, zRace))); translate(zRace, locale_string(f->locale, zRace)));
if (u->faction == f && irace != u_race(u)) { if (u->faction == f && irace != u_race(u)) {
assert(skill_enabled[SK_STEALTH] assert(skill_enabled[SK_STEALTH]
|| !"we're resetting this on load, so.. ircase should never be used"); || !"we're resetting this on load, so.. ircase should never be used");
zRace = rc_name(u_race(u), 1); zRace = rc_name(u_race(u), 1);
fprintf(F, "\"%s\";wahrerTyp\n", fprintf(F, "\"%s\";wahrerTyp\n",
add_translation(zRace, locale_string(f->locale, zRace))); translate(zRace, locale_string(f->locale, zRace)));
} }
} }
@ -880,7 +880,7 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
} }
c = hp_status(u); c = hp_status(u);
if (c && *c && (u->faction == f || omniscient(f))) { if (c && *c && (u->faction == f || omniscient(f))) {
fprintf(F, "\"%s\";hp\n", add_translation(c, fprintf(F, "\"%s\";hp\n", translate(c,
locale_string(u->faction->locale, c))); locale_string(u->faction->locale, c)));
} }
if (fval(u, UFL_HERO)) { if (fval(u, UFL_HERO)) {
@ -923,7 +923,7 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
fprintf(F, "TALENTE\n"); fprintf(F, "TALENTE\n");
} }
fprintf(F, "%d %d;%s\n", u->number * level_days(sv->level), esk, fprintf(F, "%d %d;%s\n", u->number * level_days(sv->level), esk,
add_translation(mkname("skill", skillnames[sk]), skillname(sk, translate(mkname("skill", skillnames[sk]), skillname(sk,
f->locale))); f->locale)));
} }
} }
@ -938,7 +938,7 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
const spell *sp = mage->combatspells[i].sp; const spell *sp = mage->combatspells[i].sp;
if (sp) { if (sp) {
const char *name = const char *name =
add_translation(mkname("spell", sp->sname), spell_name(sp, translate(mkname("spell", sp->sname), spell_name(sp,
f->locale)); f->locale));
fprintf(F, "KAMPFZAUBER %d\n", i); fprintf(F, "KAMPFZAUBER %d\n", i);
fprintf(F, "\"%s\";name\n", name); fprintf(F, "\"%s\";name\n", name);
@ -975,7 +975,7 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
pr = 1; pr = 1;
fputs("GEGENSTAENDE\n", F); fputs("GEGENSTAENDE\n", F);
} }
fprintf(F, "%d;%s\n", in, add_translation(ic, locale_string(f->locale, fprintf(F, "%d;%s\n", in, translate(ic, locale_string(f->locale,
ic))); ic)));
} }
@ -1054,7 +1054,7 @@ static void cr_reportspell(FILE * F, spell * sp, int level, const struct locale
{ {
int k; int k;
const char *name = const char *name =
add_translation(mkname("spell", sp->sname), spell_name(sp, lang)); translate(mkname("spell", sp->sname), spell_name(sp, lang));
fprintf(F, "ZAUBER %d\n", hashstring(sp->sname)); fprintf(F, "ZAUBER %d\n", hashstring(sp->sname));
fprintf(F, "\"%s\";name\n", name); fprintf(F, "\"%s\";name\n", name);
@ -1092,7 +1092,7 @@ static void cr_reportspell(FILE * F, spell * sp, int level, const struct locale
if (itemanz > 0) { if (itemanz > 0) {
const char *name = resourcename(rtype, 0); const char *name = resourcename(rtype, 0);
fprintf(F, "%d %d;%s\n", itemanz, costtyp == SPC_LEVEL fprintf(F, "%d %d;%s\n", itemanz, costtyp == SPC_LEVEL
|| costtyp == SPC_LINEAR, add_translation(name, LOC(lang, name))); || costtyp == SPC_LINEAR, translate(name, LOC(lang, name)));
} }
} }
} }
@ -1101,7 +1101,7 @@ static char *cr_output_resource(char *buf, const char *name,
const struct locale *loc, int amount, int level) const struct locale *loc, int amount, int level)
{ {
buf += sprintf(buf, "RESOURCE %u\n", hashstring(name)); buf += sprintf(buf, "RESOURCE %u\n", hashstring(name));
buf += sprintf(buf, "\"%s\";type\n", add_translation(name, LOC(loc, name))); buf += sprintf(buf, "\"%s\";type\n", translate(name, LOC(loc, name)));
if (amount >= 0) { if (amount >= 0) {
if (level >= 0) if (level >= 0)
buf += sprintf(buf, "%d;skill\n", level); buf += sprintf(buf, "%d;skill\n", level);
@ -1269,7 +1269,7 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr)
} }
tname = terrain_name(r); tname = terrain_name(r);
fprintf(F, "\"%s\";Terrain\n", add_translation(tname, locale_string(f->locale, fprintf(F, "\"%s\";Terrain\n", translate(tname, locale_string(f->locale,
tname))); tname)));
if (sr->mode != see_unit) if (sr->mode != see_unit)
fprintf(F, "\"%s\";visibility\n", visibility[sr->mode]); fprintf(F, "\"%s\";visibility\n", visibility[sr->mode]);
@ -1331,12 +1331,12 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr)
fputs("PREISE\n", F); fputs("PREISE\n", F);
if (lux) { if (lux) {
const char *ch = resourcename(lux->rtype, 0); const char *ch = resourcename(lux->rtype, 0);
fprintf(F, "%d;%s\n", 1, add_translation(ch, fprintf(F, "%d;%s\n", 1, translate(ch,
locale_string(f->locale, ch))); locale_string(f->locale, ch)));
} }
if (herb) { if (herb) {
const char *ch = resourcename(herb->rtype, 0); const char *ch = resourcename(herb->rtype, 0);
fprintf(F, "%d;%s\n", 1, add_translation(ch, fprintf(F, "%d;%s\n", 1, translate(ch,
locale_string(f->locale, ch))); locale_string(f->locale, ch)));
} }
} }
@ -1348,7 +1348,7 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr)
fprintf(F, "%d;%s\n", (dmd->value fprintf(F, "%d;%s\n", (dmd->value
? dmd->value * dmd->type->price ? dmd->value * dmd->type->price
: -dmd->type->price), : -dmd->type->price),
add_translation(ch, locale_string(f->locale, ch))); translate(ch, locale_string(f->locale, ch)));
dmd = dmd->next; dmd = dmd->next;
} }
} }
@ -1523,13 +1523,13 @@ report_computer(const char *filename, report_context * ctx, const char *charset)
#endif #endif
{ {
const char *zRace = rc_name(f->race, 1); const char *zRace = rc_name(f->race, 1);
fprintf(F, "\"%s\";Typ\n", add_translation(zRace, LOC(f->locale, zRace))); fprintf(F, "\"%s\";Typ\n", translate(zRace, LOC(f->locale, zRace)));
} }
prefix = get_prefix(f->attribs); prefix = get_prefix(f->attribs);
if (prefix != NULL) { if (prefix != NULL) {
prefix = mkname("prefix", prefix); prefix = mkname("prefix", prefix);
fprintf(F, "\"%s\";typprefix\n", fprintf(F, "\"%s\";typprefix\n",
add_translation(prefix, LOC(f->locale, prefix))); translate(prefix, LOC(f->locale, prefix)));
} }
fprintf(F, "%d;Rekrutierungskosten\n", f->race->recruitcost); fprintf(F, "%d;Rekrutierungskosten\n", f->race->recruitcost);
fprintf(F, "%d;Anzahl Personen\n", count_all(f)); fprintf(F, "%d;Anzahl Personen\n", count_all(f));
@ -1576,7 +1576,7 @@ report_computer(const char *filename, report_context * ctx, const char *charset)
if (prefix != NULL) { if (prefix != NULL) {
prefix = mkname("prefix", prefix); prefix = mkname("prefix", prefix);
fprintf(F, "\"%s\";typprefix\n", fprintf(F, "\"%s\";typprefix\n",
add_translation(prefix, LOC(f->locale, prefix))); translate(prefix, LOC(f->locale, prefix)));
} }
show_allies_cr(F, f, g->allies); show_allies_cr(F, f, g->allies);
} }
@ -1620,7 +1620,7 @@ report_computer(const char *filename, report_context * ctx, const char *charset)
continue; continue;
ch = resourcename(ptype->itype->rtype, 0); ch = resourcename(ptype->itype->rtype, 0);
fprintf(F, "TRANK %d\n", hashstring(ch)); fprintf(F, "TRANK %d\n", hashstring(ch));
fprintf(F, "\"%s\";Name\n", add_translation(ch, locale_string(f->locale, fprintf(F, "\"%s\";Name\n", translate(ch, locale_string(f->locale,
ch))); ch)));
fprintf(F, "%d;Stufe\n", ptype->level); fprintf(F, "%d;Stufe\n", ptype->level);
@ -1638,7 +1638,7 @@ report_computer(const char *filename, report_context * ctx, const char *charset)
while (m->number) { while (m->number) {
ch = resourcename(m->rtype, 0); ch = resourcename(m->rtype, 0);
fprintf(F, "\"%s\"\n", add_translation(ch, locale_string(f->locale, fprintf(F, "\"%s\"\n", translate(ch, locale_string(f->locale,
ch))); ch)));
m++; m++;
} }

View file

@ -1265,7 +1265,7 @@ skill_t findskill(const char *s, const struct locale * lang)
int i; int i;
const void * match; const void * match;
void **tokens = get_translations(lang, UT_SKILLS); void **tokens = get_translations(lang, UT_SKILLS);
critbit_tree *cb = (critbit_tree *)*tokens; struct critbit_tree *cb = (critbit_tree *)*tokens;
if (cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) { if (cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) {
cb_get_kv(match, &i, sizeof(int)); cb_get_kv(match, &i, sizeof(int));
result = (skill_t)i; result = (skill_t)i;
@ -1806,9 +1806,24 @@ void *gc_add(void *p)
return p; return p;
} }
void add_translation(critbit_tree **cbp, const char *key, int i) {
char buffer[256];
char * str = transliterate(buffer, sizeof(buffer)-sizeof(int), key);
critbit_tree * cb = *cbp;
if (str) {
size_t len = strlen(str);
if (!cb) {
*cbp = cb = (critbit_tree *)calloc(1, sizeof(critbit_tree *));
}
len = cb_new_kv(str, len, &i, sizeof(int), buffer);
cb_insert(cb, buffer, len);
} else {
log_error("could not transliterate '%s'\n", key);
}
}
void init_translations(const struct locale *lang, int ut, const char * (*string_cb)(int i), int maxstrings) void init_translations(const struct locale *lang, int ut, const char * (*string_cb)(int i), int maxstrings)
{ {
char buffer[256];
void **tokens; void **tokens;
int i; int i;
@ -1820,18 +1835,8 @@ void init_translations(const struct locale *lang, int ut, const char * (*string_
const char * key = s ? locale_string(lang, s) : 0; const char * key = s ? locale_string(lang, s) : 0;
key = key ? key : s; key = key ? key : s;
if (key) { if (key) {
char * str = transliterate(buffer, sizeof(buffer)-sizeof(int), key); critbit_tree ** cb = (critbit_tree **)tokens;
if (str) { add_translation(cb, key, i);
critbit_tree * cb = (critbit_tree *)*tokens;
size_t len = strlen(str);
if (!cb) {
*tokens = cb = (critbit_tree *)calloc(1, sizeof(critbit_tree *));
}
len = cb_new_kv(str, len, &i, sizeof(int), buffer);
cb_insert(cb, buffer, len);
} else {
log_error("could not transliterate '%s'\n", key);
}
} }
} }
} }

View file

@ -27,7 +27,7 @@ extern "C" {
#include "types.h" #include "types.h"
struct _dictionary_; struct _dictionary_;
struct critbit_tree;
/* experimental gameplay features (that don't affect the savefile) */ /* experimental gameplay features (that don't affect the savefile) */
/* TODO: move these settings to settings.h or into configuration files */ /* TODO: move these settings to settings.h or into configuration files */
#define GOBLINKILL /* Goblin-Spezialklau kann tödlich enden */ #define GOBLINKILL /* Goblin-Spezialklau kann tödlich enden */
@ -107,6 +107,7 @@ extern "C" {
extern const char *options[MAXOPTIONS]; extern const char *options[MAXOPTIONS];
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
void add_translation(struct critbit_tree **cb, const char *str, int i);
void init_translations(const struct locale *lang, int ut, const char * (*string_cb)(int i), int maxstrings); void init_translations(const struct locale *lang, int ut, const char * (*string_cb)(int i), int maxstrings);
int shipspeed(const struct ship *sh, const struct unit *u); int shipspeed(const struct ship *sh, const struct unit *u);

View file

@ -18,9 +18,8 @@ static const char * keyword_key(int i)
void init_keyword(const struct locale *lang, keyword_t kwd, const char *str) { void init_keyword(const struct locale *lang, keyword_t kwd, const char *str) {
void **tokens = get_translations(lang, UT_KEYWORDS); void **tokens = get_translations(lang, UT_KEYWORDS);
variant token; struct critbit_tree **cb = (critbit_tree **)tokens;
token.i = kwd; add_translation(cb, str, (int)kwd);
addtoken(tokens, str, token);
} }
keyword_t findkeyword(const char *s) { keyword_t findkeyword(const char *s) {