Annotation of libaitcfg/src/aitcfg.c, revision 1.4.2.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.4.2.1 ! misho       6: * $Id: aitcfg.c,v 1.4 2011/05/01 17:24:28 misho Exp $
1.2       misho       7: *
1.4       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: */
1.4.2.1 ! misho      63: inline int
        !            64: InitConfig(sl_config * __restrict cfg)
1.1       misho      65: {
                     66:        if (!cfg)
                     67:                return -1;
                     68: 
1.2       misho      69:        cfg->slh_first = NULL;
                     70:        return 0;
1.1       misho      71: }
                     72: 
                     73: /*
                     74:  * LoadConfig() Load config from file
                     75:  * @csConfigName = Filename of config
                     76:  * @cfg = Head list element
                     77:  * return: 0 ok; -1 error:: can`t load config
                     78: */
1.4.2.1 ! misho      79: int
        !            80: LoadConfig(const char *csConfigName, sl_config * __restrict cfg)
1.1       misho      81: {
                     82:        FILE *f;
                     83:        int ret;
                     84: 
                     85:        if (!csConfigName || !cfg)
                     86:                return -1;
                     87: 
                     88:        InitConfig(cfg);
                     89:        if (access(csConfigName, R_OK)) {
                     90:                LOGERR;
                     91:                return -1;
                     92:        }
                     93: 
                     94:        f = fopen(csConfigName, "rt");
                     95:        if (!f) {
                     96:                LOGERR;
                     97:                return -1;
                     98:        }
                     99:        
                    100:        ret ^= ret;
                    101:        ret = ReadConfig(f, cfg);
                    102: 
                    103:        fclose(f);
                    104:        return ret;
                    105: }
                    106: 
                    107: /*
                    108:  * UnloadConfig() Unload config from memory and free resources
                    109:  * @cfg = Head list element
                    110: */
1.4.2.1 ! misho     111: void
        !           112: UnloadConfig(sl_config * __restrict cfg)
1.1       misho     113: {
                    114:        struct tagPair *av;
                    115: 
                    116:        if (!cfg->slh_first)
                    117:                return;
                    118: 
1.2       misho     119:        while ((av = cfg->slh_first)) {
1.1       misho     120:                cfg->slh_first = cfg->slh_first->sle_next;
                    121: 
                    122:                if (av->psValue)
                    123:                        free(av->psValue);
                    124:                if (av->psAttribute)
                    125:                        free(av->psAttribute);
                    126:                if (av->psSection)
                    127:                        free(av->psSection);
                    128:                free(av);
                    129:        }
                    130: }
                    131: 
                    132: /*
                    133:  * CreateConfig() Create config file from memory
                    134:  * @csConfigName = New config filename
                    135:  * @cfg = Head list element
                    136:  * return: 0 ok; -1 error:: can`t save new config
                    137: */
1.4.2.1 ! misho     138: int
        !           139: CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
1.1       misho     140: {
                    141:        FILE *f;
                    142:        int ret;
                    143: 
                    144:        if (!csConfigName || !cfg)
                    145:                return -1;
                    146: 
                    147:        f = fopen(csConfigName, "wt");
                    148:        if (!f) {
                    149:                LOGERR;
                    150:                return -1;
                    151:        }
                    152:        
                    153:        ret ^= ret;
                    154:        ret = WriteConfig(f, cfg);
                    155: 
                    156:        fclose(f);
                    157:        return ret;
                    158: }
                    159: 
1.3       misho     160: /*
                    161:  * cfg_CreateConfig() Create config file from memory without whitespaces!
                    162:  * @csConfigName = New config filename
                    163:  * @cfg = Head list element
                    164:  * return: 0 ok; -1 error:: can`t save new config
                    165: */
1.4.2.1 ! misho     166: int
        !           167: cfg_CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
1.3       misho     168: {
                    169:        FILE *f;
                    170:        int ret;
                    171: 
                    172:        if (!csConfigName || !cfg)
                    173:                return -1;
                    174: 
                    175:        f = fopen(csConfigName, "wt");
                    176:        if (!f) {
                    177:                LOGERR;
                    178:                return -1;
                    179:        }
                    180:        
                    181:        ret ^= ret;
                    182:        ret = cfg_WriteConfig(f, cfg);
                    183: 
                    184:        fclose(f);
                    185:        return ret;
                    186: }
                    187: 
1.1       misho     188: // -----------------------------------------------------------
                    189: 
                    190: //
                    191: // Error maintenance functions ...
                    192: //
                    193: 
                    194: // cfg_GetErrno() Get error code of last operation
1.4.2.1 ! misho     195: inline int
        !           196: cfg_GetErrno()
1.1       misho     197: {
                    198:        return cfgErrno;
                    199: }
                    200: 
                    201: // cfg_GetError() Get error text of last operation
1.4.2.1 ! misho     202: inline const char *
        !           203: cfg_GetError()
1.1       misho     204: {
                    205:        return cfgError;
                    206: }

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