do not use unit.display directly, use getter/setter instead.

This commit is contained in:
Enno Rehling 2018-10-24 20:16:17 +02:00
parent 516a53c0c7
commit 53e123b36a
7 changed files with 21 additions and 25 deletions

View file

@ -440,12 +440,12 @@ unit *read_unit(gamedata *data)
if (unicode_utf8_trim(obuf)!=0) { if (unicode_utf8_trim(obuf)!=0) {
log_warning("trim unit %s name to '%s'", itoa36(u->no), obuf); log_warning("trim unit %s name to '%s'", itoa36(u->no), obuf);
} }
u->_name = obuf[0] ? str_strdup(obuf) : 0; unit_setname(u, obuf[0] ? obuf : NULL);
READ_STR(data->store, obuf, sizeof(obuf)); READ_STR(data->store, obuf, sizeof(obuf));
if (unicode_utf8_trim(obuf)!=0) { if (unicode_utf8_trim(obuf)!=0) {
log_warning("trim unit %s info to '%s'", itoa36(u->no), obuf); log_warning("trim unit %s info to '%s'", itoa36(u->no), obuf);
} }
u->display = obuf[0] ? str_strdup(obuf) : 0; unit_setinfo(u, obuf[0] ? obuf : NULL);
READ_INT(data->store, &number); READ_INT(data->store, &number);
set_number(u, number); set_number(u, number);
@ -544,6 +544,7 @@ unit *read_unit(gamedata *data)
void write_unit(gamedata *data, const unit * u) void write_unit(gamedata *data, const unit * u)
{ {
const char *str;
order *ord; order *ord;
int p = 0; int p = 0;
unsigned int flags = u->flags & UFL_SAVEMASK; unsigned int flags = u->flags & UFL_SAVEMASK;
@ -553,7 +554,8 @@ void write_unit(gamedata *data, const unit * u)
assert(u->faction->_alive); assert(u->faction->_alive);
write_faction_reference(u->faction, data->store); write_faction_reference(u->faction, data->store);
WRITE_STR(data->store, u->_name); WRITE_STR(data->store, u->_name);
WRITE_STR(data->store, u->display ? u->display : ""); str = unit_getinfo(u);
WRITE_STR(data->store, str ? str : "");
WRITE_INT(data->store, u->number); WRITE_INT(data->store, u->number);
WRITE_INT(data->store, u->age); WRITE_INT(data->store, u->age);
WRITE_TOK(data->store, u_race(u)->_name); WRITE_TOK(data->store, u_race(u)->_name);

View file

@ -474,8 +474,8 @@ attrib_type at_private = {
const char *u_description(const unit * u, const struct locale *lang) const char *u_description(const unit * u, const struct locale *lang)
{ {
if (u->display && u->display[0]) { if (u->_display && u->_display[0]) {
return u->display; return u->_display;
} }
else { else {
char zText[64]; char zText[64];
@ -1293,7 +1293,7 @@ void free_unit(unit * u)
{ {
assert(!u->region); assert(!u->region);
free(u->_name); free(u->_name);
free(u->display); free(u->_display);
free_order(u->thisorder); free_order(u->thisorder);
free_orders(&u->orders); free_orders(&u->orders);
if (u->skills) if (u->skills)
@ -1522,16 +1522,16 @@ void unit_setname(unit * u, const char *name)
const char *unit_getinfo(const unit * u) const char *unit_getinfo(const unit * u)
{ {
return (const char *)u->display; return (const char *)u->_display;
} }
void unit_setinfo(unit * u, const char *info) void unit_setinfo(unit * u, const char *info)
{ {
free(u->display); free(u->_display);
if (info) if (info)
u->display = str_strdup(info); u->_display = str_strdup(info);
else else
u->display = NULL; u->_display = NULL;
} }
int unit_getid(const unit * u) int unit_getid(const unit * u)

View file

@ -89,7 +89,7 @@ extern "C" {
int no; /* id */ int no; /* id */
int hp; int hp;
char *_name; char *_name;
char *display; char *_display;
struct faction *faction; struct faction *faction;
struct building *building; struct building *building;
struct ship *ship; struct ship *ship;

View file

@ -397,14 +397,13 @@ static void test_unit_description(CuTest *tc) {
rc = test_create_race("hodor"); rc = test_create_race("hodor");
u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, NULL)); u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, NULL));
CuAssertPtrEquals(tc, NULL, u->display); CuAssertStrEquals(tc, NULL, unit_getinfo(u));
CuAssertStrEquals(tc, 0, u_description(u, lang)); CuAssertStrEquals(tc, NULL, u_description(u, lang));
u->display = str_strdup("Hodor"); unit_setinfo(u, "Hodor");
CuAssertStrEquals(tc, "Hodor", u_description(u, NULL)); CuAssertStrEquals(tc, "Hodor", u_description(u, NULL));
CuAssertStrEquals(tc, "Hodor", u_description(u, lang)); CuAssertStrEquals(tc, "Hodor", u_description(u, lang));
free(u->display); unit_setinfo(u, NULL);
u->display = NULL;
locale_setstring(lang, "describe_hodor", "HODOR"); locale_setstring(lang, "describe_hodor", "HODOR");
CuAssertStrEquals(tc, "HODOR", u_description(u, lang)); CuAssertStrEquals(tc, "HODOR", u_description(u, lang));

View file

@ -1542,7 +1542,7 @@ int display_cmd(unit * u, struct order *ord)
break; break;
case P_UNIT: case P_UNIT:
s = &u->display; unit_setinfo(u, getstrtoken());
break; break;
case P_PRIVAT: case P_PRIVAT:

View file

@ -188,12 +188,12 @@ static void test_display_cmd(CuTest *tc) {
ord = create_order(K_DISPLAY, f->locale, "%s Hodor", LOC(f->locale, parameters[P_UNIT])); ord = create_order(K_DISPLAY, f->locale, "%s Hodor", LOC(f->locale, parameters[P_UNIT]));
CuAssertIntEquals(tc, 0, display_cmd(u, ord)); CuAssertIntEquals(tc, 0, display_cmd(u, ord));
CuAssertStrEquals(tc, "Hodor", u->display); CuAssertStrEquals(tc, "Hodor", unit_getinfo(u));
free_order(ord); free_order(ord);
ord = create_order(K_DISPLAY, f->locale, LOC(f->locale, parameters[P_UNIT])); ord = create_order(K_DISPLAY, f->locale, LOC(f->locale, parameters[P_UNIT]));
CuAssertIntEquals(tc, 0, display_cmd(u, ord)); CuAssertIntEquals(tc, 0, display_cmd(u, ord));
CuAssertPtrEquals(tc, NULL, u->display); CuAssertPtrEquals(tc, NULL, (void *)unit_getinfo(u));
free_order(ord); free_order(ord);
ord = create_order(K_DISPLAY, f->locale, "%s Hodor", LOC(f->locale, parameters[P_REGION])); ord = create_order(K_DISPLAY, f->locale, "%s Hodor", LOC(f->locale, parameters[P_REGION]));

View file

@ -1539,12 +1539,7 @@ int sp_undeadhero(struct castorder * co)
/* new units gets some stats from old unit */ /* new units gets some stats from old unit */
if (du->display) { unit_setinfo(u, unit_getinfo(du));
unit_setinfo(u, du->display);
}
else {
unit_setinfo(u, NULL);
}
unit_setstatus(u, du->status); unit_setstatus(u, du->status);
setguard(u, false); setguard(u, false);
for (ilist = &du->items; *ilist;) { for (ilist = &du->items; *ilist;) {