version 1.8, 2012/03/29 01:31:33
|
version 1.11, 2012/08/01 00:37:08
|
Line 57 io_arrayInit(int numItems)
|
Line 57 io_arrayInit(int numItems)
|
{ |
{ |
array_t *arr = NULL; |
array_t *arr = NULL; |
|
|
arr = malloc(sizeof(array_t)); | arr = io_malloc(sizeof(array_t)); |
if (!arr) { |
if (!arr) { |
LOGERR; |
LOGERR; |
return NULL; |
return NULL; |
} |
} |
|
|
|
arr->arr_last = -1; |
arr->arr_num = numItems; |
arr->arr_num = numItems; |
arr->arr_data = calloc(io_arraySize(arr), sizeof(void*)); | arr->arr_data = io_calloc(io_arraySize(arr), sizeof(void*)); |
if (!arr->arr_data) { |
if (!arr->arr_data) { |
LOGERR; |
LOGERR; |
free(arr); | io_free(arr); |
return NULL; |
return NULL; |
} else |
} else |
io_arrayZero(arr); |
io_arrayZero(arr); |
Line 98 io_arrayFrom(const char *** __restrict pargv, int argc
|
Line 99 io_arrayFrom(const char *** __restrict pargv, int argc
|
else |
else |
for (a = *pargv; *a; a++, num++); |
for (a = *pargv; *a; a++, num++); |
|
|
arr = malloc(sizeof(array_t)); | arr = io_malloc(sizeof(array_t)); |
if (!arr) { |
if (!arr) { |
LOGERR; |
LOGERR; |
return NULL; |
return NULL; |
} |
} |
|
|
arr->arr_num = num; |
arr->arr_num = num; |
arr->arr_data = calloc(io_arraySize(arr), sizeof(void*)); | arr->arr_data = io_calloc(io_arraySize(arr), sizeof(void*)); |
if (!arr->arr_data) { |
if (!arr->arr_data) { |
LOGERR; |
LOGERR; |
free(arr); | io_free(arr); |
return NULL; |
return NULL; |
} else | } else { |
memcpy(arr->arr_data, *pargv, io_arraySize(arr) * sizeof(void*)); |
memcpy(arr->arr_data, *pargv, io_arraySize(arr) * sizeof(void*)); |
|
arr->arr_last = io_arraySize(arr) - 1; |
|
} |
|
|
return arr; |
return arr; |
} |
} |
Line 131 io_arrayTo(array_t * __restrict arr)
|
Line 134 io_arrayTo(array_t * __restrict arr)
|
if (!arr || !io_arraySize(arr)) |
if (!arr || !io_arraySize(arr)) |
return NULL; |
return NULL; |
|
|
args = (char **) calloc(io_arraySize(arr) + 1, sizeof(char*)); | args = (char **) io_calloc(io_arraySize(arr) + 1, sizeof(char*)); |
if (!args) { |
if (!args) { |
LOGERR; |
LOGERR; |
return NULL; |
return NULL; |
Line 159 io_arrayFree(array_t * __restrict arr)
|
Line 162 io_arrayFree(array_t * __restrict arr)
|
|
|
for (i = 0; i < io_arraySize(arr); i++) |
for (i = 0; i < io_arraySize(arr); i++) |
if (arr->arr_data[i]) { |
if (arr->arr_data[i]) { |
free(arr->arr_data[i]); | io_free(arr->arr_data[i]); |
arr->arr_data[i] = NULL; |
arr->arr_data[i] = NULL; |
} |
} |
|
|
|
arr->arr_last = -1; |
} |
} |
|
|
/* |
/* |
Line 177 io_arrayDestroy(array_t ** __restrict parr)
|
Line 182 io_arrayDestroy(array_t ** __restrict parr)
|
return; |
return; |
|
|
if ((*parr)->arr_data) |
if ((*parr)->arr_data) |
free((*parr)->arr_data); | io_free((*parr)->arr_data); |
free(*parr); | io_free(*parr); |
*parr = NULL; |
*parr = NULL; |
} |
} |
|
|
Line 186 io_arrayDestroy(array_t ** __restrict parr)
|
Line 191 io_arrayDestroy(array_t ** __restrict parr)
|
* io_arrayLen() - Get last used element in dynamic array (array Length) |
* io_arrayLen() - Get last used element in dynamic array (array Length) |
* |
* |
* @arr = Array |
* @arr = Array |
* return: -1 error, 0 empty or >0 position of last used element | * return: -1 empty or >-1 position of last used element |
*/ |
*/ |
inline int |
inline int |
io_arrayLen(array_t * __restrict arr) |
io_arrayLen(array_t * __restrict arr) |
Line 199 io_arrayLen(array_t * __restrict arr)
|
Line 204 io_arrayLen(array_t * __restrict arr)
|
|
|
for (i = io_arraySize(arr); i && !arr->arr_data[i - 1]; i--); |
for (i = io_arraySize(arr); i && !arr->arr_data[i - 1]; i--); |
|
|
return i; | return --i; |
} |
} |
|
|
/* |
/* |
Line 227 io_arrayGrow(array_t * __restrict arr, int newNumItems
|
Line 232 io_arrayGrow(array_t * __restrict arr, int newNumItems
|
} else if (freeShrink) |
} else if (freeShrink) |
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]) |
free(arr->arr_data[i]); | io_free(arr->arr_data[i]); |
|
|
arr->arr_num = newNumItems; |
arr->arr_num = newNumItems; |
if (io_arraySize(arr)) { |
if (io_arraySize(arr)) { |
data = realloc(arr->arr_data, io_arraySize(arr) * sizeof(void*)); | data = io_realloc(arr->arr_data, io_arraySize(arr) * sizeof(void*)); |
if (!data) { |
if (!data) { |
LOGERR; |
LOGERR; |
return -1; |
return -1; |
Line 239 io_arrayGrow(array_t * __restrict arr, int newNumItems
|
Line 244 io_arrayGrow(array_t * __restrict arr, int newNumItems
|
arr->arr_data = data; |
arr->arr_data = data; |
|
|
memset(arr->arr_data + (io_arraySize(arr) - n), 0, n * sizeof(void*)); |
memset(arr->arr_data + (io_arraySize(arr) - n), 0, n * sizeof(void*)); |
|
arr->arr_last = io_arrayLen(arr); |
} else { |
} else { |
if (arr->arr_data) |
if (arr->arr_data) |
free(arr->arr_data); | io_free(arr->arr_data); |
arr->arr_data = NULL; |
arr->arr_data = NULL; |
|
arr->arr_last = -1; |
} |
} |
|
|
return 0; |
return 0; |
Line 298 io_arrayVacuum(array_t * __restrict arr, int fromWhere
|
Line 305 io_arrayVacuum(array_t * __restrict arr, int fromWhere
|
} |
} |
} |
} |
|
|
|
arr->arr_last = io_arrayLen(arr); |
return cx; |
return cx; |
} |
} |
|
|
Line 317 io_arrayElem(array_t * __restrict arr, int n, void *da
|
Line 325 io_arrayElem(array_t * __restrict arr, int n, void *da
|
if (!arr) |
if (!arr) |
return (void*) -1; |
return (void*) -1; |
|
|
if (n > io_arraySize(arr) && io_arrayGrow(arr, n + 1, 0)) | if (n >= io_arraySize(arr) && io_arrayGrow(arr, n + 1, 0)) |
return (void*) -1; |
return (void*) -1; |
|
|
dat = io_arrayGet(arr, n); |
dat = io_arrayGet(arr, n); |
Line 332 io_arrayElem(array_t * __restrict arr, int n, void *da
|
Line 340 io_arrayElem(array_t * __restrict arr, int n, void *da
|
* |
* |
* @arr = Array |
* @arr = Array |
* @data = Element, if set NULL return only first empty position |
* @data = Element, if set NULL return only first empty position |
|
* @mayGrow = Array may to grow, if not enough room for new element |
* return: -1 not found empty position, array is full!, >-1 return position of stored element into array |
* return: -1 not found empty position, array is full!, >-1 return position of stored element into array |
*/ |
*/ |
inline int |
inline int |
io_arrayPush(array_t * __restrict arr, void **data) | io_arrayPush(array_t * __restrict arr, void **data, int mayGrow) |
{ |
{ |
|
#if 0 |
register int i; |
register int i; |
|
#endif |
int ret = -1; |
int ret = -1; |
|
|
if (!arr) |
if (!arr) |
return -1; |
return -1; |
|
else |
|
ret = io_arrayLast(arr) + 1; |
|
if (!mayGrow && ret >= io_arraySize(arr)) |
|
return -1; |
|
if (mayGrow && ret >= io_arraySize(arr) && |
|
io_arrayGrow(arr, ret + 1, 0)) |
|
return -1; |
|
|
|
#if 0 |
for (i = 0; i < io_arraySize(arr); i++) |
for (i = 0; i < io_arraySize(arr); i++) |
if (!arr->arr_data[i]) { |
if (!arr->arr_data[i]) { |
if (data) |
if (data) |
Line 350 io_arrayPush(array_t * __restrict arr, void **data)
|
Line 369 io_arrayPush(array_t * __restrict arr, void **data)
|
ret = i; |
ret = i; |
break; |
break; |
} |
} |
|
#endif |
|
arr->arr_data[++arr->arr_last] = *data; |
|
|
return ret; |
return ret; |
} |
} |
Line 365 io_arrayPush(array_t * __restrict arr, void **data)
|
Line 386 io_arrayPush(array_t * __restrict arr, void **data)
|
inline int |
inline int |
io_arrayPop(array_t * __restrict arr, void ** __restrict data, int delAfter) |
io_arrayPop(array_t * __restrict arr, void ** __restrict data, int delAfter) |
{ |
{ |
|
#if 0 |
register int i; |
register int i; |
|
#endif |
int ret = -1; |
int ret = -1; |
|
|
if (!arr) |
if (!arr) |
return -1; |
return -1; |
|
|
|
#if 0 |
for (i = io_arraySize(arr) - 1; i >= 0; i--) |
for (i = io_arraySize(arr) - 1; i >= 0; i--) |
if (arr->arr_data[i]) { |
if (arr->arr_data[i]) { |
if (data) |
if (data) |
Line 380 io_arrayPop(array_t * __restrict arr, void ** __restri
|
Line 404 io_arrayPop(array_t * __restrict arr, void ** __restri
|
ret = i; |
ret = i; |
break; |
break; |
} |
} |
|
#endif |
|
ret = io_arrayLast(arr); |
|
|
|
if (data) |
|
*data = arr->arr_data[ret]; |
|
if (delAfter) |
|
arr->arr_data[arr->arr_last--] = NULL; |
|
|
return ret; |
return ret; |
} |
} |
|
|
Line 406 io_arrayConcat(array_t * __restrict dest, array_t * __
|
Line 437 io_arrayConcat(array_t * __restrict dest, array_t * __
|
return -1; |
return -1; |
memcpy(dest->arr_data + n, src->arr_data, io_arraySize(src) * sizeof(void*)); |
memcpy(dest->arr_data + n, src->arr_data, io_arraySize(src) * sizeof(void*)); |
|
|
|
dest->arr_last = io_arrayLen(dest); |
return io_arraySize(dest); |
return io_arraySize(dest); |
} |
} |
|
|
Line 427 io_arrayCopy(array_t ** __restrict dest, array_t * __r
|
Line 459 io_arrayCopy(array_t ** __restrict dest, array_t * __r
|
*dest = io_arrayInit(io_arraySize(src)); |
*dest = io_arrayInit(io_arraySize(src)); |
if (!*dest) |
if (!*dest) |
return -1; |
return -1; |
|
else |
|
(*dest)->arr_last = src->arr_last; |
|
|
memcpy((*dest)->arr_data, src->arr_data, io_arraySize(*dest) * sizeof(void*)); |
memcpy((*dest)->arr_data, src->arr_data, io_arraySize(*dest) * sizeof(void*)); |
return io_arraySize(*dest); |
return io_arraySize(*dest); |
} |
} |
|
|
/* |
/* |
* 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!!! |
* io_arrayMake() Parse and make array from arguments ... (input string will be modified!!! |
* and output array must be free with io_arrayDestroy() after use!) |
* and output array must be free with io_arrayDestroy() after use!) |
* |
* |
Line 490 io_arrayMake(char * __restrict psArgs, int nargs, cons
|
Line 502 io_arrayMake(char * __restrict psArgs, int nargs, cons
|
**app ? i++ : i, **app ? app++ : app); |
**app ? i++ : i, **app ? app++ : app); |
return i; |
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; |
|
} |
|
|
|