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.2 by greg, Mon Sep 21 12:02:23 1992 UTC vs.
Revision 2.16 by greg, Thu Mar 3 22:06:18 2016 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 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   * Portable i/o for binary files
6 + *
7 + * External symbols declared in rtio.h
8   */
9  
10 < #include <stdio.h>
10 > #include "copyright.h"
11  
12 + #include "rtio.h"
13  
14 < putstr(s, fp)                   /* write null-terminated string to fp */
15 < register char  *s;
16 < register FILE  *fp;
14 > #include <math.h>
15 >
16 > #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
17 > #undef getc
18 > #undef putc
19 > #define getc    getc_unlocked
20 > #define putc    putc_unlocked
21 > #endif
22 >
23 >
24 > void
25 > putstr(                         /* write null-terminated string to fp */
26 >        char  *s,
27 >        FILE  *fp
28 > )
29   {
30          do
31                  putc(*s, fp);
# Line 21 | Line 33 | register FILE  *fp;
33   }
34  
35  
36 < putint(i, siz, fp)              /* write a siz-byte integer to fp */
37 < long  i;
38 < register int  siz;
39 < register FILE  *fp;
36 > void
37 > putint(                         /* write a siz-byte integer to fp */
38 >        long  i,
39 >        int  siz,
40 >        FILE  *fp
41 > )
42   {
43          while (siz--)
44                  putc((int)(i>>(siz<<3) & 0xff), fp);
45   }
46  
47  
48 < putflt(f, fp)                   /* put out floating point number */
49 < double  f;
50 < FILE  *fp;
48 > void
49 > putflt(                         /* put out floating point number */
50 >        double  f,
51 >        FILE  *fp
52 > )
53   {
54 <        extern double  frexp();
54 >        long  m;
55          int  e;
56  
57 <        putint((long)(frexp(f,&e)*0x7fffffff), 4, fp);
57 >        m = frexp(f, &e) * 0x7fffffff;
58 >        if (e > 127) {                  /* overflow */
59 >                m = m > 0 ? (long)0x7fffffff : -(long)0x7fffffff;
60 >                e = 127;
61 >        } else if (e < -128) {          /* underflow */
62 >                m = 0;
63 >                e = 0;
64 >        }
65 >        putint(m, 4, fp);
66          putint((long)e, 1, fp);
67   }
68  
69  
70 + int
71 + putbinary(                      /* fwrite() replacement for small objects */
72 +        char *s,
73 +        int elsiz,
74 +        int nel,
75 +        FILE *fp)
76 + {
77 +        int     nbytes = elsiz*nel;
78 +
79 +        if (nbytes > 512)
80 +                return(fwrite(s, elsiz, nel, fp));
81 +        
82 +        while (nbytes-- > 0)
83 +                putc(*s++, fp);
84 +
85 +        return(nel);
86 + }
87 +
88 +
89   char *
90 < getstr(s, fp)                   /* get null-terminated string */
91 < char  *s;
92 < register FILE  *fp;
90 > getstr(                         /* get null-terminated string */
91 >        char  *s,
92 >        FILE  *fp
93 > )
94   {
95 <        register char  *cp;
96 <        register int  c;
95 >        char  *cp;
96 >        int  c;
97  
98          cp = s;
99          while ((c = getc(fp)) != EOF)
# Line 61 | Line 105 | register FILE  *fp;
105  
106  
107   long
108 < getint(siz, fp)                 /* get a siz-byte integer */
109 < int  siz;
110 < register FILE  *fp;
108 > getint(                         /* get a siz-byte integer */
109 >        int  siz,
110 >        FILE  *fp
111 > )
112   {
113 <        register int  c;
114 <        register long  r;
113 >        int  c;
114 >        long  r;
115  
116          if ((c = getc(fp)) == EOF)
117                  return(EOF);
# Line 82 | Line 127 | register FILE  *fp;
127  
128  
129   double
130 < getflt(fp)                      /* get a floating point number */
131 < FILE  *fp;
130 > getflt(                         /* get a floating point number */
131 >        FILE  *fp
132 > )
133   {
134 <        extern double  ldexp();
134 >        long    l;
135          double  d;
136  
137 <        d = (double)getint(4, fp)/0x7fffffff;
137 >        l = getint(4, fp);
138 >        if (l == EOF && feof(fp))       /* EOF? */
139 >                return((double)EOF);
140 >        if (l == 0) {
141 >                getc(fp);               /* exactly zero -- ignore exponent */
142 >                return(0.0);
143 >        }
144 >        d = (l + (l > 0 ? .5 : -.5)) * (1./0x7fffffff);
145          return(ldexp(d, (int)getint(1, fp)));
146 + }
147 +
148 +
149 + int
150 + getbinary(                      /* fread() replacement for small objects */
151 +        char *s,
152 +        int elsiz,
153 +        int nel,
154 +        FILE *fp)
155 + {
156 +        int     nbytes = elsiz*nel;
157 +        int     c;
158 +
159 +        if (nbytes > 512)
160 +                return(fread(s, elsiz, nel, fp));
161 +        
162 +        while (nbytes-- > 0) {
163 +                if ((c = getc(fp)) == EOF)
164 +                        return((elsiz*nel - nbytes)/elsiz);
165 +                *s++ = c;
166 +        }
167 +        return(nel);
168   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines