From d577d991b97ae2b5ee1af23641bcffc3f83af5b2 Mon Sep 17 00:00:00 2001 From: Pixel Date: Wed, 4 Nov 2009 11:56:41 -0800 Subject: Initial import. Contains the im, cd and iup librairies, and a "working" Makefile for them under linux. --- iup/src/iup_array.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100755 iup/src/iup_array.c (limited to 'iup/src/iup_array.c') diff --git a/iup/src/iup_array.c b/iup/src/iup_array.c new file mode 100755 index 0000000..e7d4e12 --- /dev/null +++ b/iup/src/iup_array.c @@ -0,0 +1,108 @@ +/** \file + * \brief Simple expandable array + * + * See Copyright Notice in "iup.h" + */ + +#include +#include +#include +#include + +#include "iup_array.h" +#include "iup_assert.h" + + +struct _Iarray +{ + void* data; + int count; + int max_count; + int elem_size; + int start_count; +}; + +Iarray* iupArrayCreate(int start_count, int elem_size) +{ + Iarray* iarray = (Iarray*)malloc(sizeof(Iarray)); + iarray->count = 0; + iarray->elem_size = elem_size; + iarray->max_count = start_count; + iarray->start_count = start_count; + iarray->data = malloc(elem_size*start_count); + iupASSERT(iarray->data!=NULL); + if (!iarray->data) + { + free(iarray); + return NULL; + } + memset(iarray->data, 0, elem_size*start_count); + return iarray; +} + +void iupArrayDestroy(Iarray* iarray) +{ + iupASSERT(iarray!=NULL); + if (!iarray) + return; + if (iarray->data) + { + memset(iarray->data, 0, iarray->elem_size*iarray->max_count); + free(iarray->data); + } + free(iarray); +} + +void* iupArrayGetData(Iarray* iarray) +{ + iupASSERT(iarray!=NULL); + if (!iarray) + return NULL; + return iarray->data; +} + +void* iupArrayInc(Iarray* iarray) +{ + iupASSERT(iarray!=NULL); + if (!iarray) + return NULL; + if (iarray->count >= iarray->max_count) + { + int old_count = iarray->max_count; + iarray->max_count += iarray->start_count; + iarray->data = realloc(iarray->data, iarray->elem_size*iarray->max_count); + iupASSERT(iarray->data!=NULL); + if (!iarray->data) + return NULL; + memset((unsigned char*)iarray->data + iarray->elem_size*old_count, 0, iarray->elem_size*(iarray->max_count-old_count)); + } + iarray->count++; + return iarray->data; +} + +void* iupArrayAdd(Iarray* iarray, int new_count) +{ + iupASSERT(iarray!=NULL); + if (!iarray) + return NULL; + if (iarray->count+new_count > iarray->max_count) + { + int old_count = iarray->max_count; + iarray->max_count += new_count; + iarray->data = realloc(iarray->data, iarray->elem_size*iarray->max_count); + iupASSERT(iarray->data!=NULL); + if (!iarray->data) + return NULL; + memset((unsigned char*)iarray->data + iarray->elem_size*old_count, 0, iarray->elem_size*(iarray->max_count-old_count)); + } + iarray->count += new_count; + return iarray->data; +} + +int iupArrayCount(Iarray* iarray) +{ + iupASSERT(iarray!=NULL); + if (!iarray) + return 0; + return iarray->count; +} -- cgit v1.2.3