diff --git a/src/common/attributes/raceprefix.c b/src/common/attributes/raceprefix.c index c3c050eca..fb86d6651 100644 --- a/src/common/attributes/raceprefix.c +++ b/src/common/attributes/raceprefix.c @@ -16,7 +16,32 @@ #include #include "raceprefix.h" +#include + +#include +#include + attrib_type at_raceprefix = { "raceprefix", NULL, a_finalizestring, NULL, a_writestring, a_readstring, ATF_UNIQUE }; +void +set_prefix(attrib ** ap, const char * str) +{ + attrib * a = a_find(*ap, &at_raceprefix); + if (a==NULL) { + a = a_add(ap, a_new(&at_raceprefix)); + } else { + free(a->data.v); + } + assert(a->type==&at_raceprefix); + a->data.v = strdup(str); +} + +const char * +get_prefix(const attrib * a) +{ + a = a_findc(a, &at_raceprefix); + if (a==NULL) return NULL; + return (const char *)a->data.v; +} diff --git a/src/common/attributes/raceprefix.h b/src/common/attributes/raceprefix.h index 209e2e7c6..e357fb316 100644 --- a/src/common/attributes/raceprefix.h +++ b/src/common/attributes/raceprefix.h @@ -20,7 +20,8 @@ extern "C" { #endif extern struct attrib_type at_raceprefix; - +extern void set_prefix(struct attrib ** ap, const char * str); +extern const char * get_prefix(const struct attrib * a); #ifdef __cplusplus } diff --git a/src/common/gamecode/creport.c b/src/common/gamecode/creport.c index 3ff0af341..66978ed4a 100644 --- a/src/common/gamecode/creport.c +++ b/src/common/gamecode/creport.c @@ -639,11 +639,9 @@ cr_output_unit(FILE * F, const region * r, const attrib *a_otherfaction = a_find(u->attribs, &at_otherfaction); const faction * otherfaction = a_otherfaction?get_otherfaction(a_otherfaction):NULL; /* my own faction, full info */ - const attrib * ap = 0; const attrib *a = a_find(u->attribs, &at_group); if (a!=NULL) { const group * g = (const group*)a->data.v; - ap = a_find(g->attribs, &at_raceprefix); fprintf(F, "%d;gruppe\n", g->gid); } fprintf(F, "%d;Partei\n", u->faction->no); @@ -1072,6 +1070,7 @@ report_computer(FILE * F, faction * f, const faction_list * addresses, const time_t report_time) { int i; + const char * prefix; region * r; building *b; ship *sh; @@ -1116,10 +1115,9 @@ report_computer(FILE * F, faction * f, const faction_list * addresses, const char * zRace = rc_name(f->race, 1); fprintf(F, "\"%s\";Typ\n", add_translation(zRace, LOC(f->locale, zRace))); } - a = a_find(f->attribs, &at_raceprefix); - if (a) { - const char * name = (const char*)a->data.v; - fprintf(F, "\"%s\";typprefix\n", add_translation(name, LOC(f->locale, name))); + prefix = get_prefix(f->attribs); + if (prefix!=NULL) { + fprintf(F, "\"%s\";typprefix\n", add_translation(prefix, LOC(f->locale, prefix))); } fprintf(F, "%d;Rekrutierungskosten\n", f->race->recruitcost); fprintf(F, "%d;Anzahl Personen\n", count_all(f)); @@ -1143,13 +1141,12 @@ report_computer(FILE * F, faction * f, const faction_list * addresses, { group * g; for (g=f->groups;g;g=g->next) { - const attrib *a = a_find(g->attribs, &at_raceprefix); fprintf(F, "GRUPPE %d\n", g->gid); fprintf(F, "\"%s\";name\n", g->name); - if(a) { - const char * name = (const char*)a->data.v; - fprintf(F, "\"%s\";typprefix\n", add_translation(name, LOC(f->locale, name))); + prefix = get_prefix(g->attribs); + if (prefix!=NULL) { + fprintf(F, "\"%s\";typprefix\n", add_translation(prefix, LOC(f->locale, prefix))); } show_allies(F, f, g->allies); } diff --git a/src/common/gamecode/laws.c b/src/common/gamecode/laws.c index e42554560..627f7c718 100644 --- a/src/common/gamecode/laws.c +++ b/src/common/gamecode/laws.c @@ -1466,7 +1466,7 @@ static int prefix_cmd(unit * u, struct order * ord) { attrib **ap; - attrib *a, *a2; + attrib *a; int i; const char *s; @@ -1474,7 +1474,7 @@ prefix_cmd(unit * u, struct order * ord) skip_token(); s = getstrtoken(); - if(!*s) { + if (!*s) { a = a_find(u->attribs, &at_group); if (a) { a_removeall(&((group*)a->data.v)->attribs, &at_raceprefix); @@ -1498,12 +1498,7 @@ prefix_cmd(unit * u, struct order * ord) ap = &u->faction->attribs; a = a_find(u->attribs, &at_group); if (a) ap = &((group*)a->data.v)->attribs; - - a2 = a_find(*ap, &at_raceprefix); - if(!a2) - a2 = a_add(ap, a_new(&at_raceprefix)); - - a2->data.v = strdup(race_prefixes[i]); + set_prefix(ap, race_prefixes[i]); return 0; } diff --git a/src/common/kernel/race.c b/src/common/kernel/race.c index 66c822600..3d7570349 100644 --- a/src/common/kernel/race.c +++ b/src/common/kernel/race.c @@ -345,13 +345,9 @@ raceprefix(const unit *u) { const attrib * agroup = a_findc(u->attribs, &at_group); const attrib * asource = u->faction->attribs; - const attrib * a2 = NULL; if (agroup!=NULL) asource = ((const group *)(agroup->data.v))->attribs; - a2 = a_findc(asource, &at_raceprefix); - - if (a2!=NULL) return (const char *)a2->data.v; - return NULL; + return get_prefix(asource); } const char *