Annotation of libaitio/src/array.c, revision 1.1.2.3
1.1.2.1 misho 1: /*************************************************************************
1.1.2.3 ! misho 2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
! 3: * by Michael Pounov <misho@elwix.org>
1.1.2.1 misho 4: *
5: * $Author: misho $
1.1.2.3 ! misho 6: * $Id: array.c,v 1.1.2.2 2011/04/19 22:00:44 misho Exp $
1.1.2.1 misho 7: *
1.1.2.3 ! misho 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 the University 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 THE REGENTS 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: */
1.1.2.1 misho 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(arr->arr_num, sizeof(void*));
67: if (!arr->arr_data) {
68: LOGERR;
69: free(arr);
70: return NULL;
71: } else
72: memset(arr->arr_data, 0, arr->arr_num * sizeof(void*));
73:
74: return arr;
75: }
76:
77: /*
78: * io_arrayFree() - Free all data in dynamic array
79: * (WARNING! If assign static array dont use this!!!)
80: * @arr = Array
81: * return: none
82: */
83: inline void
84: io_arrayFree(array_t * __restrict arr)
85: {
86: register int i;
87:
88: assert(arr);
89: if (!arr)
90: return;
91:
92: for (i = 0; i < arr->arr_num; i++)
93: if (arr->arr_data[i]) {
94: free(arr->arr_data[i]);
95: arr->arr_data[i] = NULL;
96: }
97: }
98:
99: /*
100: * io_arrayDestroy() - Free all data in dynamic array and Destroy dynamic array
101: * @parr = Array
102: * return: none
103: */
104: inline void
105: io_arrayDestroy(array_t ** __restrict parr)
106: {
107: assert(parr);
108: if (!parr)
109: return;
110:
111: if ((*parr)->arr_data)
112: free((*parr)->arr_data);
113: free(*parr);
114: *parr = NULL;
115: }
116:
117: /*
118: * io_arrayLen() - Get last used element in dynamic array (array Length)
119: * @arr = Array
120: * return: -1 error, 0 empty or >0 position of last used element
121: */
122: inline int
123: io_arrayLen(array_t * __restrict arr)
124: {
125: register int i;
126:
127: assert(arr);
128: if (!arr)
129: return -1;
130:
131: for (i = arr->arr_num; i && !arr->arr_data[i - 1]; i--);
132:
133: return i;
134: }
135:
136: /*
137: * io_arrayGrow() - Grow/Shrink dynamic array, Use with care when it shrink!!!
138: * @arr = Array
139: * @newNumItems = Number of Items
140: * return: -1 error, 0 ok
141: */
142: int
143: io_arrayGrow(array_t * __restrict arr, int newNumItems)
144: {
145: void **data;
146: int n = 0;
147: /* register int i; */
148:
149: assert(arr);
150: if (!arr)
151: return -1;
152:
153: if (arr->arr_num == newNumItems)
154: return 0;
155: if (arr->arr_num < newNumItems) {
156: n = newNumItems - arr->arr_num;
157: } /* else
158: for (i = newNumItems; i < arr->arr_num; i++)
159: if (arr->arr_data[i])
160: free(arr->arr_data[i]);
161: */
162:
1.1.2.2 misho 163: arr->arr_num = newNumItems;
1.1.2.1 misho 164: data = realloc(arr->arr_data, arr->arr_num * sizeof(void*));
165: if (!data) {
166: LOGERR;
167: return -1;
168: } else
169: arr->arr_data = data;
1.1.2.2 misho 170: memset(arr->arr_data + (arr->arr_num - n), 0, n * sizeof(void*));
1.1.2.1 misho 171:
172: return 0;
173: }
174:
175: /*
176: * io_arrayVacuum() - Vacuum dynamic array, empty elements will be deleted
177: * @arr = Array
178: * @fromWhere = 1 begin, 2 ALL empty elements
179: * return: -1 error, 0 ok
180: */
181: int
182: io_arrayVacuum(array_t * __restrict arr, int fromWhere)
183: {
184: register int i, j, num;
185: int cx = 0;
186:
187: assert(arr);
188: if (!arr)
189: return -1;
190: else
191: fromWhere &= 0x7;
192:
193: num = arr->arr_num;
194: /*
195: if (fromWhere & VACUUM_RIGHT) {
196: for (cx = 0, i = num - 1; i && !arr->arr_data[i]; i--, cx++);
197: num -= cx;
198: }
199: */
200: if (fromWhere & VACUUM_LEFT) {
201: for (i = 0; i < num && !arr->arr_data[i]; i++);
202:
203: memmove(arr->arr_data, arr->arr_data + i, (num - i) * sizeof(void*));
204: memset(arr->arr_data + (num - i), 0, i * sizeof(void*));
205:
206: num -= i;
207: cx += i;
208: }
209: if (fromWhere & VACUUM_BETWEEN) {
210: for (i = 0; i < num; i++) {
211: if (arr->arr_data[i])
212: continue;
213:
214: for (j = i; j < num && !arr->arr_data[j]; j++);
215:
216: memmove(arr->arr_data + i, arr->arr_data + j, (num - j) * sizeof(void*));
217: memset(arr->arr_data + i + (num - j), 0, (j - i) * sizeof(void*));
218:
219: num -= j - i;
220: cx += j - i;
221: }
222: }
223:
224: return cx;
225: }
226:
227: /*
228: * io_argsNum() Parse and calculate number of arguments
229: * @csArgs = Input arguments line
230: * @csDelim = Delimiter(s) for separate
231: * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
232: */
233: inline int
234: io_argsNum(const char *csArgs, const char *csDelim)
235: {
236: register int res;
237: char *pos;
238:
239: assert(csArgs);
240: assert(csDelim);
241: if (!csArgs || !csDelim)
242: return -1;
243:
244: for (res = 1, pos = (char*) csArgs; (pos = strpbrk(pos, csDelim)); res++, pos++);
245: return res;
246: }
247:
248: /*
249: * io_arrayMake() Parse and make array from arguments ... (input string will be modified!!!
250: * and output array must be free with io_arrayDestroy() after use!)
251: * @psArgs = Input arguments line, after execute string is modified!!!
252: * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs
253: * @csDelim = Delimiter(s) for separate
254: * @parr = Output array of arguments ... (must be free with io_arrayDestroy() after use!)
255: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
256: */
257: int
258: io_arrayMake(char * __restrict psArgs, int nargs, const char *csDelim, array_t ** __restrict parr)
259: {
260: char **app;
261: register int i;
262:
263: assert(psArgs);
264: assert(csDelim);
265: assert(parr);
266: if (!psArgs || !csDelim || !parr)
267: return -1;
268:
269: if (nargs)
270: i = nargs;
271: else
272: i = io_argsNum(psArgs, csDelim);
273: *parr = io_arrayInit(i);
274: if (!*parr)
275: return -1;
276:
277: for (i = 0, app = (char**) (*parr)->arr_data;
278: app < (char**) (*parr)->arr_data + (*parr)->arr_num &&
279: (*app = strsep((char **) &psArgs, csDelim));
280: **app ? i++ : i, **app ? app++ : app);
281: return i;
282: }
283:
284: /*
285: * io_MakeAV() Parse and make attribute/value pair
286: * @csArgs = Input argument line
287: * @csDelim = Delimiter for separate
288: * @psAttr = Output Attribute
289: * @attrLen = Size of attribute array
290: * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
291: * @valLen = Size of value array
292: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
293: */
294: int
295: io_MakeAV(const char * __restrict csArgs, const char *csDelim,
296: char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen)
297: {
298: register int ret = 0;
299: char *pos, *psBuf;
300:
301: if (!csArgs || !csDelim || !psAttr || !attrLen)
302: return -1;
303: if (psValue && !valLen)
304: return -1;
305: else
306: memset(psValue, 0, valLen);
307: psBuf = strdup(csArgs);
308: if (!psBuf) {
309: LOGERR;
310: return -1;
311: }
312:
313: pos = strpbrk(psBuf, csDelim);
314: if (pos)
315: *pos++ = 0;
316: ret++;
317: strlcpy(psAttr, psBuf, attrLen);
318:
319: if (pos && *pos) {
320: ret++;
321: if (psValue)
322: strlcpy(psValue, pos, valLen);
323: }
324:
325: free(psBuf);
326: return ret;
327: }
328:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>