--- ray/src/util/rcollate.c 2014/05/31 19:21:21 2.14 +++ ray/src/util/rcollate.c 2014/09/18 00:12:42 2.19 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.14 2014/05/31 19:21:21 greg Exp $"; +static const char RCSid[] = "$Id: rcollate.c,v 2.19 2014/09/18 00:12:42 greg Exp $"; #endif /* * Utility to re-order records in a binary or ASCII data file (matrix) @@ -274,6 +274,9 @@ 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? */ /* check settings and assign defaults */ static int @@ -293,6 +296,12 @@ check_sizes() return(0); } } + 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; if (n_comp <= 0) n_comp = 3; return(1); @@ -372,8 +381,8 @@ do_resize(FILE *fp) int columns2go = no_columns; char word[256]; /* sanity checks */ - if (comp_size) - return(output_stream(fp)); /* binary data -- just copy */ + if (comp_size || (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"); return(0); @@ -466,7 +475,8 @@ headline(char *s, void *p) n_comp = n; return(0); } - fputs(s, stdout); /* copy header info. */ + if (o_header) + fputs(s, stdout); /* copy header info. */ return(0); } @@ -474,9 +484,6 @@ headline(char *s, void *p) int main(int argc, char *argv[]) { - int i_header = 1; /* input header? */ - int o_header = 1; /* output header? */ - int transpose = 0; /* transpose rows & cols? */ int a; for (a = 1; a < argc && argv[a][0] == '-'; a++) @@ -565,8 +572,8 @@ main(int argc, char *argv[]) SET_FILE_BINARY(stdout); } /* check for no-op */ - if (!transpose & (i_header == o_header) && (comp_size || - (no_columns == ni_columns) & (no_rows == ni_rows))) { + if (!transpose & (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]); @@ -575,7 +582,7 @@ main(int argc, char *argv[]) return(0); } if (i_header) { /* read header */ - if (getheader(stdin, &headline, NULL) < 0) + if (getheader(stdin, headline, NULL) < 0) return(1); if (!check_sizes()) return(1); @@ -587,10 +594,6 @@ main(int argc, char *argv[]) return(1); if (o_header) { /* write header */ printargs(a, argv, stdout); - if (transpose && (no_rows <= 0) & (no_columns <= 0)) { - if (ni_rows > 0) no_columns = ni_rows; - if (ni_columns > 0) no_rows = ni_columns; - } if (no_rows > 0) printf("NROWS=%d\n", no_rows); if (no_columns > 0) @@ -600,7 +603,7 @@ main(int argc, char *argv[]) fputc('\n', stdout); /* finish new header */ } if (transpose) { /* transposing rows & columns? */ - MEMLOAD myMem; /* need to load into memory */ + MEMLOAD myMem; /* need to map into memory */ if (a == argc-1) { if (load_file(&myMem, stdin) <= 0) { fprintf(stderr, "%s: error loading file into memory\n", @@ -614,8 +617,8 @@ main(int argc, char *argv[]) } if (!do_transpose(&myMem)) return(1); - /* free_load(&myMem); */ - } else if (!do_resize(stdin)) /* just reshaping input */ + /* free_load(&myMem); about to exit, so don't bother */ + } else if (!do_resize(stdin)) /* reshaping input */ return(1); return(0); userr: