--- ray/src/util/rcollate.c 2014/07/09 21:45:48 2.16 +++ ray/src/util/rcollate.c 2018/10/19 16:53:37 2.28 @@ -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.28 2018/10/19 16:53:37 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.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,13 +50,52 @@ 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) { int fd; - off_t skip, flen; + off_t skip, flen, fpos; +#if defined(_WIN32) || defined(_WIN64) + /* too difficult to fix this */ + return load_stream(mp, fp); +#endif if (mp == NULL) return(-1); mp->base = NULL; @@ -79,12 +111,13 @@ 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, MAP_PRIVATE, fd, skip); + mp->base = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0); if (mp->base != MAP_FAILED) { + mp->base = (char *)mp->base + skip; mp->mapped = 1; return(1); /* mmap() success */ } - mp->base = NULL; /* fall back to reading it in... */ + mp->base = NULL; /* else fall back to reading it in... */ } #endif if (lseek(fd, skip, SEEK_SET) != skip || @@ -92,48 +125,19 @@ load_file(MEMLOAD *mp, FILE *fp) mp->len = 0; return(-1); } - if (read(fd, (char *)mp->base, mp->len) != mp->len) { - free_load(mp); - return(-1); + fpos = skip; + while (fpos < flen) { /* read() fails if n > 2 GBytes */ + ssize_t nread = read(fd, (char *)mp->base+(fpos-skip), + (flen-fpos < 1L<<24) ? flen-fpos : 1L<<24); + if (nread <= 0) { + free_load(mp); + return(-1); + } + fpos += nread; } 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 +145,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 +154,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 +167,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 */ @@ -356,9 +369,9 @@ do_transpose(const MEMLOAD *mp) print_record(rp, j*ni_columns + i); putc(tabEOL[j >= no_columns-1], stdout); } else { /* binary output */ - fwrite((char *)mp->base + - (n_comp*comp_size)*(j*ni_columns + i), - n_comp*comp_size, 1, stdout); + putbinary((char *)mp->base + + (size_t)(n_comp*comp_size)*(j*ni_columns + i), + comp_size, n_comp, stdout); } if (ferror(stdout)) { fprintf(stderr, "Error writing to stdout\n"); @@ -381,8 +394,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); @@ -435,7 +448,7 @@ done: static int headline(char *s, void *p) { - static char fmt[32]; + static char fmt[MAXFMTLEN]; int n; if (formatval(fmt, s)) { @@ -572,8 +585,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 +616,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 +630,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: