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

Comparing ray/src/common/loadvars.c (file contents):
Revision 2.1 by greg, Tue Dec 12 14:00:48 1995 UTC vs.
Revision 2.13 by schorsch, Sun Jul 27 22:12:01 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1995 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  Routines for loading and checking variables from file.
6   */
7  
8 + #include "copyright.h"
9 +
10   #include <stdio.h>
11 + #include <stdlib.h>
12   #include <ctype.h>
13 +
14 + #include "standard.h"
15   #include "vars.h"
16  
17   #define NOCHAR  127             /* constant for character to delete */
18  
19 < #ifndef malloc
18 < extern char  *malloc(), *realloc();
19 < #endif
19 > extern char  *fgetline();
20  
21  
22 + void
23   loadvars(rfname)                /* load variables into vv from file */
24   char    *rfname;
25   {
# Line 30 | Line 31 | char   *rfname;
31                  fp = stdin;
32          else if ((fp = fopen(rfname, "r")) == NULL) {
33                  perror(rfname);
34 <                exit(1);
34 >                quit(1);
35          }
36          while (fgetline(buf, sizeof(buf), fp) != NULL) {
37                  for (cp = buf; *cp; cp++) {
# Line 46 | Line 47 | char   *rfname;
47                          }
48                          break;
49                  }
50 <                setvariable(buf);
50 >                if (setvariable(buf, matchvar) < 0) {
51 >                        fprintf(stderr, "%s: unknown variable: %s\n",
52 >                                        rfname, buf);
53 >                        quit(1);
54 >                }
55          }
56          fclose(fp);
57   }
58  
59  
60 < setvariable(ass)                /* assign variable according to string */
60 > int
61 > setvariable(ass, mv)            /* assign variable according to string */
62   register char   *ass;
63 + VARIABLE        *(*mv)();
64   {
65          char    varname[32];
66          int     n;
# Line 69 | Line 76 | register char  *ass;
76                  *cp++ = *ass++;
77          *cp = '\0';
78          if (!varname[0])
79 <                return;         /* no variable name! */
79 >                return(0);      /* no variable name! */
80                                          /* trim value */
81          while (isspace(*ass) || *ass == '=')
82                  ass++;
83          for (n = strlen(ass); n > 0; n--)
84                  if (!isspace(ass[n-1]))
85                          break;
86 <        if (!n && !nowarn) {
87 <                fprintf(stderr, "%s: warning - missing value for variable '%s'\n",
81 <                                progname, varname);
82 <                return;
83 <        }
86 >        if (!n)
87 >                return(0);      /* no assignment */
88                                          /* match variable from list */
89 <        vp = matchvar(varname);
90 <        if (vp == NULL) {
91 <                fprintf(stderr, "%s: unknown variable '%s'\n",
88 <                                progname, varname);
89 <                exit(1);
90 <        }
89 >        vp = (*mv)(varname);
90 >        if (vp == NULL)
91 >                return(-1);
92                                          /* assign new value */
93 <        if (i = vp->nass) {
93 >        if ( (i = vp->nass) ) {
94                  cp = vp->value;
95                  while (i--)
96                          while (*cp++)
97                                  ;
98                  i = cp - vp->value;
99 <                vp->value = realloc(vp->value, i+n+1);
99 >                vp->value = (char *)realloc((void *)vp->value, i+n+1);
100          } else
101 <                vp->value = malloc(n+1);
101 >                vp->value = (char *)malloc(n+1);
102          if (vp->value == NULL) {
103                  perror(progname);
104 <                exit(1);
104 >                quit(1);
105          }
106          cp = vp->value+i;               /* copy value, squeezing spaces */
107          *cp = *ass;
# Line 114 | Line 115 | register char  *ass;
115          }
116          if (isspace(*cp))               /* remove trailing space */
117                  *cp = '\0';
118 <        vp->nass++;
118 >        return(++vp->nass);
119   }
120  
121  
# Line 133 | Line 134 | char   *nam;
134  
135  
136   char *
137 < nvalue(vp, n)                   /* return nth variable value */
138 < VARIABLE        *vp;
137 > nvalue(vn, n)                   /* return nth variable value */
138 > register int    vn;
139   register int    n;
140   {
141          register char   *cp;
142  
143 <        if (vp == NULL | n < 0 | n >= vp->nass)
143 >        if ((vval(vn) == NULL) | (n < 0) | (n >= vdef(vn)))
144                  return(NULL);
145 <        cp = vp->value;
145 >        cp = vval(vn);
146          while (n--)
147                  while (*cp++)
148                          ;
# Line 149 | Line 150 | register int   n;
150   }
151  
152  
153 + void
154   checkvalues()                   /* check assignments */
155   {
156          register int    i;
# Line 159 | Line 161 | checkvalues()                  /* check assignments */
161   }
162  
163  
164 + void
165   onevalue(vp)                    /* only one assignment for this variable */
166   register VARIABLE       *vp;
167   {
# Line 174 | Line 177 | register VARIABLE      *vp;
177   }
178  
179  
180 + void
181   catvalues(vp)                   /* concatenate variable values */
182   register VARIABLE       *vp;
183   {
# Line 202 | Line 206 | register char  *tv, *cv;
206   }
207  
208  
209 + void
210   boolvalue(vp)                   /* check boolean for legal values */
211   register VARIABLE       *vp;
212   {
# Line 217 | Line 222 | register VARIABLE      *vp;
222          }
223          fprintf(stderr, "%s: illegal value for boolean variable '%s'\n",
224                          progname, vp->name);
225 <        exit(1);
225 >        quit(1);
226   }
227  
228  
229 + void
230   qualvalue(vp)                   /* check qualitative var. for legal values */
231   register VARIABLE       *vp;
232   {
# Line 239 | Line 245 | register VARIABLE      *vp;
245          }
246          fprintf(stderr, "%s: illegal value for qualitative variable '%s'\n",
247                          progname, vp->name);
248 <        exit(1);
248 >        quit(1);
249   }
250  
251  
252 + void
253   intvalue(vp)                    /* check integer variable for legal values */
254   register VARIABLE       *vp;
255   {
# Line 251 | Line 258 | register VARIABLE      *vp;
258          if (isint(vp->value)) return;
259          fprintf(stderr, "%s: illegal value for integer variable '%s'\n",
260                          progname, vp->name);
261 <        exit(1);
261 >        quit(1);
262   }
263  
264  
265 + void
266   fltvalue(vp)                    /* check float variable for legal values */
267   register VARIABLE       *vp;
268   {
# Line 263 | Line 271 | register VARIABLE      *vp;
271          if (isflt(vp->value)) return;
272          fprintf(stderr, "%s: illegal value for real variable '%s'\n",
273                          progname, vp->name);
274 <        exit(1);
274 >        quit(1);
275   }
276  
277  
278 + void
279   printvars(fp)                   /* print variable values */
280   register FILE   *fp;
281   {
# Line 277 | Line 286 | register FILE  *fp;
286              for (j = 0; j < vdef(i); j++) {     /* print each assignment */
287                  fputs(vnam(i), fp);
288                  fputs("= ", fp);
289 <                k = clipline = ( vv[i].fixval == catvalues ? 64 : 320 )
289 >                k = clipline = ( vv[i].fixval == catvalues ? 64 : 236 )
290                                  - strlen(vnam(i)) ;
291 <                cp = nvalue(vv+i, j);
291 >                cp = nvalue(i, j);
292                  while (*cp) {
293                      putc(*cp++, fp);
294                      if (--k <= 0) {             /* line too long */
295                          while (*cp && !isspace(*cp))
296 <                            putc(*cp++, fp);    /* finish this word */
296 >                            fputc(*cp++, fp);   /* finish this word */
297                          if (*cp) {              /* start new line */
298 <                            putc('\n', fp);
299 <                            fputs(vnam(i), fp);
300 <                            putc('=', fp);
298 >                            if (vv[i].fixval == catvalues) {
299 >                                fputc('\n', fp);
300 >                                fputs(vnam(i), fp);
301 >                                fputc('=', fp);
302 >                            } else
303 >                                fputs(" \\\n", fp);
304                              k = clipline;
305                          }
306                      }
307                  }
308 <                putc('\n', fp);
308 >                fputc('\n', fp);
309              }
310          fflush(fp);
311   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines