Annotation of libaitcfg/src/queue.c, revision 1.14.4.2

1.2       misho       1: /*************************************************************************
                      2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.14.4.2! misho       6: * $Id: queue.c,v 1.14.4.1 2014/03/19 16:35:29 misho Exp $
1.2       misho       7: *
1.6       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: 
1.13      misho      15: Copyright 2004 - 2014
1.6       misho      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: */
1.1       misho      46: #include "global.h"
                     47: 
                     48: 
1.7       misho      49: static inline struct tagCfg *
                     50: _selectAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1       misho      51: {
1.10      misho      52:        struct tagCfg fav, *c;
1.1       misho      53: 
                     54:        if (!cfg)
                     55:                return NULL;
1.7       misho      56:        else
                     57:                memset(&fav, 0, sizeof fav);
1.1       misho      58: 
1.7       misho      59:        if (csSec && *csSec)
                     60:                AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
1.12      misho      61:                                E_ALIGN(strlen(csSec), 2) / 2);
1.7       misho      62:        if (csAttr)
                     63:                AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr, 
1.12      misho      64:                                E_ALIGN(strlen(csAttr), 2) / 2);
1.7       misho      65: 
                     66:        if (!csAttr)
                     67:                return RB_NFIND(tagRC, cfg, &fav);
1.10      misho      68:        else {
                     69:                c = RB_FIND(tagRC, cfg, &fav);
                     70:                if (!c)
                     71:                        return NULL;    /* not found */
                     72:                do {
                     73:                        if (!strcmp(AIT_GET_STR(&c->cfg_attr), csAttr))
                     74:                                return c;       /* FOUND! */
                     75:                } while (RB_NEXT(tagRC, cfg, c) && !cfg_tree_cmp(c, &fav));
                     76:                return NULL;    /* not found */
                     77:        }
1.1       misho      78: }
                     79: 
1.7       misho      80: /* --------------------------------------------------------------- */
1.1       misho      81: 
                     82: /*
1.14.4.2! misho      83:  * cfg_getSection() - Get entire section attributes into array
        !            84:  *
        !            85:  * @cfg = Config root
        !            86:  * @csSec = Config section //[{csSec}]
        !            87:  * return: NULL not found or !=NULL allocated array, must free with array_Destroy() after use!
        !            88:  */
        !            89: array_t *
        !            90: cfg_getSection(cfg_root_t * __restrict cfg, const char *csSec)
        !            91: {
        !            92:        array_t *arr = NULL;
        !            93:        struct tagCfg *av, fav;
        !            94: 
        !            95:        if (!cfg) {
        !            96:                cfg_SetErr(EINVAL, "Invalid argument(s)");
        !            97:                return NULL;
        !            98:        } else
        !            99:                memset(&fav, 0, sizeof fav);
        !           100: 
        !           101:        if (csSec && *csSec)
        !           102:                AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
        !           103:                                E_ALIGN(strlen(csSec), 2) / 2);
        !           104: 
        !           105:        av = RB_NFIND(tagRC, cfg, &fav);
        !           106:        if (!av)
        !           107:                return NULL;
        !           108:        if (strcmp(AIT_GET_STR(&av->cfg_sec), csSec))
        !           109:                return NULL;
        !           110: 
        !           111:        arr = array_Init(1);
        !           112:        if (!arr) {
        !           113:                cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
        !           114:                return NULL;
        !           115:        } else
        !           116:                array_Push(arr, av, 0);
        !           117: 
        !           118:        while (RB_NEXT(tagRC, cfg, av) && !strcmp(AIT_GET_STR(&av->cfg_sec), csSec))
        !           119:                array_Push(arr, av, 0);
        !           120: 
        !           121:        return arr;
        !           122: }
        !           123: 
        !           124: /*
1.7       misho     125:  * cfg_findAttribute() - Find attribute position in config file
                    126:  *
                    127:  * @cfg = Config root
1.1       misho     128:  * @csSec = Config section //[{csSec}]
1.14.4.1  misho     129:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
1.7       misho     130:  * return: 0 not found item, -1 error or >0 position in list
                    131:  */
1.12      misho     132: int
1.7       misho     133: cfg_findAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1       misho     134: {
1.7       misho     135:        struct tagCfg *av, fav;
1.1       misho     136:        register int cx = 0;
                    137: 
1.14.4.1  misho     138:        if (!cfg) {
1.7       misho     139:                cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1       misho     140:                return -1;
1.7       misho     141:        } else
                    142:                memset(&fav, 0, sizeof fav);
1.1       misho     143: 
1.7       misho     144:        if (csSec && *csSec)
                    145:                AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
1.12      misho     146:                                E_ALIGN(strlen(csSec), 2) / 2);
1.7       misho     147:        if (csAttr)
                    148:                AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr, 
1.12      misho     149:                                E_ALIGN(strlen(csAttr), 2) / 2);
1.7       misho     150: 
1.14      misho     151:        TAILQ_FOREACH(av, cfg, cfg_next) {
1.1       misho     152:                ++cx;
1.7       misho     153:                if (!cfg_tree_cmp(&fav, av))
                    154:                        return cx;
1.1       misho     155:        }
                    156: 
                    157:        return 0;
                    158: }
                    159: 
                    160: /*
1.7       misho     161:  * cfg_unsetAttribute() - Unset item from config and free resources
                    162:  *
                    163:  * @cfg = Config root
1.1       misho     164:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.14.4.1  misho     165:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
1.7       misho     166:  * return: 0 item not found, -1 error or 1 removed item
                    167:  */
                    168: int
                    169: cfg_unsetAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1       misho     170: {
1.7       misho     171:        struct tagCfg *av;
1.1       misho     172: 
1.14.4.1  misho     173:        if (!cfg)
1.1       misho     174:                return -1;
                    175: 
1.7       misho     176:        av = _selectAttribute(cfg, csSec, csAttr);
1.1       misho     177:        if (!av)
                    178:                return 0;
                    179: 
1.7       misho     180:        CFG_RC_LOCK(cfg);
                    181:        RB_REMOVE(tagRC, cfg, av);
1.14      misho     182:        TAILQ_REMOVE(cfg, av, cfg_next);
1.7       misho     183:        CFG_RC_UNLOCK(cfg);
                    184: 
                    185:        AIT_FREE_VAL(&av->cfg_val);
                    186:        AIT_FREE_VAL(&av->cfg_attr);
                    187:        AIT_FREE_VAL(&av->cfg_sec);
1.12      misho     188:        e_free(av);
1.7       misho     189:        return 1;
1.1       misho     190: }
                    191: 
                    192: /*
1.7       misho     193:  * cfg_setAttribute() - Set item in config or adding new item if not exists
                    194:  *
                    195:  * @cfg = Config root
1.1       misho     196:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
1.7       misho     197:  * @csAttr = Config attribute //{csAttr} = ...
1.1       misho     198:  * @csVal = Config value //... = {csVal} to setup
1.7       misho     199:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
                    200:  */
                    201: int
                    202: cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, const char *csVal)
1.1       misho     203: {
1.7       misho     204:        struct tagCfg *av, *section;
1.1       misho     205: 
                    206:        if (!cfg || !csAttr)
                    207:                return -1;
                    208: 
1.7       misho     209:        av = _selectAttribute(cfg, csSec, csAttr);
1.1       misho     210:        if (!av) {
1.7       misho     211:                /* adding new element */
                    212:                section = _selectAttribute(cfg, csSec, NULL);
1.1       misho     213: 
1.12      misho     214:                av = e_malloc(sizeof(struct tagCfg));
1.1       misho     215:                if (!av) {
                    216:                        LOGERR;
                    217:                        return -1;
                    218:                } else {
1.7       misho     219:                        memset(av, 0, sizeof(struct tagCfg));
1.1       misho     220: 
1.7       misho     221:                        CFG_RC_LOCK(cfg);
                    222:                        if (!section)
1.14      misho     223:                                TAILQ_INSERT_TAIL(cfg, av, cfg_next);
1.7       misho     224:                        else
1.14      misho     225:                                TAILQ_INSERT_BEFORE(section, av, cfg_next);
1.7       misho     226:                        CFG_RC_UNLOCK(cfg);
1.1       misho     227:                }
1.7       misho     228: 
1.1       misho     229:                if (csSec && *csSec) {
1.7       misho     230:                        AIT_SET_STR(&av->cfg_sec, csSec);
                    231:                        AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
1.12      misho     232:                                        E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
1.1       misho     233:                }
1.7       misho     234:                AIT_SET_STR(&av->cfg_val, csVal ? csVal : "");
                    235:                AIT_SET_STR(&av->cfg_attr, csAttr);
                    236:                AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
1.12      misho     237:                                E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
1.7       misho     238: 
                    239:                CFG_RC_LOCK(cfg);
                    240:                RB_INSERT(tagRC, cfg, av);
                    241:                CFG_RC_UNLOCK(cfg);
1.1       misho     242:                return 2;
                    243:        }
                    244: 
1.10      misho     245:        if (csVal && AIT_ADDR(&av->cfg_val) && 
                    246:                        strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {
1.7       misho     247:                /* Update element */
                    248:                AIT_FREE_VAL(&av->cfg_val);
                    249:                AIT_SET_STR(&av->cfg_val, csVal);
1.1       misho     250:                return 1;
                    251:        }
                    252: 
1.7       misho     253:        /* Nothing happens ... found & values is equal! */
1.1       misho     254:        return 0;
                    255: }
                    256: 
                    257: /*
1.7       misho     258:  * cfg_getAttribute() - Get item from config and return value from it
                    259:  *
                    260:  * @cfg = Config root
1.1       misho     261:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.14.4.1  misho     262:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
1.7       misho     263:  * return: NULL item not found or null parameters, !=NULL value const string
                    264:  */
1.12      misho     265: const char *
1.7       misho     266: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1       misho     267: {
1.7       misho     268:        struct tagCfg *av;
1.1       misho     269: 
1.14.4.1  misho     270:        if (!cfg)
1.1       misho     271:                return NULL;
                    272: 
1.7       misho     273:        av = _selectAttribute(cfg, csSec, csAttr);
1.1       misho     274:        if (!av)
                    275:                return NULL;
                    276: 
1.7       misho     277:        return AIT_GET_STR(&av->cfg_val);
1.4       misho     278: }
                    279: 
1.1       misho     280: /*
1.7       misho     281:  * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
                    282:  *
                    283:  * @cfg = Config root
1.1       misho     284:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.14.4.1  misho     285:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
1.7       misho     286:  * @val = Return buffer for item Value //... = {val}
                    287:  * @csDefValue = *Default Value* for return in //{val}, if not found item in config
                    288:  * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
                    289:  */
                    290: int
                    291: cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, 
                    292:                ait_val_t * __restrict val, const char *csDefValue)
1.1       misho     293: {
1.7       misho     294:        struct tagCfg *av;
1.1       misho     295:        int ret = 0;
                    296: 
1.14.4.1  misho     297:        if (!cfg || !val) {
1.7       misho     298:                cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1       misho     299:                return -1;
1.7       misho     300:        }
1.1       misho     301: 
1.9       misho     302:        AIT_INIT_VAL(val);
1.7       misho     303:        av = _selectAttribute(cfg, csSec, csAttr);
1.1       misho     304:        if (!av) {
1.7       misho     305:                /* not found item */
1.1       misho     306:                if (csDefValue) {
1.7       misho     307:                        AIT_SET_STR(val, csDefValue);
                    308:                        ret = AIT_LEN(val);
1.8       misho     309:                } else
                    310:                        AIT_INIT_VAL(val);
1.1       misho     311:                return ret;
                    312:        }
                    313: 
1.10      misho     314:        if (AIT_ISEMPTY(&av->cfg_val) || !AIT_ADDR(&av->cfg_val) || 
                    315:                        !*AIT_GET_LIKE(&av->cfg_val, char*)) {
1.7       misho     316:                /* empty value */
1.1       misho     317:                if (csDefValue) {
1.7       misho     318:                        AIT_SET_STR(val, csDefValue);
                    319:                        ret = AIT_LEN(val);
1.8       misho     320:                } else
                    321:                        AIT_INIT_VAL(val);
1.1       misho     322:        } else {
1.7       misho     323:                /* copy value */
                    324:                AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val));
                    325:                ret = AIT_LEN(val);
1.1       misho     326:        }
                    327: 
                    328:        return ret;
                    329: }

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