diff options
author | Pixel <pixel@nobis-crew.org> | 2009-11-04 11:56:41 -0800 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2009-11-04 11:59:33 -0800 |
commit | d577d991b97ae2b5ee1af23641bcffc3f83af5b2 (patch) | |
tree | 590639d50205d1bcfaff2a7d2dc6ebf3f373c7ed /cd/src/intcgm/list.c |
Initial import. Contains the im, cd and iup librairies, and a "working" Makefile for them under linux.
Diffstat (limited to 'cd/src/intcgm/list.c')
-rwxr-xr-x | cd/src/intcgm/list.c | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/cd/src/intcgm/list.c b/cd/src/intcgm/list.c new file mode 100755 index 0000000..6fefbbc --- /dev/null +++ b/cd/src/intcgm/list.c @@ -0,0 +1,117 @@ +/* +* list.c +* Generic List Manager +* TeCGraf +* +*/ + +#include <stdio.h> +#include <stdlib.h> +#include "list.h" + +TList *cgm_NewList ( void ) +{ + TList *l = (TList *) malloc ( sizeof (TList) ); + + list_nba(l) = 8; + list_head(l) = (void **)malloc(list_nba(l)*sizeof(void*)); + list_n(l) = 0; + + return l; +} + +TList *cgm_AppendList ( TList *l, void *i ) +{ + if ( l == NULL ) + return NULL; + + if (list_n(l) == list_nba(l)) + { + list_nba(l) += 32; + list_head(l) = (void **)realloc(list_head(l),list_nba(l)*sizeof(void*)); + } + + list_head(l)[list_n(l)]=i; + list_n(l)++; + + return l; +} + +TList *cgm_AddList ( TList *l, int n, void *info ) +{ + int i; + + if ( l == NULL) + return NULL; + + if ( n < 1) + n=1; + + if ( n > list_n(l) ) + return cgm_AppendList( l, info ); + + --n; /* o usuario ve a lista iniciando em 1 e a */ + /* lista e' implementada iniciando em 0 */ + + if (list_n(l) == list_nba(l)) + { + list_nba(l) *= 2; + list_head(l) = (void **)realloc(list_head(l),list_nba(l)*sizeof(void*)); + } + + for (i=list_n(l)-1; i>=n; --i) + list_head(l)[i+1]=list_head(l)[i]; + + list_head(l)[n]=info; + list_n(l)++; + return l; +} + +TList *cgm_DelList ( TList *l, int n ) +{ + int i; + + if ( l == NULL || list_n(l) == 0 || n < 0) + return NULL; + + if ( n < 1) + n=1; + + if ( n > list_n(l)) + n=list_n(l); + + --n; /* o usuario ve a lista iniciando em 1 e a */ + /* lista e' implementada iniciando em 0 */ + list_n(l)--; + + for (i=n; i<list_n(l); ++i) + list_head(l)[i]=list_head(l)[i+1]; + + return l; +} + +void *cgm_GetList ( TList *l, int n ) +{ + if ( l == NULL || n <= 0) + return NULL; + + return n > list_n(l) ? NULL : list_head(l)[n-1]; +} + +int cgm_DelEntry ( TList *l, void *entry ) +{ + int i=1; + + if ( l == NULL || list_n(l) == 0) + return 0; + + for (i=0; i< list_n(l); ++i) + if (list_head(l)[i]==entry) + { + cgm_DelList(l,i+1); /* o usuario ve a lista iniciando em 1 e a */ + /* lista e' implementada iniciando em 0 */ + return i+1; + } + return 0; +} + |