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 2.2 by greg, Mon Jan 25 12:41:24 1993 UTC vs.
Revision 2.12 by greg, Tue May 13 17:58:32 2003 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 "copyright.h"
11 +
12   #include  "standard.h"
13  
14   #include  "object.h"
# Line 18 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17  
18   #include  <ctype.h>
19  
21 extern char  *fgetword(), *strcpy();
22
20   OBJREC  *objblock[MAXOBJBLK];           /* our objects */
21   OBJECT  nobjects = 0;                   /* # of objects */
22  
23  
24 < readobj(input)                  /* read in an object file or stream */
25 < char  *input;
24 > void
25 > readobj(inpspec)                /* read in an object file or stream */
26 > char  *inpspec;
27   {
28 <        FILE  *popen();
31 <        char  *fgets(), *fgetline();
28 >        OBJECT  lastobj;
29          FILE  *infp;
30 <        char  buf[512];
30 >        char  buf[1024];
31          register int  c;
32  
33 <        if (input == NULL) {
33 >        lastobj = nobjects;
34 >        if (inpspec == 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);
36 >                inpspec = "standard input";
37 >        } else if (inpspec[0] == '!') {
38 >                if ((infp = popen(inpspec+1, "r")) == NULL) {
39 >                        sprintf(errmsg, "cannot execute \"%s\"", inpspec);
40                          error(SYSTEM, errmsg);
41                  }
42 <        } else if ((infp = fopen(input, "r")) == NULL) {
43 <                sprintf(errmsg, "cannot open scene file \"%s\"", input);
42 >        } else if ((infp = fopen(inpspec, "r")) == NULL) {
43 >                sprintf(errmsg, "cannot open scene file \"%s\"", inpspec);
44                  error(SYSTEM, errmsg);
45          }
46          while ((c = getc(infp)) != EOF) {
# Line 56 | Line 54 | char  *input;
54                          readobj(buf);
55                  } else {                                /* object */
56                          ungetc(c, infp);
57 <                        getobject(input, infp);
57 >                        getobject(inpspec, infp);
58                  }
59          }
60 <        if (input[0] == '!')
60 >        if (inpspec[0] == '!')
61                  pclose(infp);
62          else
63                  fclose(infp);
64 +        if (nobjects == lastobj) {
65 +                sprintf(errmsg, "(%s): empty file", inpspec);
66 +                error(WARNING, errmsg);
67 +        }
68   }
69  
70  
71 + void
72   getobject(name, fp)                     /* read the next object */
73   char  *name;
74   FILE  *fp;
75   {
76 + #define OALIAS  -2
77          OBJECT  obj;
78          char  sbuf[MAXSTR];
79          int  rval;
# Line 83 | Line 87 | FILE  *fp;
87          fgetword(sbuf, MAXSTR, fp);
88          if (!strcmp(sbuf, VOIDID))
89                  objp->omod = OVOID;
90 +        else if (!strcmp(sbuf, ALIASMOD))
91 +                objp->omod = OALIAS;
92          else if ((objp->omod = modifier(sbuf)) == OVOID) {
93                  sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf);
94                  error(USER, errmsg);
# Line 90 | Line 96 | FILE  *fp;
96                                          /* get type */
97          strcpy(sbuf, "EOF");
98          fgetword(sbuf, MAXSTR, fp);
99 <        if (!strcmp(sbuf, ALIASID))
94 <                objp->otype = -1;
95 <        else if ((objp->otype = otype(sbuf)) < 0) {
99 >        if ((objp->otype = otype(sbuf)) < 0) {
100                  sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf);
101                  error(USER, errmsg);
102          }
# Line 101 | Line 105 | FILE  *fp;
105          fgetword(sbuf, MAXSTR, fp);
106          objp->oname = savqstr(sbuf);
107                                          /* get arguments */
108 <        if (objp->otype == -1) {
108 >        if (objp->otype == MOD_ALIAS) {
109                  register OBJECT  alias;
110                  strcpy(sbuf, "EOF");
111                  fgetword(sbuf, MAXSTR, fp);
112                  if ((alias = modifier(sbuf)) == OVOID) {
113 <                        sprintf(errmsg,
114 <                        "(%s): bad reference \"%s\" for %s \"%s\"",
115 <                                        name, sbuf, ALIASID, objp->oname);
112 <                        error(USER, errmsg);
113 >                        sprintf(errmsg, "(%s): bad reference \"%s\"",
114 >                                        name, sbuf);
115 >                        objerror(objp, USER, errmsg);
116                  }
117 <                objp->otype = objptr(alias)->otype;
118 <                copystruct(&objp->oargs, &objptr(alias)->oargs);
117 >                if (objp->omod == OALIAS ||
118 >                                objp->omod == objptr(alias)->omod) {
119 >                        objp->omod = alias;
120 >                } else {
121 >                        objp->oargs.sarg = (char **)malloc(sizeof(char *));
122 >                        if (objp->oargs.sarg == NULL)
123 >                                error(SYSTEM, "out of memory in getobject");
124 >                        objp->oargs.nsargs = 1;
125 >                        objp->oargs.sarg[0] = savestr(sbuf);
126 >                }
127          } else if ((rval = readfargs(&objp->oargs, fp)) == 0) {
128                  sprintf(errmsg, "(%s): bad arguments", name);
129                  objerror(objp, USER, errmsg);
# Line 120 | Line 131 | FILE  *fp;
131                  sprintf(errmsg, "(%s): error reading scene", name);
132                  error(SYSTEM, errmsg);
133          }
134 +        if (objp->omod == OALIAS) {
135 +                sprintf(errmsg, "(%s): inappropriate use of '%s' modifier",
136 +                                name, ALIASMOD);
137 +                objerror(objp, USER, errmsg);
138 +        }
139                                          /* initialize */
140          objp->os = NULL;
141  
142          insertobject(obj);              /* add to global structure */
143 + #undef OALIAS
144   }
145  
146  
147 < int
147 > OBJECT
148   newobject()                             /* get a new object */
149   {
150          register int  i;
151  
152 <        if ((nobjects & 077) == 0) {            /* new block */
152 >        if ((nobjects & (OBJBLKSIZ-1)) == 0) {  /* new block */
153                  errno = 0;
154 <                i = nobjects >> 6;
154 >                i = nobjects >> OBJBLKSHFT;
155                  if (i >= MAXOBJBLK)
156                          return(OVOID);
157 <                objblock[i] = (OBJREC *)bmalloc(0100*sizeof(OBJREC));
157 >                objblock[i] = (OBJREC *)calloc(OBJBLKSIZ, sizeof(OBJREC));
158                  if (objblock[i] == NULL)
159                          return(OVOID);
160          }
161          return(nobjects++);
162 + }
163 +
164 + void
165 + freeobjects(firstobj, nobjs)            /* free a range of objects */
166 + OBJECT  firstobj, nobjs;
167 + {
168 +        register int  obj;
169 +                                        /* check bounds */
170 +        if (firstobj < 0)
171 +                return;
172 +        if (nobjs <= 0)
173 +                return;
174 +        if (firstobj + nobjs > nobjects)
175 +                return;
176 +                                        /* clear objects */
177 +        for (obj = firstobj+nobjs; obj-- > firstobj; ) {
178 +                register OBJREC  *o = objptr(obj);
179 +                free_os(o);             /* free client memory */
180 +                freeqstr(o->oname);
181 +                freefargs(&o->oargs);
182 +                bzero((void *)o, sizeof(OBJREC));
183 +        }
184 +        clearobjndx();
185 +                                        /* free objects off end */
186 +        for (obj = nobjects; obj-- > 0; )
187 +                if (objptr(obj)->oname != NULL)
188 +                        break;
189 +        ++obj;
190 +        while (nobjects > obj)          /* free empty end blocks */
191 +                if ((--nobjects & (OBJBLKSIZ-1)) == 0) {
192 +                        int     i = nobjects >> OBJBLKSHFT;
193 +                        free((void *)objblock[i]);
194 +                        objblock[i] = NULL;
195 +                }
196   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines