1 |
|
#ifndef lint |
2 |
< |
static const char RCSid[] = "$Id$"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
|
* readobj.c - routines for reading in object descriptions. |
7 |
|
* External symbols declared in object.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
/* ==================================================================== |
11 |
< |
* The Radiance Software License, Version 1.0 |
12 |
< |
* |
13 |
< |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
14 |
< |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
15 |
< |
* |
16 |
< |
* Redistribution and use in source and binary forms, with or without |
17 |
< |
* modification, are permitted provided that the following conditions |
18 |
< |
* are met: |
19 |
< |
* |
20 |
< |
* 1. Redistributions of source code must retain the above copyright |
21 |
< |
* notice, this list of conditions and the following disclaimer. |
22 |
< |
* |
23 |
< |
* 2. Redistributions in binary form must reproduce the above copyright |
24 |
< |
* notice, this list of conditions and the following disclaimer in |
25 |
< |
* the documentation and/or other materials provided with the |
26 |
< |
* distribution. |
27 |
< |
* |
28 |
< |
* 3. The end-user documentation included with the redistribution, |
29 |
< |
* if any, must include the following acknowledgment: |
30 |
< |
* "This product includes Radiance software |
31 |
< |
* (http://radsite.lbl.gov/) |
32 |
< |
* developed by the Lawrence Berkeley National Laboratory |
33 |
< |
* (http://www.lbl.gov/)." |
34 |
< |
* Alternately, this acknowledgment may appear in the software itself, |
35 |
< |
* if and wherever such third-party acknowledgments normally appear. |
36 |
< |
* |
37 |
< |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
38 |
< |
* and "The Regents of the University of California" must |
39 |
< |
* not be used to endorse or promote products derived from this |
40 |
< |
* software without prior written permission. For written |
41 |
< |
* permission, please contact [email protected]. |
42 |
< |
* |
43 |
< |
* 5. Products derived from this software may not be called "Radiance", |
44 |
< |
* nor may "Radiance" appear in their name, without prior written |
45 |
< |
* permission of Lawrence Berkeley National Laboratory. |
46 |
< |
* |
47 |
< |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
48 |
< |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
49 |
< |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
50 |
< |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
51 |
< |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
52 |
< |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
53 |
< |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
54 |
< |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
55 |
< |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
56 |
< |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
57 |
< |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
58 |
< |
* SUCH DAMAGE. |
59 |
< |
* ==================================================================== |
60 |
< |
* |
61 |
< |
* This software consists of voluntary contributions made by many |
62 |
< |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
63 |
< |
* information on Lawrence Berkeley National Laboratory, please see |
64 |
< |
* <http://www.lbl.gov/>. |
65 |
< |
*/ |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "standard.h" |
12 |
> |
#include <ctype.h> |
13 |
> |
#include <string.h> |
14 |
> |
#include <stdio.h> |
15 |
|
|
16 |
+ |
#include "platform.h" |
17 |
+ |
#include "paths.h" |
18 |
+ |
#include "standard.h" |
19 |
|
#include "object.h" |
70 |
– |
|
20 |
|
#include "otypes.h" |
21 |
|
|
73 |
– |
#include <ctype.h> |
22 |
|
|
23 |
|
OBJREC *objblock[MAXOBJBLK]; /* our objects */ |
24 |
|
OBJECT nobjects = 0; /* # of objects */ |
25 |
|
|
26 |
|
|
27 |
|
void |
28 |
< |
readobj(inpspec) /* read in an object file or stream */ |
29 |
< |
char *inpspec; |
28 |
> |
readobj( /* read in an object file or stream */ |
29 |
> |
char *inpspec |
30 |
> |
) |
31 |
|
{ |
32 |
|
OBJECT lastobj; |
33 |
|
FILE *infp; |
34 |
< |
char buf[1024]; |
35 |
< |
register int c; |
34 |
> |
char buf[2048]; |
35 |
> |
int c; |
36 |
|
|
37 |
|
lastobj = nobjects; |
38 |
|
if (inpspec == NULL) { |
47 |
|
sprintf(errmsg, "cannot open scene file \"%s\"", inpspec); |
48 |
|
error(SYSTEM, errmsg); |
49 |
|
} |
50 |
+ |
#ifdef getc_unlocked /* avoid stupid semaphores */ |
51 |
+ |
flockfile(infp); |
52 |
+ |
#endif |
53 |
|
while ((c = getc(infp)) != EOF) { |
54 |
|
if (isspace(c)) |
55 |
|
continue; |
66 |
|
} |
67 |
|
if (inpspec[0] == '!') |
68 |
|
pclose(infp); |
69 |
< |
else |
69 |
> |
else if (infp != stdin) |
70 |
|
fclose(infp); |
71 |
+ |
#ifdef getc_unlocked |
72 |
+ |
else |
73 |
+ |
funlockfile(infp); |
74 |
+ |
#endif |
75 |
|
if (nobjects == lastobj) { |
76 |
|
sprintf(errmsg, "(%s): empty file", inpspec); |
77 |
|
error(WARNING, errmsg); |
80 |
|
|
81 |
|
|
82 |
|
void |
83 |
< |
getobject(name, fp) /* read the next object */ |
84 |
< |
char *name; |
85 |
< |
FILE *fp; |
83 |
> |
getobject( /* read the next object */ |
84 |
> |
char *name, |
85 |
> |
FILE *fp |
86 |
> |
) |
87 |
|
{ |
88 |
+ |
#define OALIAS -2 |
89 |
|
OBJECT obj; |
90 |
|
char sbuf[MAXSTR]; |
91 |
|
int rval; |
92 |
< |
register OBJREC *objp; |
92 |
> |
OBJREC *objp; |
93 |
|
|
94 |
|
if ((obj = newobject()) == OVOID) |
95 |
|
error(SYSTEM, "out of object space"); |
97 |
|
/* get modifier */ |
98 |
|
strcpy(sbuf, "EOF"); |
99 |
|
fgetword(sbuf, MAXSTR, fp); |
100 |
+ |
if (strchr(sbuf, '\t')) { |
101 |
+ |
sprintf(errmsg, "(%s): illegal tab in modifier \"%s\"", |
102 |
+ |
name, sbuf); |
103 |
+ |
error(USER, errmsg); |
104 |
+ |
} |
105 |
|
if (!strcmp(sbuf, VOIDID)) |
106 |
|
objp->omod = OVOID; |
107 |
+ |
else if (!strcmp(sbuf, ALIASMOD)) |
108 |
+ |
objp->omod = OALIAS; |
109 |
|
else if ((objp->omod = modifier(sbuf)) == OVOID) { |
110 |
|
sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf); |
111 |
|
error(USER, errmsg); |
113 |
|
/* get type */ |
114 |
|
strcpy(sbuf, "EOF"); |
115 |
|
fgetword(sbuf, MAXSTR, fp); |
116 |
< |
if (!strcmp(sbuf, ALIASID)) |
152 |
< |
objp->otype = -1; |
153 |
< |
else if ((objp->otype = otype(sbuf)) < 0) { |
116 |
> |
if ((objp->otype = otype(sbuf)) < 0) { |
117 |
|
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); |
118 |
|
error(USER, errmsg); |
119 |
|
} |
120 |
|
/* get identifier */ |
121 |
|
sbuf[0] = '\0'; |
122 |
|
fgetword(sbuf, MAXSTR, fp); |
123 |
+ |
if (strchr(sbuf, '\t')) { |
124 |
+ |
sprintf(errmsg, "(%s): illegal tab in identifier \"%s\"", |
125 |
+ |
name, sbuf); |
126 |
+ |
error(USER, errmsg); |
127 |
+ |
} |
128 |
|
objp->oname = savqstr(sbuf); |
129 |
|
/* get arguments */ |
130 |
< |
if (objp->otype == -1) { |
131 |
< |
register OBJECT alias; |
130 |
> |
if (objp->otype == MOD_ALIAS) { |
131 |
> |
OBJECT alias; |
132 |
|
strcpy(sbuf, "EOF"); |
133 |
|
fgetword(sbuf, MAXSTR, fp); |
134 |
|
if ((alias = modifier(sbuf)) == OVOID) { |
135 |
< |
sprintf(errmsg, |
136 |
< |
"(%s): bad reference \"%s\" for %s \"%s\"", |
137 |
< |
name, sbuf, ALIASID, objp->oname); |
170 |
< |
error(USER, errmsg); |
135 |
> |
sprintf(errmsg, "(%s): bad reference \"%s\"", |
136 |
> |
name, sbuf); |
137 |
> |
objerror(objp, USER, errmsg); |
138 |
|
} |
139 |
< |
objp->otype = objptr(alias)->otype; |
140 |
< |
copystruct(&objp->oargs, &objptr(alias)->oargs); |
139 |
> |
if (objp->omod == OALIAS || |
140 |
> |
objp->omod == objptr(alias)->omod) { |
141 |
> |
objp->omod = alias; |
142 |
> |
} else { |
143 |
> |
objp->oargs.sarg = (char **)malloc(sizeof(char *)); |
144 |
> |
if (objp->oargs.sarg == NULL) |
145 |
> |
error(SYSTEM, "out of memory in getobject"); |
146 |
> |
objp->oargs.nsargs = 1; |
147 |
> |
objp->oargs.sarg[0] = savestr(sbuf); |
148 |
> |
} |
149 |
|
} else if ((rval = readfargs(&objp->oargs, fp)) == 0) { |
150 |
|
sprintf(errmsg, "(%s): bad arguments", name); |
151 |
|
objerror(objp, USER, errmsg); |
153 |
|
sprintf(errmsg, "(%s): error reading scene", name); |
154 |
|
error(SYSTEM, errmsg); |
155 |
|
} |
156 |
+ |
if (objp->omod == OALIAS) { |
157 |
+ |
sprintf(errmsg, "(%s): inappropriate use of '%s' modifier", |
158 |
+ |
name, ALIASMOD); |
159 |
+ |
objerror(objp, USER, errmsg); |
160 |
+ |
} |
161 |
|
/* initialize */ |
162 |
|
objp->os = NULL; |
163 |
|
|
164 |
|
insertobject(obj); /* add to global structure */ |
165 |
+ |
#undef OALIAS |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
< |
int |
170 |
< |
newobject() /* get a new object */ |
169 |
> |
OBJECT |
170 |
> |
newobject(void) /* get a new object */ |
171 |
|
{ |
172 |
< |
register int i; |
172 |
> |
int i; |
173 |
|
|
174 |
|
if ((nobjects & (OBJBLKSIZ-1)) == 0) { /* new block */ |
175 |
|
errno = 0; |
184 |
|
} |
185 |
|
|
186 |
|
void |
187 |
< |
freeobjects(firstobj, nobjs) /* free a range of objects */ |
188 |
< |
OBJECT firstobj, nobjs; |
187 |
> |
freeobjects( /* free a range of objects */ |
188 |
> |
int firstobj, |
189 |
> |
int nobjs |
190 |
> |
) |
191 |
|
{ |
192 |
< |
register int obj; |
192 |
> |
int obj; |
193 |
|
/* check bounds */ |
194 |
|
if (firstobj < 0) |
195 |
|
return; |
199 |
|
return; |
200 |
|
/* clear objects */ |
201 |
|
for (obj = firstobj+nobjs; obj-- > firstobj; ) { |
202 |
< |
register OBJREC *o = objptr(obj); |
202 |
> |
OBJREC *o = objptr(obj); |
203 |
|
free_os(o); /* free client memory */ |
204 |
|
freeqstr(o->oname); |
205 |
|
freefargs(&o->oargs); |
206 |
< |
bzero(o, sizeof(OBJREC)); |
206 |
> |
memset((void *)o, '\0', sizeof(OBJREC)); |
207 |
|
} |
225 |
– |
clearobjndx(); |
208 |
|
/* free objects off end */ |
209 |
|
for (obj = nobjects; obj-- > 0; ) |
210 |
|
if (objptr(obj)->oname != NULL) |
211 |
|
break; |
212 |
< |
++obj; |
212 |
> |
if (++obj >= nobjects) |
213 |
> |
return; |
214 |
|
while (nobjects > obj) /* free empty end blocks */ |
215 |
|
if ((--nobjects & (OBJBLKSIZ-1)) == 0) { |
216 |
|
int i = nobjects >> OBJBLKSHFT; |
217 |
|
free((void *)objblock[i]); |
218 |
|
objblock[i] = NULL; |
219 |
|
} |
220 |
+ |
truncobjndx(); /* truncate modifier look-up */ |
221 |
|
} |