--- ray/src/util/rcollate.c 2019/11/08 05:39:05 2.32 +++ ray/src/util/rcollate.c 2022/03/03 03:55:13 2.39 @@ -1,12 +1,11 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.32 2019/11/08 05:39:05 greg Exp $"; +static const char RCSid[] = "$Id: rcollate.c,v 2.39 2022/03/03 03:55:13 greg Exp $"; #endif /* * Utility to re-order records in a binary or ASCII data file (matrix) */ #include -#include #include #include "platform.h" #include "rtio.h" @@ -30,7 +29,7 @@ typedef struct { typedef struct { int nw_rec; /* number of words per record */ - long nrecs; /* number of records we found */ + ssize_t nrecs; /* number of records we found */ char *rec[1]; /* record array (extends struct) */ } RECINDEX; @@ -88,17 +87,16 @@ load_stream(MEMLOAD *mp, FILE *fp) return(mp->len > 0); } +#if defined(_WIN32) || defined(_WIN64) + /* too difficult to fix this */ +#define load_file load_stream +#else /* load a file into memory */ static int load_file(MEMLOAD *mp, FILE *fp) { int fd; off_t skip, flen, fpos; - -#if defined(_WIN32) || defined(_WIN64) - /* too difficult to fix this */ - return load_stream(mp, fp); -#endif if (mp == NULL) return(-1); mp->mapped = NULL; @@ -113,7 +111,7 @@ load_file(MEMLOAD *mp, FILE *fp) return((int)(flen - skip)); mp->len = (size_t)(flen - skip); #ifdef MAP_FILE - if (mp->len > 1L<<20) { /* map file if > 1 MByte */ + if (mp->len >= 1L<<20) { /* map file if >= 1 MByte */ mp->mapped = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0); if (mp->mapped != MAP_FAILED) { mp->base = (char *)mp->mapped + skip; @@ -139,6 +137,7 @@ load_file(MEMLOAD *mp, FILE *fp) } return(1); } +#endif /* free a record index */ #define free_records(rp) free(rp) @@ -219,7 +218,7 @@ count_columns(const RECINDEX *rp) /* copy nth record from index to stdout */ static int -print_record(const RECINDEX *rp, long n) +print_record(const RECINDEX *rp, ssize_t n) { int words2go = rp->nw_rec; char *scp; @@ -294,6 +293,7 @@ int i_header = 1; /* input header? */ int o_header = 1; /* output header? */ int outArray[MAXLEVELS][2]; /* output block nesting */ int outLevels = 0; /* number of blocking levels */ +int check = 0; /* force data check? */ /* parse RxCx... string */ static int @@ -360,10 +360,10 @@ check_sizes() } /* call to compute block input position */ -static long +static ssize_t get_block_pos(int r, int c, int blklvl[][2], int nlvls) { - long n = 0; + ssize_t n = 0; while (nlvls > 1) { int sr = r/blklvl[1][0]; @@ -379,22 +379,22 @@ get_block_pos(int r, int c, int blklvl[][2], int nlvls } /* return input offset based on array ordering and transpose option */ -static long +static ssize_t get_input_pos(int r, int c) { - long n; + ssize_t n; if (outLevels > 1) { /* block reordering */ n = get_block_pos(r, c, outArray, outLevels); if (transpose) { - r = n/no_columns; - c = n - r*no_columns; - n = (long)r*ni_columns + c; + r = n/ni_rows; + c = n - r*ni_rows; + n = (ssize_t)c*ni_columns + r; } } else if (transpose) /* transpose only */ - n = (long)c*ni_columns + r; + n = (ssize_t)c*ni_columns + r; else /* XXX should never happen! */ - n = (long)r*no_columns + c; + n = (ssize_t)r*no_columns + c; return(n); } @@ -404,13 +404,13 @@ do_reorder(const MEMLOAD *mp) { static const char tabEOL[2] = {'\t','\n'}; RECINDEX *rp = NULL; - long nrecords; + ssize_t nrecords; int i, j; /* propogate sizes */ if (ni_rows <= 0) - ni_rows = no_columns; + ni_rows = transpose ? no_columns : no_rows; if (ni_columns <= 0) - ni_columns = no_rows; + ni_columns = transpose ? no_rows : no_columns; /* get # records (& index) */ if (!comp_size) { if ((rp = index_records(mp, n_comp)) == NULL) @@ -419,10 +419,10 @@ do_reorder(const MEMLOAD *mp) ni_columns = count_columns(rp); nrecords = rp->nrecs; } else if ((ni_rows > 0) & (ni_columns > 0)) { - nrecords = ni_rows*ni_columns; + nrecords = (ssize_t)ni_rows*ni_columns; if (nrecords > mp->len/(n_comp*comp_size)) { - fprintf(stderr, - "Input too small for specified size and type\n"); + fputs("Input too small for specified size and type\n", + stderr); return(0); } } else @@ -430,22 +430,43 @@ do_reorder(const MEMLOAD *mp) /* check sizes */ if ((ni_rows <= 0) & (ni_columns > 0)) ni_rows = nrecords/ni_columns; - if ((ni_columns <= 0) & (ni_rows > 0)) + else if ((ni_columns <= 0) & (ni_rows > 0)) ni_columns = nrecords/ni_rows; - if (nrecords != ni_rows*ni_columns) + if (nrecords != (ssize_t)ni_rows*ni_columns) goto badspec; if (transpose) { if (no_columns <= 0) no_columns = ni_rows; if (no_rows <= 0) no_rows = ni_columns; - if ((no_rows != ni_columns) | (no_columns != ni_rows)) + if (outLevels <= 1 && + (no_rows != ni_columns) | (no_columns != ni_rows)) goto badspec; + } else { + if (no_columns <= 0) + no_columns = ni_columns; + if (no_rows <= 0) + no_rows = ni_rows; } + if (ni_rows*ni_columns != no_rows*no_columns) { + fputs("Number of input and output records do not match\n", + stderr); + return(0); + } + if (o_header) { /* finish header? */ + printf("NROWS=%d\n", no_rows); + printf("NCOLS=%d\n", no_columns); + fputformat(fmtid, stdout); + fputc('\n', stdout); + } /* reorder records */ for (i = 0; i < no_rows; i++) { for (j = 0; j < no_columns; j++) { - long n = get_input_pos(i, j); + ssize_t n = get_input_pos(i, j); + if (n >= nrecords) { + fputs("Index past end-of-file\n", stderr); + return(0); + } if (rp != NULL) { /* ASCII output */ print_record(rp, n); putc(tabEOL[j >= no_columns-1], stdout); @@ -471,11 +492,21 @@ badspec: static int do_resize(FILE *fp) { - long records2go = ni_rows*ni_columns; + ssize_t records2go = ni_rows*ni_columns; int columns2go = no_columns; char word[256]; + + if (o_header) { /* finish header? */ + if (no_rows > 0) + printf("NROWS=%d\n", no_rows); + if (no_columns > 0) + printf("NCOLS=%d\n", no_columns); + fputformat(fmtid, stdout); + fputc('\n', stdout); + } /* sanity checks */ - if (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows)) + if (comp_size || !check & + (no_columns == ni_columns) & (no_rows == ni_rows)) return(output_stream(fp)); /* no-op -- just copy */ if (no_columns <= 0) { fprintf(stderr, "Missing -oc specification\n"); @@ -485,7 +516,7 @@ do_resize(FILE *fp) records2go = no_rows*no_columns; else if (no_rows*no_columns != records2go) { fprintf(stderr, - "Input and output data sizes disagree (%dx%d != %dx%d)\n", + "Number of input and output records disagree (%dx%d != %dx%d)\n", ni_rows, ni_columns, no_rows, no_columns); return(0); } @@ -652,6 +683,9 @@ main(int argc, char *argv[]) case 'w': /* warnings on/off */ warnings = !warnings; break; + case 'c': /* force check operation */ + check = 1; + break; default: goto userr; } @@ -671,7 +705,7 @@ main(int argc, char *argv[]) SET_FILE_BINARY(stdout); } /* check for no-op */ - if (!transpose & (outLevels <= 1) & (i_header == o_header) && + if (!transpose & !check & (outLevels <= 1) & (i_header == o_header) && (no_columns == ni_columns) & (no_rows == ni_rows)) { if (warnings) fprintf(stderr, "%s: no-op -- copying input verbatim\n", @@ -695,15 +729,9 @@ main(int argc, char *argv[]) if (!i_header) newheader("RADIANCE", stdout); printargs(a, argv, stdout); - if (no_rows > 0) - printf("NROWS=%d\n", no_rows); - if (no_columns > 0) - printf("NCOLS=%d\n", no_columns); printf("NCOMP=%d\n", n_comp); - fputformat(fmtid, stdout); - fputc('\n', stdout); /* finish new header */ } - if (transpose | (outLevels > 1)) { /* moving stuff around? */ + if (transpose | check | (outLevels > 1) || (o_header && no_rows <= 0)) { MEMLOAD myMem; /* need to map into memory */ if (a == argc-1) { if (load_file(&myMem, stdin) <= 0) { @@ -719,12 +747,12 @@ main(int argc, char *argv[]) if (!do_reorder(&myMem)) return(1); /* free_load(&myMem); about to exit, so don't bother */ - } else if (!do_resize(stdin)) /* reshaping input */ + } else if (!do_resize(stdin)) /* just reshaping input */ return(1); return(0); userr: fprintf(stderr, -"Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n", +"Usage: %s [-h[io]][-w][-c][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n", argv[0]); return(1); }