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