ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/portio.c
Revision: 2.9
Committed: Thu Nov 13 16:42:17 2003 UTC (20 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +2 -7 lines
Log Message:
Added <math.h> include to get frexp() and ldexp() declarations

File Contents

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