Annotation of libaitcfg/inc/aitcfg.h, revision 1.6.4.8

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.8 ! misho       6: * $Id: aitcfg.h,v 1.6.4.7 2012/04/03 12:54:49 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: #ifndef __AITCFG_H
                     47: #define __AITCFG_H
                     48: 
                     49: 
1.6.4.1   misho      50: #include <aitio.h>
                     51: #include <atree.h>
1.6.4.6   misho      52: #include <pthread.h>
1.6.4.1   misho      53: 
                     54: 
                     55: struct tagCfg {
                     56:        ait_val_t               cfg_sec;
                     57:        ait_val_t               cfg_attr;
                     58:        ait_val_t               cfg_val;
1.1       misho      59: 
1.6.4.1   misho      60:        SLIST_ENTRY(tagCfg)     cfg_next;
                     61:        RB_ENTRY(tagCfg)        cfg_node;
1.1       misho      62: };
1.6.4.1   misho      63: typedef struct tagRC {
                     64:        pthread_mutex_t         rc_mtx;
1.6.4.6   misho      65: 
1.6.4.1   misho      66:        struct tagCfg           *slh_first;
                     67:        struct tagCfg           *rbh_root;
                     68: } cfg_root_t;
                     69: #define CFG_RC_LOCK(x)         pthread_mutex_lock(&(x)->rc_mtx)
                     70: #define CFG_RC_UNLOCK(x)       pthread_mutex_unlock(&(x)->rc_mtx)
                     71: 
                     72: 
                     73: // cfg_GetErrno() Get error code of last operation
                     74: inline int cfg_GetErrno();
                     75: // cfg_GetError() Get error text of last operation
                     76: inline const char *cfg_GetError();
1.1       misho      77: 
                     78: 
                     79: /*
1.6.4.1   misho      80:  * Macros for config library.
1.6.4.5   misho      81:  */
1.6.4.1   misho      82: #define CFG_ISEMPTY(x)         RB_EMPTY((x))
1.4       misho      83: 
                     84: 
1.6.4.1   misho      85: /*
1.6.4.5   misho      86:  * cfgInitConfig() - Init config root
                     87:  *
                     88:  * @cfg = Config root
                     89:  * return: -1 error or 0 ok
                     90:  */
                     91: int cfgInitConfig(cfg_root_t * __restrict cfg);
                     92: /*
1.6.4.1   misho      93:  * cfgLoadConfig() - Load config from file
                     94:  *
                     95:  * @cfgName = Config filename
                     96:  * @cfg = Config root
                     97:  * return: -1 error or 0 ok
                     98:  */
                     99: int cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg);
                    100: /*
                    101:  * cfgUnloadConfig() - Unload config from memory and free resources
                    102:  *
                    103:  * @cfg = Config root
                    104:  * return: none
                    105:  */
                    106: void cfgUnloadConfig(cfg_root_t * __restrict cfg);
1.4       misho     107: 
                    108: /*
1.6.4.1   misho     109:  * cfgReadConfig() - Read file and add new item at config root
                    110:  *
                    111:  * @f = File resource
                    112:  * @cfg = Config root
                    113:  * return: -1 error or 0 ok
                    114:  */
                    115: int cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg);
1.6.4.5   misho     116: /*
                    117:  * cfgWriteConfig() - Write config from memory
                    118:  *
                    119:  * @f = File handle
                    120:  * @cfg = Config root
                    121:  * @whitespace = Additional whitespace characters to file
                    122:  * return: -1 error or 0 ok
                    123:  */
                    124: int cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace);
1.6.4.7   misho     125: /*
1.6.4.8 ! misho     126:  * cfgConcatConfig() - Concat two configs into one
        !           127:  *
        !           128:  * @cfg = Config root
        !           129:  * @add_cfg = Concated config will be destroy after merge
        !           130:  * return: -1 error or 0 ok
        !           131:  */
        !           132: int cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg);
        !           133: /*
1.6.4.7   misho     134:  * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
                    135:  *
                    136:  * @cfg = Config root of main list
                    137:  * @add_cfg = Merged config will be destroy after merge
                    138:  * return: -1 error or 0 ok
                    139:  */
                    140: int cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg);
1.6.4.1   misho     141: 
                    142: #if 0
1.1       misho     143: /*
1.3       misho     144:  * cfg_CreateConfig() Create config file from memory without whitespaces!
                    145:  * @csConfigName = New config filename
                    146:  * @cfg = Head list element
                    147:  * return: 0 ok; -1 error:: can`t save new config
                    148: */
                    149: int cfg_CreateConfig(const char *csConfigName, sl_config * __restrict cfg);
                    150: /*
1.1       misho     151:  * CreateConfig() Create config file from memory
                    152:  * @csConfigName = New config filename
                    153:  * @cfg = Head list element
                    154:  * return: 0 ok; -1 error:: can`t save new config
                    155: */
                    156: int CreateConfig(const char *csConfigName, sl_config * __restrict cfg);
1.6.4.1   misho     157: #endif
1.1       misho     158: 
                    159: /*
1.6.4.1   misho     160:  * cfg_findAttribute() - Find attribute position in config file
                    161:  *
                    162:  * @cfg = Config root
1.1       misho     163:  * @csSec = Config section //[{csSec}]
                    164:  * @csAttr = Config attribute //{csAttr} = ...
1.6.4.2   misho     165:  * return: 0 not found item, -1 error or >0 position in list
1.6.4.1   misho     166:  */
                    167: inline int cfg_findAttribute(cfg_root_t * __restrict cfg, 
                    168:                const char *csSec, const char *csAttr);
                    169: /*
                    170:  * cfg_getAttribute() - Get item from config and return value from it
                    171:  *
                    172:  * @cfg = Config root
                    173:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
                    174:  * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
1.6.4.2   misho     175:  * return: NULL item not found or null parameters, !=NULL value const string
1.6.4.1   misho     176:  */
                    177: inline const char *cfg_getAttribute(cfg_root_t * __restrict cfg, 
                    178:                const char *csSec, const char *csAttr);
1.1       misho     179: /*
1.6.4.2   misho     180:  * cfg_setAttribute() - Set item in config or adding new item if not exists
                    181:  *
                    182:  * @cfg = Config root
1.1       misho     183:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
1.6.4.2   misho     184:  * @csAttr = Config attribute //{csAttr} = ...
1.1       misho     185:  * @csVal = Config value //... = {csVal} to setup
1.6.4.2   misho     186:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
                    187:  */
                    188: int cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
                    189:                const char *csAttr, const char *csVal);
1.1       misho     190: /*
1.6.4.3   misho     191:  * cfg_unsetAttribute() - Unset item from config and free resources
                    192:  *
                    193:  * @cfg = Config root
1.1       misho     194:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.6.4.3   misho     195:  * @csAttr = Config attribute //{csAttr} = ...
                    196:  * return: 0 item not found, -1 error or 1 removed item
                    197:  */
                    198: int cfg_unsetAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
                    199:                const char *csAttr);
1.1       misho     200: 
                    201: /*
1.6.4.4   misho     202:  * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
                    203:  *
                    204:  * @cfg = Config root
1.1       misho     205:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.6.4.4   misho     206:  * @csAttr = Config attribute //{csAttr} = ...
                    207:  * @val = Return buffer for item Value //... = {val}
                    208:  * @csDefValue = *Default Value* for return in //{val}, if not found item in config
                    209:  * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
                    210:  */
                    211: int cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
                    212:                const char *csAttr, ait_val_t * __restrict val, const char *csDefValue);
1.1       misho     213: 
                    214: 
                    215: #endif

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