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 */
#include <util/attrib.h>
#include <util/base36.h>
#include <util/lists.h>
#include <util/language.h>
#include <util/parser.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/cvector.h>
#include <util/language.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/parser.h>
#include <util/rand.h>
@ -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);
}
}
}

View File

@ -38,7 +38,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/event.h>
#include <util/functions.h>
#include <util/language.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/resolve.h>
#include <util/storage.h>
@ -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;

View File

@ -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
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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);

View File

@ -22,64 +22,69 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <platform.h>
#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;
}

View File

@ -24,17 +24,7 @@ extern "C" {
#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 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
}