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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines