| version 1.1.2.2, 2011/04/19 22:00:44 | version 1.3, 2011/06/07 11:49:39 | 
| Line 1 | Line 1 | 
 | /************************************************************************* | /************************************************************************* | 
| * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com> | * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org> | 
| *  by Michael Pounov <misho@openbsd-bg.org> | *  by Michael Pounov <misho@elwix.org> | 
 | * | * | 
 | * $Author$ | * $Author$ | 
 | * $Id$ | * $Id$ | 
 | * | * | 
| *************************************************************************/ | ************************************************************************** | 
|  | The ELWIX and AITNET software is distributed under the following | 
|  | terms: | 
|  |  | 
|  | All of the documentation and software included in the ELWIX and AITNET | 
|  | Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> | 
|  |  | 
|  | Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 | 
|  | by Michael Pounov <misho@elwix.org>.  All rights reserved. | 
|  |  | 
|  | Redistribution and use in source and binary forms, with or without | 
|  | modification, are permitted provided that the following conditions | 
|  | are met: | 
|  | 1. Redistributions of source code must retain the above copyright | 
|  | notice, this list of conditions and the following disclaimer. | 
|  | 2. Redistributions in binary form must reproduce the above copyright | 
|  | notice, this list of conditions and the following disclaimer in the | 
|  | documentation and/or other materials provided with the distribution. | 
|  | 3. All advertising materials mentioning features or use of this software | 
|  | must display the following acknowledgement: | 
|  | This product includes software developed by Michael Pounov <misho@elwix.org> | 
|  | ELWIX - Embedded LightWeight unIX and its contributors. | 
|  | 4. Neither the name of AITNET nor the names of its contributors | 
|  | may be used to endorse or promote products derived from this software | 
|  | without specific prior written permission. | 
|  |  | 
|  | THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND | 
|  | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|  | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|  | ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 
|  | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
|  | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
|  | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
|  | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
|  | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
|  | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | SUCH DAMAGE. | 
|  | */ | 
 | #include "global.h" | #include "global.h" | 
 |  |  | 
 |  |  | 
| Line 26  io_arrayInit(int numItems) | Line 63  io_arrayInit(int numItems) | 
 | } | } | 
 |  |  | 
 | arr->arr_num = 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) { | if (!arr->arr_data) { | 
 | LOGERR; | LOGERR; | 
 | free(arr); | free(arr); | 
 | return NULL; | return NULL; | 
 | } else | } else | 
| memset(arr->arr_data, 0, arr->arr_num * sizeof(void*)); | memset(arr->arr_data, 0, io_arraySize(arr) * sizeof(void*)); | 
 |  |  | 
 | return arr; | return arr; | 
 | } | } | 
 |  |  | 
 | /* | /* | 
| * io_arrayFree() - Free all data in dynamic array | * 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) + 1, sizeof(char*)); | 
|  | if (!args) { | 
|  | LOGERR; | 
|  | return NULL; | 
|  | } else | 
|  | memcpy(args, arr->arr_data, io_arraySize(arr) * sizeof(char*)); | 
|  | args[io_arraySize(arr)] = NULL; | 
|  |  | 
|  | return args; | 
|  | } | 
|  |  | 
|  | /* | 
|  | * io_arrayFree() - Free all data in dynamic array items | 
 | *      (WARNING! If assign static array dont use this!!!) | *      (WARNING! If assign static array dont use this!!!) | 
 | * @arr = Array | * @arr = Array | 
 | * return: none | * return: none | 
| Line 52  io_arrayFree(array_t * __restrict arr) | Line 154  io_arrayFree(array_t * __restrict arr) | 
 | if (!arr) | if (!arr) | 
 | return; | return; | 
 |  |  | 
| for (i = 0; i < arr->arr_num; i++) | for (i = 0; i < io_arraySize(arr); i++) | 
 | if (arr->arr_data[i]) { | if (arr->arr_data[i]) { | 
 | free(arr->arr_data[i]); | free(arr->arr_data[i]); | 
 | arr->arr_data[i] = NULL; | arr->arr_data[i] = NULL; | 
| Line 60  io_arrayFree(array_t * __restrict arr) | Line 162  io_arrayFree(array_t * __restrict arr) | 
 | } | } | 
 |  |  | 
 | /* | /* | 
| * io_arrayDestroy() - Free all data in dynamic array and Destroy dynamic array | * io_arrayDestroy() - Free and destroy dynamic array | 
 | * @parr = Array | * @parr = Array | 
 | * return: none | * return: none | 
 | */ | */ | 
| Line 91  io_arrayLen(array_t * __restrict arr) | Line 193  io_arrayLen(array_t * __restrict arr) | 
 | if (!arr) | if (!arr) | 
 | return -1; | 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; | return i; | 
 | } | } | 
| Line 115  io_arrayGrow(array_t * __restrict arr, int newNumItems | Line 217  io_arrayGrow(array_t * __restrict arr, int newNumItems | 
 |  |  | 
 | if (arr->arr_num == newNumItems) | if (arr->arr_num == newNumItems) | 
 | return 0; | return 0; | 
| if (arr->arr_num < newNumItems) { | if (io_arraySize(arr) < newNumItems) { | 
| n = newNumItems - arr->arr_num; | n = newNumItems - io_arraySize(arr); | 
 | } /* else | } /* else | 
 | for (i = newNumItems; i < arr->arr_num; i++) | for (i = newNumItems; i < arr->arr_num; i++) | 
 | if (arr->arr_data[i]) | if (arr->arr_data[i]) | 
| Line 124  io_arrayGrow(array_t * __restrict arr, int newNumItems | Line 226  io_arrayGrow(array_t * __restrict arr, int newNumItems | 
 | */ | */ | 
 |  |  | 
 | arr->arr_num = newNumItems; | arr->arr_num = newNumItems; | 
| data = realloc(arr->arr_data, arr->arr_num * sizeof(void*)); | if (io_arraySize(arr)) { | 
| if (!data) { | data = realloc(arr->arr_data, io_arraySize(arr) * sizeof(void*)); | 
| LOGERR; | if (!data) { | 
| return -1; | LOGERR; | 
| } else | return -1; | 
| arr->arr_data = data; | } else | 
| memset(arr->arr_data + (arr->arr_num - n), 0, n * sizeof(void*)); | arr->arr_data = data; | 
 |  |  | 
 |  | memset(arr->arr_data + (io_arraySize(arr) - n), 0, n * sizeof(void*)); | 
 |  | } else { | 
 |  | if (arr->arr_data) | 
 |  | free(arr->arr_data); | 
 |  | arr->arr_data = NULL; | 
 |  | } | 
 |  |  | 
 | return 0; | return 0; | 
 | } | } | 
 |  |  | 
| Line 153  io_arrayVacuum(array_t * __restrict arr, int fromWhere | Line 262  io_arrayVacuum(array_t * __restrict arr, int fromWhere | 
 | else | else | 
 | fromWhere &= 0x7; | fromWhere &= 0x7; | 
 |  |  | 
| num = arr->arr_num; | num = io_arraySize(arr); | 
 | /* | /* | 
 | if (fromWhere & VACUUM_RIGHT) { | if (fromWhere & VACUUM_RIGHT) { | 
 | for (cx = 0, i = num - 1; i && !arr->arr_data[i]; i--, cx++); | for (cx = 0, i = num - 1; i && !arr->arr_data[i]; i--, cx++); | 
| Line 185  io_arrayVacuum(array_t * __restrict arr, int fromWhere | Line 294  io_arrayVacuum(array_t * __restrict arr, int fromWhere | 
 | } | } | 
 |  |  | 
 | return cx; | return cx; | 
 |  | } | 
 |  |  | 
 |  | /* | 
 |  | * io_arrayPush() - Push element into dynamic array like stack manner, place at first empty position | 
 |  | * @arr = Array | 
 |  | * @data = Element, if set NULL return only first empty position | 
 |  | * return: -1 not found empty position, array is full!, >-1 return position of stored element into array | 
 |  | */ | 
 |  | inline int | 
 |  | io_arrayPush(array_t * __restrict arr, void **data) | 
 |  | { | 
 |  | register int i; | 
 |  | int ret = -1; | 
 |  |  | 
 |  | assert(arr); | 
 |  |  | 
 |  | for (i = 0; i < io_arraySize(arr); i++) | 
 |  | if (!arr->arr_data[i]) { | 
 |  | if (data) | 
 |  | arr->arr_data[i] = *data; | 
 |  | ret = i; | 
 |  | break; | 
 |  | } | 
 |  |  | 
 |  | return ret; | 
 |  | } | 
 |  |  | 
 |  | /* | 
 |  | * io_arrayPop() - Pop element from dynamic array like stack manner, last used position | 
 |  | * @arr = Array | 
 |  | * @data = Element, if set NULL return only first empty position | 
 |  | * @delAfter = Delete after Pop element, !=0 delete element from array after return data | 
 |  | * return: -1 not found used position, array is empty!, >-1 return element position | 
 |  | */ | 
 |  | inline int | 
 |  | io_arrayPop(array_t * __restrict arr, void ** __restrict data, int delAfter) | 
 |  | { | 
 |  | register int i; | 
 |  | int ret = -1; | 
 |  |  | 
 |  | assert(arr); | 
 |  |  | 
 |  | for (i = io_arraySize(arr) - 1; i >= 0; i--) | 
 |  | if (arr->arr_data[i]) { | 
 |  | if (data) | 
 |  | *data = arr->arr_data[i]; | 
 |  | if (delAfter) | 
 |  | arr->arr_data[i] = NULL; | 
 |  | ret = i; | 
 |  | break; | 
 |  | } | 
 |  |  | 
 |  | return ret; | 
 |  | } | 
 |  |  | 
 |  | /* | 
 |  | * io_arrayConcat() Concat source array to destination array | 
 |  | * @dest = Destination array | 
 |  | * @src = Source array | 
 |  | * return: -1 error; >0 new count of destination array | 
 |  | */ | 
 |  | int | 
 |  | io_arrayConcat(array_t * __restrict dest, array_t * __restrict src) | 
 |  | { | 
 |  | int n; | 
 |  |  | 
 |  | assert(dest); | 
 |  | assert(src); | 
 |  | if (!dest || !src) | 
 |  | return -1; | 
 |  |  | 
 |  | n = io_arraySize(dest); | 
 |  | if (io_arrayGrow(dest, n + io_arraySize(src))) | 
 |  | return -1; | 
 |  | memcpy(dest->arr_data + n, src->arr_data, io_arraySize(src) * sizeof(void*)); | 
 |  |  | 
 |  | return io_arraySize(dest); | 
 | } | } | 
 |  |  | 
 | /* | /* |