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

Comparing ray/src/common/loadvars.c (file contents):
Revision 2.9 by greg, Sat Feb 22 02:07:22 2003 UTC vs.
Revision 2.24 by greg, Sat Jun 7 05:09:45 2025 UTC

# Line 5 | Line 5 | static const char      RCSid[] = "$Id$";
5   *  Routines for loading and checking variables from file.
6   */
7  
8 < /* ====================================================================
9 < * The Radiance Software License, Version 1.0
10 < *
11 < * Copyright (c) 1990 - 2002 The Regents of the University of California,
12 < * through Lawrence Berkeley National Laboratory.   All rights reserved.
13 < *
14 < * Redistribution and use in source and binary forms, with or without
15 < * modification, are permitted provided that the following conditions
16 < * are met:
17 < *
18 < * 1. Redistributions of source code must retain the above copyright
19 < *         notice, this list of conditions and the following disclaimer.
20 < *
21 < * 2. Redistributions in binary form must reproduce the above copyright
22 < *       notice, this list of conditions and the following disclaimer in
23 < *       the documentation and/or other materials provided with the
24 < *       distribution.
25 < *
26 < * 3. The end-user documentation included with the redistribution,
27 < *           if any, must include the following acknowledgment:
28 < *             "This product includes Radiance software
29 < *                 (http://radsite.lbl.gov/)
30 < *                 developed by the Lawrence Berkeley National Laboratory
31 < *               (http://www.lbl.gov/)."
32 < *       Alternately, this acknowledgment may appear in the software itself,
33 < *       if and wherever such third-party acknowledgments normally appear.
34 < *
35 < * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36 < *       and "The Regents of the University of California" must
37 < *       not be used to endorse or promote products derived from this
38 < *       software without prior written permission. For written
39 < *       permission, please contact [email protected].
40 < *
41 < * 5. Products derived from this software may not be called "Radiance",
42 < *       nor may "Radiance" appear in their name, without prior written
43 < *       permission of Lawrence Berkeley National Laboratory.
44 < *
45 < * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46 < * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 < * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 < * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49 < * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 < * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 < * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 < * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 < * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 < * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 < * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 < * SUCH DAMAGE.
57 < * ====================================================================
58 < *
59 < * This software consists of voluntary contributions made by many
60 < * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
61 < * information on Lawrence Berkeley National Laboratory, please see
62 < * <http://www.lbl.gov/>.
63 < */
8 > #include "copyright.h"
9  
65 #include <stdio.h>
66 #include <stdlib.h>
10   #include <ctype.h>
11 +
12 + #include "standard.h"
13 + #include "paths.h"
14   #include "vars.h"
15  
16   #define NOCHAR  127             /* constant for character to delete */
# Line 73 | Line 19 | extern char  *fgetline();
19  
20  
21   void
22 < loadvars(rfname)                /* load variables into vv from file */
23 < char    *rfname;
22 > loadvars(                       /* load variables into vv from file */
23 >        const char      *rfname
24 > )
25   {
26          FILE    *fp;
27          char    buf[512];
28 <        register char   *cp;
28 >        char    *cp;
29  
30          if (rfname == NULL)
31                  fp = stdin;
# Line 106 | Line 53 | char   *rfname;
53                          quit(1);
54                  }
55          }
56 <        fclose(fp);
56 >        if (fp != stdin)
57 >                fclose(fp);
58   }
59  
60  
61   int
62 < setvariable(ass, mv)            /* assign variable according to string */
63 < register char   *ass;
64 < VARIABLE        *(*mv)();
62 > setvariable(                    /* assign variable according to string */
63 >        const char      *ass,
64 >        VARIABLE        *(*mv)(const char*)
65 > )
66   {
67 +        int     quote = '\0';
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 143 | Line 93 | VARIABLE       *(*mv)();
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 = (char *)realloc(vp->value, i+n+1);
102 >                vp->value = (char *)realloc((void *)vp->value, i+n+1);
103          } else
104                  vp->value = (char *)malloc(n+1);
105          if (vp->value == NULL) {
106                  perror(progname);
107                  quit(1);
108          }
109 <        cp = vp->value+i;               /* copy value, squeezing spaces */
109 >        cp = vp->value+i;               /* copy value */
110          *cp = *ass;
111          for (i = 1; i <= n; i++) {
112                  if (ass[i] == NOCHAR)
113                          continue;
114 <                if (isspace(*cp))
115 <                        while (isspace(ass[i]))
116 <                                i++;
114 >                if (quote) {            /* don't change quoted parts */
115 >                        quote *= (ass[i] != quote);
116 >                } else {                /* otherwise, squeeze spaces */
117 >                        if (isspace(*cp))
118 >                                while (isspace(ass[i])|(ass[i]==NOCHAR))
119 >                                        i++;
120 >                        if ((ass[i] == '"') | (ass[i] == '\''))
121 >                                quote = ass[i];
122 >                }
123                  *++cp = ass[i];
124          }
125          if (isspace(*cp))               /* remove trailing space */
# Line 173 | Line 129 | VARIABLE       *(*mv)();
129  
130  
131   VARIABLE *
132 < matchvar(nam)                   /* match a variable by its name */
133 < char    *nam;
132 > matchvar(                       /* match a variable by its name */
133 >        const char      *nam
134 > )
135   {
136          int     n = strlen(nam);
137 <        register int    i;
137 >        int     i;
138  
139          for (i = 0; i < NVARS; i++)
140                  if (n >= vv[i].nick && !strncmp(nam, vv[i].name, n))
# Line 187 | Line 144 | char   *nam;
144  
145  
146   char *
147 < nvalue(vn, n)                   /* return nth variable value */
148 < register int    vn;
149 < register int    n;
147 > nvalue(                         /* return nth variable value */
148 >        int     vn,
149 >        int     n
150 > )
151   {
152 <        register char   *cp;
152 >        char    *cp;
153  
154 <        if (vval(vn) == NULL | n < 0 | n >= vdef(vn))
154 >        if ((vval(vn) == NULL) | (n < 0) | (n >= vdef(vn)))
155                  return(NULL);
156          cp = vval(vn);
157          while (n--)
# Line 204 | Line 162 | register int   n;
162  
163  
164   void
165 < checkvalues()                   /* check assignments */
165 > checkvalues(void)               /* check assignments */
166   {
167 <        register int    i;
167 >        int     i;
168  
169          for (i = 0; i < NVARS; i++)
170                  if (vv[i].fixval != NULL)
# Line 215 | Line 173 | checkvalues()                  /* check assignments */
173  
174  
175   void
176 < onevalue(vp)                    /* only one assignment for this variable */
177 < register VARIABLE       *vp;
176 > onevalue(                       /* only one assignment for this variable */
177 >        VARIABLE        *vp
178 > )
179   {
180          if (vp->nass < 2)
181                  return;
# Line 231 | Line 190 | register VARIABLE      *vp;
190  
191  
192   void
193 < catvalues(vp)                   /* concatenate variable values */
194 < register VARIABLE       *vp;
193 > catvalues(                      /* concatenate variable values */
194 >        VARIABLE        *vp
195 > )
196   {
197 <        register char   *cp;
197 >        char    *cp;
198  
199          if (vp->nass < 2)
200                  return;
# Line 247 | Line 207 | register VARIABLE      *vp;
207  
208  
209   int
210 < badmatch(tv, cv)                /* case insensitive truncated comparison */
211 < register char   *tv, *cv;
210 > badmatch(                       /* case insensitive truncated comparison */
211 >        char    *tv,
212 >        char    *cv
213 > )
214   {
215          if (!*tv) return(1);            /* null string cannot match */
216          do
# Line 260 | Line 222 | register char  *tv, *cv;
222  
223  
224   void
225 < boolvalue(vp)                   /* check boolean for legal values */
226 < register VARIABLE       *vp;
225 > boolvalue(                      /* check boolean for legal values */
226 >        VARIABLE        *vp
227 > )
228   {
229          if (!vp->nass) return;
230          onevalue(vp);
# Line 280 | Line 243 | register VARIABLE      *vp;
243  
244  
245   void
246 < qualvalue(vp)                   /* check qualitative var. for legal values */
247 < register VARIABLE       *vp;
246 > qualvalue(                      /* check qualitative var. for legal values */
247 >        VARIABLE        *vp
248 > )
249   {
250          if (!vp->nass) return;
251          onevalue(vp);
# Line 301 | Line 265 | register VARIABLE      *vp;
265          quit(1);
266   }
267  
268 + void
269 + strvalue(                               /* check for single (quoted) string value */
270 +        VARIABLE        *vp
271 + )
272 + {
273 +        if (!vp->nass) return;
274 +        onevalue(vp);
275 +        if ((vp->value[0] == '"') | (vp->value[0] == '\'')) {
276 +                char    *cp = vp->value + strlen(vp->value+1);
277 +                if ((cp != vp->value) & (*cp == vp->value[0])) {
278 +                        vp->value++;    /* elide quotation marks */
279 +                        *cp = '\0';
280 +                }
281 +        }
282 + }
283  
284 +
285   void
286 < intvalue(vp)                    /* check integer variable for legal values */
287 < register VARIABLE       *vp;
286 > intvalue(                               /* check integer variable for legal values */
287 >        VARIABLE        *vp
288 > )
289   {
290          if (!vp->nass) return;
291          onevalue(vp);
# Line 316 | Line 297 | register VARIABLE      *vp;
297  
298  
299   void
300 < fltvalue(vp)                    /* check float variable for legal values */
301 < register VARIABLE       *vp;
300 > fltvalue(                               /* check float variable for legal values */
301 >        VARIABLE        *vp
302 > )
303   {
304          if (!vp->nass) return;
305          onevalue(vp);
# Line 328 | Line 310 | register VARIABLE      *vp;
310   }
311  
312  
313 + int
314 + singlevar(                              /* assigned single value? */
315 +        VARIABLE *vp
316 + )
317 + {
318 +        if (vp->fixval == catvalues)
319 +                return(0);
320 +
321 +        return((vp->fixval == strvalue) |
322 +                        (vp->fixval == fltvalue) |
323 +                        (vp->fixval == intvalue) |
324 +                        (vp->fixval == qualvalue) |
325 +                        (vp->fixval == boolvalue));
326 + }
327 +
328   void
329 < printvars(fp)                   /* print variable values */
330 < register FILE   *fp;
329 > printvars(                              /* print variable values */
330 >        FILE    *fp
331 > )
332   {
333          int     i, j, k, clipline;
334 <        register char   *cp;
334 >        char    *cp;
335  
336          for (i = 0; i < NVARS; i++)             /* print each variable */
337              for (j = 0; j < vdef(i); j++) {     /* print each assignment */
338                  fputs(vnam(i), fp);
339 <                fputs("= ", fp);
339 >                fputc('=', fp);
340 >                if (!singlevar(&vv[i]))
341 >                        fputc(' ', fp);
342                  k = clipline = ( vv[i].fixval == catvalues ? 64 : 236 )
343                                  - strlen(vnam(i)) ;
344                  cp = nvalue(i, j);

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)