Annotation of libaitcfg/src/aitcfg.c, revision 1.3.4.1

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.4.1 ! misho       6: * $Id: aitcfg.c,v 1.3 2009/10/19 15:00:10 misho Exp $
1.2       misho       7: *
1.3.4.1 ! 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: 
        !            15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
        !            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: #include "aitcfg.h"
                     48: 
                     49: 
                     50: #pragma GCC visibility push(hidden)
                     51: 
                     52: int cfgErrno;
                     53: char cfgError[MAX_STR + 1];
                     54: 
                     55: #pragma GCC visibility pop
                     56: 
                     57: 
                     58: /*
                     59:  * InitConfig() Head initializing function for config
                     60:  * @cfg = New head element for init
                     61:  * return: 0 ok; -1 error:: new head element is null
                     62: */
                     63: inline int InitConfig(sl_config * __restrict cfg)
                     64: {
                     65:        if (!cfg)
                     66:                return -1;
                     67: 
1.2       misho      68:        cfg->slh_first = NULL;
                     69:        return 0;
1.1       misho      70: }
                     71: 
                     72: /*
                     73:  * LoadConfig() Load config from file
                     74:  * @csConfigName = Filename of config
                     75:  * @cfg = Head list element
                     76:  * return: 0 ok; -1 error:: can`t load config
                     77: */
                     78: int LoadConfig(const char *csConfigName, sl_config * __restrict cfg)
                     79: {
                     80:        FILE *f;
                     81:        int ret;
                     82: 
                     83:        if (!csConfigName || !cfg)
                     84:                return -1;
                     85: 
                     86:        InitConfig(cfg);
                     87:        if (access(csConfigName, R_OK)) {
                     88:                LOGERR;
                     89:                return -1;
                     90:        }
                     91: 
                     92:        f = fopen(csConfigName, "rt");
                     93:        if (!f) {
                     94:                LOGERR;
                     95:                return -1;
                     96:        }
                     97:        
                     98:        ret ^= ret;
                     99:        ret = ReadConfig(f, cfg);
                    100: 
                    101:        fclose(f);
                    102:        return ret;
                    103: }
                    104: 
                    105: /*
                    106:  * UnloadConfig() Unload config from memory and free resources
                    107:  * @cfg = Head list element
                    108: */
                    109: void UnloadConfig(sl_config * __restrict cfg)
                    110: {
                    111:        struct tagPair *av;
                    112: 
                    113:        if (!cfg->slh_first)
                    114:                return;
                    115: 
1.2       misho     116:        while ((av = cfg->slh_first)) {
1.1       misho     117:                cfg->slh_first = cfg->slh_first->sle_next;
                    118: 
                    119:                if (av->psValue)
                    120:                        free(av->psValue);
                    121:                if (av->psAttribute)
                    122:                        free(av->psAttribute);
                    123:                if (av->psSection)
                    124:                        free(av->psSection);
                    125:                free(av);
                    126:        }
                    127: }
                    128: 
                    129: /*
                    130:  * CreateConfig() Create config file from memory
                    131:  * @csConfigName = New config filename
                    132:  * @cfg = Head list element
                    133:  * return: 0 ok; -1 error:: can`t save new config
                    134: */
                    135: int CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
                    136: {
                    137:        FILE *f;
                    138:        int ret;
                    139: 
                    140:        if (!csConfigName || !cfg)
                    141:                return -1;
                    142: 
                    143:        f = fopen(csConfigName, "wt");
                    144:        if (!f) {
                    145:                LOGERR;
                    146:                return -1;
                    147:        }
                    148:        
                    149:        ret ^= ret;
                    150:        ret = WriteConfig(f, cfg);
                    151: 
                    152:        fclose(f);
                    153:        return ret;
                    154: }
                    155: 
1.3       misho     156: /*
                    157:  * cfg_CreateConfig() Create config file from memory without whitespaces!
                    158:  * @csConfigName = New config filename
                    159:  * @cfg = Head list element
                    160:  * return: 0 ok; -1 error:: can`t save new config
                    161: */
                    162: int cfg_CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
                    163: {
                    164:        FILE *f;
                    165:        int ret;
                    166: 
                    167:        if (!csConfigName || !cfg)
                    168:                return -1;
                    169: 
                    170:        f = fopen(csConfigName, "wt");
                    171:        if (!f) {
                    172:                LOGERR;
                    173:                return -1;
                    174:        }
                    175:        
                    176:        ret ^= ret;
                    177:        ret = cfg_WriteConfig(f, cfg);
                    178: 
                    179:        fclose(f);
                    180:        return ret;
                    181: }
                    182: 
1.1       misho     183: // -----------------------------------------------------------
                    184: 
                    185: //
                    186: // Error maintenance functions ...
                    187: //
                    188: 
                    189: // cfg_GetErrno() Get error code of last operation
                    190: inline int cfg_GetErrno()
                    191: {
                    192:        return cfgErrno;
                    193: }
                    194: 
                    195: // cfg_GetError() Get error text of last operation
                    196: inline const char *cfg_GetError()
                    197: {
                    198:        return cfgError;
                    199: }

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