Back to index

radiance  4R0+20100331
portio.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: portio.c,v 2.14 2006/12/23 17:27:45 greg Exp $";
00003 #endif
00004 /*
00005  * Portable i/o for binary files
00006  *
00007  * External symbols declared in rtio.h
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 #include "rtio.h"
00013 
00014 #include <math.h>
00015 
00016 #ifdef getc_unlocked        /* avoid horrendous overhead of flockfile */
00017 #undef getc
00018 #undef putc
00019 #define getc    getc_unlocked
00020 #define putc    putc_unlocked
00021 #endif
00022 
00023 
00024 void
00025 putstr(s, fp)               /* write null-terminated string to fp */
00026 register char  *s;
00027 register FILE  *fp;
00028 {
00029        do
00030               putc(*s, fp);
00031        while (*s++);
00032 }
00033 
00034 
00035 void
00036 putint(i, siz, fp)          /* write a siz-byte integer to fp */
00037 long  i;
00038 register int  siz;
00039 register FILE  *fp;
00040 {
00041        while (siz--)
00042               putc((int)(i>>(siz<<3) & 0xff), fp);
00043 }
00044 
00045 
00046 void
00047 putflt(f, fp)               /* put out floating point number */
00048 double f;
00049 FILE  *fp;
00050 {
00051        long  m;
00052        int  e;
00053 
00054        m = frexp(f, &e) * 0x7fffffff;
00055        if (e > 127) {                     /* overflow */
00056               m = m > 0 ? (long)0x7fffffff : -(long)0x7fffffff;
00057               e = 127;
00058        } else if (e < -128) {             /* underflow */
00059               m = 0;
00060               e = 0;
00061        }
00062        putint(m, 4, fp);
00063        putint((long)e, 1, fp);
00064 }
00065 
00066 
00067 char *
00068 getstr(s, fp)               /* get null-terminated string */
00069 char  *s;
00070 register FILE  *fp;
00071 {
00072        register char  *cp;
00073        register int  c;
00074 
00075        cp = s;
00076        while ((c = getc(fp)) != EOF)
00077               if ((*cp++ = c) == '\0')
00078                      return(s);
00079               
00080        return(NULL);
00081 }
00082 
00083 
00084 long
00085 getint(siz, fp)                    /* get a siz-byte integer */
00086 int  siz;
00087 register FILE  *fp;
00088 {
00089        register int  c;
00090        register long  r;
00091 
00092        if ((c = getc(fp)) == EOF)
00093               return(EOF);
00094        r = 0x80&c ? -1<<8|c : c;          /* sign extend */
00095        while (--siz > 0) {
00096               if ((c = getc(fp)) == EOF)
00097                      return(EOF);
00098               r <<= 8;
00099               r |= c;
00100        }
00101        return(r);
00102 }
00103 
00104 
00105 double
00106 getflt(fp)                  /* get a floating point number */
00107 FILE  *fp;
00108 {
00109        long   l;
00110        double d;
00111 
00112        l = getint(4, fp);
00113        if (l == 0) {
00114               getc(fp);            /* exactly zero -- ignore exponent */
00115               return(0.0);
00116        }
00117        d = (l + (l > 0 ? .5 : -.5)) * (1./0x7fffffff);
00118        return(ldexp(d, (int)getint(1, fp)));
00119 }