--- ray/src/util/rcollate.c 2014/08/01 18:21:04 2.18 +++ ray/src/util/rcollate.c 2016/04/16 00:42:16 2.24 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.18 2014/08/01 18:21:04 greg Exp $"; +static const char RCSid[] = "$Id: rcollate.c,v 2.24 2016/04/16 00:42:16 greg 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.18 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) @@ -141,6 +138,7 @@ load_stream(MEMLOAD *mp, FILE *fp) static RECINDEX * index_records(const MEMLOAD *mp, int nw_rec) { + int nall = 0; RECINDEX *rp; char *cp, *mend; int n; @@ -149,7 +147,8 @@ index_records(const MEMLOAD *mp, int nw_rec) return(NULL); if (nw_rec <= 0) return(NULL); - rp = (RECINDEX *)malloc(sizeof(RECINDEX) + mp->len/(2*nw_rec)*sizeof(char *)); + nall = 1000; + rp = (RECINDEX *)malloc(sizeof(RECINDEX) + nall*sizeof(char *)); if (rp == NULL) return(NULL); rp->nw_rec = nw_rec; @@ -161,6 +160,13 @@ index_records(const MEMLOAD *mp, int nw_rec) ++cp; if (cp >= mend) break; + if (rp->nrecs >= nall) { + nall += nall>>1; /* get more record space */ + rp = (RECINDEX *)realloc(rp, + sizeof(RECINDEX) + nall*sizeof(char *)); + if (rp == NULL) + return(NULL); + } rp->rec[rp->nrecs++] = cp; /* point to first non-white */ n = rp->nw_rec; while (++cp < mend) /* find end of record */ @@ -381,8 +387,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 +578,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 +609,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", @@ -618,9 +624,6 @@ main(int argc, char *argv[]) if (!do_transpose(&myMem)) return(1); /* free_load(&myMem); about to exit, so don't bother */ - } else if (comp_size || (no_columns==ni_columns) & (no_rows==ni_rows)) { - if (!output_stream(stdin)) /* just changed header */ - return(1); } else if (!do_resize(stdin)) /* reshaping input */ return(1); return(0);