--- ray/src/util/rcollate.c 2013/09/06 21:34:39 2.4 +++ ray/src/util/rcollate.c 2014/02/21 13:49:00 2.8 @@ -1,18 +1,22 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.4 2013/09/06 21:34:39 greg Exp $"; +static const char RCSid[] = "$Id: rcollate.c,v 2.8 2014/02/21 13:49:00 greg Exp $"; #endif /* * Utility to re-order records in a binary or ASCII data file (matrix) */ #include -#include #include #include #include "platform.h" #include "rtio.h" #include "resolu.h" -#ifndef _WIN32 +#ifdef _WIN32 +#undef ftello +#define ftello ftell +#undef ssize_t +#define ssize_t size_t +#else #include #endif @@ -99,6 +103,7 @@ load_file(MEMLOAD *mp, FILE *fp) static int load_stream(MEMLOAD *mp, FILE *fp) { + size_t alloced = 0; char buf[8192]; size_t nr; @@ -110,10 +115,11 @@ load_stream(MEMLOAD *mp, FILE *fp) if (fp == NULL) return(-1); while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { - if (!mp->len) + if (!alloced) mp->base = malloc(nr); - else - mp->base = realloc(mp->base, mp->len+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); @@ -123,6 +129,8 @@ load_stream(MEMLOAD *mp, FILE *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); } @@ -274,6 +282,11 @@ do_transpose(const MEMLOAD *mp) RECINDEX *rp = NULL; long nrecords; int i, j; + /* propogate sizes */ + if (ni_rows <= 0) + ni_rows = no_columns; + if (ni_columns <= 0) + ni_columns = no_rows; /* get # records (& index) */ if (record_width > 0) { if ((rp = index_records(mp, record_width)) == NULL) @@ -291,10 +304,6 @@ do_transpose(const MEMLOAD *mp) } else nrecords = mp->len / -record_width; /* check sizes */ - if (ni_rows <= 0) - ni_rows = no_columns; - if (ni_columns <= 0) - ni_columns = no_rows; if ((ni_rows <= 0) & (ni_columns > 0)) ni_rows = nrecords/ni_columns; if ((ni_columns <= 0) & (ni_rows > 0))