Annotation of libaitio/src/array.c, revision 1.6
1.2 misho 1: /*************************************************************************
2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.6 ! misho 6: * $Id: array.c,v 1.5.2.1 2011/12/12 14:45:19 misho Exp $
1.2 misho 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;
1.3 misho 66: arr->arr_data = calloc(io_arraySize(arr), sizeof(void*));
1.2 misho 67: if (!arr->arr_data) {
68: LOGERR;
69: free(arr);
70: return NULL;
71: } else
1.5 misho 72: io_arrayZero(arr);
1.2 misho 73:
74: return arr;
75: }
76:
77: /*
1.3 misho 78: * io_arrayFrom() - Create and fill array from array with pointers
1.4 misho 79: * @pargv = Array with pointers
1.3 misho 80: * @argc = Number of Items, if 0 walk through argv and stop when reach NULL item
1.4 misho 81: * return: NULL error, != NULL allocated new array
1.3 misho 82: */
83: inline array_t *
1.4 misho 84: io_arrayFrom(const char *** __restrict pargv, int argc)
1.3 misho 85: {
86: array_t *arr = NULL;
87: const char **a = NULL;
88: register int num = 0;
89:
1.4 misho 90: assert(pargv);
91: if (!pargv || !*pargv || argc < 0)
1.3 misho 92: return NULL;
93:
94: if (argc)
95: num = argc;
96: else
1.4 misho 97: for (a = *pargv; *a; a++, num++);
1.3 misho 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
1.4 misho 112: memcpy(arr->arr_data, *pargv, io_arraySize(arr) * sizeof(void*));
1.3 misho 113:
114: return arr;
115: }
116:
117: /*
118: * io_arrayTo() - Create and fill array with pointers from dynamic array
119: * @arr = Array
1.4 misho 120: * return: NULL error, != NULL allocated memory for array, NULL terminated
1.3 misho 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
1.2 misho 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:
1.3 misho 156: for (i = 0; i < io_arraySize(arr); i++)
1.2 misho 157: if (arr->arr_data[i]) {
158: free(arr->arr_data[i]);
159: arr->arr_data[i] = NULL;
160: }
161: }
162:
163: /*
1.3 misho 164: * io_arrayDestroy() - Free and destroy dynamic array
1.2 misho 165: * @parr = Array
166: * return: none
167: */
168: inline void
169: io_arrayDestroy(array_t ** __restrict parr)
170: {
1.4 misho 171: if (!parr || !*parr)
1.2 misho 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:
1.3 misho 194: for (i = io_arraySize(arr); i && !arr->arr_data[i - 1]; i--);
1.2 misho 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
1.5 misho 203: * @freeShrink = Free elements before shrink array
1.2 misho 204: * return: -1 error, 0 ok
205: */
206: int
1.5 misho 207: io_arrayGrow(array_t * __restrict arr, int newNumItems, int freeShrink)
1.2 misho 208: {
209: void **data;
210: int n = 0;
1.5 misho 211: register int i;
1.2 misho 212:
213: if (!arr)
214: return -1;
215:
216: if (arr->arr_num == newNumItems)
217: return 0;
1.3 misho 218: if (io_arraySize(arr) < newNumItems) {
219: n = newNumItems - io_arraySize(arr);
1.5 misho 220: } else if (freeShrink)
1.2 misho 221: for (i = newNumItems; i < arr->arr_num; i++)
222: if (arr->arr_data[i])
223: free(arr->arr_data[i]);
224:
225: arr->arr_num = newNumItems;
1.3 misho 226: if (io_arraySize(arr)) {
227: data = realloc(arr->arr_data, io_arraySize(arr) * sizeof(void*));
228: if (!data) {
229: LOGERR;
230: return -1;
231: } else
232: arr->arr_data = data;
233:
234: memset(arr->arr_data + (io_arraySize(arr) - n), 0, n * sizeof(void*));
235: } else {
236: if (arr->arr_data)
237: free(arr->arr_data);
238: arr->arr_data = NULL;
239: }
1.2 misho 240:
241: return 0;
242: }
243:
244: /*
245: * io_arrayVacuum() - Vacuum dynamic array, empty elements will be deleted
246: * @arr = Array
247: * @fromWhere = 1 begin, 2 ALL empty elements
248: * return: -1 error, 0 ok
249: */
250: int
251: io_arrayVacuum(array_t * __restrict arr, int fromWhere)
252: {
253: register int i, j, num;
254: int cx = 0;
255:
256: if (!arr)
257: return -1;
258: else
259: fromWhere &= 0x7;
260:
1.3 misho 261: num = io_arraySize(arr);
1.2 misho 262: /*
263: if (fromWhere & VACUUM_RIGHT) {
264: for (cx = 0, i = num - 1; i && !arr->arr_data[i]; i--, cx++);
265: num -= cx;
266: }
267: */
268: if (fromWhere & VACUUM_LEFT) {
269: for (i = 0; i < num && !arr->arr_data[i]; i++);
1.4 misho 270: if (i) {
271: memmove(arr->arr_data, arr->arr_data + i, (num - i) * sizeof(void*));
272: memset(arr->arr_data + (num - i), 0, i * sizeof(void*));
1.2 misho 273:
1.4 misho 274: num -= i;
275: cx += i;
276: }
1.2 misho 277: }
278: if (fromWhere & VACUUM_BETWEEN) {
279: for (i = 0; i < num; i++) {
280: if (arr->arr_data[i])
281: continue;
282:
283: for (j = i; j < num && !arr->arr_data[j]; j++);
284:
285: memmove(arr->arr_data + i, arr->arr_data + j, (num - j) * sizeof(void*));
286: memset(arr->arr_data + i + (num - j), 0, (j - i) * sizeof(void*));
287:
288: num -= j - i;
289: cx += j - i;
290: }
291: }
292:
293: return cx;
294: }
295:
296: /*
1.6 ! misho 297: * io_arrayElem() - Always GET/PUT element into dynamic array, if not enough elements grow array
! 298: * @arr = Array
! 299: * @n = Position
! 300: * @data = Element, if set NULL GET element at position or !=NULL PUT element at position
! 301: * return: -1 error or !=-1 return element at position
! 302: */
! 303: inline void *
! 304: io_arrayElem(array_t * __restrict arr, int n, void **data)
! 305: {
! 306: void *dat = NULL;
! 307:
! 308: if (!arr)
! 309: return (void*) -1;
! 310:
! 311: if (n > io_arraySize(arr) && io_arrayGrow(arr, n + 1, 0))
! 312: return (void*) -1;
! 313:
! 314: dat = io_arrayGet(arr, n);
! 315: if (data)
! 316: io_arraySet(arr, n, *data);
! 317:
! 318: return dat;
! 319: }
! 320:
! 321: /*
1.2 misho 322: * io_arrayPush() - Push element into dynamic array like stack manner, place at first empty position
323: * @arr = Array
324: * @data = Element, if set NULL return only first empty position
325: * return: -1 not found empty position, array is full!, >-1 return position of stored element into array
326: */
327: inline int
328: io_arrayPush(array_t * __restrict arr, void **data)
329: {
330: register int i;
331: int ret = -1;
332:
1.6 ! misho 333: if (!arr)
! 334: return -1;
! 335:
1.3 misho 336: for (i = 0; i < io_arraySize(arr); i++)
1.2 misho 337: if (!arr->arr_data[i]) {
338: if (data)
339: arr->arr_data[i] = *data;
340: ret = i;
341: break;
342: }
343:
344: return ret;
345: }
346:
347: /*
348: * io_arrayPop() - Pop element from dynamic array like stack manner, last used position
349: * @arr = Array
1.4 misho 350: * @data = Element, if set NULL return only last used position
1.2 misho 351: * @delAfter = Delete after Pop element, !=0 delete element from array after return data
352: * return: -1 not found used position, array is empty!, >-1 return element position
353: */
354: inline int
355: io_arrayPop(array_t * __restrict arr, void ** __restrict data, int delAfter)
356: {
357: register int i;
358: int ret = -1;
359:
1.6 ! misho 360: if (!arr)
! 361: return -1;
! 362:
1.3 misho 363: for (i = io_arraySize(arr) - 1; i >= 0; i--)
1.2 misho 364: if (arr->arr_data[i]) {
365: if (data)
366: *data = arr->arr_data[i];
367: if (delAfter)
368: arr->arr_data[i] = NULL;
369: ret = i;
370: break;
371: }
372:
373: return ret;
374: }
375:
376: /*
1.3 misho 377: * io_arrayConcat() Concat source array to destination array
378: * @dest = Destination array
379: * @src = Source array
380: * return: -1 error; >0 new count of destination array
381: */
382: int
383: io_arrayConcat(array_t * __restrict dest, array_t * __restrict src)
384: {
385: int n;
386:
387: assert(dest);
388: assert(src);
389: if (!dest || !src)
390: return -1;
391:
392: n = io_arraySize(dest);
1.5 misho 393: if (io_arrayGrow(dest, n + io_arraySize(src), 0))
1.3 misho 394: return -1;
395: memcpy(dest->arr_data + n, src->arr_data, io_arraySize(src) * sizeof(void*));
396:
397: return io_arraySize(dest);
398: }
399:
400: /*
1.5 misho 401: * io_arrayCopy() Copy source array to destination array
402: * @dest = Destination array, after use free with io_arrayDestroy()
403: * @src = Source array
404: * return: -1 error; >0 count of destination array
405: */
406: int
407: io_arrayCopy(array_t ** __restrict dest, array_t * __restrict src)
408: {
409: assert(dest);
410: assert(src);
411: if (!dest || !src)
412: return -1;
413:
414: *dest = io_arrayInit(io_arraySize(src));
415: if (!*dest)
416: return -1;
417:
418: memcpy((*dest)->arr_data, src->arr_data, io_arraySize(*dest) * sizeof(void*));
419: return io_arraySize(*dest);
420: }
421:
422: /*
1.2 misho 423: * io_argsNum() Parse and calculate number of arguments
424: * @csArgs = Input arguments line
425: * @csDelim = Delimiter(s) for separate
426: * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
427: */
428: inline int
429: io_argsNum(const char *csArgs, const char *csDelim)
430: {
431: register int res;
432: char *pos;
433:
434: assert(csArgs);
435: assert(csDelim);
436: if (!csArgs || !csDelim)
437: return -1;
438:
439: for (res = 1, pos = (char*) csArgs; (pos = strpbrk(pos, csDelim)); res++, pos++);
440: return res;
441: }
442:
443: /*
444: * io_arrayMake() Parse and make array from arguments ... (input string will be modified!!!
445: * and output array must be free with io_arrayDestroy() after use!)
446: * @psArgs = Input arguments line, after execute string is modified!!!
447: * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs
448: * @csDelim = Delimiter(s) for separate
449: * @parr = Output array of arguments ... (must be free with io_arrayDestroy() after use!)
450: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
451: */
452: int
453: io_arrayMake(char * __restrict psArgs, int nargs, const char *csDelim, array_t ** __restrict parr)
454: {
455: char **app;
456: register int i;
457:
458: assert(psArgs);
459: assert(csDelim);
460: assert(parr);
461: if (!psArgs || !csDelim || !parr)
462: return -1;
463:
464: if (nargs)
465: i = nargs;
466: else
467: i = io_argsNum(psArgs, csDelim);
468: *parr = io_arrayInit(i);
469: if (!*parr)
470: return -1;
471:
472: for (i = 0, app = (char**) (*parr)->arr_data;
473: app < (char**) (*parr)->arr_data + (*parr)->arr_num &&
474: (*app = strsep((char **) &psArgs, csDelim));
475: **app ? i++ : i, **app ? app++ : app);
476: return i;
477: }
478:
479: /*
480: * io_MakeAV() Parse and make attribute/value pair
481: * @csArgs = Input argument line
482: * @csDelim = Delimiter for separate
483: * @psAttr = Output Attribute
484: * @attrLen = Size of attribute array
485: * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
486: * @valLen = Size of value array
487: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
488: */
489: int
490: io_MakeAV(const char * __restrict csArgs, const char *csDelim,
491: char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen)
492: {
493: register int ret = 0;
494: char *pos, *psBuf;
495:
496: if (!csArgs || !csDelim || !psAttr || !attrLen)
497: return -1;
498: if (psValue && !valLen)
499: return -1;
500: else
501: memset(psValue, 0, valLen);
502: psBuf = strdup(csArgs);
503: if (!psBuf) {
504: LOGERR;
505: return -1;
506: }
507:
508: pos = strpbrk(psBuf, csDelim);
509: if (pos)
510: *pos++ = 0;
511: ret++;
512: strlcpy(psAttr, psBuf, attrLen);
513:
514: if (pos && *pos) {
515: ret++;
516: if (psValue)
517: strlcpy(psValue, pos, valLen);
518: }
519:
520: free(psBuf);
521: return ret;
522: }
523:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>