--- ray/src/util/rcollate.c 2013/09/05 17:53:23 2.1 +++ ray/src/util/rcollate.c 2014/07/09 21:45:48 2.16 @@ -1,18 +1,22 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.1 2013/09/05 17:53:23 greg Exp $"; +static const char RCSid[] = "$Id: rcollate.c,v 2.16 2014/07/09 21:45:48 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 @@ -35,6 +39,8 @@ typedef struct { char *rec[1]; /* record array (extends struct) */ } RECINDEX; +int warnings = 1; /* report warnings? */ + /* free loaded file */ static void free_load(MEMLOAD *mp) @@ -73,8 +79,7 @@ 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|PROT_WRITE, - MAP_PRIVATE, fd, skip); + mp->base = mmap(NULL, mp->len, PROT_READ, MAP_PRIVATE, fd, skip); if (mp->base != MAP_FAILED) { mp->mapped = 1; return(1); /* mmap() success */ @@ -98,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; @@ -109,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); @@ -122,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); } @@ -228,11 +237,11 @@ output_stream(FILE *fp) if (fp == NULL) return(0); - fflush(stdout); /* assumes nothing in input buffer */ - while ((n = read(fileno(fp), buf, sizeof(buf))) > 0) + fflush(stdout); + while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) if (write(fileno(stdout), buf, n) != n) return(0); - return(n >= 0); + return(!ferror(fp)); } /* get next word from stream, leaving stream on EOL or start of next word */ @@ -258,13 +267,46 @@ fget_word(char buf[256], FILE *fp) return(buf); } -char *fmtid = "ascii"; /* format id */ -int record_width = 3; /* words/record (<0 binary) */ +char *fmtid = NULL; /* format id */ +int comp_size = 0; /* binary bytes/channel */ +int n_comp = 0; /* components/record */ 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 +check_sizes() +{ + if (fmtid == NULL) { + fmtid = "ascii"; + } else if (!comp_size) { + if (!strcmp(fmtid, "float")) + comp_size = sizeof(float); + else if (!strcmp(fmtid, "double")) + comp_size = sizeof(double); + else if (!strcmp(fmtid, "byte")) + comp_size = 1; + else if (strcmp(fmtid, "ascii")) { + fprintf(stderr, "Unsupported format: %s\n", fmtid); + 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); +} + /* output transposed ASCII or binary data from memory */ static int do_transpose(const MEMLOAD *mp) @@ -273,22 +315,28 @@ 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) + if (!comp_size) { + if ((rp = index_records(mp, n_comp)) == NULL) return(0); if (ni_columns <= 0) ni_columns = count_columns(rp); nrecords = rp->nrecs; - } else if ((ni_rows > 0) & (ni_columns > 0)) + } else if ((ni_rows > 0) & (ni_columns > 0)) { nrecords = ni_rows*ni_columns; - else - nrecords = mp->len / -record_width; + if (nrecords > mp->len/(n_comp*comp_size)) { + fprintf(stderr, + "Input too small for specified size and type\n"); + return(0); + } + } else + nrecords = mp->len/(n_comp*comp_size); /* 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)) @@ -309,8 +357,8 @@ do_transpose(const MEMLOAD *mp) putc(tabEOL[j >= no_columns-1], stdout); } else { /* binary output */ fwrite((char *)mp->base + - -record_width*(j*ni_columns + i), - -record_width, 1, stdout); + (n_comp*comp_size)*(j*ni_columns + i), + n_comp*comp_size, 1, stdout); } if (ferror(stdout)) { fprintf(stderr, "Error writing to stdout\n"); @@ -333,11 +381,8 @@ do_resize(FILE *fp) int columns2go = no_columns; char word[256]; /* sanity checks */ - if (record_width <= 0) { - fprintf(stderr, "Bad call to do_resize (record_width = %d)\n", - record_width); - return(0); - } + if (comp_size) + return(output_stream(fp)); /* binary data -- just copy */ if (no_columns <= 0) { fprintf(stderr, "Missing -oc specification\n"); return(0); @@ -353,9 +398,9 @@ do_resize(FILE *fp) do { /* reshape records */ int n; - for (n = record_width; n--; ) { + for (n = n_comp; n--; ) { if (fget_word(word, fp) == NULL) { - if (records2go > 0 || n < record_width-1) + if (records2go > 0 || n < n_comp-1) break; goto done; /* normal EOD */ } @@ -379,10 +424,10 @@ do_resize(FILE *fp) putc('\t', stdout); } while (--records2go); /* expected EOD? */ done: - if (columns2go != no_columns) + if (warnings && columns2go != no_columns) fprintf(stderr, "Warning -- incomplete final row\n"); - if (fget_word(word, fp) != NULL) - fprintf(stderr, "Warning -- data beyond expected EOF\n"); + if (warnings && fget_word(word, fp) != NULL) + fprintf(stderr, "Warning -- characters beyond expected EOD\n"); return(1); } @@ -390,15 +435,48 @@ done: static int headline(char *s, void *p) { - char fmt[32]; + static char fmt[32]; + int n; if (formatval(fmt, s)) { + if (fmtid == NULL) { + fmtid = fmt; + return(0); + } if (!strcmp(fmt, fmtid)) return(0); fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid); return(-1); } - fputs(s, stdout); /* copy header info. */ + if (!strncmp(s, "NROWS=", 6)) { + n = atoi(s+6); + if ((ni_rows > 0) & (n != ni_rows)) { + fputs("Incorrect input row count\n", stderr); + return(-1); + } + ni_rows = n; + return(0); + } + if (!strncmp(s, "NCOLS=", 6)) { + n = atoi(s+6); + if ((ni_columns > 0) & (n != ni_columns)) { + fputs("Incorrect input column count\n", stderr); + return(-1); + } + ni_columns = n; + return(0); + } + if (!strncmp(s, "NCOMP=", 6)) { + n = atoi(s+6); + if ((n_comp > 0) & (n != n_comp)) { + fputs("Incorrect number of components\n", stderr); + return(-1); + } + n_comp = n; + return(0); + } + if (o_header) + fputs(s, stdout); /* copy header info. */ return(0); } @@ -406,103 +484,130 @@ headline(char *s, void *p) int main(int argc, char *argv[]) { - int do_header = 1; /* header i/o? */ - int transpose = 0; /* transpose rows & cols? */ - int i; + int a; - for (i = 1; i < argc && argv[i][0] == '-'; i++) - switch (argv[i][1]) { + for (a = 1; a < argc && argv[a][0] == '-'; a++) + switch (argv[a][1]) { case 'i': /* input */ - if (argv[i][2] == 'c') /* columns */ - ni_columns = atoi(argv[++i]); - else if (argv[i][2] == 'r') - ni_rows = atoi(argv[++i]); + if (argv[a][2] == 'c') /* columns */ + ni_columns = atoi(argv[++a]); + else if (argv[a][2] == 'r') + ni_rows = atoi(argv[++a]); else goto userr; break; case 'o': /* output */ - if (argv[i][2] == 'c') /* columns */ - no_columns = atoi(argv[++i]); - else if (argv[i][2] == 'r') - no_rows = atoi(argv[++i]); + if (argv[a][2] == 'c') /* columns */ + no_columns = atoi(argv[++a]); + else if (argv[a][2] == 'r') + no_rows = atoi(argv[++a]); else goto userr; break; - case 'h': /* header on/off */ - do_header = !do_header; + case 'h': /* turn off header */ + switch (argv[a][2]) { + case 'i': + i_header = 0; + break; + case 'o': + o_header = 0; + break; + case '\0': + i_header = o_header = 0; + break; + default: + goto userr; + } break; case 't': /* transpose on/off */ transpose = !transpose; break; case 'f': /* format */ - switch (argv[i][2]) { + switch (argv[a][2]) { case 'a': /* ASCII */ case 'A': fmtid = "ascii"; - record_width = 1; + comp_size = 0; break; case 'f': /* float */ case 'F': fmtid = "float"; - record_width = -(int)sizeof(float); + comp_size = sizeof(float); break; case 'd': /* double */ case 'D': fmtid = "double"; - record_width = -(int)sizeof(double); + comp_size = sizeof(double); break; case 'b': /* binary (bytes) */ case 'B': fmtid = "byte"; - record_width = -1; + comp_size = 1; break; default: goto userr; } - if (argv[i][3]) { - if (!isdigit(argv[i][3])) + if (argv[a][3]) { + if (!isdigit(argv[a][3])) goto userr; - record_width *= atoi(argv[i]+3); - } + n_comp = atoi(argv[a]+3); + } else + n_comp = 1; break; + case 'w': /* warnings on/off */ + warnings = !warnings; + break; default: goto userr; } - if (!record_width) + if (a < argc-1) /* arg count OK? */ goto userr; - if (i < argc-1) /* arg count OK? */ - goto userr; /* open input file? */ - if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { - fprintf(stderr, "%s: cannot open for reading\n", argv[i]); + if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) { + fprintf(stderr, "%s: cannot open for reading\n", argv[a]); return(1); } - if (record_width < 0) { + if (comp_size) { SET_FILE_BINARY(stdin); SET_FILE_BINARY(stdout); } /* check for no-op */ - if (!transpose && (record_width < 0 || + if (!transpose & (i_header == o_header) && (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows))) { - fprintf(stderr, "%s: no-op -- copying input verbatim\n", + if (warnings) + fprintf(stderr, "%s: no-op -- copying input verbatim\n", argv[0]); if (!output_stream(stdin)) return(1); return(0); } - if (do_header) { /* read/write header */ - if (getheader(stdin, &headline, NULL) < 0) + if (i_header) { /* read header */ + if (getheader(stdin, headline, NULL) < 0) return(1); - printargs(argc, argv, stdout); + if (!check_sizes()) + return(1); + if (comp_size) { /* a little late... */ + SET_FILE_BINARY(stdin); + SET_FILE_BINARY(stdout); + } + } else if (!check_sizes()) + return(1); + if (o_header) { /* write header */ + printargs(a, argv, stdout); + if (no_rows > 0) + printf("NROWS=%d\n", no_rows); + if (no_columns > 0) + printf("NCOLS=%d\n", no_columns); + printf("NCOMP=%d\n", n_comp); fputformat(fmtid, stdout); fputc('\n', stdout); /* finish new header */ } if (transpose) { /* transposing rows & columns? */ MEMLOAD myMem; /* need to load into memory */ - if (i == argc-1) { + if (a == argc-1) { if (load_file(&myMem, stdin) <= 0) { fprintf(stderr, "%s: error loading file into memory\n", - argv[i]); + argv[a]); return(1); } } else if (load_stream(&myMem, stdin) <= 0) { @@ -518,7 +623,7 @@ main(int argc, char *argv[]) return(0); userr: fprintf(stderr, -"Usage: %s [-h][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", +"Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", argv[0]); return(1); }