File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / Attic / array.c
Revision 1.4.2.1: download - view: text, annotated - select for diffs - revision graph
Wed Aug 31 12:29:32 2011 UTC (13 years ago) by misho
Branches: io2_1
Diff to: branchpoint 1.4: preferred, unified
added many changes and features
- added macros for array
- reworked assert when destroy arrays
- added feature for free before shrink into io_arrayGrow
- add general feature for marshaling

    1: /*************************************************************************
    2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: array.c,v 1.4.2.1 2011/08/31 12:29:32 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: 
   48: 
   49: /*
   50:  * io_arrayInit() - Create and initialize dynamic array
   51:  * @numItems = Number of Items
   52:  * return: NULL error, != NULL allocated memory for array
   53:  */
   54: inline array_t *
   55: io_arrayInit(int numItems)
   56: {
   57: 	array_t *arr = NULL;
   58: 
   59: 	arr = malloc(sizeof(array_t));
   60: 	if (!arr) {
   61: 		LOGERR;
   62: 		return NULL;
   63: 	}
   64: 
   65: 	arr->arr_num = numItems;
   66: 	arr->arr_data = calloc(io_arraySize(arr), sizeof(void*));
   67: 	if (!arr->arr_data) {
   68: 		LOGERR;
   69: 		free(arr);
   70: 		return NULL;
   71: 	} else
   72: 		io_arrayZero(arr);
   73: 
   74: 	return arr;
   75: }
   76: 
   77: /*
   78:  * io_arrayFrom() - Create and fill array from array with pointers
   79:  * @pargv = Array with pointers
   80:  * @argc = Number of Items, if 0 walk through argv and stop when reach NULL item
   81:  * return: NULL error, != NULL allocated new array
   82:  */
   83: inline array_t *
   84: io_arrayFrom(const char *** __restrict pargv, int argc)
   85: {
   86: 	array_t *arr = NULL;
   87: 	const char **a = NULL;
   88: 	register int num = 0;
   89: 
   90: 	assert(pargv);
   91: 	if (!pargv || !*pargv || argc < 0)
   92: 		return NULL;
   93: 
   94: 	if (argc)
   95: 		num = argc;
   96: 	else
   97: 		for (a = *pargv; *a; a++, num++);
   98: 
   99: 	arr = malloc(sizeof(array_t));
  100: 	if (!arr) {
  101: 		LOGERR;
  102: 		return NULL;
  103: 	}
  104: 
  105: 	arr->arr_num = num;
  106: 	arr->arr_data = calloc(io_arraySize(arr), sizeof(void*));
  107: 	if (!arr->arr_data) {
  108: 		LOGERR;
  109: 		free(arr);
  110: 		return NULL;
  111: 	} else
  112: 		memcpy(arr->arr_data, *pargv, io_arraySize(arr) * sizeof(void*));
  113: 
  114: 	return arr;
  115: }
  116: 
  117: /*
  118:  * io_arrayTo() - Create and fill array with pointers from dynamic array
  119:  * @arr = Array
  120:  * return: NULL error, != NULL allocated memory for array, NULL terminated
  121:  */
  122: inline char **
  123: io_arrayTo(array_t * __restrict arr)
  124: {
  125: 	char **args = NULL;
  126: 
  127: 	assert(arr);
  128: 	if (!arr || !io_arraySize(arr))
  129: 		return NULL;
  130: 
  131: 	args = (char **) calloc(io_arraySize(arr) + 1, sizeof(char*));
  132: 	if (!args) {
  133: 		LOGERR;
  134: 		return NULL;
  135: 	} else
  136: 		memcpy(args, arr->arr_data, io_arraySize(arr) * sizeof(char*));
  137: 	args[io_arraySize(arr)] = NULL;
  138: 
  139: 	return args;
  140: }
  141: 
  142: /*
  143:  * io_arrayFree() - Free all data in dynamic array items
  144:  *	(WARNING! If assign static array dont use this!!!)
  145:  * @arr = Array
  146:  * return: none
  147:  */
  148: inline void
  149: io_arrayFree(array_t * __restrict arr)
  150: {
  151: 	register int i;
  152: 
  153: 	if (!arr)
  154: 		return;
  155: 
  156: 	for (i = 0; i < io_arraySize(arr); i++)
  157: 		if (arr->arr_data[i]) {
  158: 			free(arr->arr_data[i]);
  159: 			arr->arr_data[i] = NULL;
  160: 		}
  161: }
  162: 
  163: /*
  164:  * io_arrayDestroy() - Free and destroy dynamic array
  165:  * @parr = Array
  166:  * return: none
  167:  */
  168: inline void
  169: io_arrayDestroy(array_t ** __restrict parr)
  170: {
  171: 	if (!parr || !*parr)
  172: 		return;
  173: 
  174: 	if ((*parr)->arr_data)
  175: 		free((*parr)->arr_data);
  176: 	free(*parr);
  177: 	*parr = NULL;
  178: }
  179: 
  180: /*
  181:  * io_arrayLen() - Get last used element in dynamic array (array Length)
  182:  * @arr = Array
  183:  * return: -1 error, 0 empty or >0 position of last used element
  184:  */
  185: inline int
  186: io_arrayLen(array_t * __restrict arr)
  187: {
  188: 	register int i;
  189: 
  190: 	assert(arr);
  191: 	if (!arr)
  192: 		return -1;
  193: 
  194: 	for (i = io_arraySize(arr); i && !arr->arr_data[i - 1]; i--);
  195: 
  196: 	return i;
  197: }
  198: 
  199: /*
  200:  * io_arrayGrow() - Grow/Shrink dynamic array, Use with care when it shrink!!!
  201:  * @arr = Array
  202:  * @newNumItems = Number of Items
  203:  * @freeShrink = Free elements before shrink array
  204:  * return: -1 error, 0 ok
  205:  */
  206: int
  207: io_arrayGrow(array_t * __restrict arr, int newNumItems, int freeShrink)
  208: {
  209: 	void **data;
  210: 	int n = 0;
  211: 	register int i;
  212: 
  213: 	assert(arr);
  214: 	if (!arr)
  215: 		return -1;
  216: 
  217: 	if (arr->arr_num == newNumItems)
  218: 		return 0;
  219: 	if (io_arraySize(arr) < newNumItems) {
  220: 		n = newNumItems - io_arraySize(arr);
  221: 	} else if (freeShrink)
  222: 		for (i = newNumItems; i < arr->arr_num; i++)
  223: 			if (arr->arr_data[i])
  224: 				free(arr->arr_data[i]);
  225: 
  226: 	arr->arr_num = newNumItems;
  227: 	if (io_arraySize(arr)) {
  228: 		data = realloc(arr->arr_data, io_arraySize(arr) * sizeof(void*));
  229: 		if (!data) {
  230: 			LOGERR;
  231: 			return -1;
  232: 		} else
  233: 			arr->arr_data = data;
  234: 
  235: 		memset(arr->arr_data + (io_arraySize(arr) - n), 0, n * sizeof(void*));
  236: 	} else {
  237: 		if (arr->arr_data)
  238: 			free(arr->arr_data);
  239: 		arr->arr_data = NULL;
  240: 	}
  241: 
  242: 	return 0;
  243: }
  244: 
  245: /*
  246:  * io_arrayVacuum() - Vacuum dynamic array, empty elements will be deleted
  247:  * @arr = Array
  248:  * @fromWhere = 1 begin, 2 ALL empty elements
  249:  * return: -1 error, 0 ok
  250:  */
  251: int
  252: io_arrayVacuum(array_t * __restrict arr, int fromWhere)
  253: {
  254: 	register int i, j, num;
  255: 	int cx = 0;
  256: 
  257: 	assert(arr);
  258: 	if (!arr)
  259: 		return -1;
  260: 	else
  261: 		fromWhere &= 0x7;
  262: 
  263: 	num = io_arraySize(arr);
  264: 	/*
  265: 	if (fromWhere & VACUUM_RIGHT) {
  266: 		for (cx = 0, i = num - 1; i && !arr->arr_data[i]; i--, cx++);
  267: 		num -= cx;
  268: 	}
  269: 	*/
  270: 	if (fromWhere & VACUUM_LEFT) {
  271: 		for (i = 0; i < num && !arr->arr_data[i]; i++);
  272: 		if (i) {
  273: 			memmove(arr->arr_data, arr->arr_data + i, (num - i) * sizeof(void*));
  274: 			memset(arr->arr_data + (num - i), 0, i * sizeof(void*));
  275: 
  276: 			num -= i;
  277: 			cx += i;
  278: 		}
  279: 	}
  280: 	if (fromWhere & VACUUM_BETWEEN) {
  281: 		for (i = 0; i < num; i++) {
  282: 			if (arr->arr_data[i])
  283: 				continue;
  284: 
  285: 			for (j = i; j < num && !arr->arr_data[j]; j++);
  286: 
  287: 			memmove(arr->arr_data + i, arr->arr_data + j, (num - j) * sizeof(void*));
  288: 			memset(arr->arr_data + i + (num - j), 0, (j - i) * sizeof(void*));
  289: 
  290: 			num -= j - i;
  291: 			cx += j - i;
  292: 		}
  293: 	}
  294: 
  295: 	return cx;
  296: }
  297: 
  298: /*
  299:  * io_arrayPush() - Push element into dynamic array like stack manner, place at first empty position
  300:  * @arr = Array
  301:  * @data = Element, if set NULL return only first empty position
  302:  * return: -1 not found empty position, array is full!, >-1 return position of stored element into array
  303:  */
  304: inline int
  305: io_arrayPush(array_t * __restrict arr, void **data)
  306: {
  307: 	register int i;
  308: 	int ret = -1;
  309: 
  310: 	assert(arr);
  311: 
  312: 	for (i = 0; i < io_arraySize(arr); i++)
  313: 		if (!arr->arr_data[i]) {
  314: 			if (data)
  315: 				arr->arr_data[i] = *data;
  316: 			ret = i;
  317: 			break;
  318: 		}
  319: 
  320: 	return ret;
  321: }
  322: 
  323: /*
  324:  * io_arrayPop() - Pop element from dynamic array like stack manner, last used position
  325:  * @arr = Array
  326:  * @data = Element, if set NULL return only last used position
  327:  * @delAfter = Delete after Pop element, !=0 delete element from array after return data
  328:  * return: -1 not found used position, array is empty!, >-1 return element position
  329:  */
  330: inline int
  331: io_arrayPop(array_t * __restrict arr, void ** __restrict data, int delAfter)
  332: {
  333: 	register int i;
  334: 	int ret = -1;
  335: 
  336: 	assert(arr);
  337: 
  338: 	for (i = io_arraySize(arr) - 1; i >= 0; i--)
  339: 		if (arr->arr_data[i]) {
  340: 			if (data)
  341: 				*data = arr->arr_data[i];
  342: 			if (delAfter)
  343: 				arr->arr_data[i] = NULL;
  344: 			ret = i;
  345: 			break;
  346: 		}
  347: 
  348: 	return ret;
  349: }
  350: 
  351: /*
  352:  * io_arrayConcat() Concat source array to destination array
  353:  * @dest = Destination array
  354:  * @src = Source array
  355:  * return: -1 error; >0 new count of destination array
  356:  */
  357: int
  358: io_arrayConcat(array_t * __restrict dest, array_t * __restrict src)
  359: {
  360: 	int n;
  361: 
  362: 	assert(dest);
  363: 	assert(src);
  364: 	if (!dest || !src)
  365: 		return -1;
  366: 
  367: 	n = io_arraySize(dest);
  368: 	if (io_arrayGrow(dest, n + io_arraySize(src), 0))
  369: 		return -1;
  370: 	memcpy(dest->arr_data + n, src->arr_data, io_arraySize(src) * sizeof(void*));
  371: 
  372: 	return io_arraySize(dest);
  373: }
  374: 
  375: /*
  376:  * io_argsNum() Parse and calculate number of arguments
  377:  * @csArgs = Input arguments line
  378:  * @csDelim = Delimiter(s) for separate
  379:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
  380:  */
  381: inline int
  382: io_argsNum(const char *csArgs, const char *csDelim)
  383: {
  384: 	register int res;
  385: 	char *pos;
  386: 
  387: 	assert(csArgs);
  388: 	assert(csDelim);
  389: 	if (!csArgs || !csDelim)
  390: 		return -1;
  391: 
  392: 	for (res = 1, pos = (char*) csArgs; (pos = strpbrk(pos, csDelim)); res++, pos++);
  393: 	return res;
  394: }
  395: 
  396: /*
  397:  * io_arrayMake() Parse and make array from arguments ... (input string will be modified!!! 
  398:  *	and output array must be free with io_arrayDestroy() after use!)
  399:  * @psArgs = Input arguments line, after execute string is modified!!!
  400:  * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs
  401:  * @csDelim = Delimiter(s) for separate
  402:  * @parr = Output array of arguments ... (must be free with io_arrayDestroy() after use!)
  403:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  404:  */
  405: int
  406: io_arrayMake(char * __restrict psArgs, int nargs, const char *csDelim, array_t ** __restrict parr)
  407: {
  408: 	char **app;
  409: 	register int i;
  410: 
  411: 	assert(psArgs);
  412: 	assert(csDelim);
  413: 	assert(parr);
  414: 	if (!psArgs || !csDelim || !parr)
  415: 		return -1;
  416: 
  417: 	if (nargs)
  418: 		i = nargs;
  419: 	else
  420: 		i = io_argsNum(psArgs, csDelim);
  421: 	*parr = io_arrayInit(i);
  422: 	if (!*parr)
  423: 		return -1;
  424: 
  425: 	for (i = 0, app = (char**) (*parr)->arr_data; 
  426: 			app < (char**) (*parr)->arr_data + (*parr)->arr_num && 
  427: 			(*app = strsep((char **) &psArgs, csDelim)); 
  428: 		       	**app ? i++ : i, **app ? app++ : app);
  429: 	return i;
  430: }
  431: 
  432: /*
  433:  * io_MakeAV() Parse and make attribute/value pair
  434:  * @csArgs = Input argument line
  435:  * @csDelim = Delimiter for separate
  436:  * @psAttr = Output Attribute
  437:  * @attrLen = Size of attribute array
  438:  * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
  439:  * @valLen = Size of value array
  440:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  441: */
  442: int
  443: io_MakeAV(const char * __restrict csArgs, const char *csDelim, 
  444: 		char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen)
  445: {
  446: 	register int ret = 0;
  447: 	char *pos, *psBuf;
  448: 
  449: 	if (!csArgs || !csDelim || !psAttr || !attrLen)
  450: 		return -1;
  451: 	if (psValue && !valLen)
  452: 		return -1;
  453: 	else
  454: 		memset(psValue, 0, valLen);
  455: 	psBuf = strdup(csArgs);
  456: 	if (!psBuf) {
  457: 		LOGERR;
  458: 		return -1;
  459: 	}
  460: 
  461: 	pos = strpbrk(psBuf, csDelim);
  462: 	if (pos)
  463: 		*pos++ = 0;
  464: 	ret++;
  465: 	strlcpy(psAttr, psBuf, attrLen);
  466: 
  467: 	if (pos && *pos) {
  468: 		ret++;
  469: 		if (psValue)
  470: 			strlcpy(psValue, pos, valLen);
  471: 	}
  472: 
  473: 	free(psBuf);
  474: 	return ret;
  475: }
  476: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>