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.5 by greg, Thu Mar 20 12:29:20 1997 UTC vs.
Revision 2.18 by greg, Mon Jun 5 16:32:42 2023 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  
17 #ifndef malloc
18 extern char  *malloc(), *realloc();
19 #endif
20
19   extern char  *fgetline();
20  
21  
22 < loadvars(rfname)                /* load variables into vv from file */
23 < char    *rfname;
22 > void
23 > loadvars(                       /* load variables into vv from file */
24 >        const char      *rfname
25 > )
26   {
27          FILE    *fp;
28          char    buf[512];
29 <        register char   *cp;
29 >        char    *cp;
30  
31          if (rfname == NULL)
32                  fp = stdin;
# Line 48 | Line 48 | char   *rfname;
48                          }
49                          break;
50                  }
51 <                setvariable(buf);
51 >                if (setvariable(buf, matchvar) < 0) {
52 >                        fprintf(stderr, "%s: unknown variable: %s\n",
53 >                                        rfname, buf);
54 >                        quit(1);
55 >                }
56          }
57 <        fclose(fp);
57 >        if (fp != stdin)
58 >                fclose(fp);
59   }
60  
61  
62 < setvariable(ass)                /* assign variable according to string */
63 < register char   *ass;
62 > int
63 > setvariable(                    /* assign variable according to string */
64 >        const char      *ass,
65 >        VARIABLE        *(*mv)(const char*)
66 > )
67   {
68          char    varname[32];
69          int     n;
70 <        register char   *cp;
71 <        register VARIABLE       *vp;
72 <        register int    i;
70 >        char    *cp;
71 >        VARIABLE        *vp;
72 >        int     i;
73  
74          while (isspace(*ass))           /* skip leading space */
75                  ass++;
# Line 71 | Line 79 | register char  *ass;
79                  *cp++ = *ass++;
80          *cp = '\0';
81          if (!varname[0])
82 <                return;         /* no variable name! */
82 >                return(0);      /* no variable name! */
83                                          /* trim value */
84          while (isspace(*ass) || *ass == '=')
85                  ass++;
86          for (n = strlen(ass); n > 0; n--)
87                  if (!isspace(ass[n-1]))
88                          break;
89 <        if (!n) {
90 <                if (!nowarn)
83 <                        fprintf(stderr,
84 <                        "%s: warning - missing value for variable '%s'\n",
85 <                                        progname, varname);
86 <                return;
87 <        }
89 >        if (!n)
90 >                return(0);      /* no assignment */
91                                          /* match variable from list */
92 <        vp = matchvar(varname);
93 <        if (vp == NULL) {
94 <                fprintf(stderr, "%s: unknown variable '%s'\n",
92 <                                progname, varname);
93 <                quit(1);
94 <        }
92 >        vp = (*mv)(varname);
93 >        if (vp == NULL)
94 >                return(-1);
95                                          /* assign new value */
96 <        if (i = vp->nass) {
96 >        if ( (i = vp->nass) ) {
97                  cp = vp->value;
98                  while (i--)
99                          while (*cp++)
100                                  ;
101                  i = cp - vp->value;
102 <                vp->value = realloc(vp->value, i+n+1);
102 >                vp->value = (char *)realloc((void *)vp->value, i+n+1);
103          } else
104 <                vp->value = malloc(n+1);
104 >                vp->value = (char *)malloc(n+1);
105          if (vp->value == NULL) {
106                  perror(progname);
107                  quit(1);
# Line 118 | Line 118 | register char  *ass;
118          }
119          if (isspace(*cp))               /* remove trailing space */
120                  *cp = '\0';
121 <        vp->nass++;
121 >        return(++vp->nass);
122   }
123  
124  
125   VARIABLE *
126 < matchvar(nam)                   /* match a variable by its name */
127 < char    *nam;
126 > matchvar(                       /* match a variable by its name */
127 >        const char      *nam
128 > )
129   {
130          int     n = strlen(nam);
131 <        register int    i;
131 >        int     i;
132  
133          for (i = 0; i < NVARS; i++)
134                  if (n >= vv[i].nick && !strncmp(nam, vv[i].name, n))
# Line 137 | Line 138 | char   *nam;
138  
139  
140   char *
141 < nvalue(vn, n)                   /* return nth variable value */
142 < register int    vn;
143 < register int    n;
141 > nvalue(                         /* return nth variable value */
142 >        int     vn,
143 >        int     n
144 > )
145   {
146 <        register char   *cp;
146 >        char    *cp;
147  
148 <        if (vval(vn) == NULL | n < 0 | n >= vdef(vn))
148 >        if ((vval(vn) == NULL) | (n < 0) | (n >= vdef(vn)))
149                  return(NULL);
150          cp = vval(vn);
151          while (n--)
# Line 153 | Line 155 | register int   n;
155   }
156  
157  
158 < checkvalues()                   /* check assignments */
158 > void
159 > checkvalues(void)               /* check assignments */
160   {
161 <        register int    i;
161 >        int     i;
162  
163          for (i = 0; i < NVARS; i++)
164                  if (vv[i].fixval != NULL)
# Line 163 | Line 166 | checkvalues()                  /* check assignments */
166   }
167  
168  
169 < onevalue(vp)                    /* only one assignment for this variable */
170 < register VARIABLE       *vp;
169 > void
170 > onevalue(                       /* only one assignment for this variable */
171 >        VARIABLE        *vp
172 > )
173   {
174          if (vp->nass < 2)
175                  return;
# Line 178 | Line 183 | register VARIABLE      *vp;
183   }
184  
185  
186 < catvalues(vp)                   /* concatenate variable values */
187 < register VARIABLE       *vp;
186 > void
187 > catvalues(                      /* concatenate variable values */
188 >        VARIABLE        *vp
189 > )
190   {
191 <        register char   *cp;
191 >        char    *cp;
192  
193          if (vp->nass < 2)
194                  return;
# Line 194 | Line 201 | register VARIABLE      *vp;
201  
202  
203   int
204 < badmatch(tv, cv)                /* case insensitive truncated comparison */
205 < register char   *tv, *cv;
204 > badmatch(                       /* case insensitive truncated comparison */
205 >        char    *tv,
206 >        char    *cv
207 > )
208   {
209          if (!*tv) return(1);            /* null string cannot match */
210          do
# Line 206 | Line 215 | register char  *tv, *cv;
215   }
216  
217  
218 < boolvalue(vp)                   /* check boolean for legal values */
219 < register VARIABLE       *vp;
218 > void
219 > boolvalue(                      /* check boolean for legal values */
220 >        VARIABLE        *vp
221 > )
222   {
223          if (!vp->nass) return;
224          onevalue(vp);
# Line 225 | Line 236 | register VARIABLE      *vp;
236   }
237  
238  
239 < qualvalue(vp)                   /* check qualitative var. for legal values */
240 < register VARIABLE       *vp;
239 > void
240 > qualvalue(                      /* check qualitative var. for legal values */
241 >        VARIABLE        *vp
242 > )
243   {
244          if (!vp->nass) return;
245          onevalue(vp);
# Line 247 | Line 260 | register VARIABLE      *vp;
260   }
261  
262  
263 < intvalue(vp)                    /* check integer variable for legal values */
264 < register VARIABLE       *vp;
263 > void
264 > intvalue(                               /* check integer variable for legal values */
265 >        VARIABLE        *vp
266 > )
267   {
268          if (!vp->nass) return;
269          onevalue(vp);
# Line 259 | Line 274 | register VARIABLE      *vp;
274   }
275  
276  
277 < fltvalue(vp)                    /* check float variable for legal values */
278 < register VARIABLE       *vp;
277 > void
278 > fltvalue(                               /* check float variable for legal values */
279 >        VARIABLE        *vp
280 > )
281   {
282          if (!vp->nass) return;
283          onevalue(vp);
# Line 271 | Line 288 | register VARIABLE      *vp;
288   }
289  
290  
291 < printvars(fp)                   /* print variable values */
292 < register FILE   *fp;
291 > void
292 > printvars(                              /* print variable values */
293 >        FILE    *fp
294 > )
295   {
296          int     i, j, k, clipline;
297 <        register char   *cp;
297 >        char    *cp;
298  
299          for (i = 0; i < NVARS; i++)             /* print each variable */
300              for (j = 0; j < vdef(i); j++) {     /* print each assignment */
301                  fputs(vnam(i), fp);
302                  fputs("= ", fp);
303 <                k = clipline = ( vv[i].fixval == catvalues ? 64 : 320 )
303 >                k = clipline = ( vv[i].fixval == catvalues ? 64 : 236 )
304                                  - strlen(vnam(i)) ;
305                  cp = nvalue(i, j);
306                  while (*cp) {
307                      putc(*cp++, fp);
308                      if (--k <= 0) {             /* line too long */
309                          while (*cp && !isspace(*cp))
310 <                            putc(*cp++, fp);    /* finish this word */
310 >                            fputc(*cp++, fp);   /* finish this word */
311                          if (*cp) {              /* start new line */
312 <                            putc('\n', fp);
313 <                            fputs(vnam(i), fp);
314 <                            putc('=', fp);
312 >                            if (vv[i].fixval == catvalues) {
313 >                                fputc('\n', fp);
314 >                                fputs(vnam(i), fp);
315 >                                fputc('=', fp);
316 >                            } else
317 >                                fputs(" \\\n", fp);
318                              k = clipline;
319                          }
320                      }
321                  }
322 <                putc('\n', fp);
322 >                fputc('\n', fp);
323              }
324          fflush(fp);
325   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines