refactoring prior to removing static variable

This commit is contained in:
Enno Rehling 2016-08-28 18:15:00 +01:00
parent eb8db3bfb9
commit 0ac30c65a1
5 changed files with 8 additions and 5 deletions

View File

@ -119,6 +119,8 @@ extern "C" {
extern int num_races;
typedef const char *(*race_name_func)(const struct unit *);
typedef struct race {
char *_name;
float magres;

View File

@ -1416,7 +1416,8 @@ void name_unit(unit * u)
if (u_race(u)->generate_name) {
const char *gen_name = u_race(u)->generate_name(u);
if (gen_name) {
unit_setname(u, gen_name);
free(u->_name);
u->_name = _strdup(gen_name);
}
else {
unit_setname(u, racename(u->faction->locale, u, u_race(u)));

View File

@ -1797,7 +1797,7 @@ static int parse_races(xmlDocPtr doc)
}
assert(propValue != NULL);
if (strcmp((const char *)propValue, "name") == 0) {
rc->generate_name = (const char *(*)(const struct unit *))fun;
rc->generate_name = (race_name_func)fun;
}
else if (strcmp((const char *)propValue, "describe") == 0) {
rc->describe =

View File

@ -23,7 +23,6 @@ extern "C" {
#endif
struct unit;
typedef const char *(*name_func)(const struct unit *);
void register_names(void);
const char *abkz(const char *s, char *buf, size_t size, size_t maxchars);

View File

@ -2,6 +2,7 @@
#include "names.h"
#include <kernel/race.h>
#include <util/language.h>
#include <util/functions.h>
@ -10,14 +11,14 @@
static void test_names(CuTest * tc)
{
name_func foo;
race_name_func foo;
test_cleanup();
register_names();
default_locale = test_create_locale();
locale_setstring(default_locale, "undead_prefix_0", "Kleine");
locale_setstring(default_locale, "undead_name_0", "Graue");
locale_setstring(default_locale, "undead_postfix_0", "Kobolde");
CuAssertPtrNotNull(tc, foo = (name_func)get_function("nameundead"));
CuAssertPtrNotNull(tc, foo = (race_name_func)get_function("nameundead"));
CuAssertStrEquals(tc, "Kleine Graue Kobolde", foo(NULL));
CuAssertPtrNotNull(tc, get_function("nameskeleton"));
CuAssertPtrNotNull(tc, get_function("namezombie"));