ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/readobj.c
Revision: 2.4
Committed: Sat Jan 1 09:18:47 1994 UTC (30 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +18 -12 lines
Log Message:
added warning about empty input

File Contents

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