Annotation of libaitcfg/src/aitcfg.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: aitcfg.c,v 1.2.2.1 2009/09/09 10:44:41 misho Exp $
1.2       misho       7: *
                      8: *************************************************************************/
1.1       misho       9: #include "global.h"
                     10: #include "aitcfg.h"
                     11: 
                     12: 
                     13: #pragma GCC visibility push(hidden)
                     14: 
                     15: int cfgErrno;
                     16: char cfgError[MAX_STR + 1];
                     17: 
                     18: #pragma GCC visibility pop
                     19: 
                     20: 
                     21: /*
                     22:  * InitConfig() Head initializing function for config
                     23:  * @cfg = New head element for init
                     24:  * return: 0 ok; -1 error:: new head element is null
                     25: */
                     26: inline int InitConfig(sl_config * __restrict cfg)
                     27: {
                     28:        if (!cfg)
                     29:                return -1;
                     30: 
1.2       misho      31:        cfg->slh_first = NULL;
                     32:        return 0;
1.1       misho      33: }
                     34: 
                     35: /*
                     36:  * LoadConfig() Load config from file
                     37:  * @csConfigName = Filename of config
                     38:  * @cfg = Head list element
                     39:  * return: 0 ok; -1 error:: can`t load config
                     40: */
                     41: int LoadConfig(const char *csConfigName, sl_config * __restrict cfg)
                     42: {
                     43:        FILE *f;
                     44:        int ret;
                     45: 
                     46:        if (!csConfigName || !cfg)
                     47:                return -1;
                     48: 
                     49:        InitConfig(cfg);
                     50:        if (access(csConfigName, R_OK)) {
                     51:                LOGERR;
                     52:                return -1;
                     53:        }
                     54: 
                     55:        f = fopen(csConfigName, "rt");
                     56:        if (!f) {
                     57:                LOGERR;
                     58:                return -1;
                     59:        }
                     60:        
                     61:        ret ^= ret;
                     62:        ret = ReadConfig(f, cfg);
                     63: 
                     64:        fclose(f);
                     65:        return ret;
                     66: }
                     67: 
                     68: /*
                     69:  * UnloadConfig() Unload config from memory and free resources
                     70:  * @cfg = Head list element
                     71: */
                     72: void UnloadConfig(sl_config * __restrict cfg)
                     73: {
                     74:        struct tagPair *av;
                     75: 
                     76:        if (!cfg->slh_first)
                     77:                return;
                     78: 
1.2       misho      79:        while ((av = cfg->slh_first)) {
1.1       misho      80:                cfg->slh_first = cfg->slh_first->sle_next;
                     81: 
                     82:                if (av->psValue)
                     83:                        free(av->psValue);
                     84:                if (av->psAttribute)
                     85:                        free(av->psAttribute);
                     86:                if (av->psSection)
                     87:                        free(av->psSection);
                     88:                free(av);
                     89:        }
                     90: }
                     91: 
                     92: /*
                     93:  * CreateConfig() Create config file from memory
                     94:  * @csConfigName = New config filename
                     95:  * @cfg = Head list element
                     96:  * return: 0 ok; -1 error:: can`t save new config
                     97: */
                     98: int CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
                     99: {
                    100:        FILE *f;
                    101:        int ret;
                    102: 
                    103:        if (!csConfigName || !cfg)
                    104:                return -1;
                    105: 
                    106:        f = fopen(csConfigName, "wt");
                    107:        if (!f) {
                    108:                LOGERR;
                    109:                return -1;
                    110:        }
                    111:        
                    112:        ret ^= ret;
                    113:        ret = WriteConfig(f, cfg);
                    114: 
                    115:        fclose(f);
                    116:        return ret;
                    117: }
                    118: 
1.3     ! misho     119: /*
        !           120:  * cfg_CreateConfig() Create config file from memory without whitespaces!
        !           121:  * @csConfigName = New config filename
        !           122:  * @cfg = Head list element
        !           123:  * return: 0 ok; -1 error:: can`t save new config
        !           124: */
        !           125: int cfg_CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
        !           126: {
        !           127:        FILE *f;
        !           128:        int ret;
        !           129: 
        !           130:        if (!csConfigName || !cfg)
        !           131:                return -1;
        !           132: 
        !           133:        f = fopen(csConfigName, "wt");
        !           134:        if (!f) {
        !           135:                LOGERR;
        !           136:                return -1;
        !           137:        }
        !           138:        
        !           139:        ret ^= ret;
        !           140:        ret = cfg_WriteConfig(f, cfg);
        !           141: 
        !           142:        fclose(f);
        !           143:        return ret;
        !           144: }
        !           145: 
1.1       misho     146: // -----------------------------------------------------------
                    147: 
                    148: //
                    149: // Error maintenance functions ...
                    150: //
                    151: 
                    152: // cfg_GetErrno() Get error code of last operation
                    153: inline int cfg_GetErrno()
                    154: {
                    155:        return cfgErrno;
                    156: }
                    157: 
                    158: // cfg_GetError() Get error text of last operation
                    159: inline const char *cfg_GetError()
                    160: {
                    161:        return cfgError;
                    162: }

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