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

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

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