ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/meta/mgvars.c
Revision: 1.3
Committed: Mon Oct 27 10:28:59 2003 UTC (21 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 1.2: +6 -6 lines
Log Message:
Various compatibility fixes.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 1.3 static const char RCSid[] = "$Id: mgvars.c,v 1.2 2003/08/01 14:14:24 schorsch Exp $";
3 greg 1.1 #endif
4     /*
5     * mgvars.c - routines dealing with graph variables.
6     *
7     * 6/23/86
8     *
9     * Greg Ward Larson
10     */
11    
12     #include <stdio.h>
13     #include <stdlib.h>
14     #include <math.h>
15     #include <ctype.h>
16    
17 schorsch 1.3 #include "rtprocess.h"
18     #include "rtmisc.h"
19     #include "rtio.h"
20 greg 1.1 #include "mgvars.h"
21    
22     #define MAXLINE 512
23    
24     #define isid(c) (isalnum(c) || (c) == '_' || (c) == '.')
25    
26     #define isnum(c) (isdigit(c)||(c)=='-'||(c)=='+'||(c)=='.'||(c)=='e'||(c)=='E')
27    
28 schorsch 1.3 char *findfile();
29    
30 greg 1.1 extern char *progname, *libpath[];
31    
32     #ifdef DCL_ATOF
33     double atof();
34     #endif
35    
36     IVAR *ivhead = NULL; /* intermediate variables */
37    
38     VARIABLE gparam[NVARS] = { /* standard variables */
39     { "fthick", REAL, "frame thickness" },
40     { "grid", REAL, "grid on?" },
41     { "legend", STRING, "legend title" },
42     { "othick", REAL, "origin thickness" },
43     { "period", REAL, "period of polar plot" },
44     { "subtitle", STRING },
45     { "symfile", STRING, "symbol file" },
46     { "tstyle", REAL, "tick mark style" },
47     { "title", STRING },
48     { "xlabel", STRING },
49     { "xmap", FUNCTION, "x axis mapping function" },
50     { "xmax", REAL },
51     { "xmin", REAL },
52     { "xstep", REAL },
53     { "ylabel", STRING },
54     { "ymap", FUNCTION, "y axis mapping function" },
55     { "ymax", REAL },
56     { "ymin", REAL },
57     { "ystep", REAL },
58     };
59    
60     VARIABLE cparam[MAXCUR][NCVARS] = { /* curve variables */
61     {
62     { "A", FUNCTION, "function for curve A" },
63     { "Acolor", REAL, "color for A" },
64     { "Adata", DATA, "point data for A" },
65     { "Alabel", STRING },
66     { "Alintype", REAL, "line type for A" },
67     { "Anpoints", REAL, "number of points for A" },
68     { "Asymsize", REAL, "symbol size for A" },
69     { "Asymtype", STRING, "symbol type for A" },
70     { "Athick", REAL, "line thickness for A" },
71     }, {
72     { "B", FUNCTION, "function for curve B" },
73     { "Bcolor", REAL, "color for B" },
74     { "Bdata", DATA, "point data for B" },
75     { "Blabel", STRING },
76     { "Blintype", REAL, "line type for B" },
77     { "Bnpoints", REAL, "number of points for B" },
78     { "Bsymsize", REAL, "symbol size for B" },
79     { "Bsymtype", STRING, "symbol type for B" },
80     { "Bthick", REAL, "line thickness for B" },
81     }, {
82     { "C", FUNCTION, "function for curve C" },
83     { "Ccolor", REAL, "color for C" },
84     { "Cdata", DATA, "point data for C" },
85     { "Clabel", STRING },
86     { "Clintype", REAL, "line type for C" },
87     { "Cnpoints", REAL, "number of points for C" },
88     { "Csymsize", REAL, "symbol size for C" },
89     { "Csymtype", STRING, "symbol type for C" },
90     { "Cthick", REAL, "line thickness for C" },
91     }, {
92     { "D", FUNCTION, "function for curve D" },
93     { "Dcolor", REAL, "color for D" },
94     { "Ddata", DATA, "point data for D" },
95     { "Dlabel", STRING },
96     { "Dlintype", REAL, "line type for D" },
97     { "Dnpoints", REAL, "number of points for D" },
98     { "Dsymsize", REAL, "symbol size for D" },
99     { "Dsymtype", STRING, "symbol type for D" },
100     { "Dthick", REAL, "line thickness for D" },
101     }, {
102     { "E", FUNCTION, "function for curve E" },
103     { "Ecolor", REAL, "color for E" },
104     { "Edata", DATA, "point data for E" },
105     { "Elabel", STRING },
106     { "Elintype", REAL, "line type for E" },
107     { "Enpoints", REAL, "number of points for E" },
108     { "Esymsize", REAL, "symbol size for E" },
109     { "Esymtype", STRING, "symbol type for E" },
110     { "Ethick", REAL, "line thickness for E" },
111     }, {
112     { "F", FUNCTION, "function for curve F" },
113     { "Fcolor", REAL, "color for F" },
114     { "Fdata", DATA, "point data for F" },
115     { "Flabel", STRING },
116     { "Flintype", REAL, "line type for F" },
117     { "Fnpoints", REAL, "number of points for F" },
118     { "Fsymsize", REAL, "symbol size for F" },
119     { "Fsymtype", STRING, "symbol type for F" },
120     { "Fthick", REAL, "line thickness for F" },
121     }, {
122     { "G", FUNCTION, "function for curve G" },
123     { "Gcolor", REAL, "color for G" },
124     { "Gdata", DATA, "point data for G" },
125     { "Glabel", STRING },
126     { "Glintype", REAL, "line type for G" },
127     { "Gnpoints", REAL, "number of points for G" },
128     { "Gsymsize", REAL, "symbol size for G" },
129     { "Gsymtype", STRING, "symbol type for G" },
130     { "Gthick", REAL, "line thickness for G" },
131     }, {
132     { "H", FUNCTION, "function for curve H" },
133     { "Hcolor", REAL, "color for H" },
134     { "Hdata", DATA, "point data for H" },
135     { "Hlabel", STRING },
136     { "Hlintype", REAL, "line type for H" },
137     { "Hnpoints", REAL, "number of points for H" },
138     { "Hsymsize", REAL, "symbol size for H" },
139     { "Hsymtype", STRING, "symbol type for H" },
140     { "Hthick", REAL, "line thickness for H" },
141     },
142     };
143    
144    
145     mgclearall() /* clear all variable settings */
146     {
147     int j;
148     register IVAR *iv;
149     register int i;
150    
151     for (iv = ivhead; iv != NULL; iv = iv->next) {
152     dremove(iv->name);
153     freestr(iv->name);
154     freestr(iv->dfn);
155     efree((char *)iv);
156     }
157     ivhead = NULL;
158    
159     for (i = 0; i < NVARS; i++)
160     if (gparam[i].flags & DEFINED)
161     undefine(&gparam[i]);
162    
163     for (j = 0; j < MAXCUR; j++)
164     for (i = 0; i < NCVARS; i++)
165     if (cparam[j][i].flags & DEFINED)
166     undefine(&cparam[j][i]);
167     }
168    
169    
170     mgload(file) /* load a file */
171     char *file;
172     {
173     FILE *fp;
174     char sbuf[MAXLINE], *fgets();
175     int inquote;
176     register char *cp, *cp2;
177    
178     if (file == NULL) {
179     fp = stdin;
180     file = "<stdin>";
181     } else if ((fp = fopen(file, "r")) == NULL) {
182     fprintf(stderr, "%s: Cannot open: %s\n", progname, file);
183     quit(1);
184     }
185     while (fgets(sbuf+1, sizeof(sbuf)-1, fp) != NULL) {
186     inquote = 0;
187     cp2 = sbuf;
188     for (cp = sbuf+1; *cp; cp++) /* condition the input line */
189     switch (*cp) {
190     case '#':
191     if (!inquote) {
192     cp[0] = '\n';
193     cp[1] = '\0';
194     break;
195     }
196     *cp2++ = *cp;
197     break;
198     case '"':
199     inquote = !inquote;
200     break;
201     case '\\':
202     if (!cp[1])
203     break;
204     if (cp[1] == '\n') {
205     cp[0] = '\0';
206     fgets(cp, sizeof(sbuf)-(cp-sbuf), fp);
207     cp--;
208     break;
209     }
210     *cp2++ = *++cp;
211     break;
212     case ' ':
213     case '\t':
214     case '\n':
215     if (!inquote)
216     break;
217     *cp2++ = *cp;
218     break;
219     default:
220     *cp2++ = *cp;
221     break;
222     }
223     *cp2 = '\0';
224     if (inquote) {
225     fputs(sbuf, stderr);
226     fprintf(stderr, "%s: %s: Missing quote\n",
227     progname, file);
228     quit(1);
229     }
230     if (sbuf[0])
231     setmgvar(file, fp, sbuf);
232     }
233     if (fp != stdin)
234     fclose(fp);
235     }
236    
237    
238     mgsave(file) /* save our variables */
239     char *file;
240     {
241     FILE *fp;
242     int j;
243     register IVAR *iv;
244     register int i;
245    
246     if (file == NULL)
247     fp = stdout;
248     else if ((fp = fopen(file, "w")) == NULL) {
249     fprintf(stderr, "%s: Cannot write: %s\n", progname, file);
250     quit(1);
251     }
252     for (iv = ivhead; iv != NULL; iv = iv->next)
253     fprintf(fp, "%s\n", iv->dfn);
254    
255     for (i = 0; i < NVARS; i++)
256     if (gparam[i].flags & DEFINED)
257     mgprint(&gparam[i], fp);
258    
259     for (j = 0; j < MAXCUR; j++)
260     for (i = 0; i < NCVARS; i++)
261     if (cparam[j][i].flags & DEFINED)
262     mgprint(&cparam[j][i], fp);
263    
264     if (fp != stdout)
265     fclose(fp);
266     }
267    
268    
269     setmgvar(fname, fp, string) /* set a variable */
270     char *fname;
271     FILE *fp;
272     char *string;
273     {
274     char name[128];
275     FILE *fp2;
276     register int i;
277     register char *s;
278     register VARIABLE *vp;
279    
280     if (!strncmp(string, "include=", 8)) { /* include file */
281     if ((s = findfile(string+8, libpath)) == NULL) {
282     fprintf(stderr, "%s\n", string);
283     fprintf(stderr, "%s: %s: File not found: %s\n",
284     progname, fname, string+8);
285     quit(1);
286     }
287     strcpy(name, s);
288     mgload(name);
289     return;
290     }
291     s = string;
292     i = 0;
293     while (i < sizeof(name)-1 && isid(*s))
294     name[i++] = *s++;
295     name[i] = '\0';
296     vp = vlookup(name);
297     if (vp != NULL) {
298     undefine(vp);
299     switch (vp->type) {
300     case REAL:
301     case FUNCTION:
302     if ((*s == '(') != (vp->type == FUNCTION)) {
303     fprintf(stderr, "%s\n", string);
304     fprintf(stderr,
305     "%s: %s: Bad %s declaration: %s\n",
306     progname, fname,
307     vp->type == FUNCTION ?
308     "function" : "variable",
309     name);
310     quit(1);
311     }
312     scompile(string, fname, 0);
313     vp->v.dfn = savestr(string);
314     break;
315     case STRING:
316     if (*s++ != '=') {
317     fprintf(stderr, "%s\n", string);
318     fprintf(stderr, "%s: %s: Missing '='\n",
319     progname, fname);
320     quit(1);
321     }
322     vp->v.s = savestr(s);
323     break;
324     case DATA:
325     if (*s++ != '=') {
326     fprintf(stderr, "%s\n", string);
327     fprintf(stderr, "%s: %s: Missing '='\n",
328     progname, fname);
329     quit(1);
330     }
331     if (!*s) {
332     loaddata(fname, fp, &vp->v.d);
333     } else if (*s == '!') {
334     if ((fp2 = popen(s+1, "r")) == NULL) {
335     fprintf(stderr, "%s\n", string);
336     fprintf(stderr,
337     "%s: %s: Cannot execute: %s\n",
338     progname, fname, s+1);
339     quit(1);
340     }
341     loaddata(s, fp2, &vp->v.d);
342     pclose(fp2);
343     } else {
344     if ((fp2 = fopen(s, "r")) == NULL) {
345     fprintf(stderr, "%s\n", string);
346     fprintf(stderr,
347     "%s: %s: Data file not found: %s\n",
348     progname, fname, s);
349     quit(1);
350     }
351     loaddata(s, fp2, &vp->v.d);
352     fclose(fp2);
353     }
354     break;
355     }
356     vp->flags |= DEFINED;
357     } else
358     setivar(name, fname, string); /* intermediate */
359     }
360    
361    
362     setivar(vname, fname, definition) /* set an intermediate variable */
363     char *vname;
364     char *fname;
365     char *definition;
366     {
367     IVAR ivbeg;
368     register IVAR *iv;
369    
370     scompile(definition, fname, 0); /* compile the string */
371    
372     ivbeg.next = ivhead;
373     for (iv = &ivbeg; iv->next != NULL; iv = iv->next)
374     if (!strcmp(vname, iv->next->name)) {
375     iv = iv->next;
376     freestr(iv->dfn);
377     iv->dfn = savestr(definition);
378     return;
379     }
380    
381     iv->next = (IVAR *)emalloc(sizeof(IVAR));
382     iv = iv->next;
383     iv->name = savestr(vname);
384     iv->dfn = savestr(definition);
385     iv->next = NULL;
386     ivhead = ivbeg.next;
387     }
388    
389    
390     mgtoa(s, vp) /* get a variable's value in ascii form */
391     register char *s;
392     VARIABLE *vp;
393     {
394     register char *sv;
395    
396     if (!(vp->flags & DEFINED)) {
397     strcpy(s, "UNDEFINED");
398     return;
399     }
400     switch (vp->type) {
401     case REAL:
402     case FUNCTION:
403     sv = vp->v.dfn;
404     while (*sv != '=' && *sv != ':')
405     sv++;
406     while (*++sv && *sv != ';')
407     *s++ = *sv;
408     *s = '\0';
409     break;
410     case STRING:
411     strcpy(s, vp->v.s);
412     break;
413     case DATA:
414     strcpy(s, "DATA");
415     break;
416     }
417     }
418    
419    
420     mgprint(vp, fp) /* print a variable definition */
421     register VARIABLE *vp;
422     FILE *fp;
423     {
424     register int i;
425    
426     switch (vp->type) {
427     case REAL:
428     case FUNCTION:
429     fprintf(fp, "%s\n", vp->v.dfn);
430     break;
431     case STRING:
432     fprintf(fp, "%s=\"", vp->name);
433     for (i = 0; vp->v.s[i]; i++)
434     switch (vp->v.s[i]) {
435     case '"':
436     case '\\':
437     putc('\\', fp);
438     /* fall through */
439     default:
440     putc(vp->v.s[i], fp);
441     break;
442     }
443     fprintf(fp, "\"\n");
444     break;
445     case DATA:
446     fprintf(fp, "%s=", vp->name);
447     for (i = 0; i < vp->v.d.size; i++) {
448     if (i % 4 == 0)
449     fprintf(fp, "\n");
450     fprintf(fp, "\t%10e", vp->v.d.data[i]);
451     }
452     fprintf(fp, "\n;\n");
453     break;
454     }
455     }
456    
457    
458     undefine(vp) /* undefine a variable */
459     register VARIABLE *vp;
460     {
461     if (vp == NULL || !(vp->flags & DEFINED))
462     return;
463    
464     switch (vp->type) {
465     case REAL:
466     case FUNCTION:
467     dremove(vp->name);
468     freestr(vp->v.dfn);
469     break;
470     case STRING:
471     freestr(vp->v.s);
472     break;
473     case DATA:
474     efree((char *)vp->v.d.data);
475     break;
476     }
477     vp->flags &= ~DEFINED;
478     }
479    
480    
481     VARIABLE *
482     vlookup(vname) /* look up a variable by its name */
483     char *vname;
484     {
485     register int i;
486     register VARIABLE *vp;
487    
488     i = vname[0] - 'A';
489     if (i >= 0 && i < MAXCUR) /* curve variables */
490     for (vp = cparam[i], i = 0; i < NCVARS; vp++, i++)
491     if (!strcmp(vp->name, vname))
492     return(vp);
493     /* standard variables */
494     for (vp = gparam; vp < &gparam[NVARS]; vp++)
495     if (!strcmp(vp->name, vname))
496     return(vp);
497     return(NULL); /* not found */
498     }
499    
500    
501     loaddata(fname, fp, dp) /* load data from a stream */
502     char *fname;
503     FILE *fp;
504     register DARRAY *dp;
505     {
506     char sbuf[MAXLINE], *fgets();
507     register char *cp;
508    
509     dp->size = 0;
510     dp->data = NULL;
511     while (fgets(sbuf, sizeof(sbuf), fp) != NULL) {
512     cp = sbuf;
513     while (*cp) {
514     while (isspace(*cp) || *cp == ',')
515     cp++;
516     if (isnum(*cp)) {
517     dp->data = (float *)erealloc((char *)dp->data,
518     (dp->size+1)*sizeof(float));
519     dp->data[dp->size++] = atof(cp);
520     do
521     cp++;
522     while (isnum(*cp));
523     } else if (*cp == ';') {
524     return;
525     } else if (*cp) {
526     fputs(sbuf, stderr);
527     fprintf(stderr, "%s: %s: Bad data\n",
528     progname, fname);
529     quit(1);
530     }
531     }
532     }
533     }
534    
535    
536     char *
537     findfile(fname, pathlist) /* find the file fname, return full path */
538     char *fname;
539     register char **pathlist;
540     {
541     static char ffname[128];
542     char *strcpy(), *strcat();
543     register int fd;
544    
545     if (fname[0] == '/')
546     return(fname);
547    
548     while (*pathlist != NULL) {
549     strcpy(ffname, *pathlist);
550     strcat(ffname, fname);
551     if ((fd = open(ffname, 0)) != -1) {
552     close(fd);
553     return(ffname);
554     }
555     pathlist++;
556     }
557     return(NULL);
558     }
559    
560    
561     int
562     mgcurve(c, f) /* get a curve's (unmapped) values */
563     int c;
564     int (*f)();
565     {
566     int nargs;
567     double x[2], step;
568     register VARIABLE *cv;
569     register float *p;
570     register int npts;
571    
572     if (c < 0 || c >= MAXCUR)
573     return(-1);
574     cv = cparam[c];
575    
576     if (cv[C].flags & DEFINED) { /* function or map */
577    
578     nargs = fundefined(cv[C].name);
579     if (nargs < 1 || nargs > 2) {
580     fprintf(stderr, "%s: bad # of arguments for '%c'\n",
581     progname, c+'A');
582     quit(1);
583     }
584    
585     if (cv[CDATA].flags & DEFINED) { /* map */
586     npts = cv[CDATA].v.d.size / nargs;
587     p = cv[CDATA].v.d.data;
588     while (npts--) {
589     x[0] = *p++;
590     if (nargs == 2)
591     x[1] = *p++;
592     (*f)(c, x[0],
593     funvalue(cv[C].name, nargs, x));
594     }
595     npts = cv[CDATA].v.d.size / nargs;
596     } else if ( nargs == 1 && /* function */
597     gparam[XMIN].flags & DEFINED &&
598     gparam[XMAX].flags & DEFINED &&
599     cv[CNPOINTS].flags & DEFINED ) {
600     npts = varvalue(cv[CNPOINTS].name);
601     if (npts > 1)
602     step = (varvalue(gparam[XMAX].name) -
603     varvalue(gparam[XMIN].name)) /
604     (npts - 1);
605     else
606     step = 0.0;
607     for (x[0] = varvalue(gparam[XMIN].name);
608     npts--; x[0] += step)
609     (*f)(c, x[0],
610     funvalue(cv[C].name, 1, x));
611     npts = varvalue(cv[CNPOINTS].name);
612     } else {
613     fprintf(stderr,
614     "%s: function '%c' needs %cdata or xmin, xmax, %cnpoints\n",
615     progname, c+'A', c+'A', c+'A');
616     quit(1);
617     }
618    
619     } else if (cv[CDATA].flags & DEFINED) { /* data */
620    
621     npts = cv[CDATA].v.d.size / 2;
622     p = cv[CDATA].v.d.data;
623     while (npts--) {
624     (*f)(c, p[0], p[1]);
625     p += 2;
626     }
627     npts = cv[CDATA].v.d.size / 2;
628    
629     } else
630    
631     npts = 0;
632    
633     return(npts);
634     }