--- ray/src/util/eplus_idf.c 2014/02/01 01:28:43 2.1 +++ ray/src/util/eplus_idf.c 2014/02/09 22:19:30 2.5 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: eplus_idf.c,v 2.1 2014/02/01 01:28:43 greg Exp $"; +static const char RCSid[] = "$Id: eplus_idf.c,v 2.5 2014/02/09 22:19:30 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; @@ -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) @@ -163,7 +211,7 @@ idf_read_argument(char *buf, FILE *fp, int trim) if (skipwhite && isspace(c)) continue; skipwhite = 0; - if (cp-buf < IDF_MAXLINE-1) + if (cp-buf < IDF_MAXARGL-1) *cp++ = c; } if (trim) @@ -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; @@ -203,7 +250,7 @@ idf_read_comment(char *buf, int len, FILE *fp) IDF_PARAMETER * idf_readparam(IDF_LOADED *idf, FILE *fp) { - char abuf[IDF_MAXLINE], cbuf[IDF_MAXLINE]; + char abuf[IDF_MAXARGL], cbuf[IDF_MAXLINE]; int delim; IDF_PARAMETER *pnew; @@ -217,10 +264,25 @@ 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); } +/* Upper-case string hashing function */ +static unsigned long +strcasehash(const char *s) +{ + char strup[IDF_MAXARGL]; + char *cdst = strup; + + while ((*cdst++ = toupper(*s++))) + if (cdst >= strup+(sizeof(strup)-1)) { + *cdst = '\0'; + break; + } + return(lu_shash(strup)); +} + /* Initialize an IDF struct */ IDF_LOADED * idf_create(const char *hdrcomm) @@ -229,8 +291,8 @@ idf_create(const char *hdrcomm) if (idf == NULL) return(NULL); - idf->ptab.hashf = &lu_shash; - idf->ptab.keycmp = &strcmp; + idf->ptab.hashf = &strcasehash; + idf->ptab.keycmp = &strcasecmp; idf->ptab.freek = &free; lu_init(&idf->ptab, 200); if (hdrcomm != NULL && *hdrcomm) { @@ -241,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; @@ -254,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 */ @@ -270,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; @@ -278,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) { + if (!incl_comm) + fputs("\n\t", fp); fputs(fptr->arg, 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; @@ -300,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); }