ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/portio.c
Revision: 2.22
Committed: Mon Jul 15 21:27:25 2019 UTC (4 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R3
Changes since 2.21: +6 -5 lines
Log Message:
Reduced size limit for fread() & fwrite() again and other minor changes

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: portio.c,v 2.21 2019/07/05 03:04:22 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
17 void
18 putstr( /* write null-terminated string to fp */
19 char *s,
20 FILE *fp
21 )
22 {
23 do
24 putc(*s, fp);
25 while (*s++);
26 }
27
28
29 void
30 putint( /* write a siz-byte integer to fp */
31 long i,
32 int siz,
33 FILE *fp
34 )
35 {
36 siz <<= 3;
37 while ((siz -= 8) > 0)
38 putc((int)(i>>siz & 0xff), fp);
39 putc((int)(i & 0xff), fp);
40 }
41
42
43 void
44 putflt( /* put out floating point number */
45 double f,
46 FILE *fp
47 )
48 {
49 long m;
50 int e;
51
52 m = frexp(f, &e) * 0x7fffffff;
53 if (e > 127) { /* overflow */
54 m = m > 0 ? (long)0x7fffffff : -(long)0x7fffffff;
55 e = 127;
56 } else if (e < -128) { /* underflow */
57 m = 0;
58 e = 0;
59 }
60 putint(m, 4, fp);
61 putint(e, 1, fp);
62 }
63
64
65 int
66 putbinary( /* fwrite() replacement for small objects */
67 const void *p,
68 int elsiz,
69 int nel,
70 FILE *fp)
71 {
72 const char *s = (const char *)p;
73 int nbytes = elsiz*nel;
74
75 if (nbytes > 128)
76 return(fwrite(p, elsiz, nel, fp));
77
78 while (nbytes-- > 0)
79 if (putc(*s++, fp) == EOF)
80 return((elsiz*nel - nbytes)/elsiz);
81
82 return(nel);
83 }
84
85
86 char *
87 getstr( /* get null-terminated string */
88 char *s,
89 FILE *fp
90 )
91 {
92 char *cp;
93 int c;
94
95 cp = s;
96 while ((c = getc(fp)) != EOF)
97 if ((*cp++ = c) == '\0')
98 return(s);
99
100 return(NULL);
101 }
102
103
104 long
105 getint( /* get a siz-byte integer */
106 int siz,
107 FILE *fp
108 )
109 {
110 int c;
111 long r;
112
113 if ((c = getc(fp)) == EOF)
114 return(EOF);
115 r = 0x80&c ? -1<<8|c : c; /* sign extend */
116 while (--siz > 0) {
117 if ((c = getc(fp)) == EOF)
118 return(EOF);
119 r <<= 8;
120 r |= c;
121 }
122 return(r);
123 }
124
125
126 double
127 getflt( /* get a floating point number */
128 FILE *fp
129 )
130 {
131 long l;
132 double d;
133
134 l = getint(4, fp);
135 if (l == EOF && feof(fp)) /* EOF? */
136 return((double)EOF);
137 if (l == 0) {
138 getc(fp); /* exactly zero -- ignore exponent */
139 return(0.0);
140 }
141 d = (l + (l > 0 ? .5 : -.5)) * (1./0x7fffffff);
142 return(ldexp(d, (int)getint(1, fp)));
143 }
144
145
146 int
147 getbinary( /* fread() replacement for small objects */
148 void *p,
149 int elsiz,
150 int nel,
151 FILE *fp)
152 {
153 char *s = (char *)p;
154 int nbytes = elsiz*nel;
155 int c;
156
157 if (nbytes > 128)
158 return(fread(p, elsiz, nel, fp));
159
160 while (nbytes-- > 0) {
161 if ((c = getc(fp)) == EOF)
162 return((elsiz*nel - nbytes)/elsiz);
163 *s++ = c;
164 }
165 return(nel);
166 }