--- ray/src/util/rcollate.c 2020/09/07 17:08:08 2.36 +++ ray/src/util/rcollate.c 2022/03/16 17:36:45 2.40 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.36 2020/09/07 17:08:08 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) @@ -19,6 +19,8 @@ static const char RCSid[] = "$Id: rcollate.c,v 2.36 20 #include #endif +static char delims[] = " \t\n\r\f"; + #define MAXLEVELS 16 /* max RxC.. block pairs */ typedef struct { @@ -29,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) @@ -87,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; @@ -112,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; @@ -138,6 +153,7 @@ load_file(MEMLOAD *mp, FILE *fp) } return(1); } +#endif /* free a record index */ #define free_records(rp) free(rp) @@ -183,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); } @@ -218,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; @@ -226,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)) { @@ -236,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 */ @@ -281,20 +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 */ -int check = 0; /* force data check? */ - /* 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/ni_rows; c = n - r*ni_rows; - n = (long)c*ni_columns + r; + 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,7 +417,7 @@ 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) @@ -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); @@ -432,7 +445,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 != ni_rows*ni_columns) + if (nrecords != (ssize_t)ni_rows*ni_columns) goto badspec; if (transpose) { if (no_columns <= 0) @@ -456,18 +469,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++) { - 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, @@ -475,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); } } @@ -483,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); } @@ -491,7 +506,7 @@ 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]; @@ -500,6 +515,7 @@ 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 */ @@ -507,7 +523,7 @@ do_resize(FILE *fp) (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)) @@ -527,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); @@ -537,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? */ @@ -548,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); } @@ -702,6 +724,10 @@ 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 & !check & (outLevels <= 1) & (i_header == o_header) && (no_columns == ni_columns) & (no_rows == ni_rows)) { @@ -728,7 +754,6 @@ main(int argc, char *argv[]) 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)) { MEMLOAD myMem; /* need to map into memory */