SDLGameEngine

src/sgelist.c

00001 /*
00002  * Copyright (c) 2007 Heiko Irrgang
00003  *
00004  * The license and distribution terms for this file may be
00005  * found in the file COPYING in this distribution or at
00006  * http://93-interactive.com/cms/products/software/sdl-game-engine/license/
00007  */
00008 
00009 #include <sge.h>
00010 
00011 SGELIST *sgeListNew() {
00012         SGELIST *ret;
00013         sgeNew(ret, SGELIST);
00014         ret->numberOfEntries=0;
00015         ret->first=NULL;
00016         ret->last=NULL;
00017         ret->entries=NULL;
00018         return ret;
00019 }
00020 
00021 SGELISTENTRY *sgeListAdd(SGELIST *l, const char *id, void *data) {
00022         SGELISTENTRY *ret;
00023 
00024         sgeNew(ret, SGELISTENTRY);
00025         l->numberOfEntries++;
00026         if (l!=NULL) {
00027                 ret->prev=l->last;
00028         } else {
00029                 ret->prev=NULL;
00030         }
00031         if (l!=NULL && l->last!=NULL) {
00032                 l->last->next=ret;
00033         }
00034         ret->next=NULL;
00035         ret->id=strdup(id);
00036         ret->data=data;
00037 
00038         if (l==NULL) return ret;
00039 
00040         if (l->first==NULL) l->first=ret;
00041         l->last=ret;
00042 
00043         return ret;
00044 }
00045 
00046 static void sgeListFreeList(SGELISTENTRY *l) {
00047         sgeFree(l->id);
00048         sgeFree(l);
00049 }
00050 
00051 SGELISTENTRY *sgeListInsert(SGELIST *l, SGELISTENTRY *le, const char *id, void *data) {
00052         SGELISTENTRY *ret=sgeListAdd(NULL, id, data);
00053         SGELISTENTRY *tmp;
00054 
00055         l->numberOfEntries++;
00056         if (le->prev==NULL) {
00057                 ret->next=le;
00058                 le->prev=ret;
00059                 l->first=ret;
00060                 return ret;
00061         }
00062 
00063         tmp=le->prev;
00064         tmp->next=ret;
00065         ret->prev=tmp;
00066         ret->next=le;
00067         le->prev=ret;
00068 
00069         return ret;
00070 }
00071 
00072 SGELISTENTRY *sgeListSearch(SGELIST *l, char *id) {
00073         SGELISTENTRY *act=l->first;
00074 
00075         while (act!=NULL) {
00076                 if (strcmp(act->id,id)==0) return act;
00077                 act=act->next;
00078         }
00079         return NULL;
00080 }
00081 
00082 void sgeListRemove(SGELIST *l, char *id) {
00083         SGELISTENTRY *i=sgeListSearch(l, id);
00084         SGELISTENTRY *other=NULL;
00085         if (i==NULL) return;
00086 
00087         l->numberOfEntries--;
00088         if ((i->prev==NULL) && (i->next==NULL)) {
00089                 sgeListFreeList(i);
00090                 return;
00091         }
00092 
00093         if (i->prev!=NULL) {
00094                 other=i->prev;
00095                 other->next=i->next;
00096         }
00097         if (i->next!=NULL) {
00098                 other=i->next;
00099                 other->prev=i->prev;
00100         }
00101         sgeListFreeList(i);
00102         return;
00103 }
00104 
00105 void sgeListForEach(SGELIST *l, SGELISTFUNCTION function) {
00106         SGELISTENTRY *iter=l->first;
00107         do {
00108                 function(iter->id, iter->data);
00109                 iter=iter->next;
00110         } while (iter!=NULL);
00111 }
00112 
00113 void sgeListDestroy(SGELIST *l) {
00114         SGELISTENTRY *iter=l->first;
00115         SGELISTENTRY *tmp;
00116         do {
00117                 tmp=iter;
00118                 iter=iter->next;
00119                 sgeListFreeList(tmp);
00120         } while (iter!=NULL);
00121         sgeFree(l);
00122 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines