ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/portio.c
Revision: 2.27
Committed: Mon Jul 12 17:42:51 2021 UTC (2 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 2.26: +2 -2 lines
Log Message:
perf: minor efficiency improvements

File Contents

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