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.2 by greg, Wed Oct 4 16:32:04 1989 UTC vs.
Revision 2.13 by schorsch, Sat Jun 7 12:50:20 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  "standard.h"
10 > #include "copyright.h"
11  
12 < #include  "object.h"
12 > #include  <ctype.h>
13 > #include  <stdio.h>
14 > #ifdef _WIN32
15 > #define popen _popen
16 > #define pclose _pclose
17 > #endif
18  
19 + #include  "standard.h"
20 + #include  "object.h"
21   #include  "otypes.h"
22  
19 #include  <ctype.h>
23  
24   OBJREC  *objblock[MAXOBJBLK];           /* our objects */
25 < int  nobjects = 0;                      /* # of objects */
25 > OBJECT  nobjects = 0;                   /* # of objects */
26  
27  
28 < readobj(input)                  /* read in an object file or stream */
29 < char  *input;
28 > void
29 > readobj(inpspec)                /* read in an object file or stream */
30 > char  *inpspec;
31   {
32 <        FILE  *popen();
29 <        char  *fgets(), *fgetline();
32 >        OBJECT  lastobj;
33          FILE  *infp;
34 <        char  buf[512];
34 >        char  buf[1024];
35          register 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          while ((c = getc(infp)) != EOF) {
# Line 54 | Line 58 | char  *input;
58                          readobj(buf);
59                  } else {                                /* object */
60                          ungetc(c, infp);
61 <                        getobject(input, infp);
61 >                        getobject(inpspec, infp);
62                  }
63          }
64 <        if (input[0] == '!')
64 >        if (inpspec[0] == '!')
65                  pclose(infp);
66          else
67                  fclose(infp);
68 +        if (nobjects == lastobj) {
69 +                sprintf(errmsg, "(%s): empty file", inpspec);
70 +                error(WARNING, errmsg);
71 +        }
72   }
73  
74  
75 + void
76   getobject(name, fp)                     /* read the next object */
77   char  *name;
78   FILE  *fp;
79   {
80 + #define OALIAS  -2
81          OBJECT  obj;
82          char  sbuf[MAXSTR];
83 +        int  rval;
84          register OBJREC  *objp;
85  
86          if ((obj = newobject()) == OVOID)
87                  error(SYSTEM, "out of object space");
88          objp = objptr(obj);
89                                          /* get modifier */
90 <        fscanf(fp, "%s", sbuf);
90 >        strcpy(sbuf, "EOF");
91 >        fgetword(sbuf, MAXSTR, fp);
92          if (!strcmp(sbuf, VOIDID))
93                  objp->omod = OVOID;
94 +        else if (!strcmp(sbuf, ALIASMOD))
95 +                objp->omod = OALIAS;
96          else if ((objp->omod = modifier(sbuf)) == OVOID) {
97                  sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf);
98                  error(USER, errmsg);
99          }
100                                          /* get type */
101 <        fscanf(fp, "%s", sbuf);
102 <        if (!strcmp(sbuf, ALIASID))
103 <                objp->otype = -1;
90 <        else if ((objp->otype = otype(sbuf)) < 0) {
101 >        strcpy(sbuf, "EOF");
102 >        fgetword(sbuf, MAXSTR, fp);
103 >        if ((objp->otype = otype(sbuf)) < 0) {
104                  sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf);
105                  error(USER, errmsg);
106          }
107                                          /* get identifier */
108 <        fscanf(fp, "%s", sbuf);
108 >        sbuf[0] = '\0';
109 >        fgetword(sbuf, MAXSTR, fp);
110          objp->oname = savqstr(sbuf);
111                                          /* get arguments */
112 <        if (objp->otype == -1) {
112 >        if (objp->otype == MOD_ALIAS) {
113                  register OBJECT  alias;
114 <                fscanf(fp, "%s", sbuf);
114 >                strcpy(sbuf, "EOF");
115 >                fgetword(sbuf, MAXSTR, fp);
116                  if ((alias = modifier(sbuf)) == OVOID) {
117 <                        sprintf(errmsg,
118 <                        "(%s): bad reference \"%s\" for %s \"%s\"",
119 <                                        name, sbuf, ALIASID, objp->oname);
105 <                        error(USER, errmsg);
117 >                        sprintf(errmsg, "(%s): bad reference \"%s\"",
118 >                                        name, sbuf);
119 >                        objerror(objp, USER, errmsg);
120                  }
121 <                objp->otype = objptr(alias)->otype;
122 <                bcopy(&objptr(alias)->oargs, &objp->oargs, sizeof(FUNARGS));
123 <        } else if (readfargs(&objp->oargs, fp) < 0) {
121 >                if (objp->omod == OALIAS ||
122 >                                objp->omod == objptr(alias)->omod) {
123 >                        objp->omod = alias;
124 >                } else {
125 >                        objp->oargs.sarg = (char **)malloc(sizeof(char *));
126 >                        if (objp->oargs.sarg == NULL)
127 >                                error(SYSTEM, "out of memory in getobject");
128 >                        objp->oargs.nsargs = 1;
129 >                        objp->oargs.sarg[0] = savestr(sbuf);
130 >                }
131 >        } else if ((rval = readfargs(&objp->oargs, fp)) == 0) {
132                  sprintf(errmsg, "(%s): bad arguments", name);
133                  objerror(objp, USER, errmsg);
134 +        } else if (rval < 0) {
135 +                sprintf(errmsg, "(%s): error reading scene", name);
136 +                error(SYSTEM, errmsg);
137          }
138 +        if (objp->omod == OALIAS) {
139 +                sprintf(errmsg, "(%s): inappropriate use of '%s' modifier",
140 +                                name, ALIASMOD);
141 +                objerror(objp, USER, errmsg);
142 +        }
143                                          /* initialize */
144          objp->os = NULL;
115        objp->lastrno = -1;
145  
146          insertobject(obj);              /* add to global structure */
147 + #undef OALIAS
148   }
149  
150  
151 < readfargs(fa, fp)               /* read function arguments from stream */
122 < register FUNARGS  *fa;
123 < FILE  *fp;
124 < {
125 <        char  sbuf[MAXSTR];
126 <        int  n;
127 <        register int  i;
128 <
129 <        if (fscanf(fp, "%d", &n) != 1 || n < 0)
130 <                return(-1);
131 <        if (fa->nsargs = n) {
132 <                fa->sarg = (char **)bmalloc(n*sizeof(char *));
133 <                if (fa->sarg == NULL)
134 <                        goto memerr;
135 <                for (i = 0; i < fa->nsargs; i++) {
136 <                        if (fscanf(fp, "%s", sbuf) != 1)
137 <                                return(-1);
138 <                        fa->sarg[i] = savestr(sbuf);
139 <                }
140 <        } else
141 <                fa->sarg = NULL;
142 <        if (fscanf(fp, "%d", &n) != 1 || n < 0)
143 <                return(-1);
144 < #ifdef  IARGS
145 <        if (fa->niargs = n) {
146 <                fa->iarg = (long *)bmalloc(n*sizeof(int));
147 <                if (fa->iarg == NULL)
148 <                        goto memerr;
149 <                for (i = 0; i < n; i++)
150 <                        if (fscanf(fp, "%ld", &fa->iarg[i]) != 1)
151 <                                return(-1);
152 <        } else
153 <                fa->iarg = NULL;
154 < #else
155 <        if (n != 0)
156 <                return(-1);
157 < #endif
158 <        if (fscanf(fp, "%d", &n) != 1 || n < 0)
159 <                return(-1);
160 <        if (fa->nfargs = n) {
161 <                fa->farg = (double *)bmalloc(n*sizeof(double));
162 <                if (fa->farg == NULL)
163 <                        goto memerr;
164 <                for (i = 0; i < n; i++)
165 <                        if (fscanf(fp, "%lf", &fa->farg[i]) != 1)
166 <                                return(-1);
167 <        } else
168 <                fa->farg = NULL;
169 <        return(0);
170 < memerr:
171 <        error(SYSTEM, "out of memory in readfargs");
172 < }
173 <
174 <
175 < int
151 > OBJECT
152   newobject()                             /* get a new object */
153   {
154          register int  i;
155  
156 <        if ((nobjects & 077) == 0) {            /* new block */
156 >        if ((nobjects & (OBJBLKSIZ-1)) == 0) {  /* new block */
157                  errno = 0;
158 <                i = nobjects >> 6;
158 >                i = nobjects >> OBJBLKSHFT;
159                  if (i >= MAXOBJBLK)
160                          return(OVOID);
161 <                objblock[i] = (OBJREC *)malloc(0100*sizeof(OBJREC));
161 >                objblock[i] = (OBJREC *)calloc(OBJBLKSIZ, sizeof(OBJREC));
162                  if (objblock[i] == NULL)
163                          return(OVOID);
164          }
165          return(nobjects++);
166 + }
167 +
168 + void
169 + freeobjects(firstobj, nobjs)            /* free a range of objects */
170 + OBJECT  firstobj, nobjs;
171 + {
172 +        register int  obj;
173 +                                        /* check bounds */
174 +        if (firstobj < 0)
175 +                return;
176 +        if (nobjs <= 0)
177 +                return;
178 +        if (firstobj + nobjs > nobjects)
179 +                return;
180 +                                        /* clear objects */
181 +        for (obj = firstobj+nobjs; obj-- > firstobj; ) {
182 +                register OBJREC  *o = objptr(obj);
183 +                free_os(o);             /* free client memory */
184 +                freeqstr(o->oname);
185 +                freefargs(&o->oargs);
186 +                bzero((void *)o, sizeof(OBJREC));
187 +        }
188 +        clearobjndx();
189 +                                        /* free objects off end */
190 +        for (obj = nobjects; obj-- > 0; )
191 +                if (objptr(obj)->oname != NULL)
192 +                        break;
193 +        ++obj;
194 +        while (nobjects > obj)          /* free empty end blocks */
195 +                if ((--nobjects & (OBJBLKSIZ-1)) == 0) {
196 +                        int     i = nobjects >> OBJBLKSHFT;
197 +                        free((void *)objblock[i]);
198 +                        objblock[i] = NULL;
199 +                }
200   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines