Annotation of libaitcfg/src/queue.c, revision 1.6.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.6.4.2 ! misho       6: * $Id: queue.c,v 1.6.4.1 2012/04/02 14:39:03 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.6.4.1   misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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.6.4.1   misho      49: static inline struct tagCfg *
                     50: _selectAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1       misho      51: {
1.6.4.1   misho      52:        struct tagCfg fav;
1.1       misho      53: 
                     54:        if (!cfg)
                     55:                return NULL;
1.6.4.1   misho      56:        else
                     57:                memset(&fav, 0, sizeof fav);
1.1       misho      58: 
1.6.4.1   misho      59:        if (csSec && *csSec)
                     60:                AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
                     61:                                io_align(strlen(csSec), 1) / 2);
                     62:        if (csAttr)
                     63:                AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr, 
                     64:                                io_align(strlen(csAttr), 1) / 2);
                     65: 
                     66:        /*
1.6.4.2 ! misho      67:        struct tagCfg *av;
        !            68: 
1.6.4.1   misho      69:        RB_FOREACH(av, tagRC, cfg) {
                     70:                printf("sec=%s(%d) attr=%s(%d) val=%s\n", AIT_GET_LIKE(&av->cfg_sec, char*), AIT_KEY(&av->cfg_sec), 
                     71:                                AIT_GET_LIKE(&av->cfg_attr, char*), AIT_KEY(&av->cfg_attr), AIT_GET_STR(&av->cfg_val));
1.1       misho      72:        }
                     73: 
1.6.4.1   misho      74:        printf("ssss=%d aaaa=%d\n", AIT_KEY(&fav.cfg_sec), AIT_KEY(&fav.cfg_attr));
                     75:        */
                     76:        if (!csAttr)
                     77:                return RB_NFIND(tagRC, cfg, &fav);
                     78:        else
                     79:                return RB_FIND(tagRC, cfg, &fav);
1.1       misho      80: }
                     81: 
1.6.4.1   misho      82: static inline void
                     83: _destroyAttribute(struct tagCfg *av)
1.1       misho      84: {
1.6.4.1   misho      85:        if (!av)
1.1       misho      86:                return;
                     87: 
1.6.4.1   misho      88:        AIT_FREE_VAL(&av->cfg_val);
                     89:        AIT_FREE_VAL(&av->cfg_attr);
                     90:        AIT_FREE_VAL(&av->cfg_sec);
                     91:        free(av);
1.1       misho      92: }
                     93: 
                     94: // ----------------------------------------------
                     95: 
                     96: /*
1.6.4.1   misho      97:  * cfg_findAttribute() - Find attribute position in config file
                     98:  *
                     99:  * @cfg = Config root
1.1       misho     100:  * @csSec = Config section //[{csSec}]
                    101:  * @csAttr = Config attribute //{csAttr} = ...
1.6.4.2 ! misho     102:  * return: 0 not found item, -1 error or >0 position in list
1.6.4.1   misho     103:  */
                    104: inline int
                    105: cfg_findAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1       misho     106: {
1.6.4.1   misho     107:        struct tagCfg *av, fav;
1.1       misho     108:        register int cx = 0;
                    109: 
1.6.4.1   misho     110:        if (!cfg || !csAttr) {
                    111:                cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1       misho     112:                return -1;
1.6.4.1   misho     113:        } else
                    114:                memset(&fav, 0, sizeof fav);
1.1       misho     115: 
1.6.4.1   misho     116:        if (csSec && *csSec)
                    117:                AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
                    118:                                io_align(strlen(csSec), 1) / 2);
                    119:        if (csAttr)
                    120:                AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr, 
                    121:                                io_align(strlen(csAttr), 1) / 2);
                    122: 
                    123:        SLIST_FOREACH(av, cfg, cfg_next) {
1.1       misho     124:                ++cx;
1.6.4.1   misho     125:                if (!cfg_tree_cmp(&fav, av))
                    126:                        return cx;
1.1       misho     127:        }
                    128: 
                    129:        return 0;
                    130: }
1.6.4.1   misho     131: #if 0
1.1       misho     132: /*
                    133:  * cfg_UnsetAttribute() Unset item from config list and free resources
                    134:  * @cfg = Head list element
                    135:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
                    136:  * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
                    137:  * return: 0 item not found, -1 error: null parameters; >0 position in list
                    138: */
                    139: int cfg_UnsetAttribute(sl_config * __restrict cfg, const u_char *csSec, const u_char *csAttr)
                    140: {
                    141:        struct tagPair *av, *curr;
                    142:        register int cx = 0;
                    143: 
                    144:        if (!cfg || !csAttr)
                    145:                return -1;
                    146: 
                    147:        av = SelectAttribute(cfg, csSec, csAttr);
                    148:        if (!av)
                    149:                return 0;
                    150: 
                    151:        // remove element
                    152:        //      remove element when is first!
                    153:        if (cfg->slh_first == av) {
                    154:                cfg->slh_first = cfg->slh_first->sle_next;
                    155: 
                    156:                DestroyAttribute(av);
                    157:                return 1;
                    158:        }
                    159:        //      remove element in other cases...
                    160:        curr = cfg->slh_first;
                    161:        while (curr->sle_next != av) {
                    162:                ++cx;
                    163:                curr = curr->sle_next;
                    164:        }
                    165:        curr->sle_next = curr->sle_next->sle_next;
                    166: 
                    167:        DestroyAttribute(av);
                    168:        return cx;
                    169: }
1.6.4.2 ! misho     170: #endif
1.1       misho     171: 
                    172: /*
1.6.4.2 ! misho     173:  * cfg_setAttribute() - Set item in config or adding new item if not exists
        !           174:  *
        !           175:  * @cfg = Config root
1.1       misho     176:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
1.6.4.2 ! misho     177:  * @csAttr = Config attribute //{csAttr} = ...
1.1       misho     178:  * @csVal = Config value //... = {csVal} to setup
1.6.4.2 ! misho     179:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
        !           180:  */
        !           181: int
        !           182: cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, const char *csVal)
1.1       misho     183: {
1.6.4.2 ! misho     184:        struct tagCfg *av, *section;
1.1       misho     185: 
1.6.4.2 ! misho     186:        if (!cfg || !csAttr || !csVal)
1.1       misho     187:                return -1;
                    188: 
1.6.4.2 ! misho     189:        av = _selectAttribute(cfg, csSec, csAttr);
1.1       misho     190:        if (!av) {
1.6.4.2 ! misho     191:                /* adding new element */
        !           192:                section = _selectAttribute(cfg, csSec, NULL);
1.1       misho     193: 
1.6.4.2 ! misho     194:                av = malloc(sizeof(struct tagCfg));
1.1       misho     195:                if (!av) {
                    196:                        LOGERR;
                    197:                        return -1;
                    198:                } else {
1.6.4.2 ! misho     199:                        memset(av, 0, sizeof(struct tagCfg));
1.1       misho     200: 
1.6.4.2 ! misho     201:                        CFG_RC_LOCK(cfg);
        !           202:                        if (!section)
        !           203:                                SLIST_INSERT_HEAD(cfg, av, cfg_next);
        !           204:                        else
        !           205:                                SLIST_INSERT_AFTER(section, av, cfg_next);
        !           206:                        CFG_RC_UNLOCK(cfg);
1.1       misho     207:                }
1.6.4.2 ! misho     208: 
1.1       misho     209:                if (csSec && *csSec) {
1.6.4.2 ! misho     210:                        AIT_SET_STR(&av->cfg_sec, csSec);
        !           211:                        AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
        !           212:                                        io_align(AIT_LEN(&av->cfg_sec) - 1, 1) / 2);
1.1       misho     213:                }
1.6.4.2 ! misho     214:                AIT_SET_STR(&av->cfg_val, csVal);
        !           215:                AIT_SET_STR(&av->cfg_attr, csAttr);
        !           216:                AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
        !           217:                                io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2);
        !           218: 
        !           219:                CFG_RC_LOCK(cfg);
        !           220:                RB_INSERT(tagRC, cfg, av);
        !           221:                CFG_RC_UNLOCK(cfg);
1.1       misho     222:                return 2;
                    223:        }
                    224: 
1.6.4.2 ! misho     225:        if (strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {
        !           226:                /* Update element */
        !           227:                AIT_FREE_VAL(&av->cfg_val);
        !           228:                AIT_SET_STR(&av->cfg_val, csVal);
1.1       misho     229:                return 1;
                    230:        }
                    231: 
1.6.4.2 ! misho     232:        /* Nothing happens ... found & values is equal! */
1.1       misho     233:        return 0;
                    234: }
                    235: 
                    236: /*
1.6.4.1   misho     237:  * cfg_getAttribute() - Get item from config and return value from it
                    238:  *
                    239:  * @cfg = Config root
1.1       misho     240:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
                    241:  * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
1.6.4.2 ! misho     242:  * return: NULL item not found or null parameters, !=NULL value const string
1.6.4.1   misho     243:  */
                    244: inline const char *
                    245: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1       misho     246: {
1.6.4.1   misho     247:        struct tagCfg *av;
1.1       misho     248: 
                    249:        if (!cfg || !csAttr)
                    250:                return NULL;
                    251: 
1.6.4.1   misho     252:        av = _selectAttribute(cfg, csSec, csAttr);
1.1       misho     253:        if (!av)
                    254:                return NULL;
                    255: 
1.6.4.1   misho     256:        return AIT_GET_STR(&av->cfg_val);
1.1       misho     257: }
1.6.4.1   misho     258: #if 0
1.1       misho     259: // --------------------------------------------------------------
                    260: 
                    261: /*
                    262:  * cfg_LoadAttribute() Extended get attribute, if not found item return *default value*
                    263:  * @cfg = Head list element
                    264:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
                    265:  * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
                    266:  * @psVal = Return buffer for item Value //... = {psVal}
                    267:  * @ValLen = Length of buffer //{psVal} for return
                    268:  * @csDefValue = *Default Value* for return in //{psVal}, if not found item in config list
                    269:  * return: 0 item not found, -1 error: null parameters; >0 number of copied bytes in //{psVal}
                    270: */
                    271: int cfg_LoadAttribute(sl_config * __restrict cfg, const u_char *csSec, const u_char *csAttr, 
                    272:                u_char * __restrict psVal, int ValLen, const char *csDefValue)
                    273: {
                    274:        struct tagPair *av;
                    275:        int ret = 0;
                    276: 
                    277:        if (!cfg || !csAttr || !ValLen || !psVal)
                    278:                return -1;
                    279: 
                    280:        av = SelectAttribute(cfg, csSec, csAttr);
                    281:        if (!av) {
                    282:                if (csDefValue) {
1.2       misho     283:                        strlcpy((char*) psVal, csDefValue, ValLen);
1.1       misho     284:                        ret = strlen((char*) psVal);
                    285:                }
                    286: 
                    287:                return ret;
                    288:        }
                    289: 
                    290:        if (!av->psValue || !*av->psValue) {
                    291:                if (csDefValue) {
1.2       misho     292:                        strlcpy((char*) psVal, csDefValue, ValLen);
1.1       misho     293:                        ret = strlen((char*) psVal);
                    294:                }
                    295:        } else {
1.2       misho     296:                strlcpy((char*) psVal, (char*) av->psValue, ValLen);
1.1       misho     297:                ret = strlen((char*) psVal);
                    298:        }
                    299: 
                    300:        return ret;
                    301: }
1.6.4.1   misho     302: #endif

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