--- libaitcfg/src/parse.c 2012/08/06 15:08:26 1.10.2.1 +++ libaitcfg/src/parse.c 2012/08/30 13:49:44 1.10.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: parse.c,v 1.10.2.1 2012/08/06 15:08:26 misho Exp $ +* $Id: parse.c,v 1.10.2.2 2012/08/30 13:49:44 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -363,6 +363,11 @@ cfgReadLines(FILE *f, const char *delim, const char *e struct tagCfg *d, *av = NULL; char *psAttr, *psVal = NULL; + if (!cfg) + return -1; + if (!delim) + delim = ATR_LINES_DELIM; + while (!feof(f)) { memset(line, 0, sizeof line); fgets(line, sizeof line - 1, f); @@ -421,3 +426,56 @@ cfgReadLines(FILE *f, const char *delim, const char *e return 0; } +/* + * cfgWriteLines() - Write custom lines and export data to variable + * + * @f = File resource + * @delim = Custom delimiter, if =NULL default is '=' + * @eol = End of line string, if =NULL default is "\n" + * @section = Export only section, if =NULL default is all + * @cfg = Config root + * return: =NULL error or !=NULL exported data, must be free after use with io_freeVar() + */ +ait_val_t * +cfgWriteLines(FILE *f, const char *delim, const char *eol, const char *section, cfg_root_t * __restrict cfg) +{ + ait_val_t *v = NULL; + struct tagCfg *av; + + if (!cfg) + return NULL; + if (!delim) + delim = ATR_LINES_DELIM; + if (!eol) + eol = EOL_LINES_DELIM; + if (!(v = io_allocVar())) { + cfg_SetErr(io_GetErrno(), "%s", io_GetError()); + return NULL; + } else + AIT_INIT_VAL2(v, string); + + SLIST_FOREACH(av, cfg, cfg_next) { + if (AIT_ISEMPTY(&av->cfg_attr)) + continue; + if (section) { + if (!AIT_ISEMPTY(&av->cfg_sec) && *section) + continue; + if (strcmp(section, AIT_GET_STR(&av->cfg_sec))) + continue; + } + + if (!AIT_ISEMPTY(&av->cfg_sec)) { + AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_sec)); + AIT_SET_STRCAT(v, SEC_LINES_DELIM); + } + AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_attr)); + AIT_SET_STRCAT(v, delim); + if (!AIT_ISEMPTY(&av->cfg_val)) + AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_val)); + AIT_SET_STRCAT(v, eol); + } + + if (f) + fputs(AIT_GET_STR(v), f); + return v; +}