--- libaitcfg/src/aitcfg.c 2025/08/19 10:42:28 1.16.6.1 +++ libaitcfg/src/aitcfg.c 2026/08/04 18:59:27 1.18 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcfg.c,v 1.16.6.1 2025/08/19 10:42:28 misho Exp $ +* $Id: aitcfg.c,v 1.18 2026/08/04 18:59:27 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004 - 2025 +Copyright 2004 - 2026 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -160,13 +160,15 @@ cfgEndConfig(cfg_root_t **pcfg) * cfgInitConfigExt() - Init existed config root * * @cfg = Config root - * return: NULL error or !=NULL inited config root + * return: -1 error or 0 inited config root */ -cfg_root_t * +int cfgInitConfigExt(cfg_root_t * __restrict cfg) { - if (!TAILQ_EMPTY(cfg) || !RB_EMPTY(cfg)) - cfgUnloadConfig(cfg); + if (!cfg) { + cfg_SetErr(EINVAL, "Invalid parameter"); + return -1; + } memset(cfg, 0, sizeof(cfg_root_t)); @@ -174,7 +176,7 @@ cfgInitConfigExt(cfg_root_t * __restrict cfg) TAILQ_INIT(cfg); RB_INIT(cfg); - return cfg; + return 0; } /* @@ -190,19 +192,40 @@ cfgLoadConfig(const char *cfgName, cfg_root_t * __rest FILE *f; int ret; - if (!cfgName || !cfg) { + if (!cfgName || cfgInitConfigExt(cfg)) { cfg_SetErr(EINVAL, "Invalid parameter(s)"); return -1; - } else { - if (!TAILQ_EMPTY(cfg) || !RB_EMPTY(cfg)) - cfgUnloadConfig(cfg); + } - memset(cfg, 0, sizeof(cfg_root_t)); + f = fopen(cfgName, "r"); + if (!f) { + LOGERR; + return -1; + } - pthread_mutex_init(&cfg->rc_mtx, NULL); + ret = cfg_ReadConfig(f, cfg, 1); - TAILQ_INIT(cfg); - RB_INIT(cfg); + fclose(f); + return ret; +} + +/* + * cfgLoadConfigExt() - Load config from file with recursion level of includes + * + * @cfgName = Config filename + * @cfg = Config root + * @lvl = Level of include recursion + * return: -1 error or 0 ok + */ +int +cfgLoadConfigExt(const char *cfgName, cfg_root_t * __restrict cfg, int lvl) +{ + FILE *f; + int ret; + + if (!cfgName || cfgInitConfigExt(cfg)) { + cfg_SetErr(EINVAL, "Invalid parameter(s)"); + return -1; } f = fopen(cfgName, "r"); @@ -211,12 +234,11 @@ cfgLoadConfig(const char *cfgName, cfg_root_t * __rest return -1; } - ret = cfgReadConfig(f, cfg); + ret = cfg_ReadConfig(f, cfg, lvl); fclose(f); return ret; } - /* * cfgClearConfig() - Clear config and free resources * @@ -258,6 +280,7 @@ cfgUnloadConfig(cfg_root_t * __restrict cfg) cfgClearConfig(cfg); pthread_mutex_destroy(&cfg->rc_mtx); + memset(&cfg->rc_mtx, 0, sizeof cfg->rc_mtx); } /*