Annotation of libaitcfg/inc/aitcfg.h, revision 1.22.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.22.2.1! misho       6: * $Id: aitcfg.h,v 1.22 2025/10/09 16:43:04 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.22.2.1! misho      15: Copyright 2004 - 2026
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.7       misho      50: #include <pthread.h>
1.11      misho      51: #include <elwix.h>
1.1       misho      52: 
                     53: 
1.7       misho      54: struct tagCfg {
                     55:        ait_val_t               cfg_sec;
                     56:        ait_val_t               cfg_attr;
                     57:        ait_val_t               cfg_val;
1.19      misho      58:        int                     cfg_quoted;
1.1       misho      59: 
1.13      misho      60:        TAILQ_ENTRY(tagCfg)     cfg_next;
1.7       misho      61:        RB_ENTRY(tagCfg)        cfg_node;
                     62: };
                     63: typedef struct tagRC {
                     64:        pthread_mutex_t         rc_mtx;
1.4       misho      65: 
1.13      misho      66:        struct tagCfg           *tqh_first;
                     67:        struct tagCfg           **tqh_last;
1.7       misho      68:        struct tagCfg           *rbh_root;
                     69: } cfg_root_t;
                     70: #define CFG_RC_LOCK(x)         pthread_mutex_lock(&(x)->rc_mtx)
                     71: #define CFG_RC_UNLOCK(x)       pthread_mutex_unlock(&(x)->rc_mtx)
1.1       misho      72: 
1.15      misho      73: #define CFG_SECTION(x)         (assert((x)), AIT_GET_LIKE(&((struct tagCfg*) (x))->cfg_sec, const char*))
                     74: #define CFG_ATTRIBUTE(x)       (assert((x)), AIT_GET_LIKE(&((struct tagCfg*) (x))->cfg_attr, const char*))
                     75: #define CFG_VALUE(x)           (assert((x)), AIT_GET_LIKE(&((struct tagCfg*) (x))->cfg_val, const char*))
                     76: 
1.20      misho      77: #ifdef __cplusplus
                     78: extern "C" {
                     79: #endif
1.1       misho      80: 
                     81: // cfg_GetErrno() Get error code of last operation
1.11      misho      82: int cfg_GetErrno();
1.1       misho      83: // cfg_GetError() Get error text of last operation
1.11      misho      84: const char *cfg_GetError();
1.1       misho      85: 
                     86: 
                     87: /*
1.7       misho      88:  * Macros for config library.
                     89:  */
                     90: #define CFG_ISEMPTY(x)         RB_EMPTY((x))
1.1       misho      91: 
1.3       misho      92: 
                     93: /*
1.7       misho      94:  * cfgInitConfig() - Init config root
                     95:  *
1.14      misho      96:  * return: NULL error or !=NULL allocated config root
                     97:  */
                     98: cfg_root_t *cfgInitConfig();
                     99: /*
                    100:  * cfgEndConfig() - Free resources & config root
                    101:  *
                    102:  * @pcfg = Config root
                    103:  * return: none
1.7       misho     104:  */
1.14      misho     105: void cfgEndConfig(cfg_root_t **pcfg);
1.7       misho     106: /*
1.21      misho     107:  * cfgInitConfigExt() - Init existed config root
                    108:  *
                    109:  * @cfg = Config root
                    110:  * return: -1 error or 0 inited config root
                    111:  */
                    112: int cfgInitConfigExt(cfg_root_t * __restrict cfg);
                    113: /*
1.22.2.1! misho     114:  * cfgLoadConfigExt() - Load config from file with recursion level of includes
        !           115:  *
        !           116:  * @cfgName = Config filename
        !           117:  * @cfg = Config root
        !           118:  * @lvl = Level of include recursion
        !           119:  * return: -1 error or 0 ok
        !           120:  */
        !           121: int cfgLoadConfigExt(const char *cfgName, cfg_root_t * __restrict cfg, int lvl);
        !           122: /*
1.7       misho     123:  * cfgLoadConfig() - Load config from file
                    124:  *
                    125:  * @cfgName = Config filename
                    126:  * @cfg = Config root
                    127:  * return: -1 error or 0 ok
                    128:  */
                    129: int cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg);
                    130: /*
1.8       misho     131:  * cfgUnloadConfig() - Unload config from memory and destroy resources
1.7       misho     132:  *
                    133:  * @cfg = Config root
                    134:  * return: none
                    135:  */
                    136: void cfgUnloadConfig(cfg_root_t * __restrict cfg);
1.3       misho     137: /*
1.8       misho     138:  * cfgClearConfig() - Clear config and free resources
                    139:  *
                    140:  * @cfg = Config root
                    141:  * return: none
                    142:  */
                    143: void cfgClearConfig(cfg_root_t * __restrict cfg);
                    144: /*
1.7       misho     145:  * cfgCreateConfig() - Create config file from memory
                    146:  *
                    147:  * @csConfigName = New config filename
                    148:  * @cfg = Config root
                    149:  * @whitespace = Additional whitespace characters to file
                    150:  * return: -1 error or 0 ok
                    151:  */
                    152: int cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, 
                    153:                int whitespace);
                    154: 
                    155: /*
1.9       misho     156:  * cfgReadLines() - Read custom lines and add new item at config root
                    157:  *
                    158:  * @f = File resource
                    159:  * @delim = Custom delimiter, if =NULL default is '='
                    160:  * @end = Custom user end of file, if =NULL default is EOF
                    161:  * @cfg = Config root
                    162:  * return: -1 error or 0 ok
                    163:  */
                    164: int cfgReadLines(FILE *f, const char *delim, const char *end, 
                    165:                cfg_root_t * __restrict cfg);
                    166: /*
1.10      misho     167:  * cfgWriteLines() - Write custom lines and export data to variable
                    168:  *
                    169:  * @f = File resource
                    170:  * @delim = Custom delimiter, if =NULL default is '='
                    171:  * @eol = End of line string, if =NULL default is "\n"
                    172:  * @section = Export only section, if =NULL default is all
                    173:  * @cfg = Config root
1.11      misho     174:  * return: =NULL error or !=NULL exported data, must be free after use with ait_freeVar()
1.10      misho     175:  */
                    176: ait_val_t *cfgWriteLines(FILE *f, const char *delim, const char *eol, 
                    177:                const char *section, cfg_root_t * __restrict cfg);
                    178: /*
1.22.2.1! misho     179:  * cfg_ReadConfig() - Read file and add new item at config root
1.7       misho     180:  *
                    181:  * @f = File resource
                    182:  * @cfg = Config root
1.22.2.1! misho     183:  * @lvl = Level of include recursion
1.7       misho     184:  * return: -1 error or 0 ok
                    185:  */
1.22.2.1! misho     186: int cfg_ReadConfig(FILE *f, cfg_root_t * __restrict cfg, int lvl);
        !           187: #define cfgReadConfig(f, c)    cfg_ReadConfig((f), (c), 1)
1.7       misho     188: /*
                    189:  * cfgWriteConfig() - Write config from memory
                    190:  *
                    191:  * @f = File handle
                    192:  * @cfg = Config root
                    193:  * @whitespace = Additional whitespace characters to file
                    194:  * return: -1 error or 0 ok
                    195:  */
                    196: int cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace);
                    197: /*
1.18      misho     198:  * cfgWriteConfigRaw() - Write config from memory by list
                    199:  *
                    200:  * @f = File handle
                    201:  * @cfg = Config root
                    202:  * @whitespace = Additional whitespace characters to file
                    203:  * return: -1 error or 0 ok
                    204:  */
                    205: int cfgWriteConfigRaw(FILE *f, cfg_root_t * __restrict cfg, int whitespace);
                    206: /*
1.7       misho     207:  * cfgConcatConfig() - Concat two configs into one
                    208:  *
                    209:  * @cfg = Config root
                    210:  * @add_cfg = Concated config will be destroy after merge
                    211:  * return: -1 error or 0 ok
                    212:  */
                    213: int cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg);
                    214: /*
                    215:  * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
                    216:  *
                    217:  * @cfg = Config root of main list
                    218:  * @add_cfg = Merged config will be destroy after merge
                    219:  * return: -1 error or 0 ok
                    220:  */
                    221: int cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg);
                    222: 
1.17      misho     223: /*
                    224:  * cfg_dumpCfg() - dump config data
                    225:  *
                    226:  * @cfg = Config root
                    227:  * return: none
                    228:  */
                    229: void cfg_dumpCfg(cfg_root_t * __restrict cfg);
1.7       misho     230: /*
1.22      misho     231:  * cfg_delSection() - Delete entire section
                    232:  *
                    233:  * @cfg = Config root
                    234:  * @csSec = Config section //[{csSec}]
                    235:  * return: -1 error, 0 nothing deleted or >0 deleted attributes
                    236:  */
                    237: int cfg_delSection(cfg_root_t * __restrict cfg, const char *csSec);
                    238: /*
1.15      misho     239:  * cfg_getSection() - Get entire section attributes into array
                    240:  *
                    241:  * @cfg = Config root
                    242:  * @csSec = Config section //[{csSec}]
                    243:  * return: NULL not found or !=NULL allocated array, must free with array_Destroy() after use!
                    244:  */
                    245: array_t *cfg_getSection(cfg_root_t * __restrict cfg, const char *csSec);
                    246: /*
1.7       misho     247:  * cfg_findAttribute() - Find attribute position in config file
                    248:  *
                    249:  * @cfg = Config root
1.1       misho     250:  * @csSec = Config section //[{csSec}]
1.18      misho     251:  * @csAttr = Config attribute //{csAttr} = ...
1.7       misho     252:  * return: 0 not found item, -1 error or >0 position in list
                    253:  */
1.11      misho     254: int cfg_findAttribute(cfg_root_t * __restrict cfg, 
1.7       misho     255:                const char *csSec, const char *csAttr);
                    256: /*
                    257:  * cfg_getAttribute() - Get item from config and return value from it
                    258:  *
                    259:  * @cfg = Config root
                    260:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.18      misho     261:  * @csAttr = Config attribute //{csAttr} = ...
1.7       misho     262:  * return: NULL item not found or null parameters, !=NULL value const string
                    263:  */
1.11      misho     264: const char *cfg_getAttribute(cfg_root_t * __restrict cfg, 
1.7       misho     265:                const char *csSec, const char *csAttr);
                    266: /*
1.16      misho     267:  * cfg_getAttributeLong() - Get item as long from config and return value from it
                    268:  *
                    269:  * @cfg = Config root
                    270:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.18      misho     271:  * @csAttr = Config attribute //{csAttr} = ...
1.16      misho     272:  * return: value
                    273:  */
                    274: long cfg_getAttributeLong(cfg_root_t * __restrict cfg, 
                    275:                const char *csSec, const char *csAttr);
                    276: /*
                    277:  * cfg_getAttributeLLong() - Get item as long long from config and return value from it
                    278:  *
                    279:  * @cfg = Config root
                    280:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.18      misho     281:  * @csAttr = Config attribute //{csAttr} = ...
1.16      misho     282:  * return: value
                    283:  */
                    284: long long
                    285: cfg_getAttributeLLong(cfg_root_t * __restrict cfg, 
                    286:                const char *csSec, const char *csAttr);
                    287: /*
                    288:  * cfg_getAttributeDouble() - Get item as double from config and return value from it
                    289:  *
                    290:  * @cfg = Config root
                    291:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.18      misho     292:  * @csAttr = Config attribute //{csAttr} = ...
1.16      misho     293:  * return: value
                    294:  */
                    295: double cfg_getAttributeDouble(cfg_root_t * __restrict cfg, 
                    296:                const char *csSec, const char *csAttr);
                    297: /*
                    298:  * cfg_getAttributeLDouble() - Get item as long double from config and return value from it
                    299:  *
                    300:  * @cfg = Config root
                    301:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.18      misho     302:  * @csAttr = Config attribute //{csAttr} = ...
1.16      misho     303:  * return: value
                    304:  */
                    305: long double cfg_getAttributeLDouble(cfg_root_t * __restrict cfg, 
                    306:                const char *csSec, const char *csAttr);
                    307: /*
1.7       misho     308:  * cfg_setAttribute() - Set item in config or adding new item if not exists
                    309:  *
                    310:  * @cfg = Config root
1.1       misho     311:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
1.7       misho     312:  * @csAttr = Config attribute //{csAttr} = ...
1.1       misho     313:  * @csVal = Config value //... = {csVal} to setup
1.7       misho     314:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
                    315:  */
                    316: int cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
                    317:                const char *csAttr, const char *csVal);
                    318: /*
                    319:  * cfg_unsetAttribute() - Unset item from config and free resources
                    320:  *
                    321:  * @cfg = Config root
1.1       misho     322:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.18      misho     323:  * @csAttr = Config attribute //{csAttr} = ...
1.7       misho     324:  * return: 0 item not found, -1 error or 1 removed item
                    325:  */
                    326: int cfg_unsetAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
                    327:                const char *csAttr);
1.1       misho     328: 
                    329: /*
1.7       misho     330:  * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
                    331:  *
                    332:  * @cfg = Config root
1.1       misho     333:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.18      misho     334:  * @csAttr = Config attribute //{csAttr} = ...
1.7       misho     335:  * @val = Return buffer for item Value //... = {val}
                    336:  * @csDefValue = *Default Value* for return in //{val}, if not found item in config
                    337:  * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
                    338:  */
                    339: int cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
                    340:                const char *csAttr, ait_val_t * __restrict val, const char *csDefValue);
1.1       misho     341: 
1.20      misho     342: #ifdef __cplusplus
                    343: }
                    344: #endif
1.1       misho     345: 
                    346: #endif

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