--- ray/src/common/portio.c 2012/10/19 01:40:33 2.15 +++ ray/src/common/portio.c 2019/07/05 03:04:22 2.21 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: portio.c,v 2.15 2012/10/19 01:40:33 greg Exp $"; +static const char RCSid[] = "$Id: portio.c,v 2.21 2019/07/05 03:04:22 greg Exp $"; #endif /* * Portable i/o for binary files @@ -13,14 +13,7 @@ static const char RCSid[] = "$Id: portio.c,v 2.15 2012 #include -#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ -#undef getc -#undef putc -#define getc getc_unlocked -#define putc putc_unlocked -#endif - void putstr( /* write null-terminated string to fp */ char *s, @@ -40,8 +33,9 @@ putint( /* write a siz-byte integer to fp */ FILE *fp ) { - while (siz--) - putc((int)(i>>(siz<<3) & 0xff), fp); + siz <<= 3; + while ((siz -= 8) >= 0) + putc((int)(i>>siz & 0xff), fp); } @@ -67,6 +61,27 @@ putflt( /* put out floating point number */ } +int +putbinary( /* fwrite() replacement for small objects */ + const void *p, + int elsiz, + int nel, + FILE *fp) +{ + const char *s = (const char *)p; + int nbytes = elsiz*nel; + + if (nbytes > 256) + return(fwrite(p, elsiz, nel, fp)); + + while (nbytes-- > 0) + if (putc(*s++, fp) == EOF) + return((elsiz*nel - nbytes)/elsiz); + + return(nel); +} + + char * getstr( /* get null-terminated string */ char *s, @@ -124,4 +139,27 @@ getflt( /* get a floating point number */ } d = (l + (l > 0 ? .5 : -.5)) * (1./0x7fffffff); return(ldexp(d, (int)getint(1, fp))); +} + + +int +getbinary( /* fread() replacement for small objects */ + void *p, + int elsiz, + int nel, + FILE *fp) +{ + char *s = (char *)p; + int nbytes = elsiz*nel; + int c; + + if (nbytes > 256) + return(fread(p, elsiz, nel, fp)); + + while (nbytes-- > 0) { + if ((c = getc(fp)) == EOF) + return((elsiz*nel - nbytes)/elsiz); + *s++ = c; + } + return(nel); }