--- ray/src/util/eplus_idf.c 2014/02/01 02:13:24 2.2 +++ ray/src/util/eplus_idf.c 2014/02/10 04:51:26 2.6 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: eplus_idf.c,v 2.2 2014/02/01 02:13:24 greg Exp $"; +static const char RCSid[] = "$Id: eplus_idf.c,v 2.6 2014/02/10 04:51:26 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,9 +95,24 @@ idf_addfield(IDF_PARAMETER *param, const char *fval, c param->flist = fnew; else flast->next = fnew; + param->nfield++; return(fnum); } +/* Retrieve the indexed field from parameter (first field index is 1) */ +IDF_FIELD * +idf_getfield(IDF_PARAMETER *param, int fn) +{ + IDF_FIELD *fld; + + if ((param == NULL) | (fn <= 0)) + return(NULL); + fld = param->flist; + while ((--fn > 0) & (fld != NULL)) + fld = fld->next; + return(fld); +} + /* Delete the specified parameter from loaded IDF */ int idf_delparam(IDF_LOADED *idf, IDF_PARAMETER *param) @@ -120,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 @@ -141,6 +155,40 @@ 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); + /* 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) { + if (prev == NULL) + return(1); /* already in place */ + idf->pfirst = param->dnext; + } else { + if (prev == plast) + return(1); /* already in place */ + 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) @@ -186,10 +234,9 @@ idf_read_comment(char *buf, int len, FILE *fp) buf = &dummyc; len = 1; } - while ((c = getc(fp)) != EOF && isspace(c) | incomm) { - if (c == '!') - ++incomm; - else if (c == '\n') + while ((c = getc(fp)) != EOF && + (isspace(c) || (incomm += (c == '!')))) { + if (c == '\n') incomm = 0; if (cp-buf < len-1) *cp++ = c; @@ -217,7 +264,7 @@ idf_readparam(IDF_LOADED *idf, FILE *fp) idf_addfield(pnew, abuf, cbuf); } if (delim != ';') - fprintf(stderr, "Expected ';' at end of parameter list\n"); + fputs("Expected ';' at end of parameter list!\n", stderr); return(pnew); } @@ -256,11 +303,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; + char hdrcomm[256*IDF_MAXLINE]; FILE *fp; IDF_LOADED *idf; @@ -269,10 +341,8 @@ idf_load(const char *fname) else if ((fp = fopen(fname, "r")) == NULL) return(NULL); /* read header comments */ - hdrcomm = (char *)malloc(100*IDF_MAXLINE); - idf_read_comment(hdrcomm, 100*IDF_MAXLINE, fp); + idf_read_comment(hdrcomm, sizeof(hdrcomm), fp); idf = idf_create(hdrcomm); /* create IDF struct */ - free(hdrcomm); if (idf == NULL) return(NULL); /* read each parameter */ @@ -285,7 +355,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; @@ -293,18 +363,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; @@ -315,9 +391,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); }