--- ray/src/util/rcollate.c 2014/05/31 19:21:21 2.14 +++ ray/src/util/rcollate.c 2016/03/06 01:13:18 2.23 @@ -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.23 2016/03/06 01:13:18 schorsch Exp $"; #endif /* * Utility to re-order records in a binary or ASCII data file (matrix) @@ -11,22 +11,15 @@ static const char RCSid[] = "$Id: rcollate.c,v 2.14 20 #include "platform.h" #include "rtio.h" #include "resolu.h" -#ifdef _WIN32 -#undef ftello -#define ftello ftell -#undef ssize_t -#define ssize_t size_t +#if defined(_WIN32) || defined(_WIN64) + #undef ftello + #define ftello ftell + #undef ssize_t + #define ssize_t size_t #else -#include + #include #endif -#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ -#undef getc -#undef putc -#define getc getc_unlocked -#define putc putc_unlocked -#endif - typedef struct { void *base; /* pointer to base memory */ size_t len; /* allocated memory length */ @@ -57,6 +50,41 @@ free_load(MEMLOAD *mp) mp->len = 0; } +/* load memory from an input stream, starting from current position */ +static int +load_stream(MEMLOAD *mp, FILE *fp) +{ + size_t alloced = 0; + char buf[8192]; + size_t nr; + + if (mp == NULL) + return(-1); + mp->base = NULL; + mp->len = 0; + mp->mapped = 0; + if (fp == NULL) + return(-1); + while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { + if (!alloced) + mp->base = malloc(alloced = nr); + else if (mp->len+nr > alloced) + mp->base = realloc(mp->base, + alloced = alloced*(2+(nr==sizeof(buf)))/2+nr); + if (mp->base == NULL) + return(-1); + memcpy((char *)mp->base + mp->len, buf, nr); + mp->len += nr; + } + if (ferror(fp)) { + free_load(mp); + return(-1); + } + if (alloced > mp->len*5/4) /* don't waste too much space */ + mp->base = realloc(mp->base, mp->len); + return(mp->len > 0); +} + /* load a file into memory */ static int load_file(MEMLOAD *mp, FILE *fp) @@ -64,6 +92,10 @@ load_file(MEMLOAD *mp, FILE *fp) int fd; off_t skip, flen; +#if defined(_WIN32) || defined(_WIN64) + /* too difficult to fix this */ + return load_stream(mp, fp); +#endif if (mp == NULL) return(-1); mp->base = NULL; @@ -99,41 +131,6 @@ load_file(MEMLOAD *mp, FILE *fp) return(1); } -/* load memory from an input stream, starting from current position */ -static int -load_stream(MEMLOAD *mp, FILE *fp) -{ - size_t alloced = 0; - char buf[8192]; - size_t nr; - - if (mp == NULL) - return(-1); - mp->base = NULL; - mp->len = 0; - mp->mapped = 0; - if (fp == NULL) - return(-1); - while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { - if (!alloced) - mp->base = malloc(nr); - else if (mp->len+nr > alloced) - mp->base = realloc(mp->base, - alloced = alloced*(2+(nr==sizeof(buf)))/2+nr); - if (mp->base == NULL) - return(-1); - memcpy((char *)mp->base + mp->len, buf, nr); - mp->len += nr; - } - if (ferror(fp)) { - free_load(mp); - return(-1); - } - if (alloced > mp->len*5/4) /* don't waste too much space */ - mp->base = realloc(mp->base, mp->len); - return(mp->len > 0); -} - /* free a record index */ #define free_records(rp) free(rp) @@ -274,6 +271,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 +293,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 +378,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 +472,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 +481,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 +569,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 +579,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 +591,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 +600,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 +614,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: