ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/loadvars.c
Revision: 2.8
Committed: Thu Jul 9 10:47:59 1998 UTC (25 years, 9 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.7: +1 -1 lines
Log Message:
increased limit on continued lines

File Contents

# Content
1 /* Copyright (c) 1995 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Routines for loading and checking variables from file.
9 */
10
11 #include <stdio.h>
12 #include <ctype.h>
13 #include "vars.h"
14
15 #define NOCHAR 127 /* constant for character to delete */
16
17 #ifndef malloc
18 extern char *malloc(), *realloc();
19 #endif
20
21 extern char *fgetline();
22
23
24 loadvars(rfname) /* load variables into vv from file */
25 char *rfname;
26 {
27 FILE *fp;
28 char buf[512];
29 register char *cp;
30
31 if (rfname == NULL)
32 fp = stdin;
33 else if ((fp = fopen(rfname, "r")) == NULL) {
34 perror(rfname);
35 quit(1);
36 }
37 while (fgetline(buf, sizeof(buf), fp) != NULL) {
38 for (cp = buf; *cp; cp++) {
39 switch (*cp) {
40 case '\\':
41 *cp++ = NOCHAR;
42 continue;
43 case '#':
44 *cp = '\0';
45 break;
46 default:
47 continue;
48 }
49 break;
50 }
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 int
62 setvariable(ass, mv) /* assign variable according to string */
63 register char *ass;
64 VARIABLE *(*mv)();
65 {
66 char varname[32];
67 int n;
68 register char *cp;
69 register VARIABLE *vp;
70 register int i;
71
72 while (isspace(*ass)) /* skip leading space */
73 ass++;
74 cp = varname; /* extract name */
75 while (cp < varname+sizeof(varname)-1
76 && *ass && !isspace(*ass) && *ass != '=')
77 *cp++ = *ass++;
78 *cp = '\0';
79 if (!varname[0])
80 return(0); /* no variable name! */
81 /* trim value */
82 while (isspace(*ass) || *ass == '=')
83 ass++;
84 for (n = strlen(ass); n > 0; n--)
85 if (!isspace(ass[n-1]))
86 break;
87 if (!n)
88 return(0); /* no assignment */
89 /* match variable from list */
90 vp = (*mv)(varname);
91 if (vp == NULL)
92 return(-1);
93 /* assign new value */
94 if (i = vp->nass) {
95 cp = vp->value;
96 while (i--)
97 while (*cp++)
98 ;
99 i = cp - vp->value;
100 vp->value = realloc(vp->value, i+n+1);
101 } else
102 vp->value = malloc(n+1);
103 if (vp->value == NULL) {
104 perror(progname);
105 quit(1);
106 }
107 cp = vp->value+i; /* copy value, squeezing spaces */
108 *cp = *ass;
109 for (i = 1; i <= n; i++) {
110 if (ass[i] == NOCHAR)
111 continue;
112 if (isspace(*cp))
113 while (isspace(ass[i]))
114 i++;
115 *++cp = ass[i];
116 }
117 if (isspace(*cp)) /* remove trailing space */
118 *cp = '\0';
119 return(++vp->nass);
120 }
121
122
123 VARIABLE *
124 matchvar(nam) /* match a variable by its name */
125 char *nam;
126 {
127 int n = strlen(nam);
128 register int i;
129
130 for (i = 0; i < NVARS; i++)
131 if (n >= vv[i].nick && !strncmp(nam, vv[i].name, n))
132 return(vv+i);
133 return(NULL);
134 }
135
136
137 char *
138 nvalue(vn, n) /* return nth variable value */
139 register int vn;
140 register int n;
141 {
142 register char *cp;
143
144 if (vval(vn) == NULL | n < 0 | n >= vdef(vn))
145 return(NULL);
146 cp = vval(vn);
147 while (n--)
148 while (*cp++)
149 ;
150 return(cp);
151 }
152
153
154 checkvalues() /* check assignments */
155 {
156 register int i;
157
158 for (i = 0; i < NVARS; i++)
159 if (vv[i].fixval != NULL)
160 (*vv[i].fixval)(vv+i);
161 }
162
163
164 onevalue(vp) /* only one assignment for this variable */
165 register VARIABLE *vp;
166 {
167 if (vp->nass < 2)
168 return;
169 if (!nowarn)
170 fprintf(stderr,
171 "%s: warning - multiple assignment of variable '%s'\n",
172 progname, vp->name);
173 do
174 vp->value += strlen(vp->value)+1;
175 while (--vp->nass > 1);
176 }
177
178
179 catvalues(vp) /* concatenate variable values */
180 register VARIABLE *vp;
181 {
182 register char *cp;
183
184 if (vp->nass < 2)
185 return;
186 for (cp = vp->value; vp->nass > 1; vp->nass--) {
187 while (*cp)
188 cp++;
189 *cp++ = ' ';
190 }
191 }
192
193
194 int
195 badmatch(tv, cv) /* case insensitive truncated comparison */
196 register char *tv, *cv;
197 {
198 if (!*tv) return(1); /* null string cannot match */
199 do
200 if (UPPER(*tv) != *cv++)
201 return(1);
202 while (*++tv);
203 return(0); /* OK */
204 }
205
206
207 boolvalue(vp) /* check boolean for legal values */
208 register VARIABLE *vp;
209 {
210 if (!vp->nass) return;
211 onevalue(vp);
212 switch (UPPER(vp->value[0])) {
213 case 'T':
214 if (badmatch(vp->value, "TRUE")) break;
215 return;
216 case 'F':
217 if (badmatch(vp->value, "FALSE")) break;
218 return;
219 }
220 fprintf(stderr, "%s: illegal value for boolean variable '%s'\n",
221 progname, vp->name);
222 quit(1);
223 }
224
225
226 qualvalue(vp) /* check qualitative var. for legal values */
227 register VARIABLE *vp;
228 {
229 if (!vp->nass) return;
230 onevalue(vp);
231 switch (UPPER(vp->value[0])) {
232 case 'L':
233 if (badmatch(vp->value, "LOW")) break;
234 return;
235 case 'M':
236 if (badmatch(vp->value, "MEDIUM")) break;
237 return;
238 case 'H':
239 if (badmatch(vp->value, "HIGH")) break;
240 return;
241 }
242 fprintf(stderr, "%s: illegal value for qualitative variable '%s'\n",
243 progname, vp->name);
244 quit(1);
245 }
246
247
248 intvalue(vp) /* check integer variable for legal values */
249 register VARIABLE *vp;
250 {
251 if (!vp->nass) return;
252 onevalue(vp);
253 if (isint(vp->value)) return;
254 fprintf(stderr, "%s: illegal value for integer variable '%s'\n",
255 progname, vp->name);
256 quit(1);
257 }
258
259
260 fltvalue(vp) /* check float variable for legal values */
261 register VARIABLE *vp;
262 {
263 if (!vp->nass) return;
264 onevalue(vp);
265 if (isflt(vp->value)) return;
266 fprintf(stderr, "%s: illegal value for real variable '%s'\n",
267 progname, vp->name);
268 quit(1);
269 }
270
271
272 printvars(fp) /* print variable values */
273 register FILE *fp;
274 {
275 int i, j, k, clipline;
276 register char *cp;
277
278 for (i = 0; i < NVARS; i++) /* print each variable */
279 for (j = 0; j < vdef(i); j++) { /* print each assignment */
280 fputs(vnam(i), fp);
281 fputs("= ", fp);
282 k = clipline = ( vv[i].fixval == catvalues ? 64 : 236 )
283 - strlen(vnam(i)) ;
284 cp = nvalue(i, j);
285 while (*cp) {
286 putc(*cp++, fp);
287 if (--k <= 0) { /* line too long */
288 while (*cp && !isspace(*cp))
289 fputc(*cp++, fp); /* finish this word */
290 if (*cp) { /* start new line */
291 if (vv[i].fixval == catvalues) {
292 fputc('\n', fp);
293 fputs(vnam(i), fp);
294 fputc('=', fp);
295 } else
296 fputs(" \\\n", fp);
297 k = clipline;
298 }
299 }
300 }
301 fputc('\n', fp);
302 }
303 fflush(fp);
304 }