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 */ |
276 |
|
int no_rows = 0; /* number of output rows */ |
277 |
|
|
278 |
|
/* check settings and assign defaults */ |
279 |
< |
static void |
279 |
> |
static int |
280 |
|
check_sizes() |
281 |
|
{ |
282 |
|
if (fmtid == NULL) { |
288 |
|
comp_size = sizeof(double); |
289 |
|
else if (!strcmp(fmtid, "byte")) |
290 |
|
comp_size = 1; |
291 |
+ |
else if (strcmp(fmtid, "ascii")) { |
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 */ |
372 |
|
int columns2go = no_columns; |
373 |
|
char word[256]; |
374 |
|
/* sanity checks */ |
375 |
< |
if (comp_size) { |
376 |
< |
fputs("Bad call to do_resize (binary input)\n", stderr); |
372 |
< |
return(0); |
373 |
< |
} |
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); |
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); |
463 |
> |
fputs("Incorrect number of components\n", stderr); |
464 |
|
return(-1); |
465 |
|
} |
466 |
|
n_comp = n; |
477 |
|
int i_header = 1; /* input header? */ |
478 |
|
int o_header = 1; /* output header? */ |
479 |
|
int transpose = 0; /* transpose rows & cols? */ |
480 |
< |
int i; |
480 |
> |
int a; |
481 |
|
|
482 |
< |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
483 |
< |
switch (argv[i][1]) { |
482 |
> |
for (a = 1; a < argc && argv[a][0] == '-'; a++) |
483 |
> |
switch (argv[a][1]) { |
484 |
|
case 'i': /* input */ |
485 |
< |
if (argv[i][2] == 'c') /* columns */ |
486 |
< |
ni_columns = atoi(argv[++i]); |
487 |
< |
else if (argv[i][2] == 'r') |
488 |
< |
ni_rows = atoi(argv[++i]); |
485 |
> |
if (argv[a][2] == 'c') /* columns */ |
486 |
> |
ni_columns = atoi(argv[++a]); |
487 |
> |
else if (argv[a][2] == 'r') |
488 |
> |
ni_rows = atoi(argv[++a]); |
489 |
|
else |
490 |
|
goto userr; |
491 |
|
break; |
492 |
|
case 'o': /* output */ |
493 |
< |
if (argv[i][2] == 'c') /* columns */ |
494 |
< |
no_columns = atoi(argv[++i]); |
495 |
< |
else if (argv[i][2] == 'r') |
496 |
< |
no_rows = atoi(argv[++i]); |
493 |
> |
if (argv[a][2] == 'c') /* columns */ |
494 |
> |
no_columns = atoi(argv[++a]); |
495 |
> |
else if (argv[a][2] == 'r') |
496 |
> |
no_rows = atoi(argv[++a]); |
497 |
|
else |
498 |
|
goto userr; |
499 |
|
break; |
500 |
|
case 'h': /* turn off header */ |
501 |
< |
switch (argv[i][2]) { |
501 |
> |
switch (argv[a][2]) { |
502 |
|
case 'i': |
503 |
|
i_header = 0; |
504 |
|
break; |
516 |
|
transpose = !transpose; |
517 |
|
break; |
518 |
|
case 'f': /* format */ |
519 |
< |
switch (argv[i][2]) { |
519 |
> |
switch (argv[a][2]) { |
520 |
|
case 'a': /* ASCII */ |
521 |
|
case 'A': |
522 |
|
fmtid = "ascii"; |
540 |
|
default: |
541 |
|
goto userr; |
542 |
|
} |
543 |
< |
if (argv[i][3]) { |
544 |
< |
if (!isdigit(argv[i][3])) |
543 |
> |
if (argv[a][3]) { |
544 |
> |
if (!isdigit(argv[a][3])) |
545 |
|
goto userr; |
546 |
< |
n_comp = atoi(argv[i]+3); |
547 |
< |
} |
546 |
> |
n_comp = atoi(argv[a]+3); |
547 |
> |
} else |
548 |
> |
n_comp = 1; |
549 |
|
break; |
550 |
|
case 'w': /* warnings on/off */ |
551 |
|
warnings = !warnings; |
553 |
|
default: |
554 |
|
goto userr; |
555 |
|
} |
556 |
< |
if (i < argc-1) /* arg count OK? */ |
556 |
> |
if (a < argc-1) /* arg count OK? */ |
557 |
|
goto userr; |
558 |
|
/* open input file? */ |
559 |
< |
if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
560 |
< |
fprintf(stderr, "%s: cannot open for reading\n", argv[i]); |
559 |
> |
if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) { |
560 |
> |
fprintf(stderr, "%s: cannot open for reading\n", argv[a]); |
561 |
|
return(1); |
562 |
|
} |
563 |
|
if (comp_size) { |
565 |
|
SET_FILE_BINARY(stdout); |
566 |
|
} |
567 |
|
/* check for no-op */ |
568 |
< |
if (!transpose && (comp_size || |
568 |
> |
if (!transpose & (i_header == o_header) && (comp_size || |
569 |
|
(no_columns == ni_columns) & (no_rows == ni_rows))) { |
570 |
|
if (warnings) |
571 |
|
fprintf(stderr, "%s: no-op -- copying input verbatim\n", |
577 |
|
if (i_header) { /* read header */ |
578 |
|
if (getheader(stdin, &headline, NULL) < 0) |
579 |
|
return(1); |
580 |
< |
check_sizes(); |
580 |
> |
if (!check_sizes()) |
581 |
> |
return(1); |
582 |
|
if (comp_size) { /* a little late... */ |
583 |
|
SET_FILE_BINARY(stdin); |
584 |
|
SET_FILE_BINARY(stdout); |
585 |
|
} |
586 |
< |
} else |
587 |
< |
check_sizes(); |
586 |
> |
} else if (!check_sizes()) |
587 |
> |
return(1); |
588 |
|
if (o_header) { /* write header */ |
589 |
< |
printargs(argc, argv, stdout); |
589 |
> |
printargs(a, argv, stdout); |
590 |
|
if (transpose && (no_rows <= 0) & (no_columns <= 0)) { |
591 |
|
if (ni_rows > 0) no_columns = ni_rows; |
592 |
|
if (ni_columns > 0) no_rows = ni_columns; |
601 |
|
} |
602 |
|
if (transpose) { /* transposing rows & columns? */ |
603 |
|
MEMLOAD myMem; /* need to load into memory */ |
604 |
< |
if (i == argc-1) { |
604 |
> |
if (a == argc-1) { |
605 |
|
if (load_file(&myMem, stdin) <= 0) { |
606 |
|
fprintf(stderr, "%s: error loading file into memory\n", |
607 |
< |
argv[i]); |
607 |
> |
argv[a]); |
608 |
|
return(1); |
609 |
|
} |
610 |
|
} else if (load_stream(&myMem, stdin) <= 0) { |