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

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.22    ! misho       6: * $Id: queue.c,v 1.21.4.1 2026/10/05 16:38:26 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.22    ! misho      15: Copyright 2004 - 2026
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 *
1.22    ! misho      50: _selectAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, int *find)
1.1       misho      51: {
1.19      misho      52:        struct tagCfg fav, *c, *n;
1.1       misho      53: 
                     54:        if (!cfg)
                     55:                return NULL;
1.7       misho      56:        else
                     57:                memset(&fav, 0, sizeof fav);
1.22    ! misho      58:        if (find)
        !            59:                *find = 0;
1.1       misho      60: 
1.7       misho      61:        if (csSec && *csSec)
                     62:                AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
1.12      misho      63:                                E_ALIGN(strlen(csSec), 2) / 2);
1.7       misho      64:        if (csAttr)
                     65:                AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr, 
1.12      misho      66:                                E_ALIGN(strlen(csAttr), 2) / 2);
1.7       misho      67: 
1.19      misho      68:        if (!csAttr) {
                     69:                c = RB_NFIND(tagRC, cfg, &fav);
                     70:                if (!c)
                     71:                        return NULL;    /* not found */
                     72:                if (csSec && !AIT_ISEMPTY(&c->cfg_sec) && 
                     73:                                strcmp(csSec, AIT_GET_STR(&c->cfg_sec)))
                     74:                        TAILQ_FOREACH_SAFE(c, cfg, cfg_next, n) {
                     75:                                if (!AIT_ISEMPTY(&c->cfg_sec) && 
                     76:                                                !strcmp(csSec, AIT_GET_STR(&c->cfg_sec)))
                     77:                                        break;
                     78:                        }
1.22    ! misho      79:                if (find)
        !            80:                        *find = -1;
1.19      misho      81:                return c;
                     82:        } else {
1.10      misho      83:                c = RB_FIND(tagRC, cfg, &fav);
                     84:                if (!c)
                     85:                        return NULL;    /* not found */
1.22    ! misho      86:                if ((!csSec && AIT_ISEMPTY(&c->cfg_sec)) || (csSec && !AIT_ISEMPTY(&c->cfg_sec) &&
        !            87:                                        !strcmp(csSec, AIT_GET_STR(&c->cfg_sec))))
        !            88:                        if (!AIT_ISEMPTY(&c->cfg_attr) && !strcmp(csAttr, AIT_GET_STR(&c->cfg_attr))) {
        !            89:                                if (find)
        !            90:                                        *find = 1;
        !            91:                                return c;       /* FOUND! */
1.19      misho      92:                        }
                     93:                /* if cannot find right attribute */
                     94:                TAILQ_FOREACH_SAFE(c, cfg, cfg_next, n) {
                     95:                        if ((!csSec && AIT_ISEMPTY(&c->cfg_sec)) || 
                     96:                                        (csSec && !AIT_ISEMPTY(&c->cfg_sec) && 
                     97:                                         !strcmp(csSec, AIT_GET_STR(&c->cfg_sec))))
                     98:                                if (!AIT_ISEMPTY(&c->cfg_attr) && csAttr && 
1.22    ! misho      99:                                                !strcmp(csAttr, AIT_GET_STR(&c->cfg_attr))) {
        !           100:                                        if (find)
        !           101:                                                *find = 2;
1.19      misho     102:                                        return c;       /* FOUND! */
1.22    ! misho     103:                                }
1.19      misho     104:                }
1.10      misho     105:                return NULL;    /* not found */
                    106:        }
1.1       misho     107: }
                    108: 
1.7       misho     109: /* --------------------------------------------------------------- */
1.18      misho     110: 
                    111: /*
                    112:  * cfg_dumpCfg() - dump config data
                    113:  *
                    114:  * @cfg = Config root
                    115:  * return: none
                    116:  */
                    117: void
                    118: cfg_dumpCfg(cfg_root_t * __restrict cfg)
                    119: {
                    120:        struct tagCfg *r, *c, *ctmp, *q, *qtmp;
1.20      misho     121:        int syn = 0;
1.18      misho     122: 
                    123:        r = RB_ROOT(cfg);
                    124:        printf("ROOT:: KEY=%.8x [%s] %s=%s\n", ((AIT_KEY(&r->cfg_sec) << 15) | AIT_KEY(&r->cfg_attr)), 
                    125:                        AIT_ADDR(&r->cfg_sec), AIT_ADDR(&r->cfg_attr), AIT_ADDR(&r->cfg_val));
                    126: 
                    127:        RB_FOREACH_SAFE(c, tagRC, cfg, ctmp) {
                    128:                syn ^= syn;
                    129:                TAILQ_FOREACH_SAFE(q, cfg, cfg_next, qtmp) {
                    130:                        if (c == q) {
                    131:                                syn = 42;
                    132:                                break;
                    133:                        }
                    134:                }
                    135: 
                    136:                printf("%s KEY=%.8x [%s] %s=%s sync=%d\n", c == r ? "*" : "", 
                    137:                                ((AIT_KEY(&c->cfg_sec) << 15) | AIT_KEY(&c->cfg_attr)), 
                    138:                                AIT_ADDR(&c->cfg_sec), AIT_ADDR(&c->cfg_attr), AIT_ADDR(&c->cfg_val), syn);
                    139:        }
                    140: }
1.1       misho     141: 
                    142: /*
1.15      misho     143:  * cfg_getSection() - Get entire section attributes into array
                    144:  *
                    145:  * @cfg = Config root
                    146:  * @csSec = Config section //[{csSec}]
                    147:  * return: NULL not found or !=NULL allocated array, must free with array_Destroy() after use!
                    148:  */
                    149: array_t *
                    150: cfg_getSection(cfg_root_t * __restrict cfg, const char *csSec)
                    151: {
                    152:        array_t *arr = NULL;
                    153:        struct tagCfg *av, fav;
                    154: 
                    155:        if (!cfg) {
                    156:                cfg_SetErr(EINVAL, "Invalid argument(s)");
                    157:                return NULL;
                    158:        } else
                    159:                memset(&fav, 0, sizeof fav);
                    160:        if (csSec && !*csSec)
                    161:                csSec = NULL;
                    162: 
                    163:        if (csSec && *csSec)
                    164:                AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
                    165:                                E_ALIGN(strlen(csSec), 2) / 2);
                    166: 
1.17      misho     167:        arr = array_Init(0);
1.15      misho     168:        if (!arr) {
                    169:                cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                    170:                return NULL;
1.17      misho     171:        }
1.15      misho     172: 
1.17      misho     173:        TAILQ_FOREACH(av, cfg, cfg_next)
                    174:                if (AIT_KEY(&av->cfg_sec) == AIT_KEY(&fav.cfg_sec)) {
                    175:                        if (!csSec) {
                    176:                                if (AIT_ISEMPTY(&av->cfg_sec))
                    177:                                        array_Push(arr, av, 0);
                    178:                        } else {
                    179:                                if (!AIT_ISEMPTY(&av->cfg_sec) && 
                    180:                                                !strcmp(AIT_GET_STR(&av->cfg_sec), csSec))
                    181:                                        array_Push(arr, av, 0);
                    182:                        }
1.15      misho     183:                }
                    184: 
1.17      misho     185:        if (!array_Size(arr))
                    186:                array_Destroy(&arr);
1.15      misho     187: 
                    188:        return arr;
                    189: }
                    190: 
                    191: /*
1.21      misho     192:  * cfg_delSection() - Delete entire section
                    193:  *
                    194:  * @cfg = Config root
                    195:  * @csSec = Config section //[{csSec}]
                    196:  * return: -1 error, 0 nothing deleted or >0 deleted attributes
                    197:  */
                    198: int
                    199: cfg_delSection(cfg_root_t * __restrict cfg, const char *csSec)
                    200: {
                    201:        int ret = 0;
                    202:        array_t *arr = NULL;
                    203:        struct tagCfg *av;
                    204:        register int i;
                    205: 
                    206:        if (!cfg) {
                    207:                cfg_SetErr(EINVAL, "Invalid argument(s)");
                    208:                return -1;
                    209:        }
                    210:        if (csSec && !*csSec)
                    211:                csSec = NULL;
                    212: 
                    213:        arr = cfg_getSection(cfg, csSec);
                    214:        if (!arr)
                    215:                return ret;
                    216:        for (i = 0; i < array_Size(arr); i++)
                    217:                if ((av = array(arr, i, struct tagCfg*)) && AIT_ADDR(&av->cfg_attr))
                    218:                        if (cfg_unsetAttribute(cfg, csSec, AIT_GET_STR(&av->cfg_attr)) > 0)
                    219:                                ret++;
                    220:        array_Destroy(&arr);
                    221:        return ret;
                    222: }
                    223: 
                    224: /*
1.7       misho     225:  * cfg_findAttribute() - Find attribute position in config file
                    226:  *
                    227:  * @cfg = Config root
1.1       misho     228:  * @csSec = Config section //[{csSec}]
1.19      misho     229:  * @csAttr = Config attribute //{csAttr} = ...
1.7       misho     230:  * return: 0 not found item, -1 error or >0 position in list
                    231:  */
1.12      misho     232: int
1.7       misho     233: cfg_findAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1       misho     234: {
1.19      misho     235:        struct tagCfg *av, *n;
1.1       misho     236:        register int cx = 0;
                    237: 
1.15      misho     238:        if (!cfg) {
1.7       misho     239:                cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1       misho     240:                return -1;
1.19      misho     241:        }
1.1       misho     242: 
1.19      misho     243:        TAILQ_FOREACH_SAFE(av, cfg, cfg_next, n) {
                    244:                ++cx;
1.7       misho     245: 
1.19      misho     246:                if ((!csSec && AIT_ISEMPTY(&av->cfg_sec)) || 
                    247:                                (csSec && !AIT_ISEMPTY(&av->cfg_sec) && 
                    248:                                 !strcmp(csSec, AIT_GET_STR(&av->cfg_sec))))
                    249:                        if (!AIT_ISEMPTY(&av->cfg_attr) && csAttr && 
                    250:                                        !strcmp(csAttr, AIT_GET_STR(&av->cfg_attr)))
                    251:                                return cx;
1.1       misho     252:        }
                    253: 
                    254:        return 0;
                    255: }
                    256: 
                    257: /*
1.7       misho     258:  * cfg_unsetAttribute() - Unset item from config and free resources
                    259:  *
                    260:  * @cfg = Config root
1.1       misho     261:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19      misho     262:  * @csAttr = Config attribute //{csAttr} = ...
1.7       misho     263:  * return: 0 item not found, -1 error or 1 removed item
                    264:  */
                    265: int
                    266: cfg_unsetAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1       misho     267: {
1.22    ! misho     268:        struct tagCfg *av, *n;
        !           269:        int find;
1.1       misho     270: 
1.15      misho     271:        if (!cfg)
1.1       misho     272:                return -1;
                    273: 
1.22    ! misho     274:        av = _selectAttribute(cfg, csSec, csAttr, &find);
1.1       misho     275:        if (!av)
                    276:                return 0;
                    277: 
1.22    ! misho     278:        if (find == 1) {
        !           279:                CFG_RC_LOCK(cfg);
        !           280:                RB_REMOVE(tagRC, cfg, av);
        !           281:                TAILQ_REMOVE(cfg, av, cfg_next);
        !           282:                CFG_RC_UNLOCK(cfg);
        !           283:        } else {
        !           284:                CFG_RC_LOCK(cfg);
        !           285:                TAILQ_REMOVE(cfg, av, cfg_next);
        !           286:                CFG_RC_UNLOCK(cfg);
        !           287:        }
1.7       misho     288: 
                    289:        AIT_FREE_VAL(&av->cfg_val);
                    290:        AIT_FREE_VAL(&av->cfg_attr);
                    291:        AIT_FREE_VAL(&av->cfg_sec);
1.12      misho     292:        e_free(av);
1.22    ! misho     293: 
        !           294:        if (find == 1) {
        !           295:                /* if we have duplicated key then update tree */
        !           296:                TAILQ_FOREACH_SAFE(av, cfg, cfg_next, n) {
        !           297:                        if ((!csSec && AIT_ISEMPTY(&av->cfg_sec)) || 
        !           298:                                        (csSec && !AIT_ISEMPTY(&av->cfg_sec) && 
        !           299:                                         !strcmp(csSec, AIT_GET_STR(&av->cfg_sec))))
        !           300:                                if (!AIT_ISEMPTY(&av->cfg_attr) && csAttr && 
        !           301:                                                !strcmp(csAttr, AIT_GET_STR(&av->cfg_attr))) {
        !           302:                                        CFG_RC_LOCK(cfg);
        !           303:                                        RB_INSERT(tagRC, cfg, av);
        !           304:                                        CFG_RC_UNLOCK(cfg);
        !           305:                                        break;
        !           306:                                }
        !           307:                }
        !           308:        }
        !           309: 
1.7       misho     310:        return 1;
1.1       misho     311: }
                    312: 
                    313: /*
1.7       misho     314:  * cfg_setAttribute() - Set item in config or adding new item if not exists
                    315:  *
                    316:  * @cfg = Config root
1.1       misho     317:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
1.7       misho     318:  * @csAttr = Config attribute //{csAttr} = ...
1.1       misho     319:  * @csVal = Config value //... = {csVal} to setup
1.7       misho     320:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
                    321:  */
                    322: int
                    323: cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, const char *csVal)
1.1       misho     324: {
1.7       misho     325:        struct tagCfg *av, *section;
1.22    ! misho     326:        int find;
1.1       misho     327: 
                    328:        if (!cfg || !csAttr)
                    329:                return -1;
                    330: 
1.22    ! misho     331:        av = _selectAttribute(cfg, csSec, csAttr, &find);
1.1       misho     332:        if (!av) {
1.7       misho     333:                /* adding new element */
1.22    ! misho     334:                section = _selectAttribute(cfg, csSec, NULL, &find);
1.1       misho     335: 
1.12      misho     336:                av = e_malloc(sizeof(struct tagCfg));
1.1       misho     337:                if (!av) {
                    338:                        LOGERR;
                    339:                        return -1;
                    340:                } else {
1.7       misho     341:                        memset(av, 0, sizeof(struct tagCfg));
1.1       misho     342: 
1.7       misho     343:                        CFG_RC_LOCK(cfg);
                    344:                        if (!section)
1.14      misho     345:                                TAILQ_INSERT_TAIL(cfg, av, cfg_next);
1.7       misho     346:                        else
1.14      misho     347:                                TAILQ_INSERT_BEFORE(section, av, cfg_next);
1.7       misho     348:                        CFG_RC_UNLOCK(cfg);
1.1       misho     349:                }
1.7       misho     350: 
1.1       misho     351:                if (csSec && *csSec) {
1.7       misho     352:                        AIT_SET_STR(&av->cfg_sec, csSec);
                    353:                        AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
1.12      misho     354:                                        E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
1.1       misho     355:                }
1.7       misho     356:                AIT_SET_STR(&av->cfg_val, csVal ? csVal : "");
                    357:                AIT_SET_STR(&av->cfg_attr, csAttr);
                    358:                AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
1.12      misho     359:                                E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
1.7       misho     360: 
                    361:                CFG_RC_LOCK(cfg);
                    362:                RB_INSERT(tagRC, cfg, av);
                    363:                CFG_RC_UNLOCK(cfg);
1.1       misho     364:                return 2;
                    365:        }
                    366: 
1.10      misho     367:        if (csVal && AIT_ADDR(&av->cfg_val) && 
                    368:                        strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {
1.7       misho     369:                /* Update element */
                    370:                AIT_FREE_VAL(&av->cfg_val);
                    371:                AIT_SET_STR(&av->cfg_val, csVal);
1.1       misho     372:                return 1;
                    373:        }
                    374: 
1.7       misho     375:        /* Nothing happens ... found & values is equal! */
1.1       misho     376:        return 0;
                    377: }
                    378: 
                    379: /*
1.7       misho     380:  * cfg_getAttribute() - Get item from config and return value from it
                    381:  *
                    382:  * @cfg = Config root
1.1       misho     383:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19      misho     384:  * @csAttr = Config attribute //{csAttr} = ...
1.7       misho     385:  * return: NULL item not found or null parameters, !=NULL value const string
                    386:  */
1.12      misho     387: const char *
1.7       misho     388: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1       misho     389: {
1.7       misho     390:        struct tagCfg *av;
1.22    ! misho     391:        int find;
1.1       misho     392: 
1.15      misho     393:        if (!cfg)
1.1       misho     394:                return NULL;
                    395: 
1.22    ! misho     396:        av = _selectAttribute(cfg, csSec, csAttr, &find);
1.1       misho     397:        if (!av)
                    398:                return NULL;
                    399: 
1.7       misho     400:        return AIT_GET_STR(&av->cfg_val);
1.4       misho     401: }
                    402: 
1.1       misho     403: /*
1.16      misho     404:  * cfg_getAttributeLong() - Get item as long from config and return value from it
                    405:  *
                    406:  * @cfg = Config root
                    407:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19      misho     408:  * @csAttr = Config attribute //{csAttr} = ...
1.16      misho     409:  * return: value
                    410:  */
                    411: long
                    412: cfg_getAttributeLong(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
                    413: {
                    414:        const char *str = NULL;
                    415: 
                    416:        str = cfg_getAttribute(cfg, csSec, csAttr);
                    417:        return strtol(str ? str : "", NULL, 0);
                    418: }
                    419: 
                    420: /*
                    421:  * cfg_getAttributeLLong() - Get item as long long from config and return value from it
                    422:  *
                    423:  * @cfg = Config root
                    424:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19      misho     425:  * @csAttr = Config attribute //{csAttr} = ...
1.16      misho     426:  * return: value
                    427:  */
                    428: long long
                    429: cfg_getAttributeLLong(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
                    430: {
                    431:        const char *str = NULL;
                    432: 
                    433:        str = cfg_getAttribute(cfg, csSec, csAttr);
                    434:        return strtoll(str ? str : "", NULL, 0);
                    435: }
                    436: 
                    437: /*
                    438:  * cfg_getAttributeDouble() - Get item as double from config and return value from it
                    439:  *
                    440:  * @cfg = Config root
                    441:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19      misho     442:  * @csAttr = Config attribute //{csAttr} = ...
1.16      misho     443:  * return: value
                    444:  */
                    445: double
                    446: cfg_getAttributeDouble(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
                    447: {
                    448:        const char *str = NULL;
                    449: 
                    450:        str = cfg_getAttribute(cfg, csSec, csAttr);
                    451:        return strtod(str ? str : "", NULL);
                    452: }
                    453: 
                    454: /*
                    455:  * cfg_getAttributeLDouble() - Get item as long double from config and return value from it
                    456:  *
                    457:  * @cfg = Config root
                    458:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19      misho     459:  * @csAttr = Config attribute //{csAttr} = ...
1.16      misho     460:  * return: value
                    461:  */
                    462: long double
                    463: cfg_getAttributeLDouble(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
                    464: {
                    465:        const char *str = NULL;
                    466: 
                    467:        str = cfg_getAttribute(cfg, csSec, csAttr);
                    468:        return strtold(str ? str : "", NULL);
                    469: }
                    470: 
                    471: /*
1.7       misho     472:  * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
                    473:  *
                    474:  * @cfg = Config root
1.1       misho     475:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19      misho     476:  * @csAttr = Config attribute //{csAttr} = ...
1.7       misho     477:  * @val = Return buffer for item Value //... = {val}
                    478:  * @csDefValue = *Default Value* for return in //{val}, if not found item in config
                    479:  * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
                    480:  */
                    481: int
                    482: cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, 
                    483:                ait_val_t * __restrict val, const char *csDefValue)
1.1       misho     484: {
1.7       misho     485:        struct tagCfg *av;
1.1       misho     486:        int ret = 0;
1.22    ! misho     487:        int find;
1.1       misho     488: 
1.15      misho     489:        if (!cfg || !val) {
1.7       misho     490:                cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1       misho     491:                return -1;
1.7       misho     492:        }
1.1       misho     493: 
1.9       misho     494:        AIT_INIT_VAL(val);
1.22    ! misho     495:        av = _selectAttribute(cfg, csSec, csAttr, &find);
1.1       misho     496:        if (!av) {
1.7       misho     497:                /* not found item */
1.1       misho     498:                if (csDefValue) {
1.7       misho     499:                        AIT_SET_STR(val, csDefValue);
                    500:                        ret = AIT_LEN(val);
1.8       misho     501:                } else
                    502:                        AIT_INIT_VAL(val);
1.1       misho     503:                return ret;
                    504:        }
                    505: 
1.10      misho     506:        if (AIT_ISEMPTY(&av->cfg_val) || !AIT_ADDR(&av->cfg_val) || 
                    507:                        !*AIT_GET_LIKE(&av->cfg_val, char*)) {
1.7       misho     508:                /* empty value */
1.1       misho     509:                if (csDefValue) {
1.7       misho     510:                        AIT_SET_STR(val, csDefValue);
                    511:                        ret = AIT_LEN(val);
1.8       misho     512:                } else
                    513:                        AIT_INIT_VAL(val);
1.1       misho     514:        } else {
1.7       misho     515:                /* copy value */
                    516:                AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val));
                    517:                ret = AIT_LEN(val);
1.1       misho     518:        }
                    519: 
                    520:        return ret;
                    521: }

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