1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
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 |
*/ |
64 |
|
65 |
#include <stdio.h> |
66 |
#include <stdlib.h> |
67 |
#include <ctype.h> |
68 |
#include "vars.h" |
69 |
|
70 |
#define NOCHAR 127 /* constant for character to delete */ |
71 |
|
72 |
extern char *fgetline(); |
73 |
|
74 |
|
75 |
void |
76 |
loadvars(rfname) /* load variables into vv from file */ |
77 |
char *rfname; |
78 |
{ |
79 |
FILE *fp; |
80 |
char buf[512]; |
81 |
register char *cp; |
82 |
|
83 |
if (rfname == NULL) |
84 |
fp = stdin; |
85 |
else if ((fp = fopen(rfname, "r")) == NULL) { |
86 |
perror(rfname); |
87 |
quit(1); |
88 |
} |
89 |
while (fgetline(buf, sizeof(buf), fp) != NULL) { |
90 |
for (cp = buf; *cp; cp++) { |
91 |
switch (*cp) { |
92 |
case '\\': |
93 |
*cp++ = NOCHAR; |
94 |
continue; |
95 |
case '#': |
96 |
*cp = '\0'; |
97 |
break; |
98 |
default: |
99 |
continue; |
100 |
} |
101 |
break; |
102 |
} |
103 |
if (setvariable(buf, matchvar) < 0) { |
104 |
fprintf(stderr, "%s: unknown variable: %s\n", |
105 |
rfname, buf); |
106 |
quit(1); |
107 |
} |
108 |
} |
109 |
fclose(fp); |
110 |
} |
111 |
|
112 |
|
113 |
int |
114 |
setvariable(ass, mv) /* assign variable according to string */ |
115 |
register char *ass; |
116 |
VARIABLE *(*mv)(); |
117 |
{ |
118 |
char varname[32]; |
119 |
int n; |
120 |
register char *cp; |
121 |
register VARIABLE *vp; |
122 |
register int i; |
123 |
|
124 |
while (isspace(*ass)) /* skip leading space */ |
125 |
ass++; |
126 |
cp = varname; /* extract name */ |
127 |
while (cp < varname+sizeof(varname)-1 |
128 |
&& *ass && !isspace(*ass) && *ass != '=') |
129 |
*cp++ = *ass++; |
130 |
*cp = '\0'; |
131 |
if (!varname[0]) |
132 |
return(0); /* no variable name! */ |
133 |
/* trim value */ |
134 |
while (isspace(*ass) || *ass == '=') |
135 |
ass++; |
136 |
for (n = strlen(ass); n > 0; n--) |
137 |
if (!isspace(ass[n-1])) |
138 |
break; |
139 |
if (!n) |
140 |
return(0); /* no assignment */ |
141 |
/* match variable from list */ |
142 |
vp = (*mv)(varname); |
143 |
if (vp == NULL) |
144 |
return(-1); |
145 |
/* assign new value */ |
146 |
if (i = vp->nass) { |
147 |
cp = vp->value; |
148 |
while (i--) |
149 |
while (*cp++) |
150 |
; |
151 |
i = cp - vp->value; |
152 |
vp->value = (char *)realloc(vp->value, i+n+1); |
153 |
} else |
154 |
vp->value = (char *)malloc(n+1); |
155 |
if (vp->value == NULL) { |
156 |
perror(progname); |
157 |
quit(1); |
158 |
} |
159 |
cp = vp->value+i; /* copy value, squeezing spaces */ |
160 |
*cp = *ass; |
161 |
for (i = 1; i <= n; i++) { |
162 |
if (ass[i] == NOCHAR) |
163 |
continue; |
164 |
if (isspace(*cp)) |
165 |
while (isspace(ass[i])) |
166 |
i++; |
167 |
*++cp = ass[i]; |
168 |
} |
169 |
if (isspace(*cp)) /* remove trailing space */ |
170 |
*cp = '\0'; |
171 |
return(++vp->nass); |
172 |
} |
173 |
|
174 |
|
175 |
VARIABLE * |
176 |
matchvar(nam) /* match a variable by its name */ |
177 |
char *nam; |
178 |
{ |
179 |
int n = strlen(nam); |
180 |
register int i; |
181 |
|
182 |
for (i = 0; i < NVARS; i++) |
183 |
if (n >= vv[i].nick && !strncmp(nam, vv[i].name, n)) |
184 |
return(vv+i); |
185 |
return(NULL); |
186 |
} |
187 |
|
188 |
|
189 |
char * |
190 |
nvalue(vn, n) /* return nth variable value */ |
191 |
register int vn; |
192 |
register int n; |
193 |
{ |
194 |
register char *cp; |
195 |
|
196 |
if (vval(vn) == NULL | n < 0 | n >= vdef(vn)) |
197 |
return(NULL); |
198 |
cp = vval(vn); |
199 |
while (n--) |
200 |
while (*cp++) |
201 |
; |
202 |
return(cp); |
203 |
} |
204 |
|
205 |
|
206 |
void |
207 |
checkvalues() /* check assignments */ |
208 |
{ |
209 |
register int i; |
210 |
|
211 |
for (i = 0; i < NVARS; i++) |
212 |
if (vv[i].fixval != NULL) |
213 |
(*vv[i].fixval)(vv+i); |
214 |
} |
215 |
|
216 |
|
217 |
void |
218 |
onevalue(vp) /* only one assignment for this variable */ |
219 |
register VARIABLE *vp; |
220 |
{ |
221 |
if (vp->nass < 2) |
222 |
return; |
223 |
if (!nowarn) |
224 |
fprintf(stderr, |
225 |
"%s: warning - multiple assignment of variable '%s'\n", |
226 |
progname, vp->name); |
227 |
do |
228 |
vp->value += strlen(vp->value)+1; |
229 |
while (--vp->nass > 1); |
230 |
} |
231 |
|
232 |
|
233 |
void |
234 |
catvalues(vp) /* concatenate variable values */ |
235 |
register VARIABLE *vp; |
236 |
{ |
237 |
register char *cp; |
238 |
|
239 |
if (vp->nass < 2) |
240 |
return; |
241 |
for (cp = vp->value; vp->nass > 1; vp->nass--) { |
242 |
while (*cp) |
243 |
cp++; |
244 |
*cp++ = ' '; |
245 |
} |
246 |
} |
247 |
|
248 |
|
249 |
int |
250 |
badmatch(tv, cv) /* case insensitive truncated comparison */ |
251 |
register char *tv, *cv; |
252 |
{ |
253 |
if (!*tv) return(1); /* null string cannot match */ |
254 |
do |
255 |
if (UPPER(*tv) != *cv++) |
256 |
return(1); |
257 |
while (*++tv); |
258 |
return(0); /* OK */ |
259 |
} |
260 |
|
261 |
|
262 |
void |
263 |
boolvalue(vp) /* check boolean for legal values */ |
264 |
register VARIABLE *vp; |
265 |
{ |
266 |
if (!vp->nass) return; |
267 |
onevalue(vp); |
268 |
switch (UPPER(vp->value[0])) { |
269 |
case 'T': |
270 |
if (badmatch(vp->value, "TRUE")) break; |
271 |
return; |
272 |
case 'F': |
273 |
if (badmatch(vp->value, "FALSE")) break; |
274 |
return; |
275 |
} |
276 |
fprintf(stderr, "%s: illegal value for boolean variable '%s'\n", |
277 |
progname, vp->name); |
278 |
quit(1); |
279 |
} |
280 |
|
281 |
|
282 |
void |
283 |
qualvalue(vp) /* check qualitative var. for legal values */ |
284 |
register VARIABLE *vp; |
285 |
{ |
286 |
if (!vp->nass) return; |
287 |
onevalue(vp); |
288 |
switch (UPPER(vp->value[0])) { |
289 |
case 'L': |
290 |
if (badmatch(vp->value, "LOW")) break; |
291 |
return; |
292 |
case 'M': |
293 |
if (badmatch(vp->value, "MEDIUM")) break; |
294 |
return; |
295 |
case 'H': |
296 |
if (badmatch(vp->value, "HIGH")) break; |
297 |
return; |
298 |
} |
299 |
fprintf(stderr, "%s: illegal value for qualitative variable '%s'\n", |
300 |
progname, vp->name); |
301 |
quit(1); |
302 |
} |
303 |
|
304 |
|
305 |
void |
306 |
intvalue(vp) /* check integer variable for legal values */ |
307 |
register VARIABLE *vp; |
308 |
{ |
309 |
if (!vp->nass) return; |
310 |
onevalue(vp); |
311 |
if (isint(vp->value)) return; |
312 |
fprintf(stderr, "%s: illegal value for integer variable '%s'\n", |
313 |
progname, vp->name); |
314 |
quit(1); |
315 |
} |
316 |
|
317 |
|
318 |
void |
319 |
fltvalue(vp) /* check float variable for legal values */ |
320 |
register VARIABLE *vp; |
321 |
{ |
322 |
if (!vp->nass) return; |
323 |
onevalue(vp); |
324 |
if (isflt(vp->value)) return; |
325 |
fprintf(stderr, "%s: illegal value for real variable '%s'\n", |
326 |
progname, vp->name); |
327 |
quit(1); |
328 |
} |
329 |
|
330 |
|
331 |
void |
332 |
printvars(fp) /* print variable values */ |
333 |
register FILE *fp; |
334 |
{ |
335 |
int i, j, k, clipline; |
336 |
register char *cp; |
337 |
|
338 |
for (i = 0; i < NVARS; i++) /* print each variable */ |
339 |
for (j = 0; j < vdef(i); j++) { /* print each assignment */ |
340 |
fputs(vnam(i), fp); |
341 |
fputs("= ", fp); |
342 |
k = clipline = ( vv[i].fixval == catvalues ? 64 : 236 ) |
343 |
- strlen(vnam(i)) ; |
344 |
cp = nvalue(i, j); |
345 |
while (*cp) { |
346 |
putc(*cp++, fp); |
347 |
if (--k <= 0) { /* line too long */ |
348 |
while (*cp && !isspace(*cp)) |
349 |
fputc(*cp++, fp); /* finish this word */ |
350 |
if (*cp) { /* start new line */ |
351 |
if (vv[i].fixval == catvalues) { |
352 |
fputc('\n', fp); |
353 |
fputs(vnam(i), fp); |
354 |
fputc('=', fp); |
355 |
} else |
356 |
fputs(" \\\n", fp); |
357 |
k = clipline; |
358 |
} |
359 |
} |
360 |
} |
361 |
fputc('\n', fp); |
362 |
} |
363 |
fflush(fp); |
364 |
} |