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