/************************************************************************* * (C) 2011 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: array.c,v 1.9 2019/01/23 18:22:41 misho Exp $ * ************************************************************************** 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 Copyright 2004 - 2019 by Michael Pounov . 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 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" /* * array_Init() - Create and initialize dynamic array * * @numItems = Number of Items * return: NULL error, != NULL allocated memory for array */ array_t * array_Init(int numItems) { array_t *arr = NULL; arr = e_malloc(sizeof(array_t)); if (!arr) return NULL; arr->arr_last = -1; arr->arr_num = numItems; arr->arr_data = e_calloc(array_Size(arr), sizeof(intptr_t)); if (!arr->arr_data) { e_free(arr); return NULL; } else memset(arr->arr_data, 0, array_Size(arr) * sizeof(intptr_t)); return arr; } /* * array_Init2() - Initialize dynamic array * * @arr = Allocated array variable * @numItems = Number of Items * return: NULL error, != NULL allocated memory for array */ array_t * array_Init2(array_t * __restrict arr, int numItems) { if (!arr) return array_Init(numItems); if (array_Size(arr)) return NULL; /* already allocated array! */ arr->arr_last = -1; arr->arr_num = numItems; arr->arr_data = e_calloc(array_Size(arr), sizeof(intptr_t)); if (!arr->arr_data) { e_free(arr); return NULL; } else memset(arr->arr_data, 0, array_Size(arr) * sizeof(intptr_t)); return arr; } /* * array_From() - Create and fill array from array with pointers * * @pargv = Array with pointers * @argc = Number of Items, if 0 walk through argv and stop when reach NULL item * return: NULL error, != NULL allocated new array */ array_t * array_From(const char *** __restrict pargv, int argc) { array_t *arr = NULL; const char **a = NULL; register int num = 0; if (!pargv || !*pargv || argc < 0) return NULL; if (argc) num = argc; else for (a = *pargv; *a; a++, num++); arr = array_Init(num); if (!arr) return NULL; else { memcpy(arr->arr_data, *pargv, array_Size(arr) * sizeof(intptr_t)); arr->arr_last = array_Size(arr) - 1; } return arr; } /* * array_To() - Create and fill array with pointers from dynamic array * * @arr = Array * return: NULL error, != NULL allocated memory for array, NULL terminated */ char ** array_To(array_t * __restrict arr) { char **args = NULL; if (!arr || !array_Size(arr)) return NULL; args = (char **) e_calloc(array_Size(arr) + 1, sizeof(char*)); if (!args) return NULL; else memcpy(args, arr->arr_data, array_Size(arr) * sizeof(char*)); args[array_Size(arr)] = NULL; return args; } /* * array_Free() - Free all data in dynamic array items * (WARNING! If assign static array dont use this!!!) * * @arr = Array * return: none */ void array_Free(array_t * __restrict arr) { register int i; if (!arr) return; for (i = 0; i < array_Size(arr); i++) if (arr->arr_data[i]) e_free(arr->arr_data[i]); array_Zero(arr); } /* * array_Destroy() - Free and destroy dynamic array * * @parr = Array * return: none */ void array_Destroy(array_t ** __restrict parr) { if (!parr || !*parr) return; if ((*parr)->arr_data) e_free((*parr)->arr_data); e_free(*parr); *parr = NULL; } /* * array_Destroy2() - Free data in dynamic array * * @parr = Array * return: none */ void array_Destroy2(array_t * __restrict arr) { if (!arr) return; if (arr->arr_data) e_free(arr->arr_data); memset(arr, 0, sizeof(array_t)); } /* * array_Reset() - Reset array to initial state * * @parr = Array * @purge = Purge all data, if <>0 then will be free entire data memory * return: none */ void array_Reset(array_t * __restrict arr, int purge) { if (!arr) return; if (purge && arr->arr_data) { e_free(arr->arr_data); arr->arr_num = 0; arr->arr_data = e_calloc(array_Size(arr), sizeof(intptr_t)); } array_Zero(arr); } /* * array_Len() - Get last used element in dynamic array (array Length) * * @arr = Array * return: -1 empty or >-1 position of last used element */ int array_Len(array_t * __restrict arr) { register int i; if (!arr) return -1; for (i = array_Size(arr); i && !arr->arr_data[i - 1]; i--); return --i; } /* * array_Grow() - Grow/Shrink dynamic array, Use with care when it shrink!!! * * @arr = Array * @newNumItems = Number of Items * @freeShrink = Free elements before shrink array * return: -1 error, 0 ok */ int array_Grow(array_t * __restrict arr, int newNumItems, int freeShrink) { void **data; int n = 0; register int i; if (!arr) return -1; if (arr->arr_num == newNumItems) return 0; if (array_Size(arr) < newNumItems) { n = newNumItems - array_Size(arr); } else if (freeShrink) for (i = newNumItems; i < arr->arr_num; i++) if (arr->arr_data[i]) e_free(arr->arr_data[i]); arr->arr_num = newNumItems; if (array_Size(arr)) { data = e_realloc(arr->arr_data, array_Size(arr) * sizeof(intptr_t)); if (!data) return -1; else arr->arr_data = data; memset(arr->arr_data + (array_Size(arr) - n), 0, n * sizeof(intptr_t)); arr->arr_last = array_Len(arr); } else { if (arr->arr_data) e_free(arr->arr_data); arr->arr_data = NULL; arr->arr_last = -1; } return 0; } /* * array_Vacuum() - Vacuum dynamic array, empty elements will be deleted * * @arr = Array * @fromWhere = 1 begin, 2 ALL empty elements * return: -1 error, 0 ok */ int array_Vacuum(array_t * __restrict arr, int fromWhere) { register int i, j, num; int cx = 0; if (!arr) return -1; else fromWhere &= 0x7; num = array_Size(arr); #ifdef VACUUM_RIGHT if (fromWhere & VACUUM_RIGHT) { for (cx = 0, i = num - 1; i && !arr->arr_data[i]; i--, cx++); num -= cx; } #endif if (fromWhere & VACUUM_LEFT) { for (i = 0; i < num && !arr->arr_data[i]; i++); if (i) { memmove(arr->arr_data, arr->arr_data + i, (num - i) * sizeof(intptr_t)); memset(arr->arr_data + (num - i), 0, i * sizeof(intptr_t)); num -= i; cx += i; } } if (fromWhere & VACUUM_BETWEEN) { for (i = 0; i < num; i++) { if (arr->arr_data[i]) continue; for (j = i; j < num && !arr->arr_data[j]; j++); memmove(arr->arr_data + i, arr->arr_data + j, (num - j) * sizeof(intptr_t)); memset(arr->arr_data + i + (num - j), 0, (j - i) * sizeof(intptr_t)); num -= j - i; cx += j - i; } } arr->arr_last = array_Len(arr); return cx; } /* * array_Concat() Concat source array to destination array * * @dest = Destination array * @src = Source array * return: -1 error; >0 new count of destination array */ int array_Concat(array_t * __restrict dest, array_t * __restrict src) { int n; if (!dest || !src) return -1; n = array_Size(dest); if (array_Grow(dest, n + array_Size(src), 0)) return -1; memcpy(dest->arr_data + n, src->arr_data, array_Size(src) * sizeof(intptr_t)); dest->arr_last = array_Len(dest); return array_Size(dest); } /* * array_Copy() Copy source array to destination array * * @dest = Destination array, after use free with array_Destroy() * @src = Source array * return: -1 error; >0 count of destination array */ int array_Copy(array_t ** __restrict dest, array_t * __restrict src) { if (!dest || !src) return -1; *dest = array_Init(array_Size(src)); if (!*dest) return -1; else (*dest)->arr_last = src->arr_last; memcpy((*dest)->arr_data, src->arr_data, array_Size(*dest) * sizeof(intptr_t)); return array_Size(*dest); } /* * array_Elem() - Always GET/PUT element into dynamic array, if not enough elements grow array * * @arr = Array * @n = Position * @data = Element, if set NULL GET element at position or !=NULL PUT element at position * return: -1 error or !=-1 return element at position */ void * array_Elem(array_t * __restrict arr, int n, void *data) { void *dat = NULL; if (!arr) return (void*) -1; if (n >= array_Size(arr) && array_Grow(arr, n + 1, 0)) return (void*) -1; dat = array_Get(arr, n); if (data) array_Set(arr, n, data); return dat; } /* * array_Push() - 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 * @nogrow = Don't grow array if not enough space * return: -1 not found empty position, array is full!, >-1 return position of stored element into array */ int array_Push(array_t * __restrict arr, void *data, int nogrow) { int ret = -1; if (!arr) return -1; else ret = array_Last(arr) + 1; if (nogrow && ret >= array_Size(arr)) return -1; if (!nogrow && ret >= array_Size(arr) && array_Grow(arr, ret + 1, 0)) return -1; ret = ++arr->arr_last; arr->arr_data[arr->arr_last] = data; return ret; } /* * array_Pop() - Pop element from dynamic array like stack manner, last used position * * @arr = Array * @data = Element, if set NULL return only last used position * @nodel = Don't delete after Pop element * return: -1 not found used position, array is empty!, >-1 return element position */ int array_Pop(array_t * __restrict arr, void ** __restrict data, int nodel) { int ret = -1; if (!arr) return -1; if ((ret = array_Last(arr)) != -1) { if (data) *data = arr->arr_data[arr->arr_last]; if (!nodel) arr->arr_data[arr->arr_last] = NULL; arr->arr_last--; } return ret; } /* * array_Args() Parse and make array from arguments ... (input string will be modified!!! * and output array must be free with array_Destroy() after use!) * * @psArgs = Input arguments line, after execute string is modified!!! * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs * @csDelim = Delimiter(s) for separate * @parr = Output array of arguments ... (must be free with array_Destroy() after use!) * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items */ int array_Args(char * __restrict psArgs, int nargs, const char *csDelim, array_t ** __restrict parr) { char **app; register int i; if (!psArgs || !csDelim || !parr) return -1; if (nargs) i = nargs; else i = str_ArgsNum(psArgs, csDelim); *parr = array_Init(i); if (!*parr) return -1; for (i = 0, app = (char**) (*parr)->arr_data; app < (char**) (*parr)->arr_data + (*parr)->arr_num && (*app = strsep((char **) &psArgs, csDelim)); **app ? i++ : i, **app ? app++ : app); (*parr)->arr_last = i - 1; return i; }