--- ray/src/common/portio.c 2003/07/17 09:21:29 2.8 +++ ray/src/common/portio.c 2016/03/03 22:06:18 2.16 @@ -1,30 +1,31 @@ #ifndef lint -static const char RCSid[] = "$Id: portio.c,v 2.8 2003/07/17 09:21:29 schorsch Exp $"; +static const char RCSid[] = "$Id: portio.c,v 2.16 2016/03/03 22:06:18 greg Exp $"; #endif /* * Portable i/o for binary files * - * External symbols declared in standard.h + * External symbols declared in rtio.h */ #include "copyright.h" -#include - #include "rtio.h" -#ifndef frexp -extern double frexp(); +#include + +#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ +#undef getc +#undef putc +#define getc getc_unlocked +#define putc putc_unlocked #endif -#ifndef ldexp -extern double ldexp(); -#endif void -putstr(s, fp) /* write null-terminated string to fp */ -register char *s; -register FILE *fp; +putstr( /* write null-terminated string to fp */ + char *s, + FILE *fp +) { do putc(*s, fp); @@ -33,10 +34,11 @@ register FILE *fp; void -putint(i, siz, fp) /* write a siz-byte integer to fp */ -long i; -register int siz; -register FILE *fp; +putint( /* write a siz-byte integer to fp */ + long i, + int siz, + FILE *fp +) { while (siz--) putc((int)(i>>(siz<<3) & 0xff), fp); @@ -44,24 +46,54 @@ register FILE *fp; void -putflt(f, fp) /* put out floating point number */ -double f; -FILE *fp; +putflt( /* put out floating point number */ + double f, + FILE *fp +) { + long m; int e; - putint((long)(frexp(f,&e)*0x7fffffff), 4, fp); + m = frexp(f, &e) * 0x7fffffff; + if (e > 127) { /* overflow */ + m = m > 0 ? (long)0x7fffffff : -(long)0x7fffffff; + e = 127; + } else if (e < -128) { /* underflow */ + m = 0; + e = 0; + } + putint(m, 4, fp); putint((long)e, 1, fp); } +int +putbinary( /* fwrite() replacement for small objects */ + char *s, + int elsiz, + int nel, + FILE *fp) +{ + int nbytes = elsiz*nel; + + if (nbytes > 512) + return(fwrite(s, elsiz, nel, fp)); + + while (nbytes-- > 0) + putc(*s++, fp); + + return(nel); +} + + char * -getstr(s, fp) /* get null-terminated string */ -char *s; -register FILE *fp; +getstr( /* get null-terminated string */ + char *s, + FILE *fp +) { - register char *cp; - register int c; + char *cp; + int c; cp = s; while ((c = getc(fp)) != EOF) @@ -73,12 +105,13 @@ register FILE *fp; long -getint(siz, fp) /* get a siz-byte integer */ -int siz; -register FILE *fp; +getint( /* get a siz-byte integer */ + int siz, + FILE *fp +) { - register int c; - register long r; + int c; + long r; if ((c = getc(fp)) == EOF) return(EOF); @@ -94,13 +127,42 @@ register FILE *fp; double -getflt(fp) /* get a floating point number */ -FILE *fp; +getflt( /* get a floating point number */ + FILE *fp +) { long l; double d; l = getint(4, fp); + if (l == EOF && feof(fp)) /* EOF? */ + return((double)EOF); + if (l == 0) { + getc(fp); /* exactly zero -- ignore exponent */ + return(0.0); + } d = (l + (l > 0 ? .5 : -.5)) * (1./0x7fffffff); return(ldexp(d, (int)getint(1, fp))); +} + + +int +getbinary( /* fread() replacement for small objects */ + char *s, + int elsiz, + int nel, + FILE *fp) +{ + int nbytes = elsiz*nel; + int c; + + if (nbytes > 512) + return(fread(s, elsiz, nel, fp)); + + while (nbytes-- > 0) { + if ((c = getc(fp)) == EOF) + return((elsiz*nel - nbytes)/elsiz); + *s++ = c; + } + return(nel); }