| 237 |  |  | 
| 238 |  | if (fp == NULL) | 
| 239 |  | return(0); | 
| 240 | < | fflush(stdout);                 /* assumes nothing in input buffer */ | 
| 241 | < | while ((n = read(fileno(fp), buf, sizeof(buf))) > 0) | 
| 240 | > | fflush(stdout); | 
| 241 | > | while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) | 
| 242 |  | if (write(fileno(stdout), buf, n) != n) | 
| 243 |  | return(0); | 
| 244 | < | return(n >= 0); | 
| 244 | > | return(!ferror(fp)); | 
| 245 |  | } | 
| 246 |  |  | 
| 247 |  | /* get next word from stream, leaving stream on EOL or start of next word */ | 
| 267 |  | return(buf); | 
| 268 |  | } | 
| 269 |  |  | 
| 270 | < | char            *fmtid = "ascii";               /* format id */ | 
| 271 | < | int             record_width = 3;               /* words/record (<0 binary) */ | 
| 270 | > | char            *fmtid = NULL;                  /* format id */ | 
| 271 | > | int             comp_size = 0;                  /* binary bytes/channel */ | 
| 272 | > | int             n_comp = 0;                     /* components/record */ | 
| 273 |  | int             ni_columns = 0;                 /* number of input columns */ | 
| 274 |  | int             ni_rows = 0;                    /* number of input rows */ | 
| 275 |  | int             no_columns = 0;                 /* number of output columns */ | 
| 276 |  | int             no_rows = 0;                    /* number of output rows */ | 
| 277 |  |  | 
| 278 | + | /* check settings and assign defaults */ | 
| 279 | + | static int | 
| 280 | + | check_sizes() | 
| 281 | + | { | 
| 282 | + | if (fmtid == NULL) { | 
| 283 | + | fmtid = "ascii"; | 
| 284 | + | } else if (!comp_size) { | 
| 285 | + | if (!strcmp(fmtid, "float")) | 
| 286 | + | comp_size = sizeof(float); | 
| 287 | + | else if (!strcmp(fmtid, "double")) | 
| 288 | + | comp_size = sizeof(double); | 
| 289 | + | else if (!strcmp(fmtid, "byte")) | 
| 290 | + | comp_size = 1; | 
| 291 | + | else { | 
| 292 | + | fprintf(stderr, "Unsupported format: %s\n", fmtid); | 
| 293 | + | return(0); | 
| 294 | + | } | 
| 295 | + | } | 
| 296 | + | if (n_comp <= 0) | 
| 297 | + | n_comp = 3; | 
| 298 | + | return(1); | 
| 299 | + | } | 
| 300 | + |  | 
| 301 |  | /* output transposed ASCII or binary data from memory */ | 
| 302 |  | static int | 
| 303 |  | do_transpose(const MEMLOAD *mp) | 
| 312 |  | if (ni_columns <= 0) | 
| 313 |  | ni_columns = no_rows; | 
| 314 |  | /* get # records (& index) */ | 
| 315 | < | if (record_width > 0) { | 
| 316 | < | if ((rp = index_records(mp, record_width)) == NULL) | 
| 315 | > | if (!comp_size) { | 
| 316 | > | if ((rp = index_records(mp, n_comp)) == NULL) | 
| 317 |  | return(0); | 
| 318 |  | if (ni_columns <= 0) | 
| 319 |  | ni_columns = count_columns(rp); | 
| 320 |  | nrecords = rp->nrecs; | 
| 321 |  | } else if ((ni_rows > 0) & (ni_columns > 0)) { | 
| 322 |  | nrecords = ni_rows*ni_columns; | 
| 323 | < | if (nrecords > mp->len / -record_width) { | 
| 323 | > | if (nrecords > mp->len/(n_comp*comp_size)) { | 
| 324 |  | fprintf(stderr, | 
| 325 |  | "Input too small for specified size and type\n"); | 
| 326 |  | return(0); | 
| 327 |  | } | 
| 328 |  | } else | 
| 329 | < | nrecords = mp->len / -record_width; | 
| 329 | > | nrecords = mp->len/(n_comp*comp_size); | 
| 330 |  | /* check sizes */ | 
| 331 |  | if ((ni_rows <= 0) & (ni_columns > 0)) | 
| 332 |  | ni_rows = nrecords/ni_columns; | 
| 348 |  | putc(tabEOL[j >= no_columns-1], stdout); | 
| 349 |  | } else {                        /* binary output */ | 
| 350 |  | fwrite((char *)mp->base + | 
| 351 | < | -record_width*(j*ni_columns + i), | 
| 352 | < | -record_width, 1, stdout); | 
| 351 | > | (n_comp*comp_size)*(j*ni_columns + i), | 
| 352 | > | n_comp*comp_size, 1, stdout); | 
| 353 |  | } | 
| 354 |  | if (ferror(stdout)) { | 
| 355 |  | fprintf(stderr, "Error writing to stdout\n"); | 
| 372 |  | int     columns2go = no_columns; | 
| 373 |  | char    word[256]; | 
| 374 |  | /* sanity checks */ | 
| 375 | < | if (record_width <= 0) { | 
| 376 | < | fprintf(stderr, "Bad call to do_resize (record_width = %d)\n", | 
| 353 | < | record_width); | 
| 354 | < | return(0); | 
| 355 | < | } | 
| 375 | > | if (comp_size) | 
| 376 | > | return(output_stream(fp));      /* binary data -- just copy */ | 
| 377 |  | if (no_columns <= 0) { | 
| 378 |  | fprintf(stderr, "Missing -oc specification\n"); | 
| 379 |  | return(0); | 
| 389 |  | do {                                    /* reshape records */ | 
| 390 |  | int     n; | 
| 391 |  |  | 
| 392 | < | for (n = record_width; n--; ) { | 
| 392 | > | for (n = n_comp; n--; ) { | 
| 393 |  | if (fget_word(word, fp) == NULL) { | 
| 394 | < | if (records2go > 0 || n < record_width-1) | 
| 394 | > | if (records2go > 0 || n < n_comp-1) | 
| 395 |  | break; | 
| 396 |  | goto done;      /* normal EOD */ | 
| 397 |  | } | 
| 426 |  | static int | 
| 427 |  | headline(char *s, void *p) | 
| 428 |  | { | 
| 429 | < | char    fmt[32]; | 
| 429 | > | static char     fmt[32]; | 
| 430 | > | int             n; | 
| 431 |  |  | 
| 432 |  | if (formatval(fmt, s)) { | 
| 433 | + | if (fmtid == NULL) { | 
| 434 | + | fmtid = fmt; | 
| 435 | + | return(0); | 
| 436 | + | } | 
| 437 |  | if (!strcmp(fmt, fmtid)) | 
| 438 |  | return(0); | 
| 439 |  | fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid); | 
| 440 |  | return(-1); | 
| 441 |  | } | 
| 442 | + | if (!strncmp(s, "NROWS=", 6)) { | 
| 443 | + | n = atoi(s+6); | 
| 444 | + | if ((ni_rows > 0) & (n != ni_rows)) { | 
| 445 | + | fputs("Incorrect input row count\n", stderr); | 
| 446 | + | return(-1); | 
| 447 | + | } | 
| 448 | + | ni_rows = n; | 
| 449 | + | return(0); | 
| 450 | + | } | 
| 451 | + | if (!strncmp(s, "NCOLS=", 6)) { | 
| 452 | + | n = atoi(s+6); | 
| 453 | + | if ((ni_columns > 0) & (n != ni_columns)) { | 
| 454 | + | fputs("Incorrect input column count\n", stderr); | 
| 455 | + | return(-1); | 
| 456 | + | } | 
| 457 | + | ni_columns = n; | 
| 458 | + | return(0); | 
| 459 | + | } | 
| 460 | + | if (!strncmp(s, "NCOMP=", 6)) { | 
| 461 | + | n = atoi(s+6); | 
| 462 | + | if ((n_comp > 0) & (n != n_comp)) { | 
| 463 | + | fputs("Incorrect number of components", stderr); | 
| 464 | + | return(-1); | 
| 465 | + | } | 
| 466 | + | n_comp = n; | 
| 467 | + | return(0); | 
| 468 | + | } | 
| 469 |  | fputs(s, stdout);                       /* copy header info. */ | 
| 470 |  | return(0); | 
| 471 |  | } | 
| 474 |  | int | 
| 475 |  | main(int argc, char *argv[]) | 
| 476 |  | { | 
| 477 | < | int     do_header = 1;                  /* header i/o? */ | 
| 477 | > | int     i_header = 1;                   /* input header? */ | 
| 478 | > | int     o_header = 1;                   /* output header? */ | 
| 479 |  | int     transpose = 0;                  /* transpose rows & cols? */ | 
| 480 |  | int     i; | 
| 481 |  |  | 
| 497 |  | else | 
| 498 |  | goto userr; | 
| 499 |  | break; | 
| 500 | < | case 'h':                       /* header on/off */ | 
| 501 | < | do_header = !do_header; | 
| 500 | > | case 'h':                       /* turn off header */ | 
| 501 | > | switch (argv[i][2]) { | 
| 502 | > | case 'i': | 
| 503 | > | i_header = 0; | 
| 504 | > | break; | 
| 505 | > | case 'o': | 
| 506 | > | o_header = 0; | 
| 507 | > | break; | 
| 508 | > | case '\0': | 
| 509 | > | i_header = o_header = 0; | 
| 510 | > | break; | 
| 511 | > | default: | 
| 512 | > | goto userr; | 
| 513 | > | } | 
| 514 |  | break; | 
| 515 |  | case 't':                       /* transpose on/off */ | 
| 516 |  | transpose = !transpose; | 
| 520 |  | case 'a':               /* ASCII */ | 
| 521 |  | case 'A': | 
| 522 |  | fmtid = "ascii"; | 
| 523 | < | record_width = 1; | 
| 523 | > | comp_size = 0; | 
| 524 |  | break; | 
| 525 |  | case 'f':               /* float */ | 
| 526 |  | case 'F': | 
| 527 |  | fmtid = "float"; | 
| 528 | < | record_width = -(int)sizeof(float); | 
| 528 | > | comp_size = sizeof(float); | 
| 529 |  | break; | 
| 530 |  | case 'd':               /* double */ | 
| 531 |  | case 'D': | 
| 532 |  | fmtid = "double"; | 
| 533 | < | record_width = -(int)sizeof(double); | 
| 533 | > | comp_size = sizeof(double); | 
| 534 |  | break; | 
| 535 |  | case 'b':               /* binary (bytes) */ | 
| 536 |  | case 'B': | 
| 537 |  | fmtid = "byte"; | 
| 538 | < | record_width = -1; | 
| 538 | > | comp_size = 1; | 
| 539 |  | break; | 
| 540 |  | default: | 
| 541 |  | goto userr; | 
| 543 |  | if (argv[i][3]) { | 
| 544 |  | if (!isdigit(argv[i][3])) | 
| 545 |  | goto userr; | 
| 546 | < | record_width *= atoi(argv[i]+3); | 
| 546 | > | n_comp = atoi(argv[i]+3); | 
| 547 |  | } | 
| 548 |  | break; | 
| 549 |  | case 'w':                       /* warnings on/off */ | 
| 552 |  | default: | 
| 553 |  | goto userr; | 
| 554 |  | } | 
| 489 | – | if (!record_width) | 
| 490 | – | goto userr; | 
| 555 |  | if (i < argc-1)                         /* arg count OK? */ | 
| 556 |  | goto userr; | 
| 557 |  | /* open input file? */ | 
| 559 |  | fprintf(stderr, "%s: cannot open for reading\n", argv[i]); | 
| 560 |  | return(1); | 
| 561 |  | } | 
| 562 | < | if (record_width < 0) { | 
| 562 | > | if (comp_size) { | 
| 563 |  | SET_FILE_BINARY(stdin); | 
| 564 |  | SET_FILE_BINARY(stdout); | 
| 565 |  | } | 
| 566 |  | /* check for no-op */ | 
| 567 | < | if (!transpose && (record_width < 0 || | 
| 567 | > | if (!transpose & (i_header == o_header) && (comp_size || | 
| 568 |  | (no_columns == ni_columns) & (no_rows == ni_rows))) { | 
| 569 |  | if (warnings) | 
| 570 |  | fprintf(stderr, "%s: no-op -- copying input verbatim\n", | 
| 573 |  | return(1); | 
| 574 |  | return(0); | 
| 575 |  | } | 
| 576 | < | if (do_header) {                        /* read/write header */ | 
| 576 | > | if (i_header) {                         /* read header */ | 
| 577 |  | if (getheader(stdin, &headline, NULL) < 0) | 
| 578 |  | return(1); | 
| 579 | + | if (!check_sizes()) | 
| 580 | + | return(1); | 
| 581 | + | if (comp_size) {                /* a little late... */ | 
| 582 | + | SET_FILE_BINARY(stdin); | 
| 583 | + | SET_FILE_BINARY(stdout); | 
| 584 | + | } | 
| 585 | + | } else if (!check_sizes()) | 
| 586 | + | return(1); | 
| 587 | + | if (o_header) {                         /* write header */ | 
| 588 |  | printargs(argc, argv, stdout); | 
| 589 | + | if (transpose && (no_rows <= 0) & (no_columns <= 0)) { | 
| 590 | + | if (ni_rows > 0) no_columns = ni_rows; | 
| 591 | + | if (ni_columns > 0) no_rows = ni_columns; | 
| 592 | + | } | 
| 593 | + | if (no_rows > 0) | 
| 594 | + | printf("NROWS=%d\n", no_rows); | 
| 595 | + | if (no_columns > 0) | 
| 596 | + | printf("NCOLS=%d\n", no_columns); | 
| 597 | + | printf("NCOMP=%d\n", n_comp); | 
| 598 |  | fputformat(fmtid, stdout); | 
| 599 |  | fputc('\n', stdout);            /* finish new header */ | 
| 600 |  | } | 
| 619 |  | return(0); | 
| 620 |  | userr: | 
| 621 |  | fprintf(stderr, | 
| 622 | < | "Usage: %s [-h][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", | 
| 622 | > | "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", | 
| 623 |  | argv[0]); | 
| 624 |  | return(1); | 
| 625 |  | } |