--- ray/src/util/rcollate.c 2021/01/15 17:22:23 2.37 +++ ray/src/util/rcollate.c 2024/01/26 00:47:17 2.45 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.37 2021/01/15 17:22:23 greg Exp $"; +static const char RCSid[] = "$Id: rcollate.c,v 2.45 2024/01/26 00:47:17 greg Exp $"; #endif /* * Utility to re-order records in a binary or ASCII data file (matrix) @@ -29,12 +29,26 @@ typedef struct { typedef struct { int nw_rec; /* number of words per record */ - ssize_t nrecs; /* number of records we found */ + size_t nrecs; /* number of records we found */ char *rec[1]; /* record array (extends struct) */ } RECINDEX; -int warnings = 1; /* report warnings? */ +int warnings = 1; /* report warnings? */ +char *fmtid = NULL; /* format id */ +int comp_size = 0; /* binary bytes/channel */ +int n_comp = 0; /* components/record */ +int ni_columns = 0; /* number of input columns */ +int ni_rows = 0; /* number of input rows */ +int no_columns = 0; /* number of output columns */ +int no_rows = 0; /* number of output rows */ +int transpose = 0; /* transpose rows & cols? */ +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? */ + /* free loaded file */ static void free_load(MEMLOAD *mp) @@ -87,17 +101,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; @@ -112,7 +125,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; @@ -138,6 +151,7 @@ load_file(MEMLOAD *mp, FILE *fp) } return(1); } +#endif /* free a record index */ #define free_records(rp) free(rp) @@ -183,8 +197,7 @@ index_records(const MEMLOAD *mp, int nw_rec) break; /* got requisite # words */ do { /* else find next word */ if (*cp == '\n') { - fprintf(stderr, - "Unexpected EOL in record!\n"); + fputs("Unexpected EOL in record!\n", stderr); free_records(rp); return(NULL); } @@ -218,14 +231,18 @@ count_columns(const RECINDEX *rp) /* copy nth record from index to stdout */ static int -print_record(const RECINDEX *rp, ssize_t n) +print_record(const RECINDEX *rp, size_t n) { - int words2go = rp->nw_rec; - char *scp; + static char delims[] = " \t\n\r\f"; + int words2go = rp->nw_rec; + char *scp; - if ((n < 0) | (n >= rp->nrecs)) + if (n >= rp->nrecs) return(0); scp = rp->rec[n]; + + if (check && !isfltd(scp, delims)) + goto formerr; do { putc(*scp++, stdout); if (!*scp | isspace(*scp)) { @@ -236,26 +253,38 @@ print_record(const RECINDEX *rp, ssize_t n) if (++scp >= rp->rec[n+1]) break; while (!*scp | isspace(*scp)); + + if (check && !isfltd(scp, delims)) + goto formerr; } } while (scp < rp->rec[n+1]); /* caller adds record sep. */ return(1); +formerr: + fputs("Badly formed number: ", stderr); + while (*scp && !isspace(*scp)) + fputc(*scp++, stderr); + fputc('\n', stderr); + return(0); } -/* copy a stream to stdout */ -static int +/* copy a stream to stdout, return bytes written or 0 on error */ +static size_t output_stream(FILE *fp) { + size_t ntot = 0; char buf[8192]; - ssize_t n; + size_t n; if (fp == NULL) return(0); fflush(stdout); - while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) + while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { if (write(fileno(stdout), buf, n) != n) return(0); - return(!ferror(fp)); + ntot += n; + } + return(!ferror(fp) * ntot); } /* get next word from stream, leaving stream on EOL or start of next word */ @@ -281,20 +310,6 @@ fget_word(char buf[256], FILE *fp) return(buf); } -char *fmtid = NULL; /* format id */ -int comp_size = 0; /* binary bytes/channel */ -int n_comp = 0; /* components/record */ -int ni_columns = 0; /* number of input columns */ -int ni_rows = 0; /* number of input rows */ -int no_columns = 0; /* number of output columns */ -int no_rows = 0; /* number of output rows */ -int transpose = 0; /* transpose rows & cols? */ -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 get_array(const char *spec, int blklvl[][2], int nlvls) @@ -351,19 +366,23 @@ check_sizes() if (transpose && (no_rows <= 0) & (no_columns <= 0)) { if (ni_rows > 0) no_columns = ni_rows; if (ni_columns > 0) no_rows = ni_columns; - } else if ((no_rows <= 0) & (no_columns > 0) && - !((ni_rows*ni_columns) % no_columns)) - no_rows = ni_rows*ni_columns/no_columns; + } else { + if (no_columns <= 0) + no_columns = ni_columns; + if ((no_rows <= 0) & (no_columns > 0) && + !((ni_rows*ni_columns) % no_columns)) + no_rows = ni_rows*ni_columns/no_columns; + } if (n_comp <= 0) n_comp = 3; return(1); } /* call to compute block input position */ -static ssize_t +static size_t get_block_pos(int r, int c, int blklvl[][2], int nlvls) { - ssize_t n = 0; + size_t n = 0; while (nlvls > 1) { int sr = r/blklvl[1][0]; @@ -379,22 +398,22 @@ get_block_pos(int r, int c, int blklvl[][2], int nlvls } /* return input offset based on array ordering and transpose option */ -static ssize_t +static size_t get_input_pos(int r, int c) { - ssize_t n; + size_t n; if (outLevels > 1) { /* block reordering */ n = get_block_pos(r, c, outArray, outLevels); if (transpose) { r = n/ni_rows; c = n - r*ni_rows; - n = (ssize_t)c*ni_columns + r; + n = (size_t)c*ni_columns + r; } } else if (transpose) /* transpose only */ - n = (ssize_t)c*ni_columns + r; + n = (size_t)c*ni_columns + r; else /* XXX should never happen! */ - n = (ssize_t)r*no_columns + c; + n = (size_t)r*no_columns + c; return(n); } @@ -404,7 +423,7 @@ do_reorder(const MEMLOAD *mp) { static const char tabEOL[2] = {'\t','\n'}; RECINDEX *rp = NULL; - ssize_t nrecords; + size_t nrecords; int i, j; /* propogate sizes */ if (ni_rows <= 0) @@ -419,7 +438,7 @@ do_reorder(const MEMLOAD *mp) ni_columns = count_columns(rp); nrecords = rp->nrecs; } else if ((ni_rows > 0) & (ni_columns > 0)) { - nrecords = (ssize_t)ni_rows*ni_columns; + nrecords = (size_t)ni_rows*ni_columns; if (nrecords > mp->len/(n_comp*comp_size)) { fputs("Input too small for specified size and type\n", stderr); @@ -432,7 +451,7 @@ do_reorder(const MEMLOAD *mp) ni_rows = nrecords/ni_columns; else if ((ni_columns <= 0) & (ni_rows > 0)) ni_columns = nrecords/ni_rows; - if (nrecords != (ssize_t)ni_rows*ni_columns) + if (nrecords != (size_t)ni_rows*ni_columns) goto badspec; if (transpose) { if (no_columns <= 0) @@ -456,18 +475,20 @@ do_reorder(const MEMLOAD *mp) 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++) { - ssize_t n = get_input_pos(i, j); + size_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); + if (!print_record(rp, n)) + return(0); putc(tabEOL[j >= no_columns-1], stdout); } else { /* binary output */ putbinary((char *)mp->base + (n_comp*comp_size)*n, @@ -475,7 +496,7 @@ do_reorder(const MEMLOAD *mp) } } if (ferror(stdout)) { - fprintf(stderr, "Error writing to stdout\n"); + fputs("Error writing to stdout\n", stderr); return(0); } } @@ -483,15 +504,15 @@ do_reorder(const MEMLOAD *mp) free_records(rp); return(1); badspec: - fprintf(stderr, "Bad dimension(s)\n"); + fputs("Bad dimension(s)\n", stderr); return(0); } -/* resize ASCII stream input by ignoring EOLs between records */ +/* resize stream input by ignoring EOLs between ASCII records */ static int do_resize(FILE *fp) { - ssize_t records2go = ni_rows*ni_columns; + size_t records2go = ni_rows*ni_columns; int columns2go = no_columns; char word[256]; @@ -500,14 +521,27 @@ do_resize(FILE *fp) 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 || !check & - (no_columns == ni_columns) & (no_rows == ni_rows)) - return(output_stream(fp)); /* no-op -- just copy */ + if (comp_size) { /* just copy binary data? */ + size_t nwritten = output_stream(fp); + if ((no_rows > 0) & (no_columns > 0) && + nwritten != (size_t)no_rows*no_columns*n_comp*comp_size) { + if (check) { + fputs("Incorrect binary file size\n", stderr); + return(0); /* fatal if check is true */ + } + if (warnings) + fputs("Warning -- unexpected binary file size\n", stderr); + } + return(nwritten > 0); + } /* else straight ASCII data copy? */ + if (!check & (no_columns == ni_columns) & (no_rows == ni_rows)) + return(output_stream(fp) > 0); + /* need to reshape (& check?) input */ if (no_columns <= 0) { - fprintf(stderr, "Missing -oc specification\n"); + fputs("Missing -oc specification\n", stderr); return(0); } if ((records2go <= 0) & (no_rows > 0)) @@ -519,14 +553,19 @@ do_resize(FILE *fp) return(0); } do { /* reshape records */ - int n; - - for (n = n_comp; n--; ) { + int n = n_comp; + while (n--) { if (fget_word(word, fp) == NULL) { - if (records2go > 0 || n < n_comp-1) + if ((records2go > 0) | (n < n_comp-1)) break; goto done; /* normal EOD */ } + if (check && !isflt(word)) { + fputs("Badly formed number: ", stderr); + fputs(word, stderr); + fputc('\n', stderr); + return(0); + } fputs(word, stdout); if (n) { /* mid-record? */ int c = getc(fp); @@ -537,7 +576,7 @@ do_resize(FILE *fp) } } if (n >= 0) { - fprintf(stderr, "Incomplete record / unexpected EOF\n"); + fputs("Incomplete record / unexpected EOF\n", stderr); return(0); } if (--columns2go <= 0) { /* time to end output row? */ @@ -547,10 +586,22 @@ do_resize(FILE *fp) putc('\t', stdout); } while (--records2go); /* expected EOD? */ done: - if (warnings && columns2go != no_columns) - fprintf(stderr, "Warning -- incomplete final row\n"); - if (warnings && fget_word(word, fp) != NULL) - fprintf(stderr, "Warning -- characters beyond expected EOD\n"); + if (columns2go != no_columns) { + if (check) { + fputs("Incomplete final row\n", stderr); + return(0); + } + if (warnings) + fputs("Warning -- incomplete final row\n", stderr); + } + if (fget_word(word, fp) != NULL) { + if (check) { + fputs("Characters beyond expected EOD\n", stderr); + return(0); + } + if (warnings) + fputs("Warning -- characters beyond expected EOD\n", stderr); + } return(1); } @@ -562,10 +613,13 @@ headline(char *s, void *p) int n; if (formatval(fmt, s)) { + if (fmtid == fmt) return(0); if (fmtid == NULL) { fmtid = fmt; return(0); } + if (!check & (comp_size == 1) & (n_comp > 1)) + return(0); /* byte exception - skip check */ if (!strcmp(fmt, fmtid)) return(0); fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid); @@ -590,6 +644,8 @@ headline(char *s, void *p) return(0); } if (!strncmp(s, "NCOMP=", 6)) { + if (!check & (comp_size == 1) & (n_comp > 1)) + return(0); /* byte exception - ignore */ n = atoi(s+6); if ((n_comp > 0) & (n != n_comp)) { fputs("Incorrect number of components\n", stderr); @@ -694,43 +750,40 @@ main(int argc, char *argv[]) no_columns = outArray[0][1]; } /* open input file? */ - if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) { + if (a == argc-1 && freopen(argv[a], "rb", stdin) == NULL) { fprintf(stderr, "%s: cannot open for reading\n", argv[a]); return(1); - } - if (comp_size) { + } else SET_FILE_BINARY(stdin); - SET_FILE_BINARY(stdout); - } + SET_FILE_BINARY(stdout); +#ifdef getc_unlocked /* avoid stupid semaphores */ + flockfile(stdin); + flockfile(stdout); +#endif /* check for no-op */ 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", argv[0]); - if (!output_stream(stdin)) - return(1); - return(0); + return(!output_stream(stdin)); } - if (i_header) { /* read header */ - if (getheader(stdin, headline, NULL) < 0) - return(1); - if (!check_sizes()) - return(1); - if (comp_size) { /* a little late... */ - SET_FILE_BINARY(stdin); - SET_FILE_BINARY(stdout); - } - } else if (!check_sizes()) + /* read input header? */ + if (i_header && getheader(stdin, headline, NULL) < 0) return(1); - if (o_header) { /* write/add to header */ + if (!check_sizes()) /* adjust sizes */ + return(1); + if (o_header) { /* add to output header? */ if (!i_header) newheader("RADIANCE", stdout); printargs(a, argv, stdout); printf("NCOMP=%d\n", n_comp); - fputformat(fmtid, stdout); } - if (transpose | check | (outLevels > 1) || (o_header && no_rows <= 0)) { + if (!comp_size) { /* a little late, here... */ + SET_FILE_TEXT(stdin); + SET_FILE_TEXT(stdout); + } + if (transpose | (outLevels > 1) || (o_header && no_rows <= 0)) { MEMLOAD myMem; /* need to map into memory */ if (a == argc-1) { if (load_file(&myMem, stdin) <= 0) {