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

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.3     ! misho       6: * $Id: queue.c,v 1.2.2.1 2009/09/09 14:42:48 misho Exp $
1.2       misho       7: *
                      8: *************************************************************************/
1.1       misho       9: #include "global.h"
                     10: #include "aitcfg.h"
                     11: 
                     12: 
                     13: /*
                     14:  * SelectAttribute() Select item //{tagPair} from config list with attribute parameter(s)
                     15:  * @cfg = Head list element
                     16:  * @csSec = Config section //[{csSec}], if NULL search in *default* section
                     17:  * @csAttr = Config attribute //{csAttr} = ..., if NULL search in *any* attribute
                     18:  * return: NULL not found attribute; //{tagPair} selected first seen attribute item from list
                     19: */
                     20: static inline struct tagPair *SelectAttribute(sl_config * __restrict cfg, const u_char *csSec, const u_char *csAttr)
                     21: {
                     22:        struct tagPair *av;
                     23: 
                     24:        if (!cfg)
                     25:                return NULL;
                     26: 
                     27:        for (av = cfg->slh_first; av; av = av->sle_next) {
                     28:                if ((!csSec || !*csSec) && !av->psSection) {
                     29:                        if (!csAttr)
                     30:                                return av;
                     31:                        if (!strcmp((char*) av->psAttribute, (char*) csAttr))
                     32:                                return av;
                     33:                }
                     34:                if (csSec && av->psSection && !strcmp((char*) av->psSection, (char*) csSec)) {
                     35:                        if (!csAttr)
                     36:                                return av;
                     37:                        if (!strcmp((char*) av->psAttribute, (char*) csAttr))
                     38:                                return av;
                     39:                }
                     40:        }
                     41: 
                     42:        return NULL;
                     43: }
                     44: 
                     45: /*
                     46:  * DestroyAttribute() Free //{tagPair} item elements memory and destroy resource
                     47:  * @pair = Free this element
                     48: */
                     49: static inline void DestroyAttribute(struct tagPair *pair)
                     50: {
                     51:        if (!pair)
                     52:                return;
                     53: 
                     54:        if (pair->psValue)
                     55:                free(pair->psValue);
                     56:        if (pair->psAttribute)
                     57:                free(pair->psAttribute);
                     58:        if (pair->psSection)
                     59:                free(pair->psSection);
                     60: 
                     61:        free(pair);
                     62: }
                     63: 
                     64: // ----------------------------------------------
                     65: 
                     66: /*
                     67:  * cfg_FindAttribute() Find attribute position in config list
                     68:  * @cfg = Head list element
                     69:  * @csSec = Config section //[{csSec}]
                     70:  * @csAttr = Config attribute //{csAttr} = ...
                     71:  * return: 0 not found item; -1 error: null parameters; >0 position in list
                     72: */
                     73: inline int cfg_FindAttribute(sl_config * __restrict cfg, const u_char *csSec, const u_char *csAttr)
                     74: {
                     75:        struct tagPair *av;
                     76:        register int cx = 0;
                     77: 
                     78:        if (!cfg || !csAttr)
                     79:                return -1;
                     80: 
                     81:        for (av = cfg->slh_first; av; av = av->sle_next) {
                     82:                ++cx;
                     83:                if ((!csSec || !*csSec) && !av->psSection)
                     84:                        if (!strcmp((char*) av->psAttribute, (char*) csAttr))
                     85:                                return cx;
                     86:                if (csSec && av->psSection && !strcmp((char*) av->psSection, (char*) csSec))
                     87:                        if (!strcmp((char*) av->psAttribute, (char*) csAttr))
                     88:                                return cx;
                     89:        }
                     90: 
                     91:        return 0;
                     92: }
                     93: 
                     94: /*
                     95:  * cfg_UnsetAttribute() Unset item from config list and free resources
                     96:  * @cfg = Head list element
                     97:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
                     98:  * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
                     99:  * return: 0 item not found, -1 error: null parameters; >0 position in list
                    100: */
                    101: int cfg_UnsetAttribute(sl_config * __restrict cfg, const u_char *csSec, const u_char *csAttr)
                    102: {
                    103:        struct tagPair *av, *curr;
                    104:        register int cx = 0;
                    105: 
                    106:        if (!cfg || !csAttr)
                    107:                return -1;
                    108: 
                    109:        av = SelectAttribute(cfg, csSec, csAttr);
                    110:        if (!av)
                    111:                return 0;
                    112: 
                    113:        // remove element
                    114:        //      remove element when is first!
                    115:        if (cfg->slh_first == av) {
                    116:                cfg->slh_first = cfg->slh_first->sle_next;
                    117: 
                    118:                DestroyAttribute(av);
                    119:                return 1;
                    120:        }
                    121:        //      remove element in other cases...
                    122:        curr = cfg->slh_first;
                    123:        while (curr->sle_next != av) {
                    124:                ++cx;
                    125:                curr = curr->sle_next;
                    126:        }
                    127:        curr->sle_next = curr->sle_next->sle_next;
                    128: 
                    129:        DestroyAttribute(av);
                    130:        return cx;
                    131: }
                    132: 
                    133: /*
                    134:  * cfg_SetAttribute() Set item in config list or add new item if not exists
                    135:  * @cfg = Head list element
                    136:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
                    137:  * @csAttr = Config attribute //{csAttr} = ..., if NULL set as *any* attribute
                    138:  * @csVal = Config value //... = {csVal} to setup
                    139:  * return: 0 nothing changed, -1 error: not enough memory; 1 find and update item; 2 added new item
                    140: */
                    141: int cfg_SetAttribute(sl_config * __restrict cfg, const u_char *csSec, const u_char *csAttr, const u_char *csVal)
                    142: {
                    143:        struct tagPair *av, *section;
1.3     ! misho     144:        int len;
1.1       misho     145: 
                    146:        if (!cfg || !csAttr)
                    147:                return -1;
                    148: 
                    149:        av = SelectAttribute(cfg, csSec, csAttr);
                    150:        if (!av) {
                    151:                section = SelectAttribute(cfg, csSec, NULL);
                    152: 
                    153:                av = malloc(sizeof(struct tagPair));
                    154:                if (!av) {
                    155:                        LOGERR;
                    156:                        return -1;
                    157:                } else {
                    158:                        memset(av, 0, sizeof(struct tagPair));
                    159: 
                    160:                        if (!section) {
                    161:                                // add new element
                    162:                                av->sle_next = cfg->slh_first;
                    163:                                cfg->slh_first = av;
                    164:                        } else {
                    165:                                // add new element in existing section
                    166:                                av->sle_next = section->sle_next;
                    167:                                section->sle_next = av;
                    168:                        }
                    169:                }
                    170:                // added section name to element
                    171:                if (csSec && *csSec) {
1.3     ! misho     172:                        len = strlen((char*) csSec) + 1;
        !           173:                        av->psSection = malloc(len);
1.1       misho     174:                        if (!av->psSection) {
                    175:                                LOGERR;
                    176:                                free(av);
                    177:                                return -1;
                    178:                        } else {
1.3     ! misho     179:                                strlcpy((char*) av->psSection, (char*) csSec, len);
1.1       misho     180:                        }
                    181:                } else
                    182:                        av->psSection = NULL;
                    183: 
                    184:                // added attribute to element
1.3     ! misho     185:                len = strlen((char*) csAttr) + 1;
        !           186:                av->psAttribute = malloc(len);
1.1       misho     187:                if (!av->psAttribute) {
                    188:                        LOGERR;
                    189:                        free(av->psSection);
                    190:                        free(av);
                    191:                        return -1;
                    192:                } else {
1.3     ! misho     193:                        strlcpy((char*) av->psAttribute, (char*) csAttr, len);
1.1       misho     194:                }
                    195:                // added value to element
                    196:                if (csVal && *csVal) {
1.3     ! misho     197:                        len = strlen((char*) csVal) + 1;
        !           198:                        av->psValue = malloc(len);
1.1       misho     199:                        if (!av->psValue) {
                    200:                                LOGERR;
                    201:                                free(av->psAttribute);
                    202:                                free(av->psSection);
                    203:                                free(av);
                    204:                                return -1;
                    205:                        } else {
1.3     ! misho     206:                                strlcpy((char*) av->psValue, (char*) csVal, len);
1.1       misho     207:                        }
                    208:                } else {
                    209:                        av->psValue = malloc(1);
                    210:                        *av->psValue = 0;
                    211:                }
                    212: 
                    213:                // Added new element
                    214:                return 2;
                    215:        }
                    216: 
                    217:        if (strcmp((char*) csVal, (char*) av->psValue)) {
1.3     ! misho     218:                len = strlen((char*) csVal) + 1;
        !           219:                av->psValue = realloc(av->psValue, len);
        !           220:                strlcpy((char*) av->psValue, (char*) csVal, len);
1.1       misho     221: 
                    222:                // Update element
                    223:                return 1;
                    224:        }
                    225: 
                    226:        // Nothing happens ... finded & values is equal!
                    227:        return 0;
                    228: }
                    229: 
                    230: /*
                    231:  * cfg_GetAttribute() Get item from config list and return his value
                    232:  * @cfg = Head list element
                    233:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
                    234:  * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
                    235:  * return: NULL item not found or null parameters; !=NULL value const string
                    236: */
                    237: inline const u_char *cfg_GetAttribute(sl_config * __restrict cfg, const u_char *csSec, const u_char *csAttr)
                    238: {
                    239:        struct tagPair *av;
                    240: 
                    241:        if (!cfg || !csAttr)
                    242:                return NULL;
                    243: 
                    244:        av = SelectAttribute(cfg, csSec, csAttr);
                    245:        if (!av)
                    246:                return NULL;
                    247: 
                    248:        return av->psValue;
                    249: }
                    250: 
                    251: // --------------------------------------------------------------
                    252: 
                    253: /*
                    254:  * cfg_LoadAttribute() Extended get attribute, if not found item return *default value*
                    255:  * @cfg = Head list element
                    256:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
                    257:  * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
                    258:  * @psVal = Return buffer for item Value //... = {psVal}
                    259:  * @ValLen = Length of buffer //{psVal} for return
                    260:  * @csDefValue = *Default Value* for return in //{psVal}, if not found item in config list
                    261:  * return: 0 item not found, -1 error: null parameters; >0 number of copied bytes in //{psVal}
                    262: */
                    263: int cfg_LoadAttribute(sl_config * __restrict cfg, const u_char *csSec, const u_char *csAttr, 
                    264:                u_char * __restrict psVal, int ValLen, const char *csDefValue)
                    265: {
                    266:        struct tagPair *av;
                    267:        int ret = 0;
                    268: 
                    269:        if (!cfg || !csAttr || !ValLen || !psVal)
                    270:                return -1;
                    271: 
                    272:        av = SelectAttribute(cfg, csSec, csAttr);
                    273:        if (!av) {
                    274:                if (csDefValue) {
1.2       misho     275:                        strlcpy((char*) psVal, csDefValue, ValLen);
1.1       misho     276:                        ret = strlen((char*) psVal);
                    277:                }
                    278: 
                    279:                return ret;
                    280:        }
                    281: 
                    282:        if (!av->psValue || !*av->psValue) {
                    283:                if (csDefValue) {
1.2       misho     284:                        strlcpy((char*) psVal, csDefValue, ValLen);
1.1       misho     285:                        ret = strlen((char*) psVal);
                    286:                }
                    287:        } else {
1.2       misho     288:                strlcpy((char*) psVal, (char*) av->psValue, ValLen);
1.1       misho     289:                ret = strlen((char*) psVal);
                    290:        }
                    291: 
                    292:        return ret;
                    293: }

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