ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/portio.c
Revision: 2.4
Committed: Fri Nov 15 16:17:05 1996 UTC (27 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +1 -1 lines
Log Message:
minor improvement of getflt() accuracy (bug introduced 96/05/28

File Contents

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