summaryrefslogtreecommitdiff
path: root/modules/list.c
diff options
context:
space:
mode:
authororoborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e>2008-05-12 20:39:34 +0000
committeroroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e>2008-05-12 20:39:34 +0000
commit7055882c93022da3a6a1cf74cebf3d17c44ce6c4 (patch)
treebacb905bbfb33a5ee5a203c4e327495fcbd85ef7 /modules/list.c
parent46958f55432bcbb6f4e90f4dd937d8de355e5785 (diff)
- podpora modulov
- hra sa musi kompilovat s GUI ako v config.h _zakomenotvane_ #define PUBLIC_SERVER make clean make make mod su make install - hra sa musi ako server kompilovat v config.h _odkomentovane__ #define PUBLIC_SERVER make clean make make mod - pozor, urcite tam je este vela bugov //je to nedoladene zatial to ani nedavajte na server git-svn-id: http://opensvn.csie.org/tuxanci_ng@52 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
Diffstat (limited to 'modules/list.c')
-rwxr-xr-xmodules/list.c236
1 files changed, 236 insertions, 0 deletions
diff --git a/modules/list.c b/modules/list.c
new file mode 100755
index 0000000..56f7e17
--- /dev/null
+++ b/modules/list.c
@@ -0,0 +1,236 @@
+
+#include "main.h"
+#include "list.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+list_t* newList()
+{
+ list_t *new;
+
+ new = malloc( sizeof(list_t) );
+ memset(new, 0, sizeof(list_t) );
+
+ return new;
+}
+
+list_t* cloneList(list_t *p)
+{
+ list_t *new;
+
+ assert( p != NULL );
+
+ new = malloc( sizeof(list_t) );
+ memcpy(new, p, sizeof(list_t) );
+
+ if( p->list != NULL )
+ {
+ new->list = malloc( p->alloc * sizeof(void *) );
+ memcpy(new->list, p->list, p->alloc * sizeof(void *));
+ }
+
+ return new;
+}
+
+list_t* cloneListItem(list_t *p, void *f)
+{
+ list_t *new;
+ void* (*fce)(void *);
+ int i;
+
+ assert( p != NULL );
+ assert( f != NULL );
+
+ new = cloneList(p);
+ fce = f;
+
+ for(i = 0 ; i < p->count ; i++)
+ new->list[i] = fce(p->list[i]);
+
+ return new;
+}
+
+void addList(list_t *p, void *item)
+{
+ assert( p != NULL );
+
+ if( p->alloc == 0 )
+ {
+ p->alloc = LIST_ALLOC_LIMIT;
+ p->count = 1;
+ p->list = malloc(p->alloc * sizeof(void *) );
+ p->list[0] = item;
+
+ return;
+ }
+
+ if( p->count + 1 <= p->alloc )
+ {
+ p->list[p->count] = item;
+ p->count += 1;
+
+ return;
+ }
+
+ if( p->count + 1 > p->alloc )
+ {
+ void **new;
+
+#ifdef DEBUG_LIST
+ printf("realokujem z %d na %d (count=%d)\n",
+ p->alloc, p->alloc+LIST_ALLOC_LIMIT, p->count);
+#endif
+
+ p->alloc += LIST_ALLOC_LIMIT;
+ new = malloc(p->alloc * sizeof(void *) );
+ memcpy(new, p->list, p->count * sizeof(void *) );
+ free(p->list);
+ p->list = new;
+ p->list[p->count] = item;
+ p->count++;
+
+ return;
+ }
+}
+
+void insList(list_t *p, int n, void *item)
+{
+ assert( p != NULL );
+
+ addList(p, NULL); // :)
+
+ assert( n >= 0 || n < p->count );
+
+ memmove(p->list+n+1, p->list+n, ( (p->count-1) - n ) * sizeof(void *));
+
+ p->list[n] = item;
+}
+
+void *getList(list_t *p, int n)
+{
+ assert( p != NULL );
+ assert( n >= 0 || n < p->count );
+
+ return p->list[n];
+}
+
+int searchListItem(list_t *p, void *n)
+{
+ int i;
+
+ assert( p != NULL );
+
+ for( i = 0 ; i < p->count ; i++ )
+ if( p->list[i] == n )return i;
+
+ return -1;
+}
+
+void delList(list_t *p, int n)
+{
+ assert( p != NULL );
+ assert( n >= 0 || n < p->count );
+
+ memmove(p->list+n, p->list+n+1, ( (p->count-1) - n ) * sizeof(void *));
+
+ p->count--;
+
+ if( p->count + LIST_ALLOC_LIMIT < p->alloc )
+ {
+ void **new;
+
+#ifdef DEBUG_LIST
+ printf("realokujem z %d na %d (count=%d)\n",
+ p->alloc, p->alloc-LIST_ALLOC_LIMIT, p->count);
+#endif
+
+ p->alloc -= LIST_ALLOC_LIMIT;
+ new = malloc(p->alloc * sizeof(void *) );
+ memcpy(new, p->list, p->count * sizeof(void *) );
+ free(p->list);
+ p->list = new;
+ }
+}
+
+void delListItem(list_t *p, int n, void *f)
+{
+ int (*fce)(void *);
+
+ assert( p != NULL );
+ assert( n >= 0 || n < p->count );
+
+ fce = f;
+ if( fce != NULL )fce(p->list[n]);
+
+ delList(p, n);
+}
+
+void destroyList(list_t *p)
+{
+ assert( p != NULL );
+
+ if( p->list != NULL )free(p->list);
+ free(p);
+}
+
+void destroyListItem(list_t *p, void *f)
+{
+ void (*fce)(void *);
+ int i;
+
+ assert( p != NULL );
+ assert( f != NULL );
+
+ fce = f;
+
+ for(i = 0 ; i < p->count ; i++)
+ fce(p->list[i]);
+
+ destroyList(p);
+}
+
+/*
+typedef struct pokus_str
+{
+ int x;
+} pokus;
+
+pokus* newPokus(int n)
+{
+ pokus *new;
+ new = malloc(sizeof(pokus));
+ new->x = n;
+ return new;
+}
+
+pokus *clonePokus(pokus *p)
+{
+ pokus *new;
+ new = malloc(sizeof(pokus));
+ memcpy(new, p, sizeof(pokus));
+ return new;
+}
+
+void destroyPokus(pokus *p)
+{
+ free(p);
+}
+
+int main()
+{
+ list_t *my;
+ int i;
+
+ my = newList();
+
+ for(i = 0; i < 100; i++)
+ addList(my, newPokus(i));
+
+ destroyListItem(my, destroyPokus);
+
+ return 0;
+}
+*/