ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/readobj.c
(Generate patch)

Comparing ray/src/common/readobj.c (file contents):
Revision 1.1 by greg, Thu Feb 2 10:34:39 1989 UTC vs.
Revision 2.31 by greg, Tue Jun 24 21:04:16 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  readobj.c - routines for reading in object descriptions.
6   *
7 < *     7/28/85
7 > *  External symbols declared in object.h
8   */
9  
10 < #include  "standard.h"
10 > #include "copyright.h"
11  
12 < #include  "object.h"
12 > #include  <ctype.h>
13  
14 + #include  "platform.h"
15 + #include  "standard.h"
16 + #include  "object.h"
17   #include  "otypes.h"
18  
19 < #include  <ctype.h>
19 > #ifndef OBJMEMOPT
20 > #define OBJMEMOPT       0               /* optimize object block memory? */
21 > #endif
22  
23   OBJREC  *objblock[MAXOBJBLK];           /* our objects */
24 < int  nobjects = 0;                      /* # of objects */
24 > OBJECT  nobjects = 0;                   /* # of objects */
25  
26  
27 < readobj(input)                  /* read in an object file or stream */
28 < char  *input;
27 > void
28 > readobj(                                /* read in an object file or stream */
29 >        char  *inpspec
30 > )
31   {
32 <        FILE  *popen();
29 <        char  *fgets();
32 >        OBJECT  lastobj;
33          FILE  *infp;
34 <        char  buf[512];
35 <        register int  c;
34 >        char  buf[2048];
35 >        int  c;
36  
37 <        if (input == NULL) {
37 >        lastobj = nobjects;
38 >        if (inpspec == NULL) {
39                  infp = stdin;
40 <                input = "standard input";
41 <        } else if (input[0] == '!') {
42 <                if ((infp = popen(input+1, "r")) == NULL) {
43 <                        sprintf(errmsg, "cannot execute \"%s\"", input);
40 >                inpspec = "standard input";
41 >        } else if (inpspec[0] == '!') {
42 >                if ((infp = popen(inpspec+1, "r")) == NULL) {
43 >                        sprintf(errmsg, "cannot execute \"%s\"", inpspec);
44                          error(SYSTEM, errmsg);
45                  }
46 <        } else if ((infp = fopen(input, "r")) == NULL) {
47 <                sprintf(errmsg, "cannot open scene file \"%s\"", input);
46 >        } else if ((infp = fopen(inpspec, "r")) == 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;
56                  if (c == '#') {                         /* comment */
57                          fgets(buf, sizeof(buf), infp);
58 <                } else if (c == '!') {                  /* pipe */
58 >                } else if (c == '!') {                  /* command */
59                          ungetc(c, infp);
60 <                        fgets(buf, sizeof(buf), infp);
54 <                        c = strlen(buf);
55 <                        if (buf[c-1] == '\n')
56 <                                buf[c-1] = '\0';
60 >                        fgetline(buf, sizeof(buf), infp);
61                          readobj(buf);
62                  } else {                                /* object */
63                          ungetc(c, infp);
64 <                        getobject(input, infp);
64 >                        getobject(inpspec, infp);
65                  }
66          }
67 <        if (input[0] == '!')
68 <                pclose(infp);
69 <        else
67 >        if (inpspec[0] == '!') {
68 >                if (pclose(infp) != 0) {
69 >                        sprintf(errmsg, "bad status from \"%s\"", inpspec);
70 >                        error(WARNING, errmsg);
71 >                }
72 >        } else if (infp != stdin)
73                  fclose(infp);
74 + #ifdef getc_unlocked
75 +        else
76 +                funlockfile(infp);
77 + #endif
78 +        if (nobjects == lastobj) {
79 +                sprintf(errmsg, "(%s): empty file", inpspec);
80 +                error(WARNING, errmsg);
81 +        }
82   }
83  
84  
85 < getobject(name, fp)                     /* read the next object */
86 < char  *name;
87 < FILE  *fp;
85 > void
86 > getobject(                              /* read the next object */
87 >        char  *name,
88 >        FILE  *fp
89 > )
90   {
91 + #define OALIAS  -2
92          OBJECT  obj;
93          char  sbuf[MAXSTR];
94 <        register OBJREC  *objp;
94 >        int  rval;
95 >        OBJREC  *objp;
96  
97          if ((obj = newobject()) == OVOID)
98                  error(SYSTEM, "out of object space");
99          objp = objptr(obj);
100                                          /* get modifier */
101 <        fscanf(fp, "%s", sbuf);
101 >        strcpy(sbuf, "EOF");
102 >        fgetword(sbuf, MAXSTR, fp);
103 >        if (strchr(sbuf, '\t')) {
104 >                sprintf(errmsg, "(%s): illegal tab in modifier \"%s\"",
105 >                                        name, sbuf);
106 >                error(USER, errmsg);
107 >        }
108          if (!strcmp(sbuf, VOIDID))
109                  objp->omod = OVOID;
110 +        else if (!strcmp(sbuf, ALIASMOD))
111 +                objp->omod = OALIAS;
112          else if ((objp->omod = modifier(sbuf)) == OVOID) {
113                  sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf);
114                  error(USER, errmsg);
115          }
116                                          /* get type */
117 <        fscanf(fp, "%s", sbuf);
118 <        if (!strcmp(sbuf, ALIASID))
119 <                objp->otype = -1;
93 <        else if ((objp->otype = otype(sbuf)) < 0) {
117 >        strcpy(sbuf, "EOF");
118 >        fgetword(sbuf, MAXSTR, fp);
119 >        if ((objp->otype = otype(sbuf)) < 0) {
120                  sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf);
121                  error(USER, errmsg);
122          }
123                                          /* get identifier */
124 <        fscanf(fp, "%s", sbuf);
124 >        sbuf[0] = '\0';
125 >        fgetword(sbuf, MAXSTR, fp);
126 >        if (strchr(sbuf, '\t')) {
127 >                sprintf(errmsg, "(%s): illegal tab in identifier \"%s\"",
128 >                                        name, sbuf);
129 >                error(USER, errmsg);
130 >        }
131          objp->oname = savqstr(sbuf);
132                                          /* get arguments */
133 <        if (objp->otype == -1) {
134 <                register OBJECT  alias;
135 <                fscanf(fp, "%s", sbuf);
136 <                if ((alias = modifier(sbuf)) == OVOID) {
137 <                        sprintf(errmsg,
138 <                        "(%s): bad reference \"%s\" for %s \"%s\"",
139 <                                        name, sbuf, ALIASID, objp->oname);
140 <                        error(USER, errmsg);
133 >        if (objp->otype == MOD_ALIAS) {
134 >                OBJECT  ref;
135 >                OBJREC  *rfp;
136 >                strcpy(sbuf, "EOF");
137 >                fgetword(sbuf, MAXSTR, fp);
138 >                if ((ref = modifier(sbuf)) == OVOID) {
139 >                        sprintf(errmsg, "(%s): bad reference \"%s\"",
140 >                                        name, sbuf);
141 >                        objerror(objp, USER, errmsg);
142 >                }                       /* skip pass-thru aliases */
143 >                while ((rfp=objptr(ref))->otype == MOD_ALIAS &&
144 >                                !rfp->oargs.nsargs & (rfp->omod != OVOID))
145 >                        ref = rfp->omod;
146 >
147 >                if ((objp->omod == OALIAS) | (objp->omod == rfp->omod)) {
148 >                        objp->omod = ref;
149 >                } else {
150 >                        objp->oargs.sarg = (char **)malloc(sizeof(char *));
151 >                        if (objp->oargs.sarg == NULL)
152 >                                error(SYSTEM, "out of memory in getobject");
153 >                        objp->oargs.nsargs = 1;
154 >                        objp->oargs.sarg[0] = savestr(sbuf);
155                  }
156 <                objp->otype = objptr(alias)->otype;
111 <                bcopy(&objptr(alias)->oargs, &objp->oargs, sizeof(FUNARGS));
112 <        } else if (readfargs(&objp->oargs, fp) < 0) {
156 >        } else if ((rval = readfargs(&objp->oargs, fp)) == 0) {
157                  sprintf(errmsg, "(%s): bad arguments", name);
158                  objerror(objp, USER, errmsg);
159 +        } else if (rval < 0) {
160 +                sprintf(errmsg, "(%s): error reading scene", name);
161 +                error(SYSTEM, errmsg);
162          }
163 +        if (objp->omod == OALIAS) {
164 +                sprintf(errmsg, "(%s): inappropriate use of '%s' modifier",
165 +                                name, ALIASMOD);
166 +                objerror(objp, USER, errmsg);
167 +        }
168                                          /* initialize */
169          objp->os = NULL;
118        objp->lastrno = -1;
170  
171          insertobject(obj);              /* add to global structure */
172 + #undef OALIAS
173   }
174  
175  
176 < readfargs(fa, fp)               /* read function arguments from stream */
177 < register FUNARGS  *fa;
126 < FILE  *fp;
176 > static void
177 > optimize_objblock(int i)                /* consolidate memory in object block */
178   {
179 <        char  sbuf[MAXSTR];
180 <        int  n;
181 <        register int  i;
179 > #if OBJMEMOPT
180 >        OBJREC          *o, *co;
181 >        int             n = 0;
182 >        unsigned long   sargcnt = 0, iargcnt = 0, fargcnt = 0, namecnt = 0;
183  
184 <        if (fscanf(fp, "%d", &n) != 1 || n < 0)
185 <                return(-1);
186 <        if (fa->nsargs = n) {
187 <                fa->sarg = (char **)bmalloc(n*sizeof(char *));
188 <                if (fa->sarg == NULL)
189 <                        goto memerr;
190 <                for (i = 0; i < fa->nsargs; i++) {
191 <                        if (fscanf(fp, "%s", sbuf) != 1)
192 <                                return(-1);
193 <                        fa->sarg[i] = savestr(sbuf);
184 >        if (i < 0 || objblock[i] == NULL || objblock[i][OBJBLKSIZ].otype < 0)
185 >                return;                 /* invalid or already done */
186 >
187 >        for (o = objblock[i]+OBJBLKSIZ; o-- > objblock[i]; ) {
188 >                if (o->oname == NULL)   /* too early to optimize? */
189 >                        return;
190 >                if (o->os != NULL)      /* too late to optimize? */
191 >                        return;
192 >                n += (o->oargs.nsargs > 0) | (o->oargs.nfargs > 0);
193 >                sargcnt += o->oargs.nsargs;
194 >                fargcnt += o->oargs.nfargs;
195 > #ifdef  IARGS
196 >                iargcnt += o->oargs.niargs;
197 > #endif
198 >                namecnt += strlen(o->oname)+1;
199 >        }
200 >        if (n < OBJBLKSIZ/10)           /* never happens? */
201 >                return;
202 >                                        /* prep consolidation object */
203 >        co = objblock[i]+OBJBLKSIZ;
204 >        co->oargs.nsargs = sargcnt;
205 >        co->oargs.nfargs = fargcnt;
206 >        if ((co->oargs.nsargs != sargcnt) | (co->oargs.nfargs != fargcnt))
207 >                return;                 /* overrun condition */
208 >
209 >        co->oname = (char *)malloc(sizeof(char)*namecnt);
210 >        co->oargs.sarg = (char **)malloc(sizeof(char *)*sargcnt);
211 >        co->oargs.farg = (RREAL *)malloc(sizeof(RREAL)*fargcnt);
212 >        if ((co->oname == NULL) | (co->oargs.sarg == NULL) |
213 >                        (co->oargs.farg == NULL)) {
214 >                free(co->oname);
215 >                free(co->oargs.sarg); free(co->oargs.farg);
216 >                memset(co, 0, sizeof(OBJREC));
217 >                return;                 /* insufficient memory */
218 >        }
219 > #ifdef  IARGS
220 >        co->oargs.niargs = iargcnt;
221 >        co->oargs.iarg = (long *)malloc(sizeof(long)*iargcnt);
222 >        if (co->oargs.iarg == NULL) {
223 >                free(co->oname);
224 >                free(co->oargs.sarg); free(co->oargs.farg);
225 >                memset(co, 0, sizeof(OBJREC));
226 >                return;                 /* insufficient memory */
227 >        }
228 >        iargcnt = 0;
229 > #endif
230 >        namecnt = sargcnt = fargcnt = 0;
231 >        for (o = objblock[i]+OBJBLKSIZ; o-- > objblock[i]; ) {
232 >                n = strlen(o->oname)+1;
233 >                memcpy(co->oname + namecnt, o->oname, n);
234 >                freeqstr(o->oname);
235 >                o->oname = co->oname + namecnt;
236 >                namecnt += n;
237 >                if (o->oargs.nsargs) {
238 >                        memcpy(co->oargs.sarg+sargcnt, o->oargs.sarg,
239 >                                        sizeof(char *)*o->oargs.nsargs);
240 >                        free(o->oargs.sarg);
241 >                        o->oargs.sarg = co->oargs.sarg + sargcnt;
242 >                        sargcnt += o->oargs.nsargs;
243                  }
244 <        } else
245 <                fa->sarg = NULL;
246 <        if (fscanf(fp, "%d", &n) != 1 || n < 0)
247 <                return(-1);
244 >                if (o->oargs.nfargs) {
245 >                        memcpy(co->oargs.farg+fargcnt, o->oargs.farg,
246 >                                        sizeof(RREAL)*o->oargs.nfargs);
247 >                        free(o->oargs.farg);
248 >                        o->oargs.farg = co->oargs.farg + fargcnt;
249 >                        fargcnt += o->oargs.nfargs;
250 >                }
251   #ifdef  IARGS
252 <        if (fa->niargs = n) {
253 <                fa->iarg = (long *)bmalloc(n*sizeof(int));
254 <                if (fa->iarg == NULL)
255 <                        goto memerr;
256 <                for (i = 0; i < n; i++)
257 <                        if (fscanf(fp, "%ld", &fa->iarg[i]) != 1)
258 <                                return(-1);
155 <        } else
156 <                fa->iarg = NULL;
157 < #else
158 <        if (n != 0)
159 <                return(-1);
252 >                if (o->oargs.niargs) {
253 >                        memcpy(co->oargs.iarg+iargcnt, o->oargs.iarg,
254 >                                        sizeof(long)*o->oargs.niargs);
255 >                        free(o->oargs.iarg);
256 >                        o->oargs.iarg = co->oargs.iarg + iargcnt;
257 >                        iargcnt += o->oargs.niargs;
258 >                }
259   #endif
260 <        if (fscanf(fp, "%d", &n) != 1 || n < 0)
261 <                return(-1);
262 <        if (fa->nfargs = n) {
164 <                fa->farg = (double *)bmalloc(n*sizeof(double));
165 <                if (fa->farg == NULL)
166 <                        goto memerr;
167 <                for (i = 0; i < n; i++)
168 <                        if (fscanf(fp, "%lf", &fa->farg[i]) != 1)
169 <                                return(-1);
170 <        } else
171 <                fa->farg = NULL;
172 <        return(0);
173 < memerr:
174 <        error(SYSTEM, "out of memory in readfargs");
260 >        }
261 >        co->otype = -1;         /* flag for optimized block */
262 > #endif
263   }
264  
265  
266 < int
267 < newobject()                             /* get a new object */
266 > OBJECT
267 > newobject(void)                         /* get a new object */
268   {
269 <        register int  i;
269 >        int  i;
270  
271 <        if ((nobjects & 077) == 0) {            /* new block */
271 >        if ((nobjects & (OBJBLKSIZ-1)) == 0) {  /* new block */
272 >                i = nobjects >> OBJBLKSHFT;
273 >                optimize_objblock(i-1);         /* optimize previous block */
274                  errno = 0;
185                i = nobjects >> 6;
275                  if (i >= MAXOBJBLK)
276                          return(OVOID);
277 <                objblock[i] = (OBJREC *)malloc(0100*sizeof(OBJREC));
277 >                objblock[i] = (OBJREC *)calloc(OBJBLKSIZ+OBJMEMOPT,
278 >                                                sizeof(OBJREC));
279                  if (objblock[i] == NULL)
280                          return(OVOID);
281          }
282          return(nobjects++);
283 + }
284 +
285 + void
286 + freeobjects(                            /* free a range of objects */
287 +        int firstobj,
288 +        int nobjs
289 + )
290 + {
291 +        int  obj;
292 +                                        /* check bounds */
293 +        if (firstobj < 0)
294 +                return;
295 +        if (nobjs <= 0)
296 +                return;
297 +        if (firstobj + nobjs > nobjects)
298 +                return;
299 +                                        /* clear objects */
300 +        for (obj = firstobj+nobjs; obj-- > firstobj; ) {
301 +                OBJREC  *o = objptr(obj);
302 +                free_os(o);             /* free client memory */
303 +                if (!OBJMEMOPT || !objblock[obj>>OBJBLKSHFT][OBJBLKSIZ].otype) {
304 +                        freeqstr(o->oname);
305 +                        freefargs(&o->oargs);
306 +                }
307 +                memset(o, 0, sizeof(OBJREC));
308 +        }
309 +                                        /* free objects off end */
310 +        for (obj = nobjects; obj-- > 0; )
311 +                if (objptr(obj)->oname != NULL)
312 +                        break;
313 +        if (++obj >= nobjects)
314 +                return;
315 +        while (nobjects > obj)          /* free empty end blocks */
316 +                if ((--nobjects & (OBJBLKSIZ-1)) == 0) {
317 +                        int     i = nobjects >> OBJBLKSHFT;
318 +                                        /* consolidated block? */
319 +                        if (OBJMEMOPT && objblock[i][OBJBLKSIZ].otype < 0) {
320 +                                free(objblock[i][OBJBLKSIZ].oname);
321 +                                freefargs(&objblock[i][OBJBLKSIZ].oargs);
322 +                        }
323 +                        free(objblock[i]);
324 +                        objblock[i] = NULL;
325 +                }
326 +        truncobjndx();                  /* truncate modifier look-up */
327   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines