--- ray/src/util/rmatrix.c 2015/07/22 04:29:56 2.16 +++ ray/src/util/rmatrix.c 2016/08/18 00:52:48 2.21 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rmatrix.c,v 2.16 2015/07/22 04:29:56 greg Exp $"; +static const char RCSid[] = "$Id: rmatrix.c,v 2.21 2016/08/18 00:52:48 greg Exp $"; #endif /* * General matrix operations. @@ -9,8 +9,10 @@ static const char RCSid[] = "$Id: rmatrix.c,v 2.16 201 #include #include #include +#include "rtio.h" +#include "platform.h" #include "resolu.h" -#include "rtprocess.h" +#include "paths.h" #include "rmatrix.h" static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; @@ -111,9 +113,7 @@ static int rmx_load_ascii(RMATRIX *rm, FILE *fp) { int i, j, k; -#ifdef _WIN32 - _setmode(fileno(fp), _O_TEXT); -#endif + for (i = 0; i < rm->nrows; i++) for (j = 0; j < rm->ncols; j++) for (k = 0; k < rm->ncomp; k++) @@ -134,7 +134,7 @@ rmx_load_float(RMATRIX *rm, FILE *fp) } for (i = 0; i < rm->nrows; i++) for (j = 0; j < rm->ncols; j++) { - if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) + if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) return(0); for (k = rm->ncomp; k--; ) rmx_lval(rm,i,j,k) = val[k]; @@ -154,7 +154,7 @@ rmx_load_double(RMATRIX *rm, FILE *fp) } for (i = 0; i < rm->nrows; i++) for (j = 0; j < rm->ncols; j++) { - if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) + if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) return(0); for (k = rm->ncomp; k--; ) rmx_lval(rm,i,j,k) = val[k]; @@ -195,13 +195,13 @@ rmx_load(const char *inspec) if (inspec == NULL) { /* reading from stdin? */ inspec = ""; -#ifdef _WIN32 +#if defined(_WIN32) || defined(_WIN64) _setmode(fileno(stdin), _O_BINARY); #endif } else if (inspec[0] == '!') { if ((fp = popen(inspec+1, "r")) == NULL) return(NULL); -#ifdef _WIN32 +#if defined(_WIN32) || defined(_WIN64) _setmode(fileno(fp), _O_BINARY); #endif } else { @@ -216,6 +216,7 @@ rmx_load(const char *inspec) return(NULL); dnew = rmx_from_cmatrix(cm); cm_free(cm); + dnew->dtype = DTascii; return(dnew); } /* else open it ourselves */ @@ -253,6 +254,9 @@ rmx_load(const char *inspec) dnew->info = dinfo.info; switch (dinfo.dtype) { case DTascii: +#if defined(_WIN32) || defined(_WIN64) + _setmode(fileno(fp), _O_TEXT); +#endif if (!rmx_load_ascii(dnew, fp)) goto loaderr; dnew->dtype = DTascii; /* should leave double? */ @@ -300,9 +304,7 @@ static int rmx_write_ascii(const RMATRIX *rm, FILE *fp) { int i, j, k; -#ifdef _WIN32 - _setmode(fileno(fp), _O_TEXT); -#endif + for (i = 0; i < rm->nrows; i++) { for (j = 0; j < rm->ncols; j++) { for (k = 0; k < rm->ncomp; k++) @@ -328,7 +330,7 @@ rmx_write_float(const RMATRIX *rm, FILE *fp) for (j = 0; j < rm->ncols; j++) { for (k = rm->ncomp; k--; ) val[k] = (float)rmx_lval(rm,i,j,k); - if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) + if (putbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) return(0); } return(1); @@ -348,7 +350,7 @@ rmx_write_double(const RMATRIX *rm, FILE *fp) for (j = 0; j < rm->ncols; j++) { for (k = rm->ncomp; k--; ) val[k] = rmx_lval(rm,i,j,k); - if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) + if (putbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) return(0); } return(1); @@ -385,6 +387,9 @@ rmx_write(const RMATRIX *rm, int dtype, FILE *fp) if ((rm == NULL) | (fp == NULL)) return(0); +#ifdef getc_unlocked + flockfile(fp); +#endif /* complete header */ if (rm->info) fputs(rm->info, fp); @@ -429,6 +434,9 @@ rmx_write(const RMATRIX *rm, int dtype, FILE *fp) return(0); } ok &= (fflush(fp) == 0); +#ifdef getc_unlocked + funlockfile(fp); +#endif rmx_free(mydm); return(ok); }