| 6 |  | */ | 
| 7 |  |  | 
| 8 |  | #include <stdlib.h> | 
| 9 | – | #include <string.h> | 
| 9 |  | #include <ctype.h> | 
| 10 |  | #include "platform.h" | 
| 11 |  | #include "rtio.h" | 
| 12 |  | #include "resolu.h" | 
| 13 | < | #ifdef _WIN32 | 
| 14 | < | #undef ftello | 
| 15 | < | #define ftello  ftell | 
| 16 | < | #undef ssize_t | 
| 17 | < | #define ssize_t size_t | 
| 13 | > | #if defined(_WIN32) || defined(_WIN64) | 
| 14 | > | #undef ftello | 
| 15 | > | #define       ftello  ftell | 
| 16 | > | #undef ssize_t | 
| 17 | > | #define ssize_t       size_t | 
| 18 |  | #else | 
| 19 | < | #include <sys/mman.h> | 
| 19 | > | #include <sys/mman.h> | 
| 20 |  | #endif | 
| 21 |  |  | 
| 22 | < | #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */ | 
| 24 | < | #undef getc | 
| 25 | < | #undef putc | 
| 26 | < | #define getc    getc_unlocked | 
| 27 | < | #define putc    putc_unlocked | 
| 28 | < | #endif | 
| 22 | > | #define MAXLEVELS       16      /* max RxC.. block pairs */ | 
| 23 |  |  | 
| 24 |  | typedef struct { | 
| 25 | + | void    *mapped;        /* memory-mapped pointer */ | 
| 26 |  | void    *base;          /* pointer to base memory */ | 
| 27 |  | size_t  len;            /* allocated memory length */ | 
| 33 | – | int     mapped;         /* memory-mapped file? */ | 
| 28 |  | } MEMLOAD;              /* file loaded/mapped into memory */ | 
| 29 |  |  | 
| 30 |  | typedef struct { | 
| 31 |  | int     nw_rec;         /* number of words per record */ | 
| 32 | < | int     nrecs;          /* number of records we found */ | 
| 32 | > | size_t  nrecs;          /* number of records we found */ | 
| 33 |  | char    *rec[1];        /* record array (extends struct) */ | 
| 34 |  | } RECINDEX; | 
| 35 |  |  | 
| 36 | < | int             warnings = 1;   /* report warnings? */ | 
| 36 | > | int             warnings = 1;                   /* report warnings? */ | 
| 37 |  |  | 
| 38 | + | char            *fmtid = NULL;                  /* format id */ | 
| 39 | + | int             comp_size = 0;                  /* binary bytes/channel */ | 
| 40 | + | int             n_comp = 0;                     /* components/record */ | 
| 41 | + | int             ni_columns = 0;                 /* number of input columns */ | 
| 42 | + | int             ni_rows = 0;                    /* number of input rows */ | 
| 43 | + | int             no_columns = 0;                 /* number of output columns */ | 
| 44 | + | int             no_rows = 0;                    /* number of output rows */ | 
| 45 | + | int             transpose = 0;                  /* transpose rows & cols? */ | 
| 46 | + | int             i_header = 1;                   /* input header? */ | 
| 47 | + | int             o_header = 1;                   /* output header? */ | 
| 48 | + | int             outArray[MAXLEVELS][2];         /* output block nesting */ | 
| 49 | + | int             outLevels = 0;                  /* number of blocking levels */ | 
| 50 | + | int             check = 0;                      /* force data check? */ | 
| 51 | + |  | 
| 52 |  | /* free loaded file */ | 
| 53 |  | static void | 
| 54 |  | free_load(MEMLOAD *mp) | 
| 57 |  | return; | 
| 58 |  | #ifdef MAP_FILE | 
| 59 |  | if (mp->mapped) | 
| 60 | < | munmap(mp->base, mp->len); | 
| 60 | > | munmap(mp->mapped, mp->len); | 
| 61 |  | else | 
| 62 |  | #endif | 
| 63 |  | free(mp->base); | 
| 64 | + | mp->mapped = NULL; | 
| 65 |  | mp->base = NULL; | 
| 66 |  | mp->len = 0; | 
| 67 |  | } | 
| 68 |  |  | 
| 60 | – | /* load a file into memory */ | 
| 61 | – | static int | 
| 62 | – | load_file(MEMLOAD *mp, FILE *fp) | 
| 63 | – | { | 
| 64 | – | int     fd; | 
| 65 | – | off_t   skip, flen; | 
| 66 | – |  | 
| 67 | – | if (mp == NULL) | 
| 68 | – | return(-1); | 
| 69 | – | mp->base = NULL; | 
| 70 | – | mp->len = 0; | 
| 71 | – | mp->mapped = 0; | 
| 72 | – | if (fp == NULL) | 
| 73 | – | return(-1); | 
| 74 | – | fd = fileno(fp); | 
| 75 | – | skip = ftello(fp); | 
| 76 | – | flen = lseek(fd, 0, SEEK_END); | 
| 77 | – | if (flen <= skip) | 
| 78 | – | return((int)(flen - skip)); | 
| 79 | – | mp->len = (size_t)(flen - skip); | 
| 80 | – | #ifdef MAP_FILE | 
| 81 | – | if (mp->len > 1L<<20) {         /* map file if > 1 MByte */ | 
| 82 | – | mp->base = mmap(NULL, mp->len, PROT_READ, MAP_PRIVATE, fd, skip); | 
| 83 | – | if (mp->base != MAP_FAILED) { | 
| 84 | – | mp->mapped = 1; | 
| 85 | – | return(1);      /* mmap() success */ | 
| 86 | – | } | 
| 87 | – | mp->base = NULL;        /* fall back to reading it in... */ | 
| 88 | – | } | 
| 89 | – | #endif | 
| 90 | – | if (lseek(fd, skip, SEEK_SET) != skip || | 
| 91 | – | (mp->base = malloc(mp->len)) == NULL) { | 
| 92 | – | mp->len = 0; | 
| 93 | – | return(-1); | 
| 94 | – | } | 
| 95 | – | if (read(fd, (char *)mp->base, mp->len) != mp->len) { | 
| 96 | – | free_load(mp); | 
| 97 | – | return(-1); | 
| 98 | – | } | 
| 99 | – | return(1); | 
| 100 | – | } | 
| 101 | – |  | 
| 69 |  | /* load memory from an input stream, starting from current position */ | 
| 70 |  | static int | 
| 71 |  | load_stream(MEMLOAD *mp, FILE *fp) | 
| 76 |  |  | 
| 77 |  | if (mp == NULL) | 
| 78 |  | return(-1); | 
| 79 | + | mp->mapped = NULL; | 
| 80 |  | mp->base = NULL; | 
| 81 |  | mp->len = 0; | 
| 114 | – | mp->mapped = 0; | 
| 82 |  | if (fp == NULL) | 
| 83 |  | return(-1); | 
| 84 |  | while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { | 
| 85 |  | if (!alloced) | 
| 86 | < | mp->base = malloc(nr); | 
| 86 | > | mp->base = malloc(alloced = nr); | 
| 87 |  | else if (mp->len+nr > alloced) | 
| 88 |  | mp->base = realloc(mp->base, | 
| 89 |  | alloced = alloced*(2+(nr==sizeof(buf)))/2+nr); | 
| 101 |  | return(mp->len > 0); | 
| 102 |  | } | 
| 103 |  |  | 
| 104 | + | #if defined(_WIN32) || defined(_WIN64) | 
| 105 | + | /* too difficult to fix this */ | 
| 106 | + | #define load_file       load_stream | 
| 107 | + | #else | 
| 108 | + | /* load a file into memory */ | 
| 109 | + | static int | 
| 110 | + | load_file(MEMLOAD *mp, FILE *fp) | 
| 111 | + | { | 
| 112 | + | int     fd; | 
| 113 | + | off_t   skip, flen, fpos; | 
| 114 | + | if (mp == NULL) | 
| 115 | + | return(-1); | 
| 116 | + | mp->mapped = NULL; | 
| 117 | + | mp->base = NULL; | 
| 118 | + | mp->len = 0; | 
| 119 | + | if (fp == NULL) | 
| 120 | + | return(-1); | 
| 121 | + | fd = fileno(fp); | 
| 122 | + | skip = ftello(fp); | 
| 123 | + | flen = lseek(fd, 0, SEEK_END); | 
| 124 | + | if (flen <= skip) | 
| 125 | + | return((int)(flen - skip)); | 
| 126 | + | mp->len = (size_t)(flen - skip); | 
| 127 | + | #ifdef MAP_FILE | 
| 128 | + | if (mp->len >= 1L<<20) {        /* map file if >= 1 MByte */ | 
| 129 | + | mp->mapped = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0); | 
| 130 | + | if (mp->mapped != MAP_FAILED) { | 
| 131 | + | mp->base = (char *)mp->mapped + skip; | 
| 132 | + | return(1);      /* mmap() success */ | 
| 133 | + | } | 
| 134 | + | mp->mapped = NULL;      /* else fall back to reading it in... */ | 
| 135 | + | } | 
| 136 | + | #endif | 
| 137 | + | if (lseek(fd, skip, SEEK_SET) != skip || | 
| 138 | + | (mp->base = malloc(mp->len)) == NULL) { | 
| 139 | + | mp->len = 0; | 
| 140 | + | return(-1); | 
| 141 | + | } | 
| 142 | + | fpos = skip; | 
| 143 | + | while (fpos < flen) {           /* read() fails if n > 2 GBytes */ | 
| 144 | + | ssize_t nread = read(fd, (char *)mp->base+(fpos-skip), | 
| 145 | + | (flen-fpos < 1L<<24) ? flen-fpos : 1L<<24); | 
| 146 | + | if (nread <= 0) { | 
| 147 | + | free_load(mp); | 
| 148 | + | return(-1); | 
| 149 | + | } | 
| 150 | + | fpos += nread; | 
| 151 | + | } | 
| 152 | + | return(1); | 
| 153 | + | } | 
| 154 | + | #endif | 
| 155 | + |  | 
| 156 |  | /* free a record index */ | 
| 157 |  | #define free_records(rp)        free(rp) | 
| 158 |  |  | 
| 160 |  | static RECINDEX * | 
| 161 |  | index_records(const MEMLOAD *mp, int nw_rec) | 
| 162 |  | { | 
| 163 | + | int             nall = 0; | 
| 164 |  | RECINDEX        *rp; | 
| 165 |  | char            *cp, *mend; | 
| 166 |  | int             n; | 
| 169 |  | return(NULL); | 
| 170 |  | if (nw_rec <= 0) | 
| 171 |  | return(NULL); | 
| 172 | < | rp = (RECINDEX *)malloc(sizeof(RECINDEX) + mp->len/(2*nw_rec)*sizeof(char *)); | 
| 172 | > | nall = 1000; | 
| 173 | > | rp = (RECINDEX *)malloc(sizeof(RECINDEX) + nall*sizeof(char *)); | 
| 174 |  | if (rp == NULL) | 
| 175 |  | return(NULL); | 
| 176 |  | rp->nw_rec = nw_rec; | 
| 182 |  | ++cp; | 
| 183 |  | if (cp >= mend) | 
| 184 |  | break; | 
| 185 | + | if (rp->nrecs >= nall) { | 
| 186 | + | nall += nall>>1;        /* get more record space */ | 
| 187 | + | rp = (RECINDEX *)realloc(rp, | 
| 188 | + | sizeof(RECINDEX) + nall*sizeof(char *)); | 
| 189 | + | if (rp == NULL) | 
| 190 | + | return(NULL); | 
| 191 | + | } | 
| 192 |  | rp->rec[rp->nrecs++] = cp;      /* point to first non-white */ | 
| 193 |  | n = rp->nw_rec; | 
| 194 |  | while (++cp < mend)             /* find end of record */ | 
| 197 |  | break;  /* got requisite # words */ | 
| 198 |  | do {            /* else find next word */ | 
| 199 |  | if (*cp == '\n') { | 
| 200 | < | fprintf(stderr, | 
| 173 | < | "Unexpected EOL in record!\n"); | 
| 200 | > | fputs("Unexpected EOL in record!\n", stderr); | 
| 201 |  | free_records(rp); | 
| 202 |  | return(NULL); | 
| 203 |  | } | 
| 231 |  |  | 
| 232 |  | /* copy nth record from index to stdout */ | 
| 233 |  | static int | 
| 234 | < | print_record(const RECINDEX *rp, int n) | 
| 234 | > | print_record(const RECINDEX *rp, size_t n) | 
| 235 |  | { | 
| 236 | < | int     words2go = rp->nw_rec; | 
| 237 | < | char    *scp; | 
| 236 | > | static char     delims[] = " \t\n\r\f"; | 
| 237 | > | int             words2go = rp->nw_rec; | 
| 238 | > | char            *scp; | 
| 239 |  |  | 
| 240 | < | if ((n < 0) | (n >= rp->nrecs)) | 
| 240 | > | if (n >= rp->nrecs) | 
| 241 |  | return(0); | 
| 242 |  | scp = rp->rec[n]; | 
| 243 | + |  | 
| 244 | + | if (check && !isfltd(scp, delims)) | 
| 245 | + | goto formerr; | 
| 246 |  | do { | 
| 247 |  | putc(*scp++, stdout); | 
| 248 |  | if (!*scp | isspace(*scp)) { | 
| 253 |  | if (++scp >= rp->rec[n+1]) | 
| 254 |  | break; | 
| 255 |  | while (!*scp | isspace(*scp)); | 
| 256 | + |  | 
| 257 | + | if (check && !isfltd(scp, delims)) | 
| 258 | + | goto formerr; | 
| 259 |  | } | 
| 260 |  | } while (scp < rp->rec[n+1]); | 
| 261 |  | /* caller adds record sep. */ | 
| 262 |  | return(1); | 
| 263 | + | formerr: | 
| 264 | + | fputs("Badly formed number: ", stderr); | 
| 265 | + | while (*scp && !isspace(*scp)) | 
| 266 | + | fputc(*scp++, stderr); | 
| 267 | + | fputc('\n', stderr); | 
| 268 | + | return(0); | 
| 269 |  | } | 
| 270 |  |  | 
| 271 | < | /* copy a stream to stdout */ | 
| 272 | < | static int | 
| 271 | > | /* copy a stream to stdout, return bytes written or 0 on error */ | 
| 272 | > | static size_t | 
| 273 |  | output_stream(FILE *fp) | 
| 274 |  | { | 
| 275 | + | size_t  ntot = 0; | 
| 276 |  | char    buf[8192]; | 
| 277 | < | ssize_t n; | 
| 277 | > | size_t  n; | 
| 278 |  |  | 
| 279 |  | if (fp == NULL) | 
| 280 |  | return(0); | 
| 281 | < | fflush(stdout);                 /* assumes nothing in input buffer */ | 
| 282 | < | while ((n = read(fileno(fp), buf, sizeof(buf))) > 0) | 
| 281 | > | fflush(stdout); | 
| 282 | > | while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { | 
| 283 |  | if (write(fileno(stdout), buf, n) != n) | 
| 284 |  | return(0); | 
| 285 | < | return(n >= 0); | 
| 285 | > | ntot += n; | 
| 286 | > | } | 
| 287 | > | return(!ferror(fp) * ntot); | 
| 288 |  | } | 
| 289 |  |  | 
| 290 |  | /* get next word from stream, leaving stream on EOL or start of next word */ | 
| 310 |  | return(buf); | 
| 311 |  | } | 
| 312 |  |  | 
| 313 | < | char            *fmtid = "ascii";               /* format id */ | 
| 314 | < | int             record_width = 3;               /* words/record (<0 binary) */ | 
| 315 | < | int             ni_columns = 0;                 /* number of input columns */ | 
| 316 | < | int             ni_rows = 0;                    /* number of input rows */ | 
| 317 | < | int             no_columns = 0;                 /* number of output columns */ | 
| 275 | < | int             no_rows = 0;                    /* number of output rows */ | 
| 313 | > | /* parse RxCx... string */ | 
| 314 | > | static int | 
| 315 | > | get_array(const char *spec, int blklvl[][2], int nlvls) | 
| 316 | > | { | 
| 317 | > | int     n; | 
| 318 |  |  | 
| 319 | < | /* output transposed ASCII or binary data from memory */ | 
| 319 | > | if (nlvls <= 0) { | 
| 320 | > | fputs("Too many block levels!\n", stderr); | 
| 321 | > | exit(1); | 
| 322 | > | } | 
| 323 | > | if (sscanf(spec, "%dx%d", &blklvl[0][0], &blklvl[0][1]) != 2) { | 
| 324 | > | fputs("Bad block specification!\n", stderr); | 
| 325 | > | exit(1); | 
| 326 | > | } | 
| 327 | > | while (isdigit(*spec)) | 
| 328 | > | spec++; | 
| 329 | > | spec++;         /* 'x' */ | 
| 330 | > | while (isdigit(*spec)) | 
| 331 | > | spec++; | 
| 332 | > | if ((*spec != 'x') & (*spec != 'X')) { | 
| 333 | > | if (*spec) { | 
| 334 | > | fputs("Blocks must be separated by 'x' or 'X'\n", stderr); | 
| 335 | > | exit(1); | 
| 336 | > | } | 
| 337 | > | return(1); | 
| 338 | > | } | 
| 339 | > | spec++; | 
| 340 | > | n = get_array(spec, blklvl+1, nlvls-1); | 
| 341 | > | if (!n) | 
| 342 | > | return(0); | 
| 343 | > | blklvl[0][0] *= blklvl[1][0]; | 
| 344 | > | blklvl[0][1] *= blklvl[1][1]; | 
| 345 | > | return(n+1); | 
| 346 | > | } | 
| 347 | > |  | 
| 348 | > | /* check settings and assign defaults */ | 
| 349 |  | static int | 
| 350 | < | do_transpose(const MEMLOAD *mp) | 
| 350 | > | check_sizes() | 
| 351 |  | { | 
| 352 | + | if (fmtid == NULL) { | 
| 353 | + | fmtid = "ascii"; | 
| 354 | + | } else if (!comp_size) { | 
| 355 | + | if (!strcmp(fmtid, "float")) | 
| 356 | + | comp_size = sizeof(float); | 
| 357 | + | else if (!strcmp(fmtid, "double")) | 
| 358 | + | comp_size = sizeof(double); | 
| 359 | + | else if (!strcmp(fmtid, "byte")) | 
| 360 | + | comp_size = 1; | 
| 361 | + | else if (strcmp(fmtid, "ascii")) { | 
| 362 | + | fprintf(stderr, "Unsupported format: %s\n", fmtid); | 
| 363 | + | return(0); | 
| 364 | + | } | 
| 365 | + | } | 
| 366 | + | if (transpose && (no_rows <= 0) & (no_columns <= 0)) { | 
| 367 | + | if (ni_rows > 0) no_columns = ni_rows; | 
| 368 | + | if (ni_columns > 0) no_rows = ni_columns; | 
| 369 | + | } else { | 
| 370 | + | if (no_columns <= 0) | 
| 371 | + | no_columns = ni_columns; | 
| 372 | + | if ((no_rows <= 0) & (no_columns > 0) && | 
| 373 | + | !((ni_rows*ni_columns) % no_columns)) | 
| 374 | + | no_rows = ni_rows*ni_columns/no_columns; | 
| 375 | + | } | 
| 376 | + | if (n_comp <= 0) | 
| 377 | + | n_comp = 3; | 
| 378 | + | return(1); | 
| 379 | + | } | 
| 380 | + |  | 
| 381 | + | /* call to compute block input position */ | 
| 382 | + | static size_t | 
| 383 | + | get_block_pos(int r, int c, int blklvl[][2], int nlvls) | 
| 384 | + | { | 
| 385 | + | size_t  n = 0; | 
| 386 | + |  | 
| 387 | + | while (nlvls > 1) { | 
| 388 | + | int     sr = r/blklvl[1][0]; | 
| 389 | + | int     sc = c/blklvl[1][1]; | 
| 390 | + | r -= sr*blklvl[1][0]; | 
| 391 | + | c -= sc*blklvl[1][1]; | 
| 392 | + | n += sr*blklvl[1][0]*blklvl[0][1] + sc*blklvl[1][0]*blklvl[1][1]; | 
| 393 | + | blklvl++; | 
| 394 | + | nlvls--; | 
| 395 | + | } | 
| 396 | + | n += r*blklvl[0][1] + c; | 
| 397 | + | return(n); | 
| 398 | + | } | 
| 399 | + |  | 
| 400 | + | /* return input offset based on array ordering and transpose option */ | 
| 401 | + | static size_t | 
| 402 | + | get_input_pos(int r, int c) | 
| 403 | + | { | 
| 404 | + | size_t  n; | 
| 405 | + |  | 
| 406 | + | if (outLevels > 1) {            /* block reordering */ | 
| 407 | + | n = get_block_pos(r, c, outArray, outLevels); | 
| 408 | + | if (transpose) { | 
| 409 | + | r = n/ni_rows; | 
| 410 | + | c = n - r*ni_rows; | 
| 411 | + | n = (size_t)c*ni_columns + r; | 
| 412 | + | } | 
| 413 | + | } else if (transpose)           /* transpose only */ | 
| 414 | + | n = (size_t)c*ni_columns + r; | 
| 415 | + | else                            /* XXX should never happen! */ | 
| 416 | + | n = (size_t)r*no_columns + c; | 
| 417 | + | return(n); | 
| 418 | + | } | 
| 419 | + |  | 
| 420 | + | /* output reordered ASCII or binary data from memory */ | 
| 421 | + | static int | 
| 422 | + | do_reorder(const MEMLOAD *mp) | 
| 423 | + | { | 
| 424 |  | static const char       tabEOL[2] = {'\t','\n'}; | 
| 425 |  | RECINDEX                *rp = NULL; | 
| 426 | < | long                    nrecords; | 
| 426 | > | size_t                  nrecords; | 
| 427 |  | int                     i, j; | 
| 428 |  | /* propogate sizes */ | 
| 429 |  | if (ni_rows <= 0) | 
| 430 | < | ni_rows = no_columns; | 
| 430 | > | ni_rows = transpose ? no_columns : no_rows; | 
| 431 |  | if (ni_columns <= 0) | 
| 432 | < | ni_columns = no_rows; | 
| 432 | > | ni_columns = transpose ? no_rows : no_columns; | 
| 433 |  | /* get # records (& index) */ | 
| 434 | < | if (record_width > 0) { | 
| 435 | < | if ((rp = index_records(mp, record_width)) == NULL) | 
| 434 | > | if (!comp_size) { | 
| 435 | > | if ((rp = index_records(mp, n_comp)) == NULL) | 
| 436 |  | return(0); | 
| 437 |  | if (ni_columns <= 0) | 
| 438 |  | ni_columns = count_columns(rp); | 
| 439 |  | nrecords = rp->nrecs; | 
| 440 |  | } else if ((ni_rows > 0) & (ni_columns > 0)) { | 
| 441 | < | nrecords = ni_rows*ni_columns; | 
| 442 | < | if (nrecords > mp->len / -record_width) { | 
| 443 | < | fprintf(stderr, | 
| 444 | < | "Input too small for specified size and type\n"); | 
| 441 | > | nrecords = (size_t)ni_rows*ni_columns; | 
| 442 | > | if (nrecords > mp->len/(n_comp*comp_size)) { | 
| 443 | > | fputs("Input too small for specified size and type\n", | 
| 444 | > | stderr); | 
| 445 |  | return(0); | 
| 446 |  | } | 
| 447 |  | } else | 
| 448 | < | nrecords = mp->len / -record_width; | 
| 448 | > | nrecords = mp->len/(n_comp*comp_size); | 
| 449 |  | /* check sizes */ | 
| 450 |  | if ((ni_rows <= 0) & (ni_columns > 0)) | 
| 451 |  | ni_rows = nrecords/ni_columns; | 
| 452 | < | if ((ni_columns <= 0) & (ni_rows > 0)) | 
| 452 | > | else if ((ni_columns <= 0) & (ni_rows > 0)) | 
| 453 |  | ni_columns = nrecords/ni_rows; | 
| 454 | < | if (nrecords != ni_rows*ni_columns) | 
| 454 | > | if (nrecords != (size_t)ni_rows*ni_columns) | 
| 455 |  | goto badspec; | 
| 456 | < | if (no_columns <= 0) | 
| 457 | < | no_columns = ni_rows; | 
| 458 | < | if (no_rows <= 0) | 
| 459 | < | no_rows = ni_columns; | 
| 460 | < | if ((no_rows != ni_columns) | (no_columns != ni_rows)) | 
| 461 | < | goto badspec; | 
| 462 | < | /* transpose records */ | 
| 456 | > | if (transpose) { | 
| 457 | > | if (no_columns <= 0) | 
| 458 | > | no_columns = ni_rows; | 
| 459 | > | if (no_rows <= 0) | 
| 460 | > | no_rows = ni_columns; | 
| 461 | > | if (outLevels <= 1 && | 
| 462 | > | (no_rows != ni_columns) | (no_columns != ni_rows)) | 
| 463 | > | goto badspec; | 
| 464 | > | } else { | 
| 465 | > | if (no_columns <= 0) | 
| 466 | > | no_columns = ni_columns; | 
| 467 | > | if (no_rows <= 0) | 
| 468 | > | no_rows = ni_rows; | 
| 469 | > | } | 
| 470 | > | if (ni_rows*ni_columns != no_rows*no_columns) { | 
| 471 | > | fputs("Number of input and output records do not match\n", | 
| 472 | > | stderr); | 
| 473 | > | return(0); | 
| 474 | > | } | 
| 475 | > | if (o_header) {                         /* finish header? */ | 
| 476 | > | printf("NROWS=%d\n", no_rows); | 
| 477 | > | printf("NCOLS=%d\n", no_columns); | 
| 478 | > | fputformat(fmtid, stdout); | 
| 479 | > | fputc('\n', stdout); | 
| 480 | > | } | 
| 481 | > | /* reorder records */ | 
| 482 |  | for (i = 0; i < no_rows; i++) { | 
| 483 | < | for (j = 0; j < no_columns; j++) | 
| 483 | > | for (j = 0; j < no_columns; j++) { | 
| 484 | > | size_t  n = get_input_pos(i, j); | 
| 485 | > | if (n >= nrecords) { | 
| 486 | > | fputs("Index past end-of-file\n", stderr); | 
| 487 | > | return(0); | 
| 488 | > | } | 
| 489 |  | if (rp != NULL) {               /* ASCII output */ | 
| 490 | < | print_record(rp, j*ni_columns + i); | 
| 490 | > | if (!print_record(rp, n)) | 
| 491 | > | return(0); | 
| 492 |  | putc(tabEOL[j >= no_columns-1], stdout); | 
| 493 |  | } else {                        /* binary output */ | 
| 494 | < | fwrite((char *)mp->base + | 
| 495 | < | -record_width*(j*ni_columns + i), | 
| 328 | < | -record_width, 1, stdout); | 
| 494 | > | putbinary((char *)mp->base + (n_comp*comp_size)*n, | 
| 495 | > | comp_size, n_comp, stdout); | 
| 496 |  | } | 
| 497 | + | } | 
| 498 |  | if (ferror(stdout)) { | 
| 499 | < | fprintf(stderr, "Error writing to stdout\n"); | 
| 499 | > | fputs("Error writing to stdout\n", stderr); | 
| 500 |  | return(0); | 
| 501 |  | } | 
| 502 |  | } | 
| 504 |  | free_records(rp); | 
| 505 |  | return(1); | 
| 506 |  | badspec: | 
| 507 | < | fprintf(stderr, "Bad transpose specification -- check dimension(s)\n"); | 
| 507 | > | fputs("Bad dimension(s)\n", stderr); | 
| 508 |  | return(0); | 
| 509 |  | } | 
| 510 |  |  | 
| 511 | < | /* resize ASCII stream input by ignoring EOLs between records */ | 
| 511 | > | /* resize stream input by ignoring EOLs between ASCII records */ | 
| 512 |  | static int | 
| 513 |  | do_resize(FILE *fp) | 
| 514 |  | { | 
| 515 | < | long    records2go = ni_rows*ni_columns; | 
| 515 | > | size_t  records2go = ni_rows*ni_columns; | 
| 516 |  | int     columns2go = no_columns; | 
| 517 |  | char    word[256]; | 
| 518 | < | /* sanity checks */ | 
| 519 | < | if (record_width <= 0) { | 
| 520 | < | fprintf(stderr, "Bad call to do_resize (record_width = %d)\n", | 
| 521 | < | record_width); | 
| 522 | < | return(0); | 
| 518 | > |  | 
| 519 | > | if (o_header) {                         /* finish header? */ | 
| 520 | > | if (no_rows > 0) | 
| 521 | > | printf("NROWS=%d\n", no_rows); | 
| 522 | > | if (no_columns > 0) | 
| 523 | > | printf("NCOLS=%d\n", no_columns); | 
| 524 | > | fputformat(fmtid, stdout); | 
| 525 | > | fputc('\n', stdout); | 
| 526 |  | } | 
| 527 | + | if (comp_size) {                        /* just copy binary data? */ | 
| 528 | + | size_t  nwritten = output_stream(fp); | 
| 529 | + | if ((no_rows > 0) & (no_columns > 0) && | 
| 530 | + | nwritten != (size_t)no_rows*no_columns*n_comp*comp_size) { | 
| 531 | + | if (check) { | 
| 532 | + | fputs("Incorrect binary file size\n", stderr); | 
| 533 | + | return(0);      /* fatal if check is true */ | 
| 534 | + | } | 
| 535 | + | if (warnings) | 
| 536 | + | fputs("Warning -- unexpected binary file size\n", stderr); | 
| 537 | + | } | 
| 538 | + | return(nwritten > 0); | 
| 539 | + | }                                       /* else straight ASCII data copy? */ | 
| 540 | + | if (!check & (no_columns == ni_columns) & (no_rows == ni_rows)) | 
| 541 | + | return(output_stream(fp) > 0); | 
| 542 | + | /* need to reshape (& check?) input */ | 
| 543 |  | if (no_columns <= 0) { | 
| 544 | < | fprintf(stderr, "Missing -oc specification\n"); | 
| 544 | > | fputs("Missing -oc specification\n", stderr); | 
| 545 |  | return(0); | 
| 546 |  | } | 
| 547 |  | if ((records2go <= 0) & (no_rows > 0)) | 
| 548 |  | records2go = no_rows*no_columns; | 
| 549 |  | else if (no_rows*no_columns != records2go) { | 
| 550 |  | fprintf(stderr, | 
| 551 | < | "Input and output data sizes disagree (%dx%d != %dx%d)\n", | 
| 551 | > | "Number of input and output records disagree (%dx%d != %dx%d)\n", | 
| 552 |  | ni_rows, ni_columns, no_rows, no_columns); | 
| 553 |  | return(0); | 
| 554 |  | } | 
| 555 |  | do {                                    /* reshape records */ | 
| 556 | < | int     n; | 
| 557 | < |  | 
| 371 | < | for (n = record_width; n--; ) { | 
| 556 | > | int     n = n_comp; | 
| 557 | > | while (n--) { | 
| 558 |  | if (fget_word(word, fp) == NULL) { | 
| 559 | < | if (records2go > 0 || n < record_width-1) | 
| 559 | > | if ((records2go > 0) | (n < n_comp-1)) | 
| 560 |  | break; | 
| 561 |  | goto done;      /* normal EOD */ | 
| 562 |  | } | 
| 563 | + | if (check && !isflt(word)) { | 
| 564 | + | fputs("Badly formed number: ", stderr); | 
| 565 | + | fputs(word, stderr); | 
| 566 | + | fputc('\n', stderr); | 
| 567 | + | return(0); | 
| 568 | + | } | 
| 569 |  | fputs(word, stdout); | 
| 570 |  | if (n) {                /* mid-record? */ | 
| 571 |  | int     c = getc(fp); | 
| 576 |  | } | 
| 577 |  | } | 
| 578 |  | if (n >= 0) { | 
| 579 | < | fprintf(stderr, "Incomplete record / unexpected EOF\n"); | 
| 579 | > | fputs("Incomplete record / unexpected EOF\n", stderr); | 
| 580 |  | return(0); | 
| 581 |  | } | 
| 582 |  | if (--columns2go <= 0) {        /* time to end output row? */ | 
| 586 |  | putc('\t', stdout); | 
| 587 |  | } while (--records2go);                 /* expected EOD? */ | 
| 588 |  | done: | 
| 589 | < | if (warnings && columns2go != no_columns) | 
| 590 | < | fprintf(stderr, "Warning -- incomplete final row\n"); | 
| 591 | < | if (warnings && fget_word(word, fp) != NULL) | 
| 592 | < | fprintf(stderr, "Warning -- characters beyond expected EOD\n"); | 
| 589 | > | if (columns2go != no_columns) { | 
| 590 | > | if (check) { | 
| 591 | > | fputs("Incomplete final row\n", stderr); | 
| 592 | > | return(0); | 
| 593 | > | } | 
| 594 | > | if (warnings) | 
| 595 | > | fputs("Warning -- incomplete final row\n", stderr); | 
| 596 | > | } | 
| 597 | > | if (fget_word(word, fp) != NULL) { | 
| 598 | > | if (check) { | 
| 599 | > | fputs("Characters beyond expected EOD\n", stderr); | 
| 600 | > | return(0); | 
| 601 | > | } | 
| 602 | > | if (warnings) | 
| 603 | > | fputs("Warning -- characters beyond expected EOD\n", stderr); | 
| 604 | > | } | 
| 605 |  | return(1); | 
| 606 |  | } | 
| 607 |  |  | 
| 609 |  | static int | 
| 610 |  | headline(char *s, void *p) | 
| 611 |  | { | 
| 612 | < | char    fmt[32]; | 
| 612 | > | static char     fmt[MAXFMTLEN]; | 
| 613 | > | int             n; | 
| 614 |  |  | 
| 615 |  | if (formatval(fmt, s)) { | 
| 616 | + | if (fmtid == fmt) return(0); | 
| 617 | + | if (fmtid == NULL) { | 
| 618 | + | fmtid = fmt; | 
| 619 | + | return(0); | 
| 620 | + | } | 
| 621 | + | if ((comp_size == 1) & (n_comp > 1)) | 
| 622 | + | return(0);              /* byte exception - skip check */ | 
| 623 |  | if (!strcmp(fmt, fmtid)) | 
| 624 |  | return(0); | 
| 625 |  | fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid); | 
| 626 |  | return(-1); | 
| 627 |  | } | 
| 628 | < | fputs(s, stdout);                       /* copy header info. */ | 
| 628 | > | if (!strncmp(s, "NROWS=", 6)) { | 
| 629 | > | n = atoi(s+6); | 
| 630 | > | if ((ni_rows > 0) & (n != ni_rows)) { | 
| 631 | > | fputs("Incorrect input row count\n", stderr); | 
| 632 | > | return(-1); | 
| 633 | > | } | 
| 634 | > | ni_rows = n; | 
| 635 | > | return(0); | 
| 636 | > | } | 
| 637 | > | if (!strncmp(s, "NCOLS=", 6)) { | 
| 638 | > | n = atoi(s+6); | 
| 639 | > | if ((ni_columns > 0) & (n != ni_columns)) { | 
| 640 | > | fputs("Incorrect input column count\n", stderr); | 
| 641 | > | return(-1); | 
| 642 | > | } | 
| 643 | > | ni_columns = n; | 
| 644 | > | return(0); | 
| 645 | > | } | 
| 646 | > | if (!strncmp(s, "NCOMP=", 6)) { | 
| 647 | > | if ((comp_size == 1) & (n_comp > 1)) | 
| 648 | > | return(0);              /* byte exception - ignore */ | 
| 649 | > | n = atoi(s+6); | 
| 650 | > | if ((n_comp > 0) & (n != n_comp)) { | 
| 651 | > | fputs("Incorrect number of components\n", stderr); | 
| 652 | > | return(-1); | 
| 653 | > | } | 
| 654 | > | n_comp = n; | 
| 655 | > | return(0); | 
| 656 | > | } | 
| 657 | > | if (o_header) | 
| 658 | > | fputs(s, stdout);               /* copy header info. */ | 
| 659 |  | return(0); | 
| 660 |  | } | 
| 661 |  |  | 
| 663 |  | int | 
| 664 |  | main(int argc, char *argv[]) | 
| 665 |  | { | 
| 666 | < | int     do_header = 1;                  /* header i/o? */ | 
| 425 | < | int     transpose = 0;                  /* transpose rows & cols? */ | 
| 426 | < | int     i; | 
| 666 | > | int     a; | 
| 667 |  |  | 
| 668 | < | for (i = 1; i < argc && argv[i][0] == '-'; i++) | 
| 669 | < | switch (argv[i][1]) { | 
| 668 | > | for (a = 1; a < argc && argv[a][0] == '-'; a++) | 
| 669 | > | switch (argv[a][1]) { | 
| 670 |  | case 'i':                       /* input */ | 
| 671 | < | if (argv[i][2] == 'c')  /* columns */ | 
| 672 | < | ni_columns = atoi(argv[++i]); | 
| 673 | < | else if (argv[i][2] == 'r') | 
| 674 | < | ni_rows = atoi(argv[++i]); | 
| 671 | > | if (argv[a][2] == 'c')  /* columns */ | 
| 672 | > | ni_columns = atoi(argv[++a]); | 
| 673 | > | else if (argv[a][2] == 'r') | 
| 674 | > | ni_rows = atoi(argv[++a]); | 
| 675 |  | else | 
| 676 |  | goto userr; | 
| 677 |  | break; | 
| 678 |  | case 'o':                       /* output */ | 
| 679 | < | if (argv[i][2] == 'c')  /* columns */ | 
| 680 | < | no_columns = atoi(argv[++i]); | 
| 681 | < | else if (argv[i][2] == 'r') | 
| 682 | < | no_rows = atoi(argv[++i]); | 
| 683 | < | else | 
| 679 | > | if (argv[a][2] == 'c')  /* columns */ | 
| 680 | > | no_columns = atoi(argv[++a]); | 
| 681 | > | else if (argv[a][2] == 'r') | 
| 682 | > | no_rows = atoi(argv[++a]); | 
| 683 | > | else if (argv[a][2] || | 
| 684 | > | !(outLevels=get_array(argv[++a], outArray, MAXLEVELS))) | 
| 685 |  | goto userr; | 
| 686 |  | break; | 
| 687 | < | case 'h':                       /* header on/off */ | 
| 688 | < | do_header = !do_header; | 
| 687 | > | case 'h':                       /* turn off header */ | 
| 688 | > | switch (argv[a][2]) { | 
| 689 | > | case 'i': | 
| 690 | > | i_header = 0; | 
| 691 | > | break; | 
| 692 | > | case 'o': | 
| 693 | > | o_header = 0; | 
| 694 | > | break; | 
| 695 | > | case '\0': | 
| 696 | > | i_header = o_header = 0; | 
| 697 | > | break; | 
| 698 | > | default: | 
| 699 | > | goto userr; | 
| 700 | > | } | 
| 701 |  | break; | 
| 702 |  | case 't':                       /* transpose on/off */ | 
| 703 |  | transpose = !transpose; | 
| 704 |  | break; | 
| 705 |  | case 'f':                       /* format */ | 
| 706 | < | switch (argv[i][2]) { | 
| 706 | > | switch (argv[a][2]) { | 
| 707 |  | case 'a':               /* ASCII */ | 
| 708 |  | case 'A': | 
| 709 |  | fmtid = "ascii"; | 
| 710 | < | record_width = 1; | 
| 710 | > | comp_size = 0; | 
| 711 |  | break; | 
| 712 |  | case 'f':               /* float */ | 
| 713 |  | case 'F': | 
| 714 |  | fmtid = "float"; | 
| 715 | < | record_width = -(int)sizeof(float); | 
| 715 | > | comp_size = sizeof(float); | 
| 716 |  | break; | 
| 717 |  | case 'd':               /* double */ | 
| 718 |  | case 'D': | 
| 719 |  | fmtid = "double"; | 
| 720 | < | record_width = -(int)sizeof(double); | 
| 720 | > | comp_size = sizeof(double); | 
| 721 |  | break; | 
| 722 |  | case 'b':               /* binary (bytes) */ | 
| 723 |  | case 'B': | 
| 724 |  | fmtid = "byte"; | 
| 725 | < | record_width = -1; | 
| 725 | > | comp_size = 1; | 
| 726 |  | break; | 
| 727 |  | default: | 
| 728 |  | goto userr; | 
| 729 |  | } | 
| 730 | < | if (argv[i][3]) { | 
| 731 | < | if (!isdigit(argv[i][3])) | 
| 730 | > | if (argv[a][3]) { | 
| 731 | > | if (!isdigit(argv[a][3])) | 
| 732 |  | goto userr; | 
| 733 | < | record_width *= atoi(argv[i]+3); | 
| 734 | < | } | 
| 733 | > | n_comp = atoi(argv[a]+3); | 
| 734 | > | } else | 
| 735 | > | n_comp = 1; | 
| 736 |  | break; | 
| 737 |  | case 'w':                       /* warnings on/off */ | 
| 738 |  | warnings = !warnings; | 
| 739 |  | break; | 
| 740 | + | case 'c':                       /* force check operation */ | 
| 741 | + | check = 1; | 
| 742 | + | break; | 
| 743 |  | default: | 
| 744 |  | goto userr; | 
| 745 |  | } | 
| 746 | < | if (!record_width) | 
| 746 | > | if (a < argc-1)                         /* arg count OK? */ | 
| 747 |  | goto userr; | 
| 748 | < | if (i < argc-1)                         /* arg count OK? */ | 
| 749 | < | goto userr; | 
| 748 | > | if (outLevels) {                        /* should check consistency? */ | 
| 749 | > | no_rows = outArray[0][0]; | 
| 750 | > | no_columns = outArray[0][1]; | 
| 751 | > | } | 
| 752 |  | /* open input file? */ | 
| 753 | < | if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { | 
| 754 | < | fprintf(stderr, "%s: cannot open for reading\n", argv[i]); | 
| 753 | > | if (a == argc-1 && freopen(argv[a], "rb", stdin) == NULL) { | 
| 754 | > | fprintf(stderr, "%s: cannot open for reading\n", argv[a]); | 
| 755 |  | return(1); | 
| 756 | < | } | 
| 498 | < | if (record_width < 0) { | 
| 756 | > | } else | 
| 757 |  | SET_FILE_BINARY(stdin); | 
| 758 | < | SET_FILE_BINARY(stdout); | 
| 759 | < | } | 
| 758 | > | SET_FILE_BINARY(stdout); | 
| 759 | > | #ifdef getc_unlocked                            /* avoid stupid semaphores */ | 
| 760 | > | flockfile(stdin); | 
| 761 | > | flockfile(stdout); | 
| 762 | > | #endif | 
| 763 |  | /* check for no-op */ | 
| 764 | < | if (!transpose && (record_width < 0 || | 
| 765 | < | (no_columns == ni_columns) & (no_rows == ni_rows))) { | 
| 764 | > | if (!transpose & !check & (outLevels <= 1) & (i_header == o_header) && | 
| 765 | > | (no_columns == ni_columns) & (no_rows == ni_rows)) { | 
| 766 |  | if (warnings) | 
| 767 |  | fprintf(stderr, "%s: no-op -- copying input verbatim\n", | 
| 768 |  | argv[0]); | 
| 769 | < | if (!output_stream(stdin)) | 
| 509 | < | return(1); | 
| 510 | < | return(0); | 
| 769 | > | return(!output_stream(stdin)); | 
| 770 |  | } | 
| 771 | < | if (do_header) {                        /* read/write header */ | 
| 772 | < | if (getheader(stdin, &headline, NULL) < 0) | 
| 773 | < | return(1); | 
| 774 | < | printargs(argc, argv, stdout); | 
| 775 | < | fputformat(fmtid, stdout); | 
| 776 | < | fputc('\n', stdout);            /* finish new header */ | 
| 771 | > | /* read input header? */ | 
| 772 | > | if (i_header && getheader(stdin, headline, NULL) < 0) | 
| 773 | > | return(1); | 
| 774 | > | if (!check_sizes())                     /* adjust sizes */ | 
| 775 | > | return(1); | 
| 776 | > | if (o_header) {                         /* add to output header? */ | 
| 777 | > | if (!i_header) | 
| 778 | > | newheader("RADIANCE", stdout); | 
| 779 | > | printargs(a, argv, stdout); | 
| 780 | > | printf("NCOMP=%d\n", n_comp); | 
| 781 |  | } | 
| 782 | < | if (transpose) {                        /* transposing rows & columns? */ | 
| 783 | < | MEMLOAD myMem;                  /* need to load into memory */ | 
| 784 | < | if (i == argc-1) { | 
| 782 | > | if (!comp_size) {                       /* a little late, here... */ | 
| 783 | > | SET_FILE_TEXT(stdin); | 
| 784 | > | SET_FILE_TEXT(stdout); | 
| 785 | > | } | 
| 786 | > | if (transpose | (outLevels > 1) || (o_header && no_rows <= 0)) { | 
| 787 | > | MEMLOAD myMem;                  /* need to map into memory */ | 
| 788 | > | if (a == argc-1) { | 
| 789 |  | if (load_file(&myMem, stdin) <= 0) { | 
| 790 |  | fprintf(stderr, "%s: error loading file into memory\n", | 
| 791 | < | argv[i]); | 
| 791 | > | argv[a]); | 
| 792 |  | return(1); | 
| 793 |  | } | 
| 794 |  | } else if (load_stream(&myMem, stdin) <= 0) { | 
| 796 |  | argv[0]); | 
| 797 |  | return(1); | 
| 798 |  | } | 
| 799 | < | if (!do_transpose(&myMem)) | 
| 799 | > | if (!do_reorder(&myMem)) | 
| 800 |  | return(1); | 
| 801 | < | /* free_load(&myMem); */ | 
| 801 | > | /* free_load(&myMem);   about to exit, so don't bother */ | 
| 802 |  | } else if (!do_resize(stdin))           /* just reshaping input */ | 
| 803 |  | return(1); | 
| 804 |  | return(0); | 
| 805 |  | userr: | 
| 806 |  | fprintf(stderr, | 
| 807 | < | "Usage: %s [-h][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", | 
| 807 | > | "Usage: %s [-h[io]][-w][-c][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n", | 
| 808 |  | argv[0]); | 
| 809 |  | return(1); | 
| 810 |  | } |