Merge pull request #43 from badgerman/master

Fix FFL_NOTIMEOUT related crash in E2
This commit is contained in:
Enno Rehling 2014-10-18 19:41:21 +02:00
commit 723a9507a3
12 changed files with 920 additions and 846 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de> Copyright (c) 1998-2014, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de> Christian Schlittchen <corwin@amber.kn-bremen.de>
@ -251,7 +251,7 @@ extern "C" {
bool is_cursed_internal(struct attrib *ap, const curse_type * ctype); bool is_cursed_internal(struct attrib *ap, const curse_type * ctype);
/* ignoriert CURSE_ISNEW */ /* ignoriert CURSE_ISNEW */
extern void remove_curse(struct attrib **ap, const struct curse *c); bool remove_curse(struct attrib **ap, const struct curse *c);
/* löscht einen konkreten Spruch auf einem Objekt. /* löscht einen konkreten Spruch auf einem Objekt.
*/ */
@ -280,11 +280,6 @@ extern "C" {
* unterschiedlich gewünscht sein * unterschiedlich gewünscht sein
* */ * */
extern struct curse *get_cursex(struct attrib *ap, const curse_type * ctype,
variant data, bool(*compare) (const struct curse *, variant));
/* gibt pointer auf die erste curse-struct zurück, deren Typ ctype ist,
* und für die compare() true liefert, oder einen NULL-pointer.
* */
extern struct curse *get_curse(struct attrib *ap, const curse_type * ctype); extern struct curse *get_curse(struct attrib *ap, const curse_type * ctype);
/* gibt pointer auf die erste curse-struct zurück, deren Typ ctype ist, /* gibt pointer auf die erste curse-struct zurück, deren Typ ctype ist,
* oder einen NULL-pointer * oder einen NULL-pointer
@ -303,9 +298,6 @@ extern "C" {
extern void curse_done(struct attrib *a); extern void curse_done(struct attrib *a);
extern int curse_age(struct attrib *a); extern int curse_age(struct attrib *a);
extern bool cmp_curse(const struct attrib *a, const void *data);
extern bool cmp_cursetype(const struct attrib *a, const void *data);
extern float destr_curse(struct curse *c, int cast_level, float force); extern float destr_curse(struct curse *c, int cast_level, float force);
extern int resolve_curse(variant data, void *address); extern int resolve_curse(variant data, void *address);

View file

@ -1,5 +1,6 @@
#include <platform.h> #include <platform.h>
#include <kernel/ally.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/types.h> #include <kernel/types.h>
#include <kernel/race.h> #include <kernel/race.h>
@ -13,6 +14,21 @@
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
static void test_remove_empty_factions_allies(CuTest *tc) {
faction *f1, *f2;
region *r;
test_cleanup();
r = test_create_region(0, 0, 0);
f1 = test_create_faction(0);
test_create_unit(f1, r);
f2 = test_create_faction(0);
ally_add(&f1->allies, f2);
remove_empty_factions();
CuAssertPtrEquals(tc, 0, f1->allies);
test_cleanup();
}
static void test_remove_empty_factions(CuTest *tc) { static void test_remove_empty_factions(CuTest *tc) {
faction *f, *fm; faction *f, *fm;
int fno; int fno;
@ -95,6 +111,7 @@ CuSuite *get_faction_suite(void)
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_addfaction); SUITE_ADD_TEST(suite, test_addfaction);
SUITE_ADD_TEST(suite, test_remove_empty_factions); SUITE_ADD_TEST(suite, test_remove_empty_factions);
SUITE_ADD_TEST(suite, test_remove_empty_factions_allies);
SUITE_ADD_TEST(suite, test_remove_dead_factions); SUITE_ADD_TEST(suite, test_remove_dead_factions);
SUITE_ADD_TEST(suite, test_get_monsters); SUITE_ADD_TEST(suite, test_get_monsters);
return suite; return suite;

View file

@ -113,8 +113,6 @@ char *rns(FILE * f, char *c, size_t size)
return c; return c;
} }
extern unsigned int __at_hashkey(const char *s);
static unit *unitorders(FILE * F, int enc, struct faction *f) static unit *unitorders(FILE * F, int enc, struct faction *f)
{ {
@ -1315,7 +1313,6 @@ faction *readfaction(struct gamedata * data)
f->flags = FFL_NPC | FFL_NOIDLEOUT; f->flags = FFL_NPC | FFL_NOIDLEOUT;
} }
} }
assert((f->flags&FFL_SAVEMASK) == f->flags);
a_read(data->store, &f->attribs, f); a_read(data->store, &f->attribs, f);
if (data->version >= CLAIM_VERSION) { if (data->version >= CLAIM_VERSION) {

View file

@ -35,16 +35,6 @@
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h>
#include <util/event.h>
#include <util/goodies.h>
#include <util/language.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/umlaut.h>
#include <util/parser.h>
#include <util/rng.h>
#include <storage.h> #include <storage.h>
/* libc includes */ /* libc includes */
@ -56,18 +46,11 @@
static int read_permissions(attrib * a, void *owner, struct storage *store) static int read_permissions(attrib * a, void *owner, struct storage *store)
{ {
attrib *attr = NULL; attrib *attr = NULL;
a_read(store, &attr, NULL); a_read(store, &attr, owner);
a_free(attr); a_remove(&attr, a);
return AT_READ_OK; return AT_READ_OK;
} }
struct attrib_type at_permissions = {
"GM:permissions",
NULL, NULL, NULL,
NULL, read_permissions,
ATF_UNIQUE
};
static int read_gmcreate(attrib * a, void *owner, struct storage *store) static int read_gmcreate(attrib * a, void *owner, struct storage *store)
{ {
char zText[32]; char zText[32];
@ -75,15 +58,8 @@ static int read_gmcreate(attrib * a, void *owner, struct storage *store)
return AT_READ_OK; return AT_READ_OK;
} }
/* at_gmcreate specifies that the owner can create items of a particular type */
attrib_type at_gmcreate = {
"GM:create",
NULL, NULL, NULL,
NULL, read_gmcreate
};
void register_gmcmd(void) void register_gmcmd(void)
{ {
at_register(&at_gmcreate); at_deprecate("GM:create", read_gmcreate);
at_register(&at_permissions); at_deprecate("GM:permissions", read_permissions);
} }

View file

@ -30,6 +30,7 @@ int RunAllTests(void)
ADD_TESTS(suite, race); ADD_TESTS(suite, race);
/* util */ /* util */
ADD_TESTS(suite, config); ADD_TESTS(suite, config);
ADD_TESTS(suite, attrib);
ADD_TESTS(suite, base36); ADD_TESTS(suite, base36);
ADD_TESTS(suite, bsdstring); ADD_TESTS(suite, bsdstring);
ADD_TESTS(suite, functions); ADD_TESTS(suite, functions);

View file

@ -1,25 +1,38 @@
#ifndef ERESSEA_TESTS_H #ifndef ERESSEA_TESTS_H
#define ERESSEA_TESTS_H #define ERESSEA_TESTS_H
#include <stdlib.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void test_cleanup(void); struct region;
struct unit;
struct faction;
struct building;
struct ship;
struct item_type;
struct building_type;
struct ship_type;
struct terrain_type;
struct terrain_type * test_create_terrain(const char * name, unsigned int flags); void test_cleanup(void);
struct race *test_create_race(const char *name);
struct region *test_create_region(int x, int y,
const struct terrain_type *terrain);
struct faction *test_create_faction(const struct race *rc);
struct unit *test_create_unit(struct faction *f, struct region *r);
void test_create_world(void);
struct building * test_create_building(struct region * r, const struct building_type * btype);
struct ship * test_create_ship(struct region * r, const struct ship_type * stype);
struct item_type * test_create_itemtype(const char * name);
struct ship_type *test_create_shiptype(const char * name);
struct building_type *test_create_buildingtype(const char *name);
int RunAllTests(void); struct terrain_type * test_create_terrain(const char * name, unsigned int flags);
struct race *test_create_race(const char *name);
struct region *test_create_region(int x, int y,
const struct terrain_type *terrain);
struct faction *test_create_faction(const struct race *rc);
struct unit *test_create_unit(struct faction *f, struct region *r);
void test_create_world(void);
struct building * test_create_building(struct region * r, const struct building_type * btype);
struct ship * test_create_ship(struct region * r, const struct ship_type * stype);
struct item_type * test_create_itemtype(const char * name);
struct ship_type *test_create_shiptype(const char * name);
struct building_type *test_create_buildingtype(const char *name);
int RunAllTests(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -1,7 +1,7 @@
/* /*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de> Copyright (c) 1998-2014, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de> Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above purpose with or without fee is hereby granted, provided that the above
@ -41,68 +41,66 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stdio.h> #include <stdio.h>
typedef struct removecurse_data { typedef struct removecurse_data {
curse *curse; curse *curse;
unit *target; unit *target;
} removecurse_data; } removecurse_data;
static void removecurse_init(trigger * t) static void removecurse_init(trigger * t)
{ {
t->data.v = calloc(sizeof(removecurse_data), 1); t->data.v = calloc(sizeof(removecurse_data), 1);
} }
static void removecurse_free(trigger * t) static void removecurse_free(trigger * t)
{ {
free(t->data.v); free(t->data.v);
} }
static int removecurse_handle(trigger * t, void *data) static int removecurse_handle(trigger * t, void *data)
{ {
/* call an event handler on removecurse. /* call an event handler on removecurse.
* data.v -> ( variant event, int timer ) * data.v -> ( variant event, int timer )
*/ */
removecurse_data *td = (removecurse_data *) t->data.v; removecurse_data *td = (removecurse_data *)t->data.v;
if (td->curse && td->target) { if (td->curse && td->target) {
attrib *a = a_select(td->target->attribs, td->curse, cmp_curse); if (!remove_curse(&td->target->attribs, td->curse)) {
if (a) { log_error("could not perform removecurse::handle()\n");
a_remove(&td->target->attribs, a); }
} else }
log_error("could not perform removecurse::handle()\n"); unused_arg(data);
} return 0;
unused_arg(data);
return 0;
} }
static void removecurse_write(const trigger * t, struct storage *store) static void removecurse_write(const trigger * t, struct storage *store)
{ {
removecurse_data *td = (removecurse_data *) t->data.v; removecurse_data *td = (removecurse_data *)t->data.v;
WRITE_TOK(store, td->target ? itoa36(td->target->no) : 0); WRITE_TOK(store, td->target ? itoa36(td->target->no) : 0);
WRITE_INT(store, td->curse ? td->curse->no : 0); WRITE_INT(store, td->curse ? td->curse->no : 0);
} }
static int removecurse_read(trigger * t, struct storage *store) static int removecurse_read(trigger * t, struct storage *store)
{ {
removecurse_data *td = (removecurse_data *) t->data.v; removecurse_data *td = (removecurse_data *)t->data.v;
read_reference(&td->target, store, read_unit_reference, resolve_unit); read_reference(&td->target, store, read_unit_reference, resolve_unit);
read_reference(&td->curse, store, read_int, resolve_curse); read_reference(&td->curse, store, read_int, resolve_curse);
return AT_READ_OK; return AT_READ_OK;
} }
trigger_type tt_removecurse = { trigger_type tt_removecurse = {
"removecurse", "removecurse",
removecurse_init, removecurse_init,
removecurse_free, removecurse_free,
removecurse_handle, removecurse_handle,
removecurse_write, removecurse_write,
removecurse_read removecurse_read
}; };
trigger *trigger_removecurse(curse * c, unit * target) trigger *trigger_removecurse(curse * c, unit * target)
{ {
trigger *t = t_new(&tt_removecurse); trigger *t = t_new(&tt_removecurse);
removecurse_data *td = (removecurse_data *) t->data.v; removecurse_data *td = (removecurse_data *)t->data.v;
td->curse = c; td->curse = c;
td->target = target; td->target = target;
return t; return t;
} }

View file

@ -2,6 +2,7 @@ project(util C)
SET(_TEST_FILES SET(_TEST_FILES
base36.test.c base36.test.c
attrib.test.c
strings.test.c strings.test.c
bsdstring.test.c bsdstring.test.c
functions.test.c functions.test.c

View file

@ -1,7 +1,7 @@
/* /*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de> Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de> Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above purpose with or without fee is hereby granted, provided that the above
@ -29,322 +29,329 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stdlib.h> #include <stdlib.h>
#define MAXATHASH 61 #define MAXATHASH 61
attrib_type *at_hash[MAXATHASH]; static attrib_type *at_hash[MAXATHASH];
static unsigned int __at_hashkey(const char *s) static unsigned int __at_hashkey(const char *s)
{ {
int key = 0; int key = 0;
size_t i = strlen(s); size_t i = strlen(s);
while (i > 0) { while (i > 0) {
key = (s[--i] + key * 37); key = (s[--i] + key * 37);
} }
return key & 0x7fffffff; return key & 0x7fffffff;
} }
void at_register(attrib_type * at) void at_register(attrib_type * at)
{ {
attrib_type *find; attrib_type *find;
if (at->read == NULL) { if (at->read == NULL) {
log_warning("registering non-persistent attribute %s.\n", at->name); log_warning("registering non-persistent attribute %s.\n", at->name);
} }
at->hashkey = __at_hashkey(at->name); at->hashkey = __at_hashkey(at->name);
find = at_hash[at->hashkey % MAXATHASH]; find = at_hash[at->hashkey % MAXATHASH];
while (find && at->hashkey != find->hashkey) while (find && at->hashkey != find->hashkey) {
find = find->nexthash; find = find->nexthash;
if (find && find == at) { }
log_warning("attribute '%s' was registered more than once\n", at->name); if (find && find == at) {
return; log_warning("attribute '%s' was registered more than once\n", at->name);
} else { return;
assert(!find || !"hashkey is already in use"); }
} else {
at->nexthash = at_hash[at->hashkey % MAXATHASH]; assert(!find || !"hashkey is already in use");
at_hash[at->hashkey % MAXATHASH] = at; }
at->nexthash = at_hash[at->hashkey % MAXATHASH];
at_hash[at->hashkey % MAXATHASH] = at;
} }
static attrib_type *at_find(unsigned int hk) static attrib_type *at_find(unsigned int hk)
{ {
const char *translate[3][2] = { const char *translate[3][2] = {
{"zielregion", "targetregion"}, /* remapping: from 'zielregion, heute targetregion */ { "zielregion", "targetregion" }, /* remapping: from 'zielregion, heute targetregion */
{"verzaubert", "curse"}, /* remapping: früher verzaubert, jetzt curse */ { "verzaubert", "curse" }, /* remapping: früher verzaubert, jetzt curse */
{NULL, NULL} { NULL, NULL }
}; };
attrib_type *find = at_hash[hk % MAXATHASH]; attrib_type *find = at_hash[hk % MAXATHASH];
while (find && hk != find->hashkey) while (find && hk != find->hashkey)
find = find->nexthash; find = find->nexthash;
if (!find) { if (!find) {
int i = 0; int i = 0;
while (translate[i][0]) { while (translate[i][0]) {
if (__at_hashkey(translate[i][0]) == hk) if (__at_hashkey(translate[i][0]) == hk)
return at_find(__at_hashkey(translate[i][1])); return at_find(__at_hashkey(translate[i][1]));
++i; ++i;
}
} }
} return find;
return find;
} }
attrib *a_select(attrib * a, const void *data, attrib *a_select(attrib * a, const void *data,
bool(*compare) (const attrib *, const void *)) bool(*compare) (const attrib *, const void *))
{ {
while (a && !compare(a, data)) while (a && !compare(a, data))
a = a->next; a = a->next;
return a; return a;
} }
attrib *a_find(attrib * a, const attrib_type * at) attrib *a_find(attrib * a, const attrib_type * at)
{ {
while (a && a->type != at) while (a && a->type != at)
a = a->nexttype; a = a->nexttype;
return a; return a;
} }
const attrib *a_findc(const attrib * a, const attrib_type * at) const attrib *a_findc(const attrib * a, const attrib_type * at)
{ {
while (a && a->type != at) while (a && a->type != at)
a = a->nexttype; a = a->nexttype;
return a; return a;
} }
static attrib *a_insert(attrib * head, attrib * a) static attrib *a_insert(attrib * head, attrib * a)
{ {
attrib **pa = &head->next; attrib **pa = &head->next;
assert(!(a->type->flags & ATF_UNIQUE)); assert(!(a->type->flags & ATF_UNIQUE));
assert(head && head->type == a->type); assert(head && head->type == a->type);
while (*pa && (*pa)->type == a->type) { while (*pa && (*pa)->type == a->type) {
pa = &(*pa)->next; pa = &(*pa)->next;
} }
a->next = *pa; a->next = *pa;
return *pa = a; return *pa = a;
} }
attrib *a_add(attrib ** pa, attrib * a) attrib *a_add(attrib ** pa, attrib * a)
{ {
attrib *first = *pa; attrib *first = *pa;
assert(a->next == NULL && a->nexttype == NULL); assert(a->next == NULL && a->nexttype == NULL);
if (first == NULL) if (first == NULL)
return *pa = a; return *pa = a;
if (first->type == a->type) { if (first->type == a->type) {
return a_insert(first, a); return a_insert(first, a);
}
for (;;) {
attrib *next = first->nexttype;
if (next == NULL) {
/* the type is not in the list, append it behind the last type */
attrib **insert = &first->next;
first->nexttype = a;
while (*insert)
insert = &(*insert)->next;
*insert = a;
break;
} }
if (next->type == a->type) { for (;;) {
return a_insert(next, a); attrib *next = first->nexttype;
if (next == NULL) {
/* the type is not in the list, append it behind the last type */
attrib **insert = &first->next;
first->nexttype = a;
while (*insert)
insert = &(*insert)->next;
*insert = a;
break;
}
if (next->type == a->type) {
return a_insert(next, a);
}
first = next;
} }
first = next; return a;
}
return a;
}
void a_free(attrib * a)
{
const attrib_type *at = a->type;
if (at->finalize)
at->finalize(a);
free(a);
} }
static int a_unlink(attrib ** pa, attrib * a) static int a_unlink(attrib ** pa, attrib * a)
{ {
attrib **pnexttype = pa; attrib **pnexttype = pa;
attrib **pnext = NULL; attrib **pnext = NULL;
assert(a != NULL); assert(a != NULL);
while (*pnexttype) { while (*pnexttype) {
attrib *next = *pnexttype; attrib *next = *pnexttype;
if (next->type == a->type) if (next->type == a->type)
break; break;
pnexttype = &next->nexttype; pnexttype = &next->nexttype;
pnext = &next->next; pnext = &next->next;
}
if (*pnexttype && (*pnexttype)->type == a->type) {
if (*pnexttype == a) {
*pnexttype = a->next;
if (a->next != a->nexttype) {
a->next->nexttype = a->nexttype;
}
if (pnext == NULL)
return 1;
while (*pnext && (*pnext)->type != a->type)
pnext = &(*pnext)->next;
} else {
pnext = &(*pnexttype)->next;
} }
while (*pnext && (*pnext)->type == a->type) { if (*pnexttype && (*pnexttype)->type == a->type) {
if (*pnext == a) { if (*pnexttype == a) {
*pnext = a->next; *pnexttype = a->next;
return 1; if (a->next != a->nexttype) {
} a->next->nexttype = a->nexttype;
pnext = &(*pnext)->next; }
if (pnext == NULL)
return 1;
while (*pnext && (*pnext)->type != a->type)
pnext = &(*pnext)->next;
}
else {
pnext = &(*pnexttype)->next;
}
while (*pnext && (*pnext)->type == a->type) {
if (*pnext == a) {
*pnext = a->next;
return 1;
}
pnext = &(*pnext)->next;
}
} }
} return 0;
return 0; }
static void a_free(attrib * a)
{
const attrib_type *at = a->type;
if (at->finalize)
at->finalize(a);
free(a);
} }
int a_remove(attrib ** pa, attrib * a) int a_remove(attrib ** pa, attrib * a)
{ {
int ok; int ok;
assert(a != NULL); assert(a != NULL);
ok = a_unlink(pa, a); ok = a_unlink(pa, a);
if (ok) if (ok)
a_free(a); a_free(a);
return ok; return ok;
} }
void a_removeall(attrib ** pa, const attrib_type * at) void a_removeall(attrib ** pa, const attrib_type * at)
{ {
attrib **pnexttype = pa; attrib **pnexttype = pa;
attrib **pnext = NULL; attrib **pnext = NULL;
while (*pnexttype) { while (*pnexttype) {
attrib *next = *pnexttype; attrib *next = *pnexttype;
if (next->type == at) if (next->type == at)
break; break;
pnexttype = &next->nexttype; pnexttype = &next->nexttype;
pnext = &next->next; pnext = &next->next;
} }
if (*pnexttype && (*pnexttype)->type == at) { if (*pnexttype && (*pnexttype)->type == at) {
attrib *a = *pnexttype; attrib *a = *pnexttype;
*pnexttype = a->nexttype; *pnexttype = a->nexttype;
if (pnext) { if (pnext) {
while (*pnext && (*pnext)->type != at) while (*pnext && (*pnext)->type != at)
pnext = &(*pnext)->next; pnext = &(*pnext)->next;
*pnext = a->nexttype; *pnext = a->nexttype;
}
while (a && a->type == at) {
attrib *ra = a;
a = a->next;
a_free(ra);
}
} }
while (a && a->type == at) {
attrib *ra = a;
a = a->next;
a_free(ra);
}
}
} }
attrib *a_new(const attrib_type * at) attrib *a_new(const attrib_type * at)
{ {
attrib *a = (attrib *) calloc(1, sizeof(attrib)); attrib *a = (attrib *)calloc(1, sizeof(attrib));
assert(at != NULL); assert(at != NULL);
a->type = at; a->type = at;
if (at->initialize) if (at->initialize)
at->initialize(a); at->initialize(a);
return a; return a;
} }
int a_age(attrib ** p) int a_age(attrib ** p)
{ {
attrib **ap = p; attrib **ap = p;
/* Attribute altern, und die Entfernung (age()==0) eines Attributs /* Attribute altern, und die Entfernung (age()==0) eines Attributs
* hat Einfluß auf den Besitzer */ * hat Einfluß auf den Besitzer */
while (*ap) { while (*ap) {
attrib *a = *ap; attrib *a = *ap;
if (a->type->age) { if (a->type->age) {
int result = a->type->age(a); int result = a->type->age(a);
assert(result >= 0 || !"age() returned a negative value"); assert(result >= 0 || !"age() returned a negative value");
if (result == 0) { if (result == 0) {
a_remove(p, a); a_remove(p, a);
continue; continue;
} }
}
ap = &a->next;
} }
ap = &a->next; return (*p != NULL);
}
return (*p != NULL);
} }
static critbit_tree cb_deprecated = { 0 }; static critbit_tree cb_deprecated = { 0 };
void at_deprecate(const char * name, int (*reader)(attrib *, void *, struct storage *)) void at_deprecate(const char * name, int(*reader)(attrib *, void *, struct storage *))
{ {
char buffer[64]; char buffer[64];
size_t len = strlen(name); size_t len = strlen(name);
len = cb_new_kv(name, len, &reader, sizeof(reader), buffer); len = cb_new_kv(name, len, &reader, sizeof(reader), buffer);
cb_insert(&cb_deprecated, buffer, len); cb_insert(&cb_deprecated, buffer, len);
} }
int a_read(struct storage *store, attrib ** attribs, void *owner) int a_read(struct storage *store, attrib ** attribs, void *owner)
{ {
int key, retval = AT_READ_OK; int key, retval = AT_READ_OK;
char zText[128]; char zText[128];
zText[0] = 0;
key = -1;
READ_TOK(store, zText, sizeof(zText));
if (strcmp(zText, "end") == 0)
return retval;
else
key = __at_hashkey(zText);
while (key != -1) {
int (*reader)(attrib *, void *, struct storage *) = 0;
attrib_type *at = at_find(key);
attrib * na = 0;
if (at) {
reader = at->read;
na = a_new(at);
} else {
const void * kv;
cb_find_prefix(&cb_deprecated, zText, strlen(zText)+1, &kv, 1, 0);
if (kv) {
cb_get_kv(kv, &reader, sizeof(reader));
} else {
fprintf(stderr, "attribute hash: %d (%s)\n", key, zText);
assert(at || !"attribute not registered");
}
}
if (reader) {
int i = reader(na, owner, store);
if (na) {
switch (i) {
case AT_READ_OK:
a_add(attribs, na);
break;
case AT_READ_FAIL:
retval = AT_READ_FAIL;
a_free(na);
break;
default:
assert(!"invalid return value");
break;
}
}
} else {
assert(!"error: no registered callback can read attribute");
}
zText[0] = 0;
key = -1;
READ_TOK(store, zText, sizeof(zText)); READ_TOK(store, zText, sizeof(zText));
if (!strcmp(zText, "end")) if (strcmp(zText, "end") == 0)
break; return retval;
key = __at_hashkey(zText); else
} key = __at_hashkey(zText);
return retval;
while (key != -1) {
int(*reader)(attrib *, void *, struct storage *) = 0;
attrib_type *at = at_find(key);
attrib * na = 0;
if (at) {
reader = at->read;
na = a_new(at);
}
else {
const void * kv;
cb_find_prefix(&cb_deprecated, zText, strlen(zText) + 1, &kv, 1, 0);
if (kv) {
cb_get_kv(kv, &reader, sizeof(reader));
}
else {
fprintf(stderr, "attribute hash: %d (%s)\n", key, zText);
assert(at || !"attribute not registered");
}
}
if (reader) {
int i = reader(na, owner, store);
if (na) {
switch (i) {
case AT_READ_OK:
a_add(attribs, na);
break;
case AT_READ_FAIL:
retval = AT_READ_FAIL;
a_free(na);
break;
default:
assert(!"invalid return value");
break;
}
}
}
else {
assert(!"error: no registered callback can read attribute");
}
READ_TOK(store, zText, sizeof(zText));
if (!strcmp(zText, "end"))
break;
key = __at_hashkey(zText);
}
return retval;
} }
void a_write(struct storage *store, const attrib * attribs, const void *owner) void a_write(struct storage *store, const attrib * attribs, const void *owner)
{ {
const attrib *na = attribs; const attrib *na = attribs;
while (na) { while (na) {
if (na->type->write) { if (na->type->write) {
assert(na->type->hashkey || !"attribute not registered"); assert(na->type->hashkey || !"attribute not registered");
WRITE_TOK(store, na->type->name); WRITE_TOK(store, na->type->name);
na->type->write(na, owner, store); na->type->write(na, owner, store);
na = na->next; na = na->next;
} else { }
na = na->nexttype; else {
na = na->nexttype;
}
} }
} WRITE_TOK(store, "end");
WRITE_TOK(store, "end");
} }

View file

@ -1,7 +1,7 @@
/* /*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de> Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de> Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above purpose with or without fee is hereby granted, provided that the above
@ -18,66 +18,66 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef ATTRIB_H #ifndef ATTRIB_H
#define ATTRIB_H #define ATTRIB_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
struct gamedata; struct gamedata;
struct storage; struct storage;
typedef void (*afun) (void); typedef void(*afun) (void);
typedef struct attrib { typedef struct attrib {
const struct attrib_type *type; const struct attrib_type *type;
union { union {
afun f; afun f;
void *v; void *v;
int i; int i;
float flt; float flt;
char c; char c;
short s; short s;
short sa[2]; short sa[2];
char ca[4]; char ca[4];
} data; } data;
/* internal data, do not modify: */ /* internal data, do not modify: */
struct attrib *next; /* next attribute in the list */ struct attrib *next; /* next attribute in the list */
struct attrib *nexttype; /* skip to attribute of a different type */ struct attrib *nexttype; /* skip to attribute of a different type */
} attrib; } attrib;
#define ATF_UNIQUE (1<<0) /* only one per attribute-list */ #define ATF_UNIQUE (1<<0) /* only one per attribute-list */
#define ATF_PRESERVE (1<<1) /* preserve order in list. append to back */ #define ATF_PRESERVE (1<<1) /* preserve order in list. append to back */
#define ATF_USER_DEFINED (1<<2) /* use this to make udf */ #define ATF_USER_DEFINED (1<<2) /* use this to make udf */
typedef struct attrib_type { typedef struct attrib_type {
const char *name; const char *name;
void (*initialize) (struct attrib *); void(*initialize) (struct attrib *);
void (*finalize) (struct attrib *); void(*finalize) (struct attrib *);
int (*age) (struct attrib *); int(*age) (struct attrib *);
/* age returns 0 if the attribute needs to be removed, !=0 otherwise */ /* age returns 0 if the attribute needs to be removed, !=0 otherwise */
void(*write) (const struct attrib *, const void *owner, struct storage *); void(*write) (const struct attrib *, const void *owner, struct storage *);
int(*read) (struct attrib *, void *owner, struct storage *); /* return AT_READ_OK on success, AT_READ_FAIL if attrib needs removal */ int(*read) (struct attrib *, void *owner, struct storage *); /* return AT_READ_OK on success, AT_READ_FAIL if attrib needs removal */
unsigned int flags; unsigned int flags;
/* ---- internal data, do not modify: ---- */ /* ---- internal data, do not modify: ---- */
struct attrib_type *nexthash; struct attrib_type *nexthash;
unsigned int hashkey; unsigned int hashkey;
} attrib_type; } attrib_type;
extern void at_register(attrib_type * at); extern void at_register(attrib_type * at);
extern void at_deprecate(const char * name, int (*reader)(attrib *, void *, struct storage *)); extern void at_deprecate(const char * name, int(*reader)(attrib *, void *, struct storage *));
extern attrib *a_select(attrib * a, const void *data, extern attrib *a_select(attrib * a, const void *data,
bool(*compare) (const attrib *, const void *)); bool(*compare) (const attrib *, const void *));
extern attrib *a_find(attrib * a, const attrib_type * at); extern attrib *a_find(attrib * a, const attrib_type * at);
extern const attrib *a_findc(const attrib * a, const attrib_type * at); extern const attrib *a_findc(const attrib * a, const attrib_type * at);
extern attrib *a_add(attrib ** pa, attrib * at); extern attrib *a_add(attrib ** pa, attrib * at);
extern int a_remove(attrib ** pa, attrib * at); extern int a_remove(attrib ** pa, attrib * at);
extern void a_removeall(attrib ** a, const attrib_type * at); extern void a_removeall(attrib ** a, const attrib_type * at);
extern attrib *a_new(const attrib_type * at); extern attrib *a_new(const attrib_type * at);
extern void a_free(attrib * a);
extern int a_age(attrib ** attribs); extern int a_age(attrib ** attribs);
extern int a_read(struct storage *store, attrib ** attribs, void *owner); extern int a_read(struct storage *store, attrib ** attribs, void *owner);
extern void a_write(struct storage *store, const attrib * attribs, extern void a_write(struct storage *store, const attrib * attribs,
const void *owner); const void *owner);
#define DEFAULT_AGE NULL #define DEFAULT_AGE NULL
#define DEFAULT_INIT NULL #define DEFAULT_INIT NULL

84
src/util/attrib.test.c Normal file
View file

@ -0,0 +1,84 @@
#include <platform.h>
#include "attrib.h"
#include <CuTest.h>
#include <tests.h>
static void test_attrib_new(CuTest * tc)
{
attrib_type at_test = { "test" };
attrib * a;
CuAssertPtrNotNull(tc, (a = a_new(&at_test)));
CuAssertPtrEquals(tc, 0, a->next);
CuAssertPtrEquals(tc, 0, a->nexttype);
CuAssertPtrEquals(tc, (void *)a->type, (void *)&at_test);
a_remove(&a, a);
CuAssertPtrEquals(tc, 0, a);
}
static void test_attrib_add(CuTest * tc)
{
attrib_type at_foo = { "foo" };
attrib_type at_bar = { "bar" };
attrib *a, *alist = 0;
CuAssertPtrNotNull(tc, (a = a_new(&at_foo)));
CuAssertPtrEquals(tc, a, a_add(&alist, a));
CuAssertPtrEquals(tc, a, alist);
CuAssertPtrNotNull(tc, (a = a_add(&alist, a_new(&at_foo))));
CuAssertPtrEquals_Msg(tc, "new attribute not added after existing", alist->next, a);
CuAssertPtrNotNull(tc, (a = a_add(&alist, a_new(&at_bar))));
CuAssertPtrEquals_Msg(tc, "new atribute not added at end of list", alist->next->next, a);
CuAssertPtrNotNull(tc, (a = a_add(&alist, a_new(&at_foo))));
CuAssertPtrEquals_Msg(tc, "messages not sorted by type", alist->next->next, a);
a_removeall(&alist, &at_foo);
a_removeall(&alist, &at_bar);
}
static void test_attrib_remove(CuTest * tc)
{
attrib_type at_foo = { "foo" };
attrib *a, *alist = 0;
CuAssertPtrNotNull(tc, a_add(&alist, a_new(&at_foo)));
CuAssertPtrNotNull(tc, a = a_add(&alist, a_new(&at_foo)));
CuAssertIntEquals(tc, 1, a_remove(&alist, a));
CuAssertPtrNotNull(tc, alist);
CuAssertIntEquals(tc, 1, a_remove(&alist, alist));
CuAssertPtrEquals(tc, 0, alist);
}
static void test_attrib_nexttype(CuTest * tc)
{
attrib_type at_foo = { "foo" };
attrib_type at_bar = { "bar" };
attrib *a, *alist = 0;
CuAssertPtrNotNull(tc, (a = a_new(&at_foo)));
CuAssertPtrEquals(tc, 0, a->nexttype);
CuAssertPtrEquals(tc, a, a_add(&alist, a));
CuAssertPtrEquals(tc, 0, alist->nexttype);
CuAssertPtrNotNull(tc, a_add(&alist, a_new(&at_foo)));
CuAssertPtrEquals(tc, 0, alist->nexttype);
CuAssertPtrNotNull(tc, (a = a_add(&alist, a_new(&at_bar))));
CuAssertPtrEquals(tc, a, alist->nexttype);
CuAssertPtrEquals(tc, 0, a->nexttype);
a_removeall(&alist, &at_foo);
a_removeall(&alist, &at_bar);
}
CuSuite *get_attrib_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_attrib_new);
SUITE_ADD_TEST(suite, test_attrib_add);
SUITE_ADD_TEST(suite, test_attrib_remove);
SUITE_ADD_TEST(suite, test_attrib_nexttype);
return suite;
}