4 |
|
/* |
5 |
|
* Portable i/o for binary files |
6 |
|
* |
7 |
< |
* External symbols declared in standard.h |
7 |
> |
* External symbols declared in rtio.h |
8 |
|
*/ |
9 |
|
|
10 |
|
#include "copyright.h" |
11 |
|
|
12 |
– |
#include <stdio.h> |
13 |
– |
|
12 |
|
#include "rtio.h" |
13 |
|
|
14 |
|
#include <math.h> |
15 |
|
|
16 |
|
|
17 |
|
void |
18 |
< |
putstr(s, fp) /* write null-terminated string to fp */ |
19 |
< |
register char *s; |
20 |
< |
register FILE *fp; |
18 |
> |
putstr( /* write null-terminated string to fp */ |
19 |
> |
char *s, |
20 |
> |
FILE *fp |
21 |
> |
) |
22 |
|
{ |
23 |
|
do |
24 |
|
putc(*s, fp); |
27 |
|
|
28 |
|
|
29 |
|
void |
30 |
< |
putint(i, siz, fp) /* write a siz-byte integer to fp */ |
31 |
< |
long i; |
32 |
< |
register int siz; |
33 |
< |
register FILE *fp; |
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); |
39 |
|
|
40 |
|
|
41 |
|
void |
42 |
< |
putflt(f, fp) /* put out floating point number */ |
43 |
< |
double f; |
44 |
< |
FILE *fp; |
42 |
> |
putflt( /* put out floating point number */ |
43 |
> |
double f, |
44 |
> |
FILE *fp |
45 |
> |
) |
46 |
|
{ |
47 |
+ |
long m; |
48 |
|
int e; |
49 |
|
|
50 |
< |
putint((long)(frexp(f,&e)*0x7fffffff), 4, fp); |
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(s, fp) /* get null-terminated string */ |
84 |
< |
char *s; |
85 |
< |
register FILE *fp; |
83 |
> |
getstr( /* get null-terminated string */ |
84 |
> |
char *s, |
85 |
> |
FILE *fp |
86 |
> |
) |
87 |
|
{ |
88 |
< |
register char *cp; |
89 |
< |
register int c; |
88 |
> |
char *cp; |
89 |
> |
int c; |
90 |
|
|
91 |
|
cp = s; |
92 |
|
while ((c = getc(fp)) != EOF) |
98 |
|
|
99 |
|
|
100 |
|
long |
101 |
< |
getint(siz, fp) /* get a siz-byte integer */ |
102 |
< |
int siz; |
103 |
< |
register FILE *fp; |
101 |
> |
getint( /* get a siz-byte integer */ |
102 |
> |
int siz, |
103 |
> |
FILE *fp |
104 |
> |
) |
105 |
|
{ |
106 |
< |
register int c; |
107 |
< |
register long r; |
106 |
> |
int c; |
107 |
> |
long r; |
108 |
|
|
109 |
|
if ((c = getc(fp)) == EOF) |
110 |
|
return(EOF); |
120 |
|
|
121 |
|
|
122 |
|
double |
123 |
< |
getflt(fp) /* get a floating point number */ |
124 |
< |
FILE *fp; |
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 |
|
} |