ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/eplus_idf.c
(Generate patch)

Comparing ray/src/util/eplus_idf.c (file contents):
Revision 2.2 by greg, Sat Feb 1 02:13:24 2014 UTC vs.
Revision 2.4 by greg, Sun Feb 9 05:49:21 2014 UTC

# Line 97 | Line 97 | idf_addfield(IDF_PARAMETER *param, const char *fval, c
97          return(fnum);
98   }
99  
100 + /* Retrieve the indexed field from parameter (first field index is 1) */
101 + IDF_FIELD *
102 + idf_getfield(IDF_PARAMETER *param, int fn)
103 + {
104 +        IDF_FIELD       *fld;
105 +
106 +        if ((param == NULL) | (fn <= 0))
107 +                return(NULL);
108 +        fld = param->flist;
109 +        while ((--fn > 0) & (fld != NULL))
110 +                fld = fld->next;
111 +        return(fld);
112 + }
113 +
114   /* Delete the specified parameter from loaded IDF */
115   int
116   idf_delparam(IDF_LOADED *idf, IDF_PARAMETER *param)
# Line 120 | Line 134 | idf_delparam(IDF_LOADED *idf, IDF_PARAMETER *param)
134                  plast->pnext = param->pnext;
135                                          /* remove from global list */
136          for (plast = NULL, pptr = idf->pfirst;
137 <                                pptr != NULL; plast = pptr, pptr = pptr->dnext)
138 <                if (pptr == param)
139 <                        break;
126 <        if (pptr == NULL)
127 <                return(0);
137 >                                pptr != param; plast = pptr, pptr = pptr->dnext)
138 >                if (pptr == NULL)
139 >                        return(0);
140          if (plast == NULL)
141                  idf->pfirst = param->dnext;
142          else
# Line 141 | Line 153 | idf_delparam(IDF_LOADED *idf, IDF_PARAMETER *param)
153          return(1);
154   }
155  
156 + /* Move the specified parameter to the given position in the IDF */
157 + int
158 + idf_movparam(IDF_LOADED *idf, IDF_PARAMETER *param, IDF_PARAMETER *prev)
159 + {
160 +        IDF_PARAMETER   *pptr, *plast;
161 +
162 +        if ((idf == NULL) | (param == NULL))
163 +                return(0);
164 +                                        /* find in IDF list, first*/
165 +        for (plast = NULL, pptr = idf->pfirst;
166 +                                pptr != param; plast = pptr, pptr = pptr->dnext)
167 +                if (pptr == NULL)
168 +                        return(0);
169 +        if (plast == NULL) {
170 +                if (prev == NULL)
171 +                        return(1);      /* already in place */
172 +                idf->pfirst = param->dnext;
173 +        } else {
174 +                if (prev == plast)
175 +                        return(1);      /* already in place */
176 +                plast->dnext = param->dnext;
177 +        }
178 +        if (idf->plast == param)
179 +                idf->plast = plast;
180 +        if (prev == NULL) {             /* means they want it at beginning */
181 +                param->dnext = idf->pfirst;
182 +                idf->pfirst = param;
183 +        } else {
184 +                param->dnext = prev->dnext;
185 +                prev->dnext = param;
186 +        }
187 +        return(1);
188 + }
189 +
190   /* Get a named parameter list */
191   IDF_PARAMETER *
192   idf_getparam(IDF_LOADED *idf, const char *pname)
# Line 186 | Line 232 | idf_read_comment(char *buf, int len, FILE *fp)
232                  buf = &dummyc;
233                  len = 1;
234          }
235 <        while ((c = getc(fp)) != EOF && isspace(c) | incomm) {
236 <                if (c == '!')
237 <                        ++incomm;
192 <                else if (c == '\n')
235 >        while ((c = getc(fp)) != EOF &&
236 >                        (isspace(c) || (incomm += (c == '!')))) {
237 >                if (c == '\n')
238                          incomm = 0;
239                  if (cp-buf < len-1)
240                          *cp++ = c;
# Line 217 | Line 262 | idf_readparam(IDF_LOADED *idf, FILE *fp)
262                          idf_addfield(pnew, abuf, cbuf);
263                  }
264          if (delim != ';')
265 <                fprintf(stderr, "Expected ';' at end of parameter list\n");
265 >                fputs("Expected ';' at end of parameter list!\n", stderr);
266          return(pnew);
267   }
268  
# Line 256 | Line 301 | idf_create(const char *hdrcomm)
301          return(idf);
302   }
303  
304 + /* Add comment(s) to header */
305 + int
306 + idf_add2hdr(IDF_LOADED *idf, const char *hdrcomm)
307 + {
308 +        int     olen, len;
309 +
310 +        if ((idf == NULL) | (hdrcomm == NULL))
311 +                return(0);
312 +        len = strlen(hdrcomm);
313 +        if (!len)
314 +                return(0);
315 +        if (idf->hrem == NULL)
316 +                olen = 0;
317 +        else
318 +                olen = strlen(idf->hrem);
319 +        if (olen)
320 +                idf->hrem = (char *)realloc(idf->hrem, olen+len+1);
321 +        else
322 +                idf->hrem = (char *)malloc(len+1);
323 +        if (idf->hrem == NULL)
324 +                return(0);
325 +        strcpy(idf->hrem+olen, hdrcomm);
326 +        return(1);
327 + }
328 +
329   /* Load an Input Data File */
330   IDF_LOADED *
331   idf_load(const char *fname)
332   {
333 <        char            *hdrcomm;
333 >        char            hdrcomm[256*IDF_MAXLINE];
334          FILE            *fp;
335          IDF_LOADED      *idf;
336  
# Line 269 | Line 339 | idf_load(const char *fname)
339          else if ((fp = fopen(fname, "r")) == NULL)
340                  return(NULL);
341                                          /* read header comments */
342 <        hdrcomm = (char *)malloc(100*IDF_MAXLINE);
273 <        idf_read_comment(hdrcomm, 100*IDF_MAXLINE, fp);
342 >        idf_read_comment(hdrcomm, sizeof(hdrcomm), fp);
343          idf = idf_create(hdrcomm);      /* create IDF struct */
275        free(hdrcomm);
344          if (idf == NULL)
345                  return(NULL);
346                                          /* read each parameter */
# Line 285 | Line 353 | idf_load(const char *fname)
353  
354   /* Write a parameter and fields to an open file */
355   int
356 < idf_writeparam(IDF_PARAMETER *param, FILE *fp)
356 > idf_writeparam(IDF_PARAMETER *param, FILE *fp, int incl_comm)
357   {
358          IDF_FIELD       *fptr;
359  
# Line 293 | Line 361 | idf_writeparam(IDF_PARAMETER *param, FILE *fp)
361                  return(0);
362          fputs(param->pname, fp);
363          fputc(',', fp);
364 <        fputs(param->rem, fp);
364 >        if (incl_comm)
365 >                fputs(param->rem, fp);
366          for (fptr = param->flist; fptr != NULL; fptr = fptr->next) {
367 +                if (!incl_comm)
368 +                        fputs("\n\t", fp);
369                  fputs(fptr->arg, fp);
370                  fputc((fptr->next==NULL ? ';' : ','), fp);
371 <                fputs(fptr->rem, fp);
371 >                if (incl_comm)
372 >                        fputs(fptr->rem, fp);
373          }
374 +        if (!incl_comm)
375 +                fputs("\n\n", fp);
376          return(!ferror(fp));
377   }
378  
379   /* Write out an Input Data File */
380   int
381 < idf_write(IDF_LOADED *idf, const char *fname)
381 > idf_write(IDF_LOADED *idf, const char *fname, int incl_comm)
382   {
383          FILE            *fp;
384          IDF_PARAMETER   *pptr;
# Line 315 | Line 389 | idf_write(IDF_LOADED *idf, const char *fname)
389                  fp = stdout;            /* open file if not stdout */
390          else if ((fp = fopen(fname, "w")) == NULL)
391                  return(0);
392 <        fputs(idf->hrem, fp);           /* write header then parameters */
392 >        if (incl_comm)
393 >                fputs(idf->hrem, fp);   /* write header then parameters */
394          for (pptr = idf->pfirst; pptr != NULL; pptr = pptr->dnext)
395 <                if (!idf_writeparam(pptr, fp)) {
395 >                if (!idf_writeparam(pptr, fp, incl_comm>0)) {
396                          fclose(fp);
397                          return(0);
398                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines