Skip to content

Commit

Permalink
feat: added output stream error-checking to putstr(), putint(), and p…
Browse files Browse the repository at this point in the history
…utflt()
  • Loading branch information
Gregungory committed Feb 19, 2021
1 parent 10917d9 commit 276c4a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
19 changes: 11 additions & 8 deletions src/common/portio.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef lint
static const char RCSid[] = "$Id: portio.c,v 2.24 2021/02/19 02:59:32 greg Exp $";
static const char RCSid[] = "$Id: portio.c,v 2.25 2021/02/19 16:15:23 greg Exp $";
#endif
/*
* Portable i/o for binary files
Expand All @@ -14,19 +14,21 @@ static const char RCSid[] = "$Id: portio.c,v 2.24 2021/02/19 02:59:32 greg Exp $
#include <math.h>


void
int
putstr( /* write null-terminated string to fp */
char *s,
FILE *fp
)
{
do
putc(*s, fp);
while (*s++);
while (*++s);

return(putc(0, fp)); /* terminator */
}


void
int
putint( /* write a siz-byte integer to fp */
long i,
int siz,
Expand All @@ -40,11 +42,12 @@ putint( /* write a siz-byte integer to fp */
siz <<= 3;
while ((siz -= 8) > 0)
putc((int)(i>>siz & 0xff), fp);
putc((int)(i & 0xff), fp);

return(putc((int)(i & 0xff), fp) == EOF ? EOF : 0);
}


void
int
putflt( /* put out floating point number */
double f,
FILE *fp
Expand All @@ -62,7 +65,7 @@ putflt( /* put out floating point number */
e = 0;
}
putint(m, 4, fp);
putint(e, 1, fp);
return(putint(e, 1, fp));
}


Expand Down Expand Up @@ -118,7 +121,7 @@ getint( /* get a siz-byte integer */
return(EOF);
r = c;
if (c & 0x80) /* sign extend? */
r |= ~255L;
r |= -256L;
while (--siz > 0) {
if ((c = getc(fp)) == EOF)
return(EOF);
Expand Down
8 changes: 4 additions & 4 deletions src/common/rtio.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* RCSid $Id: rtio.h,v 3.24 2021/01/15 18:31:38 greg Exp $ */
/* RCSid $Id: rtio.h,v 3.25 2021/02/19 16:15:23 greg Exp $ */
/*
* Radiance i/o and string routines
*/
Expand Down Expand Up @@ -93,9 +93,9 @@ extern void swap16(char *wp, size_t n);
extern void swap32(char *wp, size_t n);
extern void swap64(char *wp, size_t n);
/* defined in portio.c */
extern void putstr(char *s, FILE *fp);
extern void putint(long i, int siz, FILE *fp);
extern void putflt(double f, FILE *fp);
extern int putstr(char *s, FILE *fp);
extern int putint(long i, int siz, FILE *fp);
extern int putflt(double f, FILE *fp);
extern size_t putbinary(const void *s, size_t elsiz, size_t nel, FILE *fp);
extern char *getstr(char *s, FILE *fp);
extern long getint(int siz, FILE *fp);
Expand Down

0 comments on commit 276c4a8

Please sign in to comment.