cleaning up the lists functionality a bit. I have plans.

This commit is contained in:
Enno Rehling 2011-02-21 22:00:03 -08:00
parent e8d00d8744
commit 0adf7899dd
9 changed files with 73 additions and 91 deletions

View file

@ -31,7 +31,6 @@ without prior permission by the authors of Eressea.
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/lists.h>
#include <util/language.h> #include <util/language.h>
#include <util/parser.h> #include <util/parser.h>
#include <util/rng.h> #include <util/rng.h>

View file

@ -58,7 +58,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/bsdstring.h> #include <util/bsdstring.h>
#include <util/cvector.h> #include <util/cvector.h>
#include <util/language.h> #include <util/language.h>
#include <util/lists.h>
#include <util/log.h> #include <util/log.h>
#include <util/parser.h> #include <util/parser.h>
#include <util/rand.h> #include <util/rand.h>
@ -4175,7 +4174,11 @@ battle_stats(FILE * F, battle * b)
for (stat=stats;stat!=NULL;stat=stat->next) { 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); 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);
}
} }
} }

View file

@ -38,7 +38,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/event.h> #include <util/event.h>
#include <util/functions.h> #include <util/functions.h>
#include <util/language.h> #include <util/language.h>
#include <util/lists.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
#include <util/storage.h> #include <util/storage.h>
@ -454,6 +453,7 @@ add_buildinglist(building_list **blist, building *b)
building * building *
new_building(const struct building_type * btype, region * r, const struct locale * lang) new_building(const struct building_type * btype, region * r, const struct locale * lang)
{ {
building ** bptr = &r->buildings;
building *b = (building *) calloc(1, sizeof(building)); building *b = (building *) calloc(1, sizeof(building));
static boolean init_lighthouse = false; static boolean init_lighthouse = false;
static const struct building_type * bt_lighthouse = 0; 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->type = btype;
b->region = r; b->region = r;
addlist(&r->buildings, b); while (*bptr) bptr=&(*bptr)->next;
*bptr = b;
if (b->type==bt_lighthouse) { if (b->type==bt_lighthouse) {
r->flags |= RF_LIGHTHOUSE; r->flags |= RF_LIGHTHOUSE;

View file

@ -729,7 +729,7 @@ verify_data(void)
if (verbosity>=1) puts(" - Überprüfe Daten auf Korrektheit..."); if (verbosity>=1) puts(" - Überprüfe Daten auf Korrektheit...");
list_foreach(faction, factions, f) { for (f=factions; f; f=f->next) {
mage = 0; mage = 0;
alchemist = 0; alchemist = 0;
for (u=f->units;u;u=u->nextF) { for (u=f->units;u;u=u->nextF) {
@ -751,7 +751,6 @@ verify_data(void)
if (alchemist > 3) if (alchemist > 3)
log_error(("Partei %s hat %d Alchemisten.\n", factionid(f), alchemist)); log_error(("Partei %s hat %d Alchemisten.\n", factionid(f), alchemist));
} }
list_next(f);
#endif #endif
} }

View file

@ -75,13 +75,13 @@ typedef struct faction {
int max_spelllevel; int max_spelllevel;
struct spell_list * spellbook; struct spell_list * spellbook;
const struct locale * locale; const struct locale * locale;
int lastorders; /* enno: short? */ int lastorders;
int age; /* enno: short? */ int age;
struct ursprung *ursprung; struct ursprung *ursprung;
const struct race * race; const struct race * race;
magic_t magiegebiet; magic_t magiegebiet;
int newbies; int newbies;
int num_people; /* Anzahl Personen ohne Monster */ int num_people; /* Anzahl Personen ohne Monster */
int num_total; /* Anzahl Personen mit Monstern */ int num_total; /* Anzahl Personen mit Monstern */
int options; int options;
int no_units; int no_units;
@ -114,8 +114,8 @@ typedef struct faction {
extern struct faction *factions; extern struct faction *factions;
typedef struct faction_list { typedef struct faction_list {
struct faction_list * next; struct faction_list * next;
struct faction * data; struct faction * data;
} faction_list; } faction_list;
void flist_add(struct faction_list ** flist, struct faction * sf); void flist_add(struct faction_list ** flist, struct faction * sf);

View file

@ -609,3 +609,8 @@ write_order(const order * ord, char * buffer, size_t size)
} }
return buffer; return buffer;
} }
void push_order(order ** ordp, order * ord) {
while (*ordp) ordp=&(*ordp)->next;
*ordp = ord;
}

View file

@ -44,6 +44,8 @@ extern order * copy_order(const order * ord);
extern void free_order(order * ord); extern void free_order(order * ord);
extern void free_orders(order ** olist); extern void free_orders(order ** olist);
extern void push_order(struct order ** olist, struct order * ord);
/* access functions for orders */ /* access functions for orders */
extern keyword_t get_keyword(const order * ord); extern keyword_t get_keyword(const order * ord);
extern void set_order(order ** destp, order * src); extern void set_order(order ** destp, order * src);

View file

@ -22,64 +22,69 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <platform.h> #include <platform.h>
#include "lists.h" #include "lists.h"
typedef struct void_list {
struct void_list * next;
void * data;
} void_list;
void void
addlist(void *l1, void *p1) 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 **l;
void_list *p, *q; void_list *p, *q;
l = (void_list **)l1; l = (void_list **)l1;
p = (void_list *)p1; p = (void_list *)p1;
assert(p->next == 0); assert(p->next == 0);
if (*l) { if (*l) {
for (q = *l; q->next; q = q->next) for (q = *l; q->next; q = q->next)
assert(q); assert(q);
q->next = p; q->next = p;
} else } else
*l = p; *l = p;
} }
void static void
choplist(void * a, void * b) choplist(void * a, void * b)
{ {
void_list **l = (void_list**)a, *p = (void_list*)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 /* 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 * kept in order to use (and free) p; if omitted, this will be a
* memory leak */ * memory leak */
void_list **q; void_list **q;
for (q = l; *q; q = &((*q)->next)) { for (q = l; *q; q = &((*q)->next)) {
if (*q == p) { if (*q == p) {
*q = p->next; *q = p->next;
p->next = 0; p->next = 0;
break; break;
} }
} }
} }
void void
translist(void *l1, void *l2, void *p) 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); choplist(l1, p);
addlist(l2, p); addlist(l2, p);
} }
void void
insertlist(void_list ** l, void_list * p) 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; p->next = *l;
*l = p; *l = p;
} }
@ -87,57 +92,38 @@ void
removelist(void *l, void *p) removelist(void *l, void *p)
{ {
/* remove entry p from list l; free p */ /* remove entry p from list l; free p */
choplist(l, p); choplist(l, p);
free(p); free(p);
} }
void void
freelist(void *p1) 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) { while (p) {
p2 = p->next; p2 = p->next;
free(p); free(p);
p = p2; p = p2;
} }
} }
unsigned int unsigned int
listlen(void *l) listlen(void *l)
{ {
/* count entries p in list l */ /* count entries p in list l */
unsigned int i; unsigned int i;
void_list *p; void_list *p;
for (p = (void_list *)l, i = 0; p; p = p->next, i++); for (p = (void_list *)l, i = 0; p; p = p->next, i++);
return 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;
}

View file

@ -24,17 +24,7 @@ extern "C" {
#include <stddef.h> #include <stddef.h>
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 addlist(void *l1, void *p1);
void choplist(void * l, void * p);
void translist(void *l1, void *l2, void *p); void translist(void *l1, void *l2, void *p);
#ifndef MALLOCDBG #ifndef MALLOCDBG
void freelist(void *p1); void freelist(void *p1);
@ -45,9 +35,6 @@ void removelist(void *l, void *p);
#endif #endif
unsigned int listlen(void *l); unsigned int listlen(void *l);
#define addlist2(l, p) (*l = p, l = &p->next)
void *listelem(void *l, int n);
#ifdef __cplusplus #ifdef __cplusplus
} }