--- ray/src/util/rcollate.c 2019/11/08 16:36:36 2.33 +++ ray/src/util/rcollate.c 2022/03/16 17:36:45 2.40 @@ -1,12 +1,11 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.33 2019/11/08 16:36:36 greg Exp $"; +static const char RCSid[] = "$Id: rcollate.c,v 2.40 2022/03/16 17:36:45 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" @@ -20,6 +19,8 @@ static const char RCSid[] = "$Id: rcollate.c,v 2.33 20 #include #endif +static char delims[] = " \t\n\r\f"; + #define MAXLEVELS 16 /* max RxC.. block pairs */ typedef struct { @@ -30,12 +31,26 @@ 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; -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) @@ -88,17 +103,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 +127,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 +153,7 @@ load_file(MEMLOAD *mp, FILE *fp) } return(1); } +#endif /* free a record index */ #define free_records(rp) free(rp) @@ -184,8 +199,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); } @@ -219,7 +233,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; @@ -227,6 +241,9 @@ print_record(const RECINDEX *rp, long n) if ((n < 0) | (n >= rp->nrecs)) return(0); scp = rp->rec[n]; + + if (check && !isfltd(scp, delims)) + goto formerr; do { putc(*scp++, stdout); if (!*scp | isspace(*scp)) { @@ -237,10 +254,19 @@ print_record(const RECINDEX *rp, long 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 */ @@ -282,19 +308,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 */ - /* parse RxCx... string */ static int get_array(const char *spec, int blklvl[][2], int nlvls) @@ -360,10 +373,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 +392,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 +417,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,7 +432,7 @@ 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)) { fputs("Input too small for specified size and type\n", stderr); @@ -430,16 +443,17 @@ 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) @@ -452,16 +466,23 @@ do_reorder(const MEMLOAD *mp) 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); + 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, @@ -469,7 +490,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); } } @@ -477,7 +498,7 @@ 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); } @@ -485,21 +506,31 @@ 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"); + fputs("Missing -oc specification\n", stderr); return(0); } if ((records2go <= 0) & (no_rows > 0)) 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); } @@ -512,6 +543,12 @@ do_resize(FILE *fp) break; goto done; /* normal EOD */ } + if (check && !isfltd(word, delims)) { + fputs("Badly formed number: ", stderr); + fputs(word, stderr); + fputc('\n', stderr); + return(0); + } fputs(word, stdout); if (n) { /* mid-record? */ int c = getc(fp); @@ -522,7 +559,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? */ @@ -533,9 +570,9 @@ do_resize(FILE *fp) } while (--records2go); /* expected EOD? */ done: if (warnings && columns2go != no_columns) - fprintf(stderr, "Warning -- incomplete final row\n"); + fputs("Warning -- incomplete final row\n", stderr); if (warnings && fget_word(word, fp) != NULL) - fprintf(stderr, "Warning -- characters beyond expected EOD\n"); + fputs("Warning -- characters beyond expected EOD\n", stderr); return(1); } @@ -666,6 +703,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; } @@ -684,8 +724,12 @@ main(int argc, char *argv[]) SET_FILE_BINARY(stdin); SET_FILE_BINARY(stdout); } +#ifdef getc_unlocked /* avoid stupid semaphores */ + flockfile(stdin); + flockfile(stdout); +#endif /* 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", @@ -709,15 +753,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) { @@ -733,12 +771,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); }