Annotation of libaitio/src/array.c, revision 1.2.4.1

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.2.4.1 ! misho       6: * $Id: array.c,v 1.2 2011/04/20 22:56:32 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;
                     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: /*
1.2.4.1 ! misho      78:  * io_arrayFree() - Free all data in dynamic array items
1.2       misho      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: /*
1.2.4.1 ! misho     100:  * io_arrayDestroy() - Free and destroy dynamic array
1.2       misho     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: 
                    163:        arr->arr_num = newNumItems;
                    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;
                    170:        memset(arr->arr_data + (arr->arr_num - n), 0, n * sizeof(void*));
                    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_arrayPush() - Push element into dynamic array like stack manner, place at first empty position
                    229:  * @arr = Array
                    230:  * @data = Element, if set NULL return only first empty position
                    231:  * return: -1 not found empty position, array is full!, >-1 return position of stored element into array
                    232:  */
                    233: inline int
                    234: io_arrayPush(array_t * __restrict arr, void **data)
                    235: {
                    236:        register int i;
                    237:        int ret = -1;
                    238: 
                    239:        assert(arr);
                    240: 
                    241:        for (i = 0; i < arr->arr_num; i++)
                    242:                if (!arr->arr_data[i]) {
                    243:                        if (data)
                    244:                                arr->arr_data[i] = *data;
                    245:                        ret = i;
                    246:                        break;
                    247:                }
                    248: 
                    249:        return ret;
                    250: }
                    251: 
                    252: /*
                    253:  * io_arrayPop() - Pop element from dynamic array like stack manner, last used position
                    254:  * @arr = Array
                    255:  * @data = Element, if set NULL return only first empty position
                    256:  * @delAfter = Delete after Pop element, !=0 delete element from array after return data
                    257:  * return: -1 not found used position, array is empty!, >-1 return element position
                    258:  */
                    259: inline int
                    260: io_arrayPop(array_t * __restrict arr, void ** __restrict data, int delAfter)
                    261: {
                    262:        register int i;
                    263:        int ret = -1;
                    264: 
                    265:        assert(arr);
                    266: 
                    267:        for (i = arr->arr_num - 1; i >= 0; i--)
                    268:                if (arr->arr_data[i]) {
                    269:                        if (data)
                    270:                                *data = arr->arr_data[i];
                    271:                        if (delAfter)
                    272:                                arr->arr_data[i] = NULL;
                    273:                        ret = i;
                    274:                        break;
                    275:                }
                    276: 
                    277:        return ret;
                    278: }
                    279: 
                    280: /*
1.2.4.1 ! misho     281:  * io_arrayConcat() Concat source array to destination array
        !           282:  * @dest = Destination array
        !           283:  * @src = Source array
        !           284:  * return: -1 error; >0 new count of destination array
        !           285:  */
        !           286: int
        !           287: io_arrayConcat(array_t * __restrict dest, array_t * __restrict src)
        !           288: {
        !           289:        int n;
        !           290: 
        !           291:        assert(dest);
        !           292:        assert(src);
        !           293:        if (!dest || !src)
        !           294:                return -1;
        !           295: 
        !           296:        n = dest->arr_num;
        !           297:        if (io_arrayGrow(dest, n + src->arr_num))
        !           298:                return -1;
        !           299:        memcpy(dest->arr_data + n, src->arr_data, src->arr_num * sizeof(void*));
        !           300: 
        !           301:        return io_arraySize(dest);
        !           302: }
        !           303: 
        !           304: /*
1.2       misho     305:  * io_argsNum() Parse and calculate number of arguments
                    306:  * @csArgs = Input arguments line
                    307:  * @csDelim = Delimiter(s) for separate
                    308:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
                    309:  */
                    310: inline int
                    311: io_argsNum(const char *csArgs, const char *csDelim)
                    312: {
                    313:        register int res;
                    314:        char *pos;
                    315: 
                    316:        assert(csArgs);
                    317:        assert(csDelim);
                    318:        if (!csArgs || !csDelim)
                    319:                return -1;
                    320: 
                    321:        for (res = 1, pos = (char*) csArgs; (pos = strpbrk(pos, csDelim)); res++, pos++);
                    322:        return res;
                    323: }
                    324: 
                    325: /*
                    326:  * io_arrayMake() Parse and make array from arguments ... (input string will be modified!!! 
                    327:  *     and output array must be free with io_arrayDestroy() after use!)
                    328:  * @psArgs = Input arguments line, after execute string is modified!!!
                    329:  * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs
                    330:  * @csDelim = Delimiter(s) for separate
                    331:  * @parr = Output array of arguments ... (must be free with io_arrayDestroy() after use!)
                    332:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
                    333:  */
                    334: int
                    335: io_arrayMake(char * __restrict psArgs, int nargs, const char *csDelim, array_t ** __restrict parr)
                    336: {
                    337:        char **app;
                    338:        register int i;
                    339: 
                    340:        assert(psArgs);
                    341:        assert(csDelim);
                    342:        assert(parr);
                    343:        if (!psArgs || !csDelim || !parr)
                    344:                return -1;
                    345: 
                    346:        if (nargs)
                    347:                i = nargs;
                    348:        else
                    349:                i = io_argsNum(psArgs, csDelim);
                    350:        *parr = io_arrayInit(i);
                    351:        if (!*parr)
                    352:                return -1;
                    353: 
                    354:        for (i = 0, app = (char**) (*parr)->arr_data; 
                    355:                        app < (char**) (*parr)->arr_data + (*parr)->arr_num && 
                    356:                        (*app = strsep((char **) &psArgs, csDelim)); 
                    357:                        **app ? i++ : i, **app ? app++ : app);
                    358:        return i;
                    359: }
                    360: 
                    361: /*
                    362:  * io_MakeAV() Parse and make attribute/value pair
                    363:  * @csArgs = Input argument line
                    364:  * @csDelim = Delimiter for separate
                    365:  * @psAttr = Output Attribute
                    366:  * @attrLen = Size of attribute array
                    367:  * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
                    368:  * @valLen = Size of value array
                    369:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
                    370: */
                    371: int
                    372: io_MakeAV(const char * __restrict csArgs, const char *csDelim, 
                    373:                char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen)
                    374: {
                    375:        register int ret = 0;
                    376:        char *pos, *psBuf;
                    377: 
                    378:        if (!csArgs || !csDelim || !psAttr || !attrLen)
                    379:                return -1;
                    380:        if (psValue && !valLen)
                    381:                return -1;
                    382:        else
                    383:                memset(psValue, 0, valLen);
                    384:        psBuf = strdup(csArgs);
                    385:        if (!psBuf) {
                    386:                LOGERR;
                    387:                return -1;
                    388:        }
                    389: 
                    390:        pos = strpbrk(psBuf, csDelim);
                    391:        if (pos)
                    392:                *pos++ = 0;
                    393:        ret++;
                    394:        strlcpy(psAttr, psBuf, attrLen);
                    395: 
                    396:        if (pos && *pos) {
                    397:                ret++;
                    398:                if (psValue)
                    399:                        strlcpy(psValue, pos, valLen);
                    400:        }
                    401: 
                    402:        free(psBuf);
                    403:        return ret;
                    404: }
                    405: 

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