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

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 $
        !             6: * $Id: array.c,v 1.1.2.5 2011/04/20 22:55:41 misho Exp $
        !             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: /*
        !            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: 
        !           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: /*
        !           281:  * io_argsNum() Parse and calculate number of arguments
        !           282:  * @csArgs = Input arguments line
        !           283:  * @csDelim = Delimiter(s) for separate
        !           284:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
        !           285:  */
        !           286: inline int
        !           287: io_argsNum(const char *csArgs, const char *csDelim)
        !           288: {
        !           289:        register int res;
        !           290:        char *pos;
        !           291: 
        !           292:        assert(csArgs);
        !           293:        assert(csDelim);
        !           294:        if (!csArgs || !csDelim)
        !           295:                return -1;
        !           296: 
        !           297:        for (res = 1, pos = (char*) csArgs; (pos = strpbrk(pos, csDelim)); res++, pos++);
        !           298:        return res;
        !           299: }
        !           300: 
        !           301: /*
        !           302:  * io_arrayMake() Parse and make array from arguments ... (input string will be modified!!! 
        !           303:  *     and output array must be free with io_arrayDestroy() after use!)
        !           304:  * @psArgs = Input arguments line, after execute string is modified!!!
        !           305:  * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs
        !           306:  * @csDelim = Delimiter(s) for separate
        !           307:  * @parr = Output array of arguments ... (must be free with io_arrayDestroy() after use!)
        !           308:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
        !           309:  */
        !           310: int
        !           311: io_arrayMake(char * __restrict psArgs, int nargs, const char *csDelim, array_t ** __restrict parr)
        !           312: {
        !           313:        char **app;
        !           314:        register int i;
        !           315: 
        !           316:        assert(psArgs);
        !           317:        assert(csDelim);
        !           318:        assert(parr);
        !           319:        if (!psArgs || !csDelim || !parr)
        !           320:                return -1;
        !           321: 
        !           322:        if (nargs)
        !           323:                i = nargs;
        !           324:        else
        !           325:                i = io_argsNum(psArgs, csDelim);
        !           326:        *parr = io_arrayInit(i);
        !           327:        if (!*parr)
        !           328:                return -1;
        !           329: 
        !           330:        for (i = 0, app = (char**) (*parr)->arr_data; 
        !           331:                        app < (char**) (*parr)->arr_data + (*parr)->arr_num && 
        !           332:                        (*app = strsep((char **) &psArgs, csDelim)); 
        !           333:                        **app ? i++ : i, **app ? app++ : app);
        !           334:        return i;
        !           335: }
        !           336: 
        !           337: /*
        !           338:  * io_MakeAV() Parse and make attribute/value pair
        !           339:  * @csArgs = Input argument line
        !           340:  * @csDelim = Delimiter for separate
        !           341:  * @psAttr = Output Attribute
        !           342:  * @attrLen = Size of attribute array
        !           343:  * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
        !           344:  * @valLen = Size of value array
        !           345:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
        !           346: */
        !           347: int
        !           348: io_MakeAV(const char * __restrict csArgs, const char *csDelim, 
        !           349:                char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen)
        !           350: {
        !           351:        register int ret = 0;
        !           352:        char *pos, *psBuf;
        !           353: 
        !           354:        if (!csArgs || !csDelim || !psAttr || !attrLen)
        !           355:                return -1;
        !           356:        if (psValue && !valLen)
        !           357:                return -1;
        !           358:        else
        !           359:                memset(psValue, 0, valLen);
        !           360:        psBuf = strdup(csArgs);
        !           361:        if (!psBuf) {
        !           362:                LOGERR;
        !           363:                return -1;
        !           364:        }
        !           365: 
        !           366:        pos = strpbrk(psBuf, csDelim);
        !           367:        if (pos)
        !           368:                *pos++ = 0;
        !           369:        ret++;
        !           370:        strlcpy(psAttr, psBuf, attrLen);
        !           371: 
        !           372:        if (pos && *pos) {
        !           373:                ret++;
        !           374:                if (psValue)
        !           375:                        strlcpy(psValue, pos, valLen);
        !           376:        }
        !           377: 
        !           378:        free(psBuf);
        !           379:        return ret;
        !           380: }
        !           381: 

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