--- ray/src/util/eplus_idf.c 2014/02/13 17:33:37 2.9 +++ ray/src/util/eplus_idf.c 2014/03/05 19:49:39 2.12 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: eplus_idf.c,v 2.9 2014/02/13 17:33:37 greg Exp $"; +static const char RCSid[] = "$Id: eplus_idf.c,v 2.12 2014/03/05 19:49:39 greg Exp $"; #endif /* * eplus_idf.c @@ -225,19 +225,21 @@ idf_read_comment(char *buf, int len, FILE *fp) { int incomm = 0; char *cp = buf; - char dummyc; + char dummys[2]; int c; if ((buf == NULL) | (len <= 0)) { - buf = &dummyc; - len = 1; + buf = dummys; + len = sizeof(dummys); } while ((c = getc(fp)) != EOF && (isspace(c) || (incomm += (c == '!')))) { if (c == '\n') incomm = 0; - if (cp-buf < len-1) + if (cp-buf < len-2) *cp++ = c; + else if (cp-buf == len-2) + *cp++ = '\n'; } *cp = '\0'; if (c != EOF) @@ -351,6 +353,16 @@ idf_load(const char *fname) return(idf); /* success! */ } +/* Check string for end-of-line */ +static int +idf_hasEOL(const char *s) +{ + while (*s) + if (*s++ == '\n') + return(1); + return(0); +} + /* Write a object and fields to an open file */ int idf_writeparam(IDF_OBJECT *param, FILE *fp, int incl_comm) @@ -363,16 +375,25 @@ idf_writeparam(IDF_OBJECT *param, FILE *fp, int incl_c fputc(',', fp); if (incl_comm) fputs(param->rem, fp); + else + fputc('\n', fp); for (fptr = param->flist; fptr != NULL; fptr = fptr->next) { if (!incl_comm) - fputs("\n ", fp); + fputs(" ", fp); fputs(fptr->val, fp); - fputc((fptr->next==NULL ? ';' : ','), fp); + if (fptr->next == NULL) { + fputc(';', fp); + if (incl_comm && !idf_hasEOL(fptr->rem)) + fputc('\n', fp); + } else + fputc(',', fp); if (incl_comm) fputs(fptr->rem, fp); + else + fputc('\n', fp); } if (!incl_comm) - fputs("\n\n", fp); + fputc('\n', fp); return(!ferror(fp)); } @@ -407,9 +428,18 @@ idf_free(IDF_LOADED *idf) { if (idf == NULL) return; + while (idf->pfirst != NULL) { + IDF_OBJECT *pdel = idf->pfirst; + idf->pfirst = pdel->dnext; + while (pdel->flist != NULL) { + IDF_FIELD *fdel = pdel->flist; + pdel->flist = fdel->next; + free(fdel); + } + free(pdel); + } + lu_done(&idf->ptab); if (idf->hrem != NULL) free(idf->hrem); - while (idf->pfirst != NULL) - idf_delobject(idf, idf->pfirst); - lu_done(&idf->ptab); + free(idf); }