1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: portio.c,v 2.9 2003/11/13 16:42:17 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Portable i/o for binary files |
6 |
* |
7 |
* External symbols declared in standard.h |
8 |
*/ |
9 |
|
10 |
#include "copyright.h" |
11 |
|
12 |
#include <stdio.h> |
13 |
|
14 |
#include "rtio.h" |
15 |
|
16 |
#include <math.h> |
17 |
|
18 |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ |
19 |
#define getc getc_unlocked |
20 |
#define putc putc_unlocked |
21 |
#endif |
22 |
|
23 |
|
24 |
void |
25 |
putstr(s, fp) /* write null-terminated string to fp */ |
26 |
register char *s; |
27 |
register FILE *fp; |
28 |
{ |
29 |
do |
30 |
putc(*s, fp); |
31 |
while (*s++); |
32 |
} |
33 |
|
34 |
|
35 |
void |
36 |
putint(i, siz, fp) /* write a siz-byte integer to fp */ |
37 |
long i; |
38 |
register int siz; |
39 |
register FILE *fp; |
40 |
{ |
41 |
while (siz--) |
42 |
putc((int)(i>>(siz<<3) & 0xff), fp); |
43 |
} |
44 |
|
45 |
|
46 |
void |
47 |
putflt(f, fp) /* put out floating point number */ |
48 |
double f; |
49 |
FILE *fp; |
50 |
{ |
51 |
int e; |
52 |
|
53 |
putint((long)(frexp(f,&e)*0x7fffffff), 4, fp); |
54 |
putint((long)e, 1, fp); |
55 |
} |
56 |
|
57 |
|
58 |
char * |
59 |
getstr(s, fp) /* get null-terminated string */ |
60 |
char *s; |
61 |
register FILE *fp; |
62 |
{ |
63 |
register char *cp; |
64 |
register int c; |
65 |
|
66 |
cp = s; |
67 |
while ((c = getc(fp)) != EOF) |
68 |
if ((*cp++ = c) == '\0') |
69 |
return(s); |
70 |
|
71 |
return(NULL); |
72 |
} |
73 |
|
74 |
|
75 |
long |
76 |
getint(siz, fp) /* get a siz-byte integer */ |
77 |
int siz; |
78 |
register FILE *fp; |
79 |
{ |
80 |
register int c; |
81 |
register long r; |
82 |
|
83 |
if ((c = getc(fp)) == EOF) |
84 |
return(EOF); |
85 |
r = 0x80&c ? -1<<8|c : c; /* sign extend */ |
86 |
while (--siz > 0) { |
87 |
if ((c = getc(fp)) == EOF) |
88 |
return(EOF); |
89 |
r <<= 8; |
90 |
r |= c; |
91 |
} |
92 |
return(r); |
93 |
} |
94 |
|
95 |
|
96 |
double |
97 |
getflt(fp) /* get a floating point number */ |
98 |
FILE *fp; |
99 |
{ |
100 |
long l; |
101 |
double d; |
102 |
|
103 |
l = getint(4, fp); |
104 |
d = (l + (l > 0 ? .5 : -.5)) * (1./0x7fffffff); |
105 |
return(ldexp(d, (int)getint(1, fp))); |
106 |
} |