File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / Attic / array.c
Revision 1.1.2.1: download - view: text, annotated - select for diffs - revision graph
Tue Apr 19 21:07:32 2011 UTC (13 years, 6 months ago) by misho
Branches: io1_7
added new feature
dynamic array!

/*************************************************************************
* (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: array.c,v 1.1.2.1 2011/04/19 21:07:32 misho Exp $
*
*************************************************************************/
#include "global.h"


/*
 * io_arrayInit() - Create and initialize dynamic array
 * @numItems = Number of Items
 * return: NULL error, != NULL allocated memory for array
 */
inline array_t *
io_arrayInit(int numItems)
{
	array_t *arr = NULL;

	arr = malloc(sizeof(array_t));
	if (!arr) {
		LOGERR;
		return NULL;
	}

	arr->arr_num = numItems;
	arr->arr_data = calloc(arr->arr_num, sizeof(void*));
	if (!arr->arr_data) {
		LOGERR;
		free(arr);
		return NULL;
	} else
		memset(arr->arr_data, 0, arr->arr_num * sizeof(void*));

	return arr;
}

/*
 * io_arrayFree() - Free all data in dynamic array
 *	(WARNING! If assign static array dont use this!!!)
 * @arr = Array
 * return: none
 */
inline void
io_arrayFree(array_t * __restrict arr)
{
	register int i;

	assert(arr);
	if (!arr)
		return;

	for (i = 0; i < arr->arr_num; i++)
		if (arr->arr_data[i]) {
			free(arr->arr_data[i]);
			arr->arr_data[i] = NULL;
		}
}

/*
 * io_arrayDestroy() - Free all data in dynamic array and Destroy dynamic array
 * @parr = Array
 * return: none
 */
inline void
io_arrayDestroy(array_t ** __restrict parr)
{
	assert(parr);
	if (!parr)
		return;

	if ((*parr)->arr_data)
		free((*parr)->arr_data);
	free(*parr);
	*parr = NULL;
}

/*
 * io_arrayLen() - Get last used element in dynamic array (array Length)
 * @arr = Array
 * return: -1 error, 0 empty or >0 position of last used element
 */
inline int
io_arrayLen(array_t * __restrict arr)
{
	register int i;

	assert(arr);
	if (!arr)
		return -1;

	for (i = arr->arr_num; i && !arr->arr_data[i - 1]; i--);

	return i;
}

/*
 * io_arrayGrow() - Grow/Shrink dynamic array, Use with care when it shrink!!!
 * @arr = Array
 * @newNumItems = Number of Items
 * return: -1 error, 0 ok
 */
int
io_arrayGrow(array_t * __restrict arr, int newNumItems)
{
	void **data;
	int n = 0;
/*	register int i; */

	assert(arr);
	if (!arr)
		return -1;

	if (arr->arr_num == newNumItems)
		return 0;
	if (arr->arr_num < newNumItems) {
		n = newNumItems - arr->arr_num;
	} /* else
		for (i = newNumItems; i < arr->arr_num; i++)
			if (arr->arr_data[i])
				free(arr->arr_data[i]);
				*/

	data = realloc(arr->arr_data, arr->arr_num * sizeof(void*));
	if (!data) {
		LOGERR;
		return -1;
	} else
		arr->arr_data = data;
	memset(arr->arr_data + arr->arr_num, 0, n * sizeof(void*));
	arr->arr_num = newNumItems;

	return 0;
}

/*
 * io_arrayVacuum() - Vacuum dynamic array, empty elements will be deleted
 * @arr = Array
 * @fromWhere = 1 begin, 2 ALL empty elements
 * return: -1 error, 0 ok
 */
int
io_arrayVacuum(array_t * __restrict arr, int fromWhere)
{
	register int i, j, num;
	int cx = 0;

	assert(arr);
	if (!arr)
		return -1;
	else
		fromWhere &= 0x7;

	num = arr->arr_num;
	/*
	if (fromWhere & VACUUM_RIGHT) {
		for (cx = 0, i = num - 1; i && !arr->arr_data[i]; i--, cx++);
		num -= cx;
	}
	*/
	if (fromWhere & VACUUM_LEFT) {
		for (i = 0; i < num && !arr->arr_data[i]; i++);

		memmove(arr->arr_data, arr->arr_data + i, (num - i) * sizeof(void*));
		memset(arr->arr_data + (num - i), 0, i * sizeof(void*));

		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(void*));
			memset(arr->arr_data + i + (num - j), 0, (j - i) * sizeof(void*));

			num -= j - i;
			cx += j - i;
		}
	}

	return cx;
}

/*
 * io_argsNum() Parse and calculate number of arguments
 * @csArgs = Input arguments line
 * @csDelim = Delimiter(s) for separate
 * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
 */
inline int
io_argsNum(const char *csArgs, const char *csDelim)
{
	register int res;
	char *pos;

	assert(csArgs);
	assert(csDelim);
	if (!csArgs || !csDelim)
		return -1;

	for (res = 1, pos = (char*) csArgs; (pos = strpbrk(pos, csDelim)); res++, pos++);
	return res;
}

/*
 * io_arrayMake() Parse and make array from arguments ... (input string will be modified!!! 
 *	and output array must be free with io_arrayDestroy() 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 io_arrayDestroy() after use!)
 * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
 */
int
io_arrayMake(char * __restrict psArgs, int nargs, const char *csDelim, array_t ** __restrict parr)
{
	char **app;
	register int i;

	assert(psArgs);
	assert(csDelim);
	assert(parr);
	if (!psArgs || !csDelim || !parr)
		return -1;

	if (nargs)
		i = nargs;
	else
		i = io_argsNum(psArgs, csDelim);
	*parr = io_arrayInit(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);
	return i;
}

/*
 * io_MakeAV() Parse and make attribute/value pair
 * @csArgs = Input argument line
 * @csDelim = Delimiter for separate
 * @psAttr = Output Attribute
 * @attrLen = Size of attribute array
 * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
 * @valLen = Size of value array
 * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
*/
int
io_MakeAV(const char * __restrict csArgs, const char *csDelim, 
		char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen)
{
	register int ret = 0;
	char *pos, *psBuf;

	if (!csArgs || !csDelim || !psAttr || !attrLen)
		return -1;
	if (psValue && !valLen)
		return -1;
	else
		memset(psValue, 0, valLen);
	psBuf = strdup(csArgs);
	if (!psBuf) {
		LOGERR;
		return -1;
	}

	pos = strpbrk(psBuf, csDelim);
	if (pos)
		*pos++ = 0;
	ret++;
	strlcpy(psAttr, psBuf, attrLen);

	if (pos && *pos) {
		ret++;
		if (psValue)
			strlcpy(psValue, pos, valLen);
	}

	free(psBuf);
	return ret;
}


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