--- libaitcfg/src/parse.c 2013/05/30 09:12:27 1.12 +++ libaitcfg/src/parse.c 2014/01/29 23:48:34 1.13 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: parse.c,v 1.12 2013/05/30 09:12:27 misho Exp $ +* $Id: parse.c,v 1.13 2014/01/29 23:48:34 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, 2012, 2013 +Copyright 2004 - 2014 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -138,12 +138,12 @@ cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg) } /* section */ if (*line == '[') { - AIT_SET_STR(&av->cfg_val, line); psAttr = line + strlen(line) - 1; if (*psAttr == ']') { *psAttr = 0; flg = 0; strlcpy(szSection, line + 1, sizeof szSection); + AIT_SET_STR(&av->cfg_sec, line); } else EDEBUG(7, "Ignore section '%s' ... not found ']'", line); continue; @@ -196,7 +196,7 @@ cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, i { struct tagCfg *av; time_t tim; - char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { 0 }; + char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { [0 ... STRSIZ - 1] = 0 }; if (!f || !cfg) { cfg_SetErr(EINVAL, "Invalid parameter(s)"); @@ -206,20 +206,29 @@ cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, i CFG_RC_LOCK(cfg); _invertQueue(cfg); SLIST_FOREACH(av, cfg, cfg_next) { - /* add +1 line for section [] */ + /* empty lines or comment */ + if (AIT_ISEMPTY(&av->cfg_attr)) { + if (AIT_ISEMPTY(&av->cfg_val)) + continue; + strlcpy(line, AIT_GET_STR(&av->cfg_val), sizeof line); + goto skip_sec; + } + + /* section [] */ if (!AIT_ISEMPTY(&av->cfg_sec) && AIT_ADDR(&av->cfg_sec) && strcmp(AIT_GET_STR(&av->cfg_sec), szSection)) { strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection); - if (!cfg_Write(f, "\n[%s]\n", AIT_GET_STR(&av->cfg_sec))) { + if (!cfg_Write(f, "[%s]\n", AIT_GET_STR(&av->cfg_sec))) { LOGERR; + _invertQueue(cfg); CFG_RC_UNLOCK(cfg); return -1; } - } - if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) { + } else if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) { memset(szSection, 0, sizeof szSection); - if (!cfg_Write(f, "\n[]\n")) { + if (!cfg_Write(f, "[]\n")) { LOGERR; + _invertQueue(cfg); CFG_RC_UNLOCK(cfg); return -1; } @@ -236,7 +245,7 @@ cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, i } if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string) strlcat(line, AIT_GET_STRZ(&av->cfg_val), sizeof line); - +skip_sec: /* write */ if (!cfg_Write(f, "%s\n", line)) { LOGERR;