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.4 by greg, Thu Sep 6 23:32:43 1990 UTC vs.
Revision 2.11 by greg, Tue Mar 11 19:29:04 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 22 | Line 21 | 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();
29 <        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 54 | 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;
80          register OBJREC  *objp;
81  
82          if ((obj = newobject()) == OVOID)
83                  error(SYSTEM, "out of object space");
84          objp = objptr(obj);
85                                          /* get modifier */
86 <        fscanf(fp, "%s", sbuf);
86 >        strcpy(sbuf, "EOF");
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);
95          }
96                                          /* get type */
97 <        fscanf(fp, "%s", sbuf);
98 <        if (!strcmp(sbuf, ALIASID))
99 <                objp->otype = -1;
90 <        else if ((objp->otype = otype(sbuf)) < 0) {
97 >        strcpy(sbuf, "EOF");
98 >        fgetword(sbuf, MAXSTR, fp);
99 >        if ((objp->otype = otype(sbuf)) < 0) {
100                  sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf);
101                  error(USER, errmsg);
102          }
103                                          /* get identifier */
104 <        fscanf(fp, "%s", sbuf);
104 >        sbuf[0] = '\0';
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 <                fscanf(fp, "%s", sbuf);
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);
105 <                        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);
119 <        } else if (readfargs(&objp->oargs, fp) < 0) {
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);
130 +        } else if (rval < 0) {
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;
115        objp->lastrno = -1;
141  
142          insertobject(obj);              /* add to global structure */
143 + #undef OALIAS
144   }
145  
146  
147 < 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
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 *)malloc(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(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