ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/portio.c
Revision: 2.16
Committed: Thu Mar 3 22:06:18 2016 UTC (8 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.15: +42 -1 lines
Log Message:
Added fread() and fwrite() replacements

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: portio.c,v 2.15 2012/10/19 01:40:33 greg Exp $";
3 #endif
4 /*
5 * Portable i/o for binary files
6 *
7 * External symbols declared in rtio.h
8 */
9
10 #include "copyright.h"
11
12 #include "rtio.h"
13
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);
32 while (*s++);
33 }
34
35
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 void
49 putflt( /* put out floating point number */
50 double f,
51 FILE *fp
52 )
53 {
54 long m;
55 int e;
56
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( /* get null-terminated string */
91 char *s,
92 FILE *fp
93 )
94 {
95 char *cp;
96 int c;
97
98 cp = s;
99 while ((c = getc(fp)) != EOF)
100 if ((*cp++ = c) == '\0')
101 return(s);
102
103 return(NULL);
104 }
105
106
107 long
108 getint( /* get a siz-byte integer */
109 int siz,
110 FILE *fp
111 )
112 {
113 int c;
114 long r;
115
116 if ((c = getc(fp)) == EOF)
117 return(EOF);
118 r = 0x80&c ? -1<<8|c : c; /* sign extend */
119 while (--siz > 0) {
120 if ((c = getc(fp)) == EOF)
121 return(EOF);
122 r <<= 8;
123 r |= c;
124 }
125 return(r);
126 }
127
128
129 double
130 getflt( /* get a floating point number */
131 FILE *fp
132 )
133 {
134 long l;
135 double d;
136
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 }