ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/loadvars.c
Revision: 2.11
Committed: Wed Apr 23 00:52:33 2003 UTC (21 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +1 -1 lines
Log Message:
Added (void *) cast to realloc calls

File Contents

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