|
version 1.2.4.1, 2011/05/19 02:22:46
|
version 1.4.2.1, 2011/08/31 12:29:32
|
|
Line 63 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*)); | io_arrayZero(arr); |
| |
|
| return arr; |
return arr; |
| } |
} |
| |
|
| /* |
/* |
| |
* io_arrayFrom() - 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 |
| |
*/ |
| |
inline array_t * |
| |
io_arrayFrom(const char *** __restrict pargv, int argc) |
| |
{ |
| |
array_t *arr = NULL; |
| |
const char **a = NULL; |
| |
register int num = 0; |
| |
|
| |
assert(pargv); |
| |
if (!pargv || !*pargv || argc < 0) |
| |
return NULL; |
| |
|
| |
if (argc) |
| |
num = argc; |
| |
else |
| |
for (a = *pargv; *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, *pargv, 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, NULL terminated |
| |
*/ |
| |
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 |
* 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 |
|
Line 85 io_arrayFree(array_t * __restrict arr)
|
Line 150 io_arrayFree(array_t * __restrict arr)
|
| { |
{ |
| register int i; |
register int i; |
| |
|
| assert(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 104 io_arrayFree(array_t * __restrict arr)
|
Line 168 io_arrayFree(array_t * __restrict arr)
|
| inline void |
inline void |
| io_arrayDestroy(array_t ** __restrict parr) |
io_arrayDestroy(array_t ** __restrict parr) |
| { |
{ |
| assert(parr); | if (!parr || !*parr) |
| if (!parr) | |
| return; |
return; |
| |
|
| if ((*parr)->arr_data) |
if ((*parr)->arr_data) |
|
Line 128 io_arrayLen(array_t * __restrict arr)
|
Line 191 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 137 io_arrayLen(array_t * __restrict arr)
|
Line 200 io_arrayLen(array_t * __restrict arr)
|
| * io_arrayGrow() - Grow/Shrink dynamic array, Use with care when it shrink!!! |
* io_arrayGrow() - Grow/Shrink dynamic array, Use with care when it shrink!!! |
| * @arr = Array |
* @arr = Array |
| * @newNumItems = Number of Items |
* @newNumItems = Number of Items |
| |
* @freeShrink = Free elements before shrink array |
| * return: -1 error, 0 ok |
* return: -1 error, 0 ok |
| */ |
*/ |
| int |
int |
| io_arrayGrow(array_t * __restrict arr, int newNumItems) | io_arrayGrow(array_t * __restrict arr, int newNumItems, int freeShrink) |
| { |
{ |
| void **data; |
void **data; |
| int n = 0; |
int n = 0; |
| /* register int i; */ | register int i; |
| |
|
| assert(arr); |
assert(arr); |
| if (!arr) |
if (!arr) |
|
Line 152 io_arrayGrow(array_t * __restrict arr, int newNumItems
|
Line 216 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 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]); |
free(arr->arr_data[i]); |
| */ |
|
| |
|
| 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 190 io_arrayVacuum(array_t * __restrict arr, int fromWhere
|
Line 260 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 199 io_arrayVacuum(array_t * __restrict arr, int fromWhere
|
Line 269 io_arrayVacuum(array_t * __restrict arr, int fromWhere
|
| */ |
*/ |
| if (fromWhere & VACUUM_LEFT) { |
if (fromWhere & VACUUM_LEFT) { |
| for (i = 0; i < num && !arr->arr_data[i]; i++); |
for (i = 0; i < num && !arr->arr_data[i]; i++); |
| |
if (i) { |
| |
memmove(arr->arr_data, arr->arr_data + i, (num - i) * sizeof(void*)); |
| |
memset(arr->arr_data + (num - i), 0, i * sizeof(void*)); |
| |
|
| memmove(arr->arr_data, arr->arr_data + i, (num - i) * sizeof(void*)); | num -= i; |
| memset(arr->arr_data + (num - i), 0, i * sizeof(void*)); | cx += i; |
| } |
| num -= i; | |
| cx += i; | |
| } |
} |
| if (fromWhere & VACUUM_BETWEEN) { |
if (fromWhere & VACUUM_BETWEEN) { |
| for (i = 0; i < num; i++) { |
for (i = 0; i < num; i++) { |
|
Line 238 io_arrayPush(array_t * __restrict arr, void **data)
|
Line 309 io_arrayPush(array_t * __restrict arr, void **data)
|
| |
|
| assert(arr); |
assert(arr); |
| |
|
| 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]) { |
| if (data) |
if (data) |
| arr->arr_data[i] = *data; |
arr->arr_data[i] = *data; |
|
Line 252 io_arrayPush(array_t * __restrict arr, void **data)
|
Line 323 io_arrayPush(array_t * __restrict arr, void **data)
|
| /* |
/* |
| * io_arrayPop() - Pop element from dynamic array like stack manner, last used position |
* io_arrayPop() - Pop element from dynamic array like stack manner, last used position |
| * @arr = Array |
* @arr = Array |
| * @data = Element, if set NULL return only first empty position | * @data = Element, if set NULL return only last used position |
| * @delAfter = Delete after Pop element, !=0 delete element from array after return data |
* @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 |
* return: -1 not found used position, array is empty!, >-1 return element position |
| */ |
*/ |
|
Line 264 io_arrayPop(array_t * __restrict arr, void ** __restri
|
Line 335 io_arrayPop(array_t * __restrict arr, void ** __restri
|
| |
|
| assert(arr); |
assert(arr); |
| |
|
| for (i = arr->arr_num - 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) |
| *data = arr->arr_data[i]; |
*data = arr->arr_data[i]; |
|
Line 293 io_arrayConcat(array_t * __restrict dest, array_t * __
|
Line 364 io_arrayConcat(array_t * __restrict dest, array_t * __
|
| if (!dest || !src) |
if (!dest || !src) |
| return -1; |
return -1; |
| |
|
| n = dest->arr_num; | n = io_arraySize(dest); |
| if (io_arrayGrow(dest, n + src->arr_num)) | if (io_arrayGrow(dest, n + io_arraySize(src), 0)) |
| return -1; |
return -1; |
| memcpy(dest->arr_data + n, src->arr_data, src->arr_num * sizeof(void*)); | memcpy(dest->arr_data + n, src->arr_data, io_arraySize(src) * sizeof(void*)); |
| |
|
| return io_arraySize(dest); |
return io_arraySize(dest); |
| } |
} |