--- ray/src/util/rcollate.c 2014/07/09 21:45:48 2.16 +++ 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.16 2014/07/09 21:45:48 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.16 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) @@ -381,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); @@ -572,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]); @@ -603,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", @@ -617,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: