--- libaitio/src/Attic/array.c 2011/05/19 02:22:46 1.2.4.1 +++ libaitio/src/Attic/array.c 2011/05/19 03:01:20 1.2.4.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: array.c,v 1.2.4.1 2011/05/19 02:22:46 misho Exp $ +* $Id: array.c,v 1.2.4.2 2011/05/19 03:01:20 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -63,18 +63,82 @@ io_arrayInit(int numItems) } arr->arr_num = numItems; - arr->arr_data = calloc(arr->arr_num, sizeof(void*)); + arr->arr_data = calloc(io_arraySize(arr), sizeof(void*)); if (!arr->arr_data) { LOGERR; free(arr); return NULL; } else - memset(arr->arr_data, 0, arr->arr_num * sizeof(void*)); + memset(arr->arr_data, 0, io_arraySize(arr) * sizeof(void*)); return arr; } /* + * io_arrayFrom() - Create and fill array from array with pointers + * @argv = Array with pointers + * @argc = Number of Items, if 0 walk through argv and stop when reach NULL item + * return: NULL error, != NULL allocated memory for array + */ +inline array_t * +io_arrayFrom(const char **argv, int argc) +{ + array_t *arr = NULL; + const char **a = NULL; + register int num = 0; + + assert(argv); + if (!argv || !*argv || argc < 0) + return NULL; + + if (argc) + num = argc; + else + for (a = argv; *a; a++, num++); + + arr = malloc(sizeof(array_t)); + if (!arr) { + LOGERR; + return NULL; + } + + arr->arr_num = num; + arr->arr_data = calloc(io_arraySize(arr), sizeof(void*)); + if (!arr->arr_data) { + LOGERR; + free(arr); + return NULL; + } else + memcpy(arr->arr_data, argv, io_arraySize(arr) * sizeof(void*)); + + return arr; +} + +/* + * io_arrayTo() - Create and fill array with pointers from dynamic array + * @arr = Array + * return: NULL error, != NULL allocated memory for array + */ +inline char ** +io_arrayTo(array_t * __restrict arr) +{ + char **args = NULL; + + assert(arr); + if (!arr || !io_arraySize(arr)) + return NULL; + + args = (char **) calloc(io_arraySize(arr), sizeof(char*)); + if (!args) { + LOGERR; + return NULL; + } else + memcpy(args, arr->arr_data, io_arraySize(arr) * sizeof(char*)); + + return args; +} + +/* * io_arrayFree() - Free all data in dynamic array items * (WARNING! If assign static array dont use this!!!) * @arr = Array @@ -89,7 +153,7 @@ io_arrayFree(array_t * __restrict arr) if (!arr) return; - for (i = 0; i < arr->arr_num; i++) + for (i = 0; i < io_arraySize(arr); i++) if (arr->arr_data[i]) { free(arr->arr_data[i]); arr->arr_data[i] = NULL; @@ -128,7 +192,7 @@ io_arrayLen(array_t * __restrict arr) if (!arr) return -1; - for (i = arr->arr_num; i && !arr->arr_data[i - 1]; i--); + for (i = io_arraySize(arr); i && !arr->arr_data[i - 1]; i--); return i; } @@ -152,8 +216,8 @@ io_arrayGrow(array_t * __restrict arr, int newNumItems if (arr->arr_num == newNumItems) return 0; - if (arr->arr_num < newNumItems) { - n = newNumItems - arr->arr_num; + if (io_arraySize(arr) < newNumItems) { + n = newNumItems - io_arraySize(arr); } /* else for (i = newNumItems; i < arr->arr_num; i++) if (arr->arr_data[i]) @@ -161,13 +225,13 @@ io_arrayGrow(array_t * __restrict arr, int newNumItems */ arr->arr_num = newNumItems; - data = realloc(arr->arr_data, arr->arr_num * sizeof(void*)); + data = realloc(arr->arr_data, io_arraySize(arr) * sizeof(void*)); if (!data) { LOGERR; return -1; } else arr->arr_data = data; - memset(arr->arr_data + (arr->arr_num - n), 0, n * sizeof(void*)); + memset(arr->arr_data + (io_arraySize(arr) - n), 0, n * sizeof(void*)); return 0; } @@ -190,7 +254,7 @@ io_arrayVacuum(array_t * __restrict arr, int fromWhere else fromWhere &= 0x7; - num = arr->arr_num; + num = io_arraySize(arr); /* if (fromWhere & VACUUM_RIGHT) { for (cx = 0, i = num - 1; i && !arr->arr_data[i]; i--, cx++); @@ -238,7 +302,7 @@ io_arrayPush(array_t * __restrict arr, void **data) assert(arr); - for (i = 0; i < arr->arr_num; i++) + for (i = 0; i < io_arraySize(arr); i++) if (!arr->arr_data[i]) { if (data) arr->arr_data[i] = *data; @@ -264,7 +328,7 @@ io_arrayPop(array_t * __restrict arr, void ** __restri assert(arr); - for (i = arr->arr_num - 1; i >= 0; i--) + for (i = io_arraySize(arr) - 1; i >= 0; i--) if (arr->arr_data[i]) { if (data) *data = arr->arr_data[i]; @@ -293,10 +357,10 @@ io_arrayConcat(array_t * __restrict dest, array_t * __ if (!dest || !src) return -1; - n = dest->arr_num; - if (io_arrayGrow(dest, n + src->arr_num)) + n = io_arraySize(dest); + if (io_arrayGrow(dest, n + io_arraySize(src))) return -1; - memcpy(dest->arr_data + n, src->arr_data, src->arr_num * sizeof(void*)); + memcpy(dest->arr_data + n, src->arr_data, io_arraySize(src) * sizeof(void*)); return io_arraySize(dest); }