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

Comparing ray/src/common/readoct.c (file contents):
Revision 1.14 by greg, Wed Oct 23 11:53:38 1991 UTC vs.
Revision 2.10 by greg, Thu Nov 18 09:27:01 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "otypes.h"
20  
21 < extern double  atof();
22 < static double  getflt();
23 < static long  getint();
24 < static char  *getstr();
21 > static double  ogetflt();
22 > static long  ogetint();
23 > static char  *ogetstr();
24 > static int  getobj(), octerror(), skiptree();
25   static OCTREE  getfullnode(), gettree();
26  
27   static char  *infn;                     /* input file name */
# Line 38 | Line 38 | int  load;
38   CUBE  *scene;
39   char  *ofn[];
40   {
41        extern int  fputs();
41          char  sbuf[512];
42          int  nf;
43 <        OBJECT  fnobjects;
43 >        OBJECT  fnobjects;
44          register int  i;
45 +        long  m;
46          
47          if (fname == NULL) {
48                  infn = "standard input";
# Line 55 | Line 55 | char  *ofn[];
55                          error(SYSTEM, errmsg);
56                  }
57          }
58 + #ifdef MSDOS
59 +        setmode(fileno(infp), O_BINARY);
60 + #endif
61                                          /* get header */
62          if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : NULL) < 0)
63                  octerror(USER, "not an octree");
64                                          /* check format */
65 <        if ((objsize = getint(2)-OCTMAGIC) <= 0 ||
65 >        if ((objsize = ogetint(2)-OCTMAGIC) <= 0 ||
66                          objsize > MAXOBJSIZ || objsize > sizeof(long))
67                  octerror(USER, "incompatible octree format");
68                                          /* get boundaries */
69          if (load & IO_BOUNDS) {
70                  for (i = 0; i < 3; i++)
71 <                        scene->cuorg[i] = atof(getstr(sbuf));
72 <                scene->cusize = atof(getstr(sbuf));
71 >                        scene->cuorg[i] = atof(ogetstr(sbuf));
72 >                scene->cusize = atof(ogetstr(sbuf));
73          } else {
74                  for (i = 0; i < 4; i++)
75 <                        getstr(sbuf);
75 >                        ogetstr(sbuf);
76          }
77          objorig = nobjects;             /* set object offset */
78          nf = 0;                         /* get object files */
79 <        while (*getstr(sbuf)) {
79 >        while (*ogetstr(sbuf)) {
80                  if (load & IO_SCENE)
81                          readobj(sbuf);
82                  if (load & IO_FILES)
# Line 83 | Line 86 | char  *ofn[];
86          if (load & IO_FILES)
87                  ofn[nf] = NULL;
88                                          /* get number of objects */
89 <        fnobjects = getint(objsize);
89 >        fnobjects = m = ogetint(objsize);
90 >        if (fnobjects != m)
91 >                octerror(USER, "too many objects");
92  
93 <        if (load & IO_TREE) {
89 <                                                /* get the octree */
93 >        if (load & IO_TREE)                     /* get the octree */
94                  scene->cutree = gettree();
95 <                                                /* get the scene */
96 <                if (nf == 0 && load & IO_SCENE) {
97 <                        for (i = 0; *getstr(sbuf); i++)
98 <                                if ((otypmap[i] = otype(sbuf)) < 0) {
99 <                                        sprintf(errmsg, "unknown type \"%s\"",
100 <                                                        sbuf);
101 <                                        octerror(WARNING, errmsg);
102 <                                }
103 <                        while (getobj() != OVOID)
104 <                                ;
105 <                } else if (load & IO_SCENE) {           /* consistency checks */
106 <                                                /* check object count */
107 <                        if (nobjects != objorig+fnobjects)
108 <                                octerror(USER, "bad object count; octree stale?");
109 <                                                /* check for non-surfaces */
110 <                        if (nonsurfinset(objorig, fnobjects))
111 <                                octerror(USER, "non-surface in set; octree stale?");
112 <                }
113 <        }
95 >        else if (load & IO_SCENE && nf == 0)
96 >                skiptree();
97 >                
98 >        if (load & IO_SCENE)            /* get the scene */
99 >            if (nf == 0) {
100 >                for (i = 0; *ogetstr(sbuf); i++)
101 >                        if ((otypmap[i] = otype(sbuf)) < 0) {
102 >                                sprintf(errmsg, "unknown type \"%s\"", sbuf);
103 >                                octerror(WARNING, errmsg);
104 >                        }
105 >                while (getobj() != OVOID)
106 >                        ;
107 >            } else {                    /* consistency checks */
108 >                                /* check object count */
109 >                if (nobjects != objorig+fnobjects)
110 >                        octerror(USER, "bad object count; octree stale?");
111 >                                /* check for non-surfaces */
112 >                if (nonsurfinset(objorig, fnobjects))
113 >                        octerror(USER, "modifier in tree; octree stale?");
114 >            }
115          fclose(infp);
116          return(nf);
117   }
118  
119  
120   static char *
121 < getstr(s)                       /* get null-terminated string */
121 > ogetstr(s)                      /* get null-terminated string */
122   char  *s;
123   {
124 <        register char  *cp;
120 <        register int  c;
124 >        extern char  *getstr();
125  
126 <        cp = s;
127 <        while ((c = getc(infp)) != EOF)
128 <                if ((*cp++ = c) == '\0')
125 <                        return(s);
126 <                
127 <        octerror(USER, "truncated octree");
126 >        if (getstr(s, infp) == NULL)
127 >                octerror(USER, "truncated octree");
128 >        return(s);
129   }
130  
131  
132   static OCTREE
133   getfullnode()                   /* get a set, return fullnode */
134   {
135 <        OBJECT  set[MAXSET+1];
135 >        OBJECT  set[MAXSET+1];
136          register int  i;
137          register long  m;
138  
139 <        m = getint(objsize);
139 <        if (m > MAXSET)
139 >        if ((set[0] = ogetint(objsize)) > MAXSET)
140                  octerror(USER, "bad set in getfullnode");
141        set[0] = m;
141          for (i = 1; i <= set[0]; i++) {
142 <                m = getint(objsize) + objorig;
143 <                if ((OBJECT)m != m)
142 >                m = ogetint(objsize) + objorig;
143 >                if ((set[i] = m) != m)
144                          octerror(USER, "too many objects");
146                set[i] = m;
145          }
146          return(fullnode(set));
147   }      
148  
149  
150   static long
151 < getint(siz)                     /* get a siz-byte integer */
152 < register int  siz;
151 > ogetint(siz)                    /* get a siz-byte integer */
152 > int  siz;
153   {
154 <        register int  c;
154 >        extern long  getint();
155          register long  r;
156  
157 <        if ((c = getc(infp)) == EOF)
158 <                goto end_file;
159 <        r = 0x80&c ? -1<<8|c : c;               /* sign extend */
162 <        while (--siz > 0) {
163 <                if ((c = getc(infp)) == EOF)
164 <                        goto end_file;
165 <                r <<= 8;
166 <                r |= c;
167 <        }
157 >        r = getint(siz, infp);
158 >        if (feof(infp))
159 >                octerror(USER, "truncated octree");
160          return(r);
169 end_file:
170        octerror(USER, "truncated octree");
161   }
162  
163  
164   static double
165 < getflt()                        /* get a floating point number */
165 > ogetflt()                       /* get a floating point number */
166   {
167 <        extern double  ldexp();
168 <        double  d;
167 >        extern double  getflt();
168 >        double  r;
169  
170 <        d = (double)getint(4)/0x7fffffff;
171 <        return(ldexp(d, (int)getint(1)));
170 >        r = getflt(infp);
171 >        if (feof(infp))
172 >                octerror(USER, "truncated octree");
173 >        return(r);
174   }
175          
176  
177   static OCTREE
178   gettree()                       /* get a pre-ordered octree */
179   {
180 <        register OCTREE  ot;
180 >        register OCTREE  ot;
181          register int  i;
182          
183          switch (getc(infp)) {
# Line 208 | Line 200 | gettree()                      /* get a pre-ordered octree */
200  
201  
202   static
203 + skiptree()                              /* skip octree on input */
204 + {
205 +        register int  i;
206 +        
207 +        switch (getc(infp)) {
208 +        case OT_EMPTY:
209 +                return;
210 +        case OT_FULL:
211 +                for (i = ogetint(objsize)*objsize; i-- > 0; )
212 +                        if (getc(infp) == EOF)
213 +                                octerror(USER, "truncated octree");
214 +                return;
215 +        case OT_TREE:
216 +                for (i = 0; i < 8; i++)
217 +                        skiptree();
218 +                return;
219 +        case EOF:
220 +                octerror(USER, "truncated octree");
221 +        default:
222 +                octerror(USER, "damaged octree");
223 +        }
224 + }
225 +
226 +
227 + static
228   getobj()                                /* get next object */
229   {
230          char  sbuf[MAXSTR];
231          int  obj;
232          register int  i;
233          register long  m;
234 <        register OBJREC  *objp;
234 >        register OBJREC  *objp;
235  
236 <        i = getint(1);
236 >        i = ogetint(1);
237          if (i == -1)
238                  return(OVOID);          /* terminator */
239          if ((obj = newobject()) == OVOID)
# Line 224 | Line 241 | getobj()                               /* get next object */
241          objp = objptr(obj);
242          if ((objp->otype = otypmap[i]) < 0)
243                  octerror(USER, "reference to unknown type");
244 <        if ((m = getint(objsize)) != OVOID) {
244 >        if ((m = ogetint(objsize)) != OVOID) {
245                  m += objorig;
246                  if ((OBJECT)m != m)
247                          octerror(USER, "too many objects");
248          }
249          objp->omod = m;
250 <        objp->oname = savqstr(getstr(sbuf));
251 <        if (objp->oargs.nsargs = getint(2)) {
250 >        objp->oname = savqstr(ogetstr(sbuf));
251 >        if (objp->oargs.nsargs = ogetint(2)) {
252                  objp->oargs.sarg = (char **)bmalloc
253                                  (objp->oargs.nsargs*sizeof(char *));
254                  if (objp->oargs.sarg == NULL)
255                          goto memerr;
256                  for (i = 0; i < objp->oargs.nsargs; i++)
257 <                        objp->oargs.sarg[i] = savestr(getstr(sbuf));
257 >                        objp->oargs.sarg[i] = savestr(ogetstr(sbuf));
258          } else
259                  objp->oargs.sarg = NULL;
260 < #ifdef  IARGS
261 <        if (objp->oargs.niargs = getint(2)) {
260 > #ifdef  IARGS
261 >        if (objp->oargs.niargs = ogetint(2)) {
262                  objp->oargs.iarg = (long *)bmalloc
263                                  (objp->oargs.niargs*sizeof(long));
264                  if (objp->oargs.iarg == NULL)
265                          goto memerr;
266                  for (i = 0; i < objp->oargs.niargs; i++)
267 <                        objp->oargs.iarg[i] = getint(4);
267 >                        objp->oargs.iarg[i] = ogetint(4);
268          } else
269                  objp->oargs.iarg = NULL;
270   #endif
271 <        if (objp->oargs.nfargs = getint(2)) {
272 <                objp->oargs.farg = (double *)bmalloc
273 <                                (objp->oargs.nfargs*sizeof(double));
271 >        if (objp->oargs.nfargs = ogetint(2)) {
272 >                objp->oargs.farg = (FLOAT *)bmalloc
273 >                                (objp->oargs.nfargs*sizeof(FLOAT));
274                  if (objp->oargs.farg == NULL)
275                          goto memerr;
276                  for (i = 0; i < objp->oargs.nfargs; i++)
277 <                        objp->oargs.farg[i] = getflt();
277 >                        objp->oargs.farg[i] = ogetflt();
278          } else
279                  objp->oargs.farg = NULL;
280                                                  /* initialize */
281          objp->os = NULL;
265        objp->lastrno = -1;
282                                                  /* insert */
283          insertobject(obj);
284          return(obj);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines