--- ray/src/util/rcollate.c 2013/09/05 17:53:23 2.1 +++ ray/src/util/rcollate.c 2013/09/06 21:43:29 2.5 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.1 2013/09/05 17:53:23 greg Exp $"; +static const char RCSid[] = "$Id: rcollate.c,v 2.5 2013/09/06 21:43:29 greg Exp $"; #endif /* * Utility to re-order records in a binary or ASCII data file (matrix) @@ -35,6 +35,8 @@ typedef struct { char *rec[1]; /* record array (extends struct) */ } RECINDEX; +int warnings = 1; /* report warnings? */ + /* free loaded file */ static void free_load(MEMLOAD *mp) @@ -73,8 +75,7 @@ load_file(MEMLOAD *mp, FILE *fp) mp->len = (size_t)(flen - skip); #ifdef MAP_FILE if (mp->len > 1L<<20) { /* map file if > 1 MByte */ - mp->base = mmap(NULL, mp->len, PROT_READ|PROT_WRITE, - MAP_PRIVATE, fd, skip); + mp->base = mmap(NULL, mp->len, PROT_READ, MAP_PRIVATE, fd, skip); if (mp->base != MAP_FAILED) { mp->mapped = 1; return(1); /* mmap() success */ @@ -273,6 +274,11 @@ do_transpose(const MEMLOAD *mp) RECINDEX *rp = NULL; long nrecords; int i, j; + /* propogate sizes */ + if (ni_rows <= 0) + ni_rows = no_columns; + if (ni_columns <= 0) + ni_columns = no_rows; /* get # records (& index) */ if (record_width > 0) { if ((rp = index_records(mp, record_width)) == NULL) @@ -280,15 +286,16 @@ do_transpose(const MEMLOAD *mp) if (ni_columns <= 0) ni_columns = count_columns(rp); nrecords = rp->nrecs; - } else if ((ni_rows > 0) & (ni_columns > 0)) + } else if ((ni_rows > 0) & (ni_columns > 0)) { nrecords = ni_rows*ni_columns; - else + if (nrecords > mp->len / -record_width) { + fprintf(stderr, + "Input too small for specified size and type\n"); + return(0); + } + } else nrecords = mp->len / -record_width; /* check sizes */ - if (ni_rows <= 0) - ni_rows = no_columns; - if (ni_columns <= 0) - ni_columns = no_rows; if ((ni_rows <= 0) & (ni_columns > 0)) ni_rows = nrecords/ni_columns; if ((ni_columns <= 0) & (ni_rows > 0)) @@ -379,10 +386,10 @@ do_resize(FILE *fp) putc('\t', stdout); } while (--records2go); /* expected EOD? */ done: - if (columns2go != no_columns) + if (warnings && columns2go != no_columns) fprintf(stderr, "Warning -- incomplete final row\n"); - if (fget_word(word, fp) != NULL) - fprintf(stderr, "Warning -- data beyond expected EOF\n"); + if (warnings && fget_word(word, fp) != NULL) + fprintf(stderr, "Warning -- characters beyond expected EOD\n"); return(1); } @@ -465,6 +472,9 @@ main(int argc, char *argv[]) record_width *= atoi(argv[i]+3); } break; + case 'w': /* warnings on/off */ + warnings = !warnings; + break; default: goto userr; } @@ -484,7 +494,8 @@ main(int argc, char *argv[]) /* check for no-op */ if (!transpose && (record_width < 0 || (no_columns == ni_columns) & (no_rows == ni_rows))) { - fprintf(stderr, "%s: no-op -- copying input verbatim\n", + if (warnings) + fprintf(stderr, "%s: no-op -- copying input verbatim\n", argv[0]); if (!output_stream(stdin)) return(1); @@ -518,7 +529,7 @@ main(int argc, char *argv[]) return(0); userr: fprintf(stderr, -"Usage: %s [-h][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", +"Usage: %s [-h][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", argv[0]); return(1); }