remove equipment.name, save an allocation.

This commit is contained in:
Enno Rehling 2017-09-18 20:03:23 +02:00
parent b4fc7a1ab4
commit f43ec9ee0f
4 changed files with 34 additions and 10 deletions

View File

@ -210,6 +210,32 @@ typedef struct eq_entry {
equipment *value;
} eq_entry;
typedef struct name_cb_data {
const equipment *find;
const char *result;
} name_cb_data;
static int equipment_name_cb(const void * match, const void * key, size_t keylen, void *cbdata) {
const eq_entry *ent = (const eq_entry *)match;
name_cb_data *query = (name_cb_data *)cbdata;
if (ent->value == query->find) {
query->result = ent->key;
return 1;
}
return 0;
}
const char *equipment_name(const struct equipment *eq)
{
name_cb_data data;
data.find = eq;
data.result = NULL;
cb_foreach(&cb_equipments, "", 0, equipment_name_cb, &data);
return data.result;
}
equipment *get_equipment(const char *eqname)
{
const void *match;
@ -237,7 +263,6 @@ equipment *create_equipment(const char *eqname)
memcpy(ent.key, eqname, len);
ent.value = (equipment *)calloc(1, sizeof(equipment));
ent.value->name = strdup(eqname);
cb_insert(&cb_equipments, &ent, sizeof(ent));
return ent.value;
@ -254,7 +279,6 @@ equipment *get_or_create_equipment(const char *eqname)
static void free_equipment(equipment *eq) {
int i;
free(eq->name);
if (eq->spells) {
selist_foreach(eq->spells, free_ls);
selist_free(eq->spells);

View File

@ -45,7 +45,6 @@ extern "C" {
} subset;
typedef struct equipment {
char *name;
struct itemdata *items;
char *skills[MAXSKILLS];
struct selist *spells;
@ -55,6 +54,7 @@ extern "C" {
void equipment_done(void);
const char *equipment_name(const struct equipment *eq);
struct equipment *get_or_create_equipment(const char *eqname);
struct equipment *get_equipment(const char *eqname);
struct equipment *create_equipment(const char *eqname);

View File

@ -53,19 +53,19 @@ static void test_get_equipment(CuTest * tc)
test_setup();
eq = create_equipment("catapultammo123");
CuAssertPtrNotNull(tc, eq);
CuAssertStrEquals(tc, "catapultammo123", eq->name);
CuAssertStrEquals(tc, "catapultammo123", equipment_name(eq));
eq = get_equipment("catapultammo123");
CuAssertPtrNotNull(tc, eq);
CuAssertStrEquals(tc, "catapultammo123", eq->name);
CuAssertStrEquals(tc, "catapultammo123", equipment_name(eq));
eq = get_equipment("catapult");
CuAssertPtrEquals(tc, NULL, eq);
eq = create_equipment("catapult");
eq = get_equipment("catapult");
CuAssertPtrNotNull(tc, eq);
CuAssertStrEquals(tc, "catapult", eq->name);
CuAssertStrEquals(tc, "catapult", equipment_name(eq));
eq = get_equipment("catapultammo123");
CuAssertPtrNotNull(tc, eq);
CuAssertStrEquals(tc, "catapultammo123", eq->name);
CuAssertStrEquals(tc, "catapultammo123", equipment_name(eq));
test_cleanup();
}

View File

@ -960,7 +960,7 @@ static void add_spells(equipment * eq, xmlNodeSetPtr nsetItems)
equipment_addspell(eq, name, level);
}
else {
log_error("spell '%s' for equipment-set '%s' has no level\n", name, eq->name);
log_error("spell '%s' for equipment-set '%s' has no level\n", name, equipment_name(eq));
}
xmlFree(propValue);
}
@ -980,7 +980,7 @@ static void add_skills(equipment * eq, xmlNodeSetPtr nsetSkills)
assert(propValue != NULL);
sk = findskill((const char *)propValue);
if (sk == NOSKILL) {
log_error("unknown skill '%s' in equipment-set %s\n", (const char *)propValue, eq->name);
log_error("unknown skill '%s' in equipment-set %s\n", (const char *)propValue, equipment_name(eq));
xmlFree(propValue);
}
else {
@ -1044,7 +1044,7 @@ add_subsets(xmlDocPtr doc, equipment * eq, xmlNodeSetPtr nsetSubsets)
}
}
if (totalChance > 1.0f) {
log_error("total chance exceeds 1.0: %f in equipment set %s.\n", totalChance, eq->name);
log_error("total chance exceeds 1.0: %f in equipment set %s.\n", totalChance, equipment_name(eq));
}
}
xmlXPathFreeObject(xpathResult);