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.19 by greg, Fri Jun 9 22:52:47 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 +        int     quote = '\0';
69          char    varname[32];
70          int     n;
71 <        register char   *cp;
72 <        register VARIABLE       *vp;
73 <        register int    i;
71 >        char    *cp;
72 >        VARIABLE        *vp;
73 >        int     i;
74  
75          while (isspace(*ass))           /* skip leading space */
76                  ass++;
# Line 71 | Line 80 | register char  *ass;
80                  *cp++ = *ass++;
81          *cp = '\0';
82          if (!varname[0])
83 <                return;         /* no variable name! */
83 >                return(0);      /* no variable name! */
84                                          /* trim value */
85          while (isspace(*ass) || *ass == '=')
86                  ass++;
87          for (n = strlen(ass); n > 0; n--)
88                  if (!isspace(ass[n-1]))
89                          break;
90 <        if (!n) {
91 <                if (!nowarn)
83 <                        fprintf(stderr,
84 <                        "%s: warning - missing value for variable '%s'\n",
85 <                                        progname, varname);
86 <                return;
87 <        }
90 >        if (!n)
91 >                return(0);      /* no assignment */
92                                          /* match variable from list */
93 <        vp = matchvar(varname);
94 <        if (vp == NULL) {
95 <                fprintf(stderr, "%s: unknown variable '%s'\n",
92 <                                progname, varname);
93 <                quit(1);
94 <        }
93 >        vp = (*mv)(varname);
94 >        if (vp == NULL)
95 >                return(-1);
96                                          /* assign new value */
97 <        if (i = vp->nass) {
97 >        if ( (i = vp->nass) ) {
98                  cp = vp->value;
99                  while (i--)
100                          while (*cp++)
101                                  ;
102                  i = cp - vp->value;
103 <                vp->value = realloc(vp->value, i+n+1);
103 >                vp->value = (char *)realloc((void *)vp->value, i+n+1);
104          } else
105 <                vp->value = malloc(n+1);
105 >                vp->value = (char *)malloc(n+1);
106          if (vp->value == NULL) {
107                  perror(progname);
108                  quit(1);
109          }
110 <        cp = vp->value+i;               /* copy value, squeezing spaces */
110 >        cp = vp->value+i;               /* copy value */
111          *cp = *ass;
112          for (i = 1; i <= n; i++) {
113                  if (ass[i] == NOCHAR)
114                          continue;
115 <                if (isspace(*cp))
116 <                        while (isspace(ass[i]))
117 <                                i++;
115 >                if (quote) {            /* don't change quoted parts */
116 >                        quote *= (ass[i] != quote);
117 >                } else {                /* otherwise, squeeze spaces */
118 >                        if (isspace(*cp))
119 >                                while (isspace(ass[i]))
120 >                                        i++;
121 >                        if ((ass[i] == '"') | (ass[i] == '\''))
122 >                                quote = ass[i];
123 >                }
124                  *++cp = ass[i];
125          }
126          if (isspace(*cp))               /* remove trailing space */
127                  *cp = '\0';
128 <        vp->nass++;
128 >        return(++vp->nass);
129   }
130  
131  
132   VARIABLE *
133 < matchvar(nam)                   /* match a variable by its name */
134 < char    *nam;
133 > matchvar(                       /* match a variable by its name */
134 >        const char      *nam
135 > )
136   {
137          int     n = strlen(nam);
138 <        register int    i;
138 >        int     i;
139  
140          for (i = 0; i < NVARS; i++)
141                  if (n >= vv[i].nick && !strncmp(nam, vv[i].name, n))
# Line 137 | Line 145 | char   *nam;
145  
146  
147   char *
148 < nvalue(vn, n)                   /* return nth variable value */
149 < register int    vn;
150 < register int    n;
148 > nvalue(                         /* return nth variable value */
149 >        int     vn,
150 >        int     n
151 > )
152   {
153 <        register char   *cp;
153 >        char    *cp;
154  
155 <        if (vval(vn) == NULL | n < 0 | n >= vdef(vn))
155 >        if ((vval(vn) == NULL) | (n < 0) | (n >= vdef(vn)))
156                  return(NULL);
157          cp = vval(vn);
158          while (n--)
# Line 153 | Line 162 | register int   n;
162   }
163  
164  
165 < checkvalues()                   /* check assignments */
165 > void
166 > checkvalues(void)               /* check assignments */
167   {
168 <        register int    i;
168 >        int     i;
169  
170          for (i = 0; i < NVARS; i++)
171                  if (vv[i].fixval != NULL)
# Line 163 | Line 173 | checkvalues()                  /* check assignments */
173   }
174  
175  
176 < onevalue(vp)                    /* only one assignment for this variable */
177 < register VARIABLE       *vp;
176 > void
177 > onevalue(                       /* only one assignment for this variable */
178 >        VARIABLE        *vp
179 > )
180   {
181          if (vp->nass < 2)
182                  return;
# Line 178 | Line 190 | register VARIABLE      *vp;
190   }
191  
192  
193 < catvalues(vp)                   /* concatenate variable values */
194 < register VARIABLE       *vp;
193 > void
194 > catvalues(                      /* concatenate variable values */
195 >        VARIABLE        *vp
196 > )
197   {
198 <        register char   *cp;
198 >        char    *cp;
199  
200          if (vp->nass < 2)
201                  return;
# Line 194 | Line 208 | register VARIABLE      *vp;
208  
209  
210   int
211 < badmatch(tv, cv)                /* case insensitive truncated comparison */
212 < register char   *tv, *cv;
211 > badmatch(                       /* case insensitive truncated comparison */
212 >        char    *tv,
213 >        char    *cv
214 > )
215   {
216          if (!*tv) return(1);            /* null string cannot match */
217          do
# Line 206 | Line 222 | register char  *tv, *cv;
222   }
223  
224  
225 < boolvalue(vp)                   /* check boolean for legal values */
226 < register VARIABLE       *vp;
225 > void
226 > boolvalue(                      /* check boolean for legal values */
227 >        VARIABLE        *vp
228 > )
229   {
230          if (!vp->nass) return;
231          onevalue(vp);
# Line 225 | Line 243 | register VARIABLE      *vp;
243   }
244  
245  
246 < qualvalue(vp)                   /* check qualitative var. for legal values */
247 < register VARIABLE       *vp;
246 > void
247 > qualvalue(                      /* check qualitative var. for legal values */
248 >        VARIABLE        *vp
249 > )
250   {
251          if (!vp->nass) return;
252          onevalue(vp);
# Line 247 | Line 267 | register VARIABLE      *vp;
267   }
268  
269  
270 < intvalue(vp)                    /* check integer variable for legal values */
271 < register VARIABLE       *vp;
270 > void
271 > intvalue(                               /* check integer variable for legal values */
272 >        VARIABLE        *vp
273 > )
274   {
275          if (!vp->nass) return;
276          onevalue(vp);
# Line 259 | Line 281 | register VARIABLE      *vp;
281   }
282  
283  
284 < fltvalue(vp)                    /* check float variable for legal values */
285 < register VARIABLE       *vp;
284 > void
285 > fltvalue(                               /* check float variable for legal values */
286 >        VARIABLE        *vp
287 > )
288   {
289          if (!vp->nass) return;
290          onevalue(vp);
# Line 271 | Line 295 | register VARIABLE      *vp;
295   }
296  
297  
298 < printvars(fp)                   /* print variable values */
299 < register FILE   *fp;
298 > void
299 > printvars(                              /* print variable values */
300 >        FILE    *fp
301 > )
302   {
303          int     i, j, k, clipline;
304 <        register char   *cp;
304 >        char    *cp;
305  
306          for (i = 0; i < NVARS; i++)             /* print each variable */
307              for (j = 0; j < vdef(i); j++) {     /* print each assignment */
308                  fputs(vnam(i), fp);
309                  fputs("= ", fp);
310 <                k = clipline = ( vv[i].fixval == catvalues ? 64 : 320 )
310 >                k = clipline = ( vv[i].fixval == catvalues ? 64 : 236 )
311                                  - strlen(vnam(i)) ;
312                  cp = nvalue(i, j);
313                  while (*cp) {
314                      putc(*cp++, fp);
315                      if (--k <= 0) {             /* line too long */
316                          while (*cp && !isspace(*cp))
317 <                            putc(*cp++, fp);    /* finish this word */
317 >                            fputc(*cp++, fp);   /* finish this word */
318                          if (*cp) {              /* start new line */
319 <                            putc('\n', fp);
320 <                            fputs(vnam(i), fp);
321 <                            putc('=', fp);
319 >                            if (vv[i].fixval == catvalues) {
320 >                                fputc('\n', fp);
321 >                                fputs(vnam(i), fp);
322 >                                fputc('=', fp);
323 >                            } else
324 >                                fputs(" \\\n", fp);
325                              k = clipline;
326                          }
327                      }
328                  }
329 <                putc('\n', fp);
329 >                fputc('\n', fp);
330              }
331          fflush(fp);
332   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines