From 0adf7899ddf51c781a6287298d6b84e028e63c39 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 21 Feb 2011 22:00:03 -0800 Subject: [PATCH] cleaning up the lists functionality a bit. I have plans. --- src/kernel/alliance.c | 1 - src/kernel/battle.c | 7 ++- src/kernel/building.c | 5 +- src/kernel/config.c | 3 +- src/kernel/faction.h | 10 ++-- src/kernel/order.c | 5 ++ src/kernel/order.h | 2 + src/util/lists.c | 118 +++++++++++++++++++----------------------- src/util/lists.h | 13 ----- 9 files changed, 73 insertions(+), 91 deletions(-) diff --git a/src/kernel/alliance.c b/src/kernel/alliance.c index cec763793..cfacb00c2 100644 --- a/src/kernel/alliance.c +++ b/src/kernel/alliance.c @@ -31,7 +31,6 @@ without prior permission by the authors of Eressea. /* util includes */ #include #include -#include #include #include #include diff --git a/src/kernel/battle.c b/src/kernel/battle.c index db0284ec6..0aaa0e840 100644 --- a/src/kernel/battle.c +++ b/src/kernel/battle.c @@ -58,7 +58,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include @@ -4175,7 +4174,11 @@ battle_stats(FILE * F, battle * b) for (stat=stats;stat!=NULL;stat=stat->next) { fprintf(F, "%s %u : %u\n", stat->wtype?stat->wtype->itype->rtype->_name[0]:"none", stat->level, stat->number); } - freelist(stats); + while(stats) { + stat_info * stat = stats; + stats = stat->next; + free(stat); + } } } diff --git a/src/kernel/building.c b/src/kernel/building.c index 3b2553812..0ab58de70 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -38,7 +38,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include @@ -454,6 +453,7 @@ add_buildinglist(building_list **blist, building *b) building * new_building(const struct building_type * btype, region * r, const struct locale * lang) { + building ** bptr = &r->buildings; building *b = (building *) calloc(1, sizeof(building)); static boolean init_lighthouse = false; static const struct building_type * bt_lighthouse = 0; @@ -469,7 +469,8 @@ new_building(const struct building_type * btype, region * r, const struct locale b->type = btype; b->region = r; - addlist(&r->buildings, b); + while (*bptr) bptr=&(*bptr)->next; + *bptr = b; if (b->type==bt_lighthouse) { r->flags |= RF_LIGHTHOUSE; diff --git a/src/kernel/config.c b/src/kernel/config.c index 62c2de98a..d0885efd2 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -729,7 +729,7 @@ verify_data(void) if (verbosity>=1) puts(" - Überprüfe Daten auf Korrektheit..."); - list_foreach(faction, factions, f) { + for (f=factions; f; f=f->next) { mage = 0; alchemist = 0; for (u=f->units;u;u=u->nextF) { @@ -751,7 +751,6 @@ verify_data(void) if (alchemist > 3) log_error(("Partei %s hat %d Alchemisten.\n", factionid(f), alchemist)); } - list_next(f); #endif } diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 910929937..198af7d88 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -75,13 +75,13 @@ typedef struct faction { int max_spelllevel; struct spell_list * spellbook; const struct locale * locale; - int lastorders; /* enno: short? */ - int age; /* enno: short? */ + int lastorders; + int age; struct ursprung *ursprung; const struct race * race; magic_t magiegebiet; int newbies; - int num_people; /* Anzahl Personen ohne Monster */ + int num_people; /* Anzahl Personen ohne Monster */ int num_total; /* Anzahl Personen mit Monstern */ int options; int no_units; @@ -114,8 +114,8 @@ typedef struct faction { extern struct faction *factions; typedef struct faction_list { - struct faction_list * next; - struct faction * data; + struct faction_list * next; + struct faction * data; } faction_list; void flist_add(struct faction_list ** flist, struct faction * sf); diff --git a/src/kernel/order.c b/src/kernel/order.c index e1513a01f..ad05af80a 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -609,3 +609,8 @@ write_order(const order * ord, char * buffer, size_t size) } return buffer; } + +void push_order(order ** ordp, order * ord) { + while (*ordp) ordp=&(*ordp)->next; + *ordp = ord; +} \ No newline at end of file diff --git a/src/kernel/order.h b/src/kernel/order.h index f840ad7fc..7b229644d 100644 --- a/src/kernel/order.h +++ b/src/kernel/order.h @@ -44,6 +44,8 @@ extern order * copy_order(const order * ord); extern void free_order(order * ord); extern void free_orders(order ** olist); +extern void push_order(struct order ** olist, struct order * ord); + /* access functions for orders */ extern keyword_t get_keyword(const order * ord); extern void set_order(order ** destp, order * src); diff --git a/src/util/lists.c b/src/util/lists.c index cff4b287c..4c4173cb5 100644 --- a/src/util/lists.c +++ b/src/util/lists.c @@ -22,64 +22,69 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "lists.h" +typedef struct void_list { + struct void_list * next; + void * data; +} void_list; + void addlist(void *l1, void *p1) { - /* add entry p to the end of list l */ + /* add entry p to the end of list l */ - void_list **l; - void_list *p, *q; + void_list **l; + void_list *p, *q; - l = (void_list **)l1; - p = (void_list *)p1; - assert(p->next == 0); + l = (void_list **)l1; + p = (void_list *)p1; + assert(p->next == 0); - if (*l) { - for (q = *l; q->next; q = q->next) - assert(q); - q->next = p; - } else - *l = p; + if (*l) { + for (q = *l; q->next; q = q->next) + assert(q); + q->next = p; + } else + *l = p; } -void +static void choplist(void * a, void * b) { - void_list **l = (void_list**)a, *p = (void_list*)b; - /* remove entry p from list l - when called, a pointer to p must be - * kept in order to use (and free) p; if omitted, this will be a - * memory leak */ + void_list **l = (void_list**)a, *p = (void_list*)b; + /* remove entry p from list l - when called, a pointer to p must be + * kept in order to use (and free) p; if omitted, this will be a + * memory leak */ - void_list **q; + void_list **q; - for (q = l; *q; q = &((*q)->next)) { - if (*q == p) { - *q = p->next; - p->next = 0; - break; - } - } + for (q = l; *q; q = &((*q)->next)) { + if (*q == p) { + *q = p->next; + p->next = 0; + break; + } + } } void translist(void *l1, void *l2, void *p) { - /* remove entry p from list l1 and add it at the end of list l2 */ + /* remove entry p from list l1 and add it at the end of list l2 */ - choplist(l1, p); - addlist(l2, p); + choplist(l1, p); + addlist(l2, p); } void insertlist(void_list ** l, void_list * p) { - /* insert entry p at the beginning of list l */ + /* insert entry p at the beginning of list l */ - p->next = *l; - *l = p; + p->next = *l; + *l = p; } @@ -87,57 +92,38 @@ void removelist(void *l, void *p) { - /* remove entry p from list l; free p */ + /* remove entry p from list l; free p */ - choplist(l, p); - free(p); + choplist(l, p); + free(p); } void freelist(void *p1) { - /* remove all entries following and including entry p from a listlist */ + /* remove all entries following and including entry p from a listlist */ - void_list *p, *p2; + void_list *p, *p2; - p = (void_list *)p1; + p = (void_list *)p1; - while (p) { - p2 = p->next; - free(p); - p = p2; - } + while (p) { + p2 = p->next; + free(p); + p = p2; + } } unsigned int listlen(void *l) { - /* count entries p in list l */ + /* count entries p in list l */ - unsigned int i; - void_list *p; + unsigned int i; + void_list *p; - for (p = (void_list *)l, i = 0; p; p = p->next, i++); - return i; + for (p = (void_list *)l, i = 0; p; p = p->next, i++); + return i; } - -/* Hilfsfunktion, um das Debugging zu erleichtern. Statt print - * (cast)foo->next->next->next->next nur noch - * print (cast)listelem(foo, 3) */ - -void * -listelem(void *l, int n) -{ - int i=0; - - while(i < n && l != NULL) { - l = ((void_list *)l)->next; - i++; - } - - return l; -} - - diff --git a/src/util/lists.h b/src/util/lists.h index 588cc4878..848831817 100644 --- a/src/util/lists.h +++ b/src/util/lists.h @@ -24,17 +24,7 @@ extern "C" { #include -typedef struct void_list { - struct void_list * next; - void * data; -} void_list; - -#define list_foreach(type, list, item) item=list; while (item!=NULL) { type* __next__=item->next; -#define list_continue(item) { item=__next__; continue; } -#define list_next(item) item=__next__; } - void addlist(void *l1, void *p1); -void choplist(void * l, void * p); void translist(void *l1, void *l2, void *p); #ifndef MALLOCDBG void freelist(void *p1); @@ -45,9 +35,6 @@ void removelist(void *l, void *p); #endif unsigned int listlen(void *l); -#define addlist2(l, p) (*l = p, l = &p->next) - -void *listelem(void *l, int n); #ifdef __cplusplus }