--- libaitcfg/src/parse.c 2012/08/30 14:16:51 1.10.2.3 +++ libaitcfg/src/parse.c 2012/09/18 13:24:50 1.10.2.5 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: parse.c,v 1.10.2.3 2012/08/30 14:16:51 misho Exp $ +* $Id: parse.c,v 1.10.2.5 2012/09/18 13:24:50 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -47,19 +47,6 @@ SUCH DAMAGE. #include "aitcfg.h" -static inline int -cfg_Write(FILE *f, char *fmt, ...) -{ - int ret = 0; - va_list lst; - - va_start(lst, fmt); - ret = vfprintf(f, fmt, lst); - va_end(lst); - - return ret; -} - static inline void _invertQueue(cfg_root_t * __restrict cfg) { @@ -88,6 +75,11 @@ cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg) int flg = 0; char *psAttr, *psVal, szSection[STRSIZ] = { 0 }; + if (!f || !cfg) { + cfg_SetErr(EINVAL, "Invalid parameter(s)"); + return -1; + } + while (!feof(f)) { memset(line, 0, sizeof line); fgets(line, sizeof line - 1, f); @@ -124,7 +116,7 @@ cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg) /* *NEW PAIR* alloc new pair element */ av = io_malloc(sizeof(struct tagCfg)); if (!av) { - LOGERR; + cfg_SetErr(io_GetErrno(), "%s", io_GetError()); return -1; } else { memset(av, 0, sizeof(struct tagCfg)); @@ -172,7 +164,7 @@ cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg) if (*szSection) { AIT_SET_STR(&av->cfg_sec, szSection); AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), - io_align(AIT_LEN(&av->cfg_sec) - 1, 1) / 2); + io_align(AIT_LEN(&av->cfg_sec) - 1, 2) / 2); } io_RTrimStr(psAttr); @@ -182,7 +174,7 @@ cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg) AIT_SET_STR(&av->cfg_val, psVal); AIT_SET_STR(&av->cfg_attr, psAttr); AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), - io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2); + io_align(AIT_LEN(&av->cfg_attr) - 1, 2) / 2); CFG_RC_LOCK(cfg); RB_INSERT(tagRC, cfg, av); @@ -207,6 +199,11 @@ cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, i time_t tim; char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { 0 }; + if (!f || !cfg) { + cfg_SetErr(EINVAL, "Invalid parameter(s)"); + return -1; + } + CFG_RC_LOCK(cfg); _invertQueue(cfg); SLIST_FOREACH(av, cfg, cfg_next) { @@ -244,6 +241,7 @@ cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, i /* write */ if (!cfg_Write(f, "%s\n", line)) { LOGERR; + _invertQueue(cfg); CFG_RC_UNLOCK(cfg); return -1; } @@ -406,13 +404,13 @@ cfgReadLines(FILE *f, const char *delim, const char *e if (psSec) { AIT_SET_STR(&av->cfg_sec, psSec); AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), - io_align(AIT_LEN(&av->cfg_sec) - 1, 1) / 2); + io_align(AIT_LEN(&av->cfg_sec) - 1, 2) / 2); } if (psVal) AIT_SET_STR(&av->cfg_val, psVal); AIT_SET_STR(&av->cfg_attr, psAttr); AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), - io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2); + io_align(AIT_LEN(&av->cfg_attr) - 1, 2) / 2); CFG_RC_LOCK(cfg); /* find & delete duplicates */