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

Comparing ray/src/common/portio.c (file contents):
Revision 2.19 by greg, Thu Aug 18 00:52:48 2016 UTC vs.
Revision 2.27 by greg, Mon Jul 12 17:42:51 2021 UTC

# Line 14 | Line 14 | static const char      RCSid[] = "$Id$";
14   #include <math.h>
15  
16  
17 < void
17 > int
18   putstr(                         /* write null-terminated string to fp */
19          char  *s,
20          FILE  *fp
# Line 23 | Line 23 | putstr(                                /* write null-terminated string to fp */
23          do
24                  putc(*s, fp);
25          while (*s++);
26 +
27 +        return(ferror(fp) ? EOF : 0);
28   }
29  
30  
31 < void
31 > int
32   putint(                         /* write a siz-byte integer to fp */
33          long  i,
34          int  siz,
35          FILE  *fp
36   )
37   {
38 +        while (siz > sizeof(long)) {
39 +                putc((i<0)*0xff, fp);
40 +                siz--;
41 +        }
42          siz <<= 3;
43 <        while ((siz -= 8) >= 0)
43 >        while ((siz -= 8) > 0)
44                  putc((int)(i>>siz & 0xff), fp);
45 +
46 +        return(putc((int)(i & 0xff), fp) == EOF ? EOF : 0);
47   }
48  
49  
50 < void
50 > int
51   putflt(                         /* put out floating point number */
52          double  f,
53          FILE  *fp
# Line 57 | Line 65 | putflt(                                /* put out floating point number */
65                  e = 0;
66          }
67          putint(m, 4, fp);
68 <        putint((long)e, 1, fp);
68 >        return(putint(e, 1, fp));
69   }
70  
71  
72 < int
72 > size_t
73   putbinary(                      /* fwrite() replacement for small objects */
74          const void *p,
75 <        int elsiz,
76 <        int nel,
75 >        size_t elsiz,
76 >        size_t nel,
77          FILE *fp)
78   {
79          const char      *s = (const char *)p;
80 <        int             nbytes = elsiz*nel;
80 >        size_t          nbytes = elsiz*nel;
81  
82 <        if (nbytes > 512)
82 >        if (nbytes > 128)
83                  return(fwrite(p, elsiz, nel, fp));
84          
85          while (nbytes-- > 0)
86 <                putc(*s++, fp);
86 >                if (putc(*s++, fp) == EOF)
87 >                        return((elsiz*nel - nbytes)/elsiz);
88  
89          return(nel);
90   }
# Line 110 | Line 119 | getint(                                /* get a siz-byte integer */
119  
120          if ((c = getc(fp)) == EOF)
121                  return(EOF);
122 <        r = 0x80&c ? -1<<8|c : c;               /* sign extend */
122 >        r = c;
123 >        if (c & 0x80)           /* sign extend? */
124 >                r |= -256L;
125          while (--siz > 0) {
126                  if ((c = getc(fp)) == EOF)
127                          return(EOF);
# Line 136 | Line 147 | getflt(                                /* get a floating point number */
147                  getc(fp);               /* exactly zero -- ignore exponent */
148                  return(0.0);
149          }
150 <        d = (l + (l > 0 ? .5 : -.5)) * (1./0x7fffffff);
150 >        d = (l + .5 - (l<0)) * (1./0x7fffffff);
151          return(ldexp(d, (int)getint(1, fp)));
152   }
153  
154  
155 < int
155 > size_t
156   getbinary(                      /* fread() replacement for small objects */
157          void *p,
158 <        int elsiz,
159 <        int nel,
158 >        size_t elsiz,
159 >        size_t nel,
160          FILE *fp)
161   {
162          char    *s = (char *)p;
163 <        int     nbytes = elsiz*nel;
163 >        size_t  nbytes = elsiz*nel;
164          int     c;
165  
166 <        if (nbytes > 512)
166 >        if (nbytes > 128)
167                  return(fread(p, elsiz, nel, fp));
168          
169          while (nbytes-- > 0) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines