--- ray/src/util/eplus_idf.c 2014/02/09 02:18:16 2.3 +++ ray/src/util/eplus_idf.c 2014/02/11 23:30:11 2.7 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: eplus_idf.c,v 2.3 2014/02/09 02:18:16 greg Exp $"; +static const char RCSid[] = "$Id: eplus_idf.c,v 2.7 2014/02/11 23:30:11 greg Exp $"; #endif /* * eplus_idf.c @@ -44,6 +44,7 @@ idf_newparam(IDF_LOADED *idf, const char *pname, const if (pnew == NULL) return(NULL); strcpy(pnew->rem, comm); + pnew->nfield = 0; pnew->flist = NULL; pnew->pname = pent->key; /* add to table */ pnew->pnext = (IDF_PARAMETER *)pent->data; @@ -76,7 +77,7 @@ idf_addfield(IDF_PARAMETER *param, const char *fval, c if (fnew == NULL) return(0); fnew->next = NULL; - cp = fnew->arg; /* copy argument and comments */ + cp = fnew->val; /* copy value and comments */ while ((*cp++ = *fval++)) ; fnew->rem = cp; @@ -94,6 +95,7 @@ idf_addfield(IDF_PARAMETER *param, const char *fval, c param->flist = fnew; else flast->next = fnew; + param->nfield++; return(fnum); } @@ -134,11 +136,9 @@ idf_delparam(IDF_LOADED *idf, IDF_PARAMETER *param) plast->pnext = param->pnext; /* remove from global list */ for (plast = NULL, pptr = idf->pfirst; - pptr != NULL; plast = pptr, pptr = pptr->dnext) - if (pptr == param) - break; - if (pptr == NULL) - return(0); + pptr != param; plast = pptr, pptr = pptr->dnext) + if (pptr == NULL) + return(0); if (plast == NULL) idf->pfirst = param->dnext; else @@ -155,6 +155,38 @@ idf_delparam(IDF_LOADED *idf, IDF_PARAMETER *param) return(1); } +/* Move the specified parameter to the given position in the IDF */ +int +idf_movparam(IDF_LOADED *idf, IDF_PARAMETER *param, IDF_PARAMETER *prev) +{ + IDF_PARAMETER *pptr, *plast; + + if ((idf == NULL) | (param == NULL)) + return(0); + /* quick check if already there */ + if (param == (prev==NULL ? idf->pfirst : prev->dnext)) + return(1); + /* find in IDF list, first*/ + for (plast = NULL, pptr = idf->pfirst; + pptr != param; plast = pptr, pptr = pptr->dnext) + if (pptr == NULL) + return(0); + if (plast == NULL) + idf->pfirst = param->dnext; + else + plast->dnext = param->dnext; + if (idf->plast == param) + idf->plast = plast; + if (prev == NULL) { /* means they want it at beginning */ + param->dnext = idf->pfirst; + idf->pfirst = param; + } else { + param->dnext = prev->dnext; + prev->dnext = param; + } + return(1); +} + /* Get a named parameter list */ IDF_PARAMETER * idf_getparam(IDF_LOADED *idf, const char *pname) @@ -269,11 +301,36 @@ idf_create(const char *hdrcomm) return(idf); } +/* Add comment(s) to header */ +int +idf_add2hdr(IDF_LOADED *idf, const char *hdrcomm) +{ + int olen, len; + + if ((idf == NULL) | (hdrcomm == NULL)) + return(0); + len = strlen(hdrcomm); + if (!len) + return(0); + if (idf->hrem == NULL) + olen = 0; + else + olen = strlen(idf->hrem); + if (olen) + idf->hrem = (char *)realloc(idf->hrem, olen+len+1); + else + idf->hrem = (char *)malloc(len+1); + if (idf->hrem == NULL) + return(0); + strcpy(idf->hrem+olen, hdrcomm); + return(1); +} + /* Load an Input Data File */ IDF_LOADED * idf_load(const char *fname) { - char hdrcomm[300*IF_MAXLINE]; + char hdrcomm[256*IDF_MAXLINE]; FILE *fp; IDF_LOADED *idf; @@ -296,7 +353,7 @@ idf_load(const char *fname) /* Write a parameter and fields to an open file */ int -idf_writeparam(IDF_PARAMETER *param, FILE *fp) +idf_writeparam(IDF_PARAMETER *param, FILE *fp, int incl_comm) { IDF_FIELD *fptr; @@ -304,18 +361,24 @@ idf_writeparam(IDF_PARAMETER *param, FILE *fp) return(0); fputs(param->pname, fp); fputc(',', fp); - fputs(param->rem, fp); + if (incl_comm) + fputs(param->rem, fp); for (fptr = param->flist; fptr != NULL; fptr = fptr->next) { - fputs(fptr->arg, fp); + if (!incl_comm) + fputs("\n ", fp); + fputs(fptr->val, fp); fputc((fptr->next==NULL ? ';' : ','), fp); - fputs(fptr->rem, fp); + if (incl_comm) + fputs(fptr->rem, fp); } + if (!incl_comm) + fputs("\n\n", fp); return(!ferror(fp)); } /* Write out an Input Data File */ int -idf_write(IDF_LOADED *idf, const char *fname) +idf_write(IDF_LOADED *idf, const char *fname, int incl_comm) { FILE *fp; IDF_PARAMETER *pptr; @@ -326,9 +389,10 @@ idf_write(IDF_LOADED *idf, const char *fname) fp = stdout; /* open file if not stdout */ else if ((fp = fopen(fname, "w")) == NULL) return(0); - fputs(idf->hrem, fp); /* write header then parameters */ + if (incl_comm) + fputs(idf->hrem, fp); /* write header then parameters */ for (pptr = idf->pfirst; pptr != NULL; pptr = pptr->dnext) - if (!idf_writeparam(pptr, fp)) { + if (!idf_writeparam(pptr, fp, incl_comm>0)) { fclose(fp); return(0); }