ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/readobj.c
Revision: 1.6
Committed: Fri Jul 19 09:25:43 1991 UTC (32 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +5 -55 lines
Log Message:
added separate readfargs() and freefargs() routines

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1986 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * readobj.c - routines for reading in object descriptions.
9     *
10     * 7/28/85
11     */
12    
13     #include "standard.h"
14    
15     #include "object.h"
16    
17     #include "otypes.h"
18    
19     #include <ctype.h>
20    
21     OBJREC *objblock[MAXOBJBLK]; /* our objects */
22 greg 1.4 OBJECT nobjects = 0; /* # of objects */
23 greg 1.1
24    
25     readobj(input) /* read in an object file or stream */
26     char *input;
27     {
28     FILE *popen();
29 greg 1.2 char *fgets(), *fgetline();
30 greg 1.1 FILE *infp;
31     char buf[512];
32     register int c;
33    
34     if (input == NULL) {
35     infp = stdin;
36     input = "standard input";
37     } else if (input[0] == '!') {
38     if ((infp = popen(input+1, "r")) == NULL) {
39     sprintf(errmsg, "cannot execute \"%s\"", input);
40     error(SYSTEM, errmsg);
41     }
42     } else if ((infp = fopen(input, "r")) == NULL) {
43     sprintf(errmsg, "cannot open scene file \"%s\"", input);
44     error(SYSTEM, errmsg);
45     }
46     while ((c = getc(infp)) != EOF) {
47     if (isspace(c))
48     continue;
49     if (c == '#') { /* comment */
50     fgets(buf, sizeof(buf), infp);
51 greg 1.2 } else if (c == '!') { /* command */
52 greg 1.1 ungetc(c, infp);
53 greg 1.2 fgetline(buf, sizeof(buf), infp);
54 greg 1.1 readobj(buf);
55     } else { /* object */
56     ungetc(c, infp);
57     getobject(input, infp);
58     }
59     }
60     if (input[0] == '!')
61     pclose(infp);
62     else
63     fclose(infp);
64     }
65    
66    
67     getobject(name, fp) /* read the next object */
68     char *name;
69     FILE *fp;
70     {
71     OBJECT obj;
72     char sbuf[MAXSTR];
73 greg 1.6 int rval;
74 greg 1.1 register OBJREC *objp;
75    
76     if ((obj = newobject()) == OVOID)
77     error(SYSTEM, "out of object space");
78     objp = objptr(obj);
79     /* get modifier */
80     fscanf(fp, "%s", sbuf);
81     if (!strcmp(sbuf, VOIDID))
82     objp->omod = OVOID;
83     else if ((objp->omod = modifier(sbuf)) == OVOID) {
84     sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf);
85     error(USER, errmsg);
86     }
87     /* get type */
88     fscanf(fp, "%s", sbuf);
89     if (!strcmp(sbuf, ALIASID))
90     objp->otype = -1;
91     else if ((objp->otype = otype(sbuf)) < 0) {
92     sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf);
93     error(USER, errmsg);
94     }
95     /* get identifier */
96     fscanf(fp, "%s", sbuf);
97     objp->oname = savqstr(sbuf);
98     /* get arguments */
99     if (objp->otype == -1) {
100     register OBJECT alias;
101     fscanf(fp, "%s", sbuf);
102     if ((alias = modifier(sbuf)) == OVOID) {
103     sprintf(errmsg,
104     "(%s): bad reference \"%s\" for %s \"%s\"",
105     name, sbuf, ALIASID, objp->oname);
106     error(USER, errmsg);
107     }
108     objp->otype = objptr(alias)->otype;
109 greg 1.3 copystruct(&objp->oargs, &objptr(alias)->oargs);
110 greg 1.6 } else if ((rval = readfargs(&objp->oargs, fp)) == 0) {
111 greg 1.1 sprintf(errmsg, "(%s): bad arguments", name);
112     objerror(objp, USER, errmsg);
113 greg 1.6 } else if (rval < 0) {
114     sprintf(errmsg, "(%s): error reading scene", name);
115     error(SYSTEM, errmsg);
116 greg 1.1 }
117     /* initialize */
118     objp->os = NULL;
119     objp->lastrno = -1;
120    
121     insertobject(obj); /* add to global structure */
122     }
123    
124    
125     int
126     newobject() /* get a new object */
127     {
128     register int i;
129    
130     if ((nobjects & 077) == 0) { /* new block */
131     errno = 0;
132     i = nobjects >> 6;
133     if (i >= MAXOBJBLK)
134     return(OVOID);
135 greg 1.5 objblock[i] = (OBJREC *)bmalloc(0100*sizeof(OBJREC));
136 greg 1.1 if (objblock[i] == NULL)
137     return(OVOID);
138     }
139     return(nobjects++);
140     }