--- libaitcfg/src/aitcfg.c 2011/05/01 17:24:28 1.4 +++ libaitcfg/src/aitcfg.c 2012/04/04 13:11:49 1.5 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcfg.c,v 1.4 2011/05/01 17:24:28 misho Exp $ +* $Id: aitcfg.c,v 1.5 2012/04/04 13:11:49 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -49,117 +49,149 @@ SUCH DAMAGE. #pragma GCC visibility push(hidden) -int cfgErrno; -char cfgError[MAX_STR + 1]; +int cfg_Errno; +char cfg_Error[STRSIZ]; +inline int +cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b) +{ + int ret; + + assert(a && b); + + ret = ((AIT_KEY(&a->cfg_sec) << 16) | AIT_KEY(&a->cfg_attr)) - + ((AIT_KEY(&b->cfg_sec) << 16) | AIT_KEY(&b->cfg_attr)); + + if (ret < 0) + return -1; + else if (ret > 0) + return 1; + + return ret; +} + +RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp); + #pragma GCC visibility pop +// cfg_GetErrno() Get error code of last operation +inline int +cfg_GetErrno() +{ + return cfg_Errno; +} + +// cfg_GetError() Get error text of last operation +inline const char * +cfg_GetError() +{ + return cfg_Error; +} + +// cfg_SetErr() Set error to variables for internal use!!! +inline void +cfg_SetErr(int eno, char *estr, ...) +{ + va_list lst; + + cfg_Errno = eno; + memset(cfg_Error, 0, sizeof cfg_Error); + va_start(lst, estr); + vsnprintf(cfg_Error, sizeof cfg_Error, estr, lst); + va_end(lst); +} + + /* - * InitConfig() Head initializing function for config - * @cfg = New head element for init - * return: 0 ok; -1 error:: new head element is null -*/ -inline int InitConfig(sl_config * __restrict cfg) + * cfgInitConfig() - Init config root + * + * @cfg = Config root + * return: -1 error or 0 ok + */ +int +cfgInitConfig(cfg_root_t * __restrict cfg) { if (!cfg) return -1; - cfg->slh_first = NULL; + pthread_mutex_init(&cfg->rc_mtx, NULL); + + SLIST_INIT(cfg); + RB_INIT(cfg); return 0; } /* - * LoadConfig() Load config from file - * @csConfigName = Filename of config - * @cfg = Head list element - * return: 0 ok; -1 error:: can`t load config -*/ -int LoadConfig(const char *csConfigName, sl_config * __restrict cfg) + * cfgLoadConfig() - Load config from file + * + * @cfgName = Config filename + * @cfg = Config root + * return: -1 error or 0 ok + */ +int +cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg) { FILE *f; int ret; - if (!csConfigName || !cfg) + if (!cfgName || !cfg) { + cfg_SetErr(EINVAL, "Invalid parameter(s)"); return -1; + } else + cfgInitConfig(cfg); - InitConfig(cfg); - if (access(csConfigName, R_OK)) { - LOGERR; - return -1; - } - - f = fopen(csConfigName, "rt"); + f = fopen(cfgName, "r"); if (!f) { LOGERR; return -1; } - - ret ^= ret; - ret = ReadConfig(f, cfg); + ret = cfgReadConfig(f, cfg); + fclose(f); return ret; } /* - * UnloadConfig() Unload config from memory and free resources - * @cfg = Head list element -*/ -void UnloadConfig(sl_config * __restrict cfg) + * cfgUnloadConfig() - Unload config from memory and free resources + * + * @cfg = Config root + * return: none + */ +void +cfgUnloadConfig(cfg_root_t * __restrict cfg) { - struct tagPair *av; + struct tagCfg *av; - if (!cfg->slh_first) + if (!cfg) return; - while ((av = cfg->slh_first)) { - cfg->slh_first = cfg->slh_first->sle_next; + CFG_RC_LOCK(cfg); + while ((av = SLIST_FIRST(cfg))) { + SLIST_REMOVE_HEAD(cfg, cfg_next); - if (av->psValue) - free(av->psValue); - if (av->psAttribute) - free(av->psAttribute); - if (av->psSection) - free(av->psSection); + AIT_FREE_VAL(&av->cfg_val); + AIT_FREE_VAL(&av->cfg_attr); + AIT_FREE_VAL(&av->cfg_sec); free(av); } -} + cfg->rbh_root = NULL; + CFG_RC_UNLOCK(cfg); -/* - * CreateConfig() Create config file from memory - * @csConfigName = New config filename - * @cfg = Head list element - * return: 0 ok; -1 error:: can`t save new config -*/ -int CreateConfig(const char *csConfigName, sl_config * __restrict cfg) -{ - FILE *f; - int ret; - - if (!csConfigName || !cfg) - return -1; - - f = fopen(csConfigName, "wt"); - if (!f) { - LOGERR; - return -1; - } - - ret ^= ret; - ret = WriteConfig(f, cfg); - - fclose(f); - return ret; + pthread_mutex_destroy(&cfg->rc_mtx); } /* - * cfg_CreateConfig() Create config file from memory without whitespaces! + * cfgCreateConfig() - Create config file from memory + * * @csConfigName = New config filename - * @cfg = Head list element - * return: 0 ok; -1 error:: can`t save new config -*/ -int cfg_CreateConfig(const char *csConfigName, sl_config * __restrict cfg) + * @cfg = Config root + * @whitespace = Additional whitespace characters to file + * return: -1 error or 0 ok + */ +int +cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, int whitespace) { FILE *f; int ret; @@ -167,33 +199,14 @@ int cfg_CreateConfig(const char *csConfigName, sl_conf if (!csConfigName || !cfg) return -1; - f = fopen(csConfigName, "wt"); + f = fopen(csConfigName, "w"); if (!f) { LOGERR; return -1; } - ret ^= ret; - ret = cfg_WriteConfig(f, cfg); + ret = cfgWriteConfig(f, cfg, whitespace); fclose(f); return ret; -} - -// ----------------------------------------------------------- - -// -// Error maintenance functions ... -// - -// cfg_GetErrno() Get error code of last operation -inline int cfg_GetErrno() -{ - return cfgErrno; -} - -// cfg_GetError() Get error text of last operation -inline const char *cfg_GetError() -{ - return cfgError; }