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