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 */ |
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 |
+ |
int transpose = 0; /* transpose rows & cols? */ |
278 |
+ |
int i_header = 1; /* input header? */ |
279 |
+ |
int o_header = 1; /* output header? */ |
280 |
|
|
281 |
|
/* check settings and assign defaults */ |
282 |
|
static int |
291 |
|
comp_size = sizeof(double); |
292 |
|
else if (!strcmp(fmtid, "byte")) |
293 |
|
comp_size = 1; |
294 |
< |
else { |
294 |
> |
else if (strcmp(fmtid, "ascii")) { |
295 |
|
fprintf(stderr, "Unsupported format: %s\n", fmtid); |
296 |
|
return(0); |
297 |
|
} |
298 |
|
} |
299 |
+ |
if (transpose && (no_rows <= 0) & (no_columns <= 0)) { |
300 |
+ |
if (ni_rows > 0) no_columns = ni_rows; |
301 |
+ |
if (ni_columns > 0) no_rows = ni_columns; |
302 |
+ |
} else if ((no_rows <= 0) & (no_columns > 0) && |
303 |
+ |
!((ni_rows*ni_columns) % no_columns)) |
304 |
+ |
no_rows = ni_rows*ni_columns/no_columns; |
305 |
|
if (n_comp <= 0) |
306 |
|
n_comp = 3; |
307 |
|
return(1); |
469 |
|
if (!strncmp(s, "NCOMP=", 6)) { |
470 |
|
n = atoi(s+6); |
471 |
|
if ((n_comp > 0) & (n != n_comp)) { |
472 |
< |
fputs("Incorrect number of components", stderr); |
472 |
> |
fputs("Incorrect number of components\n", stderr); |
473 |
|
return(-1); |
474 |
|
} |
475 |
|
n_comp = n; |
476 |
|
return(0); |
477 |
|
} |
478 |
< |
fputs(s, stdout); /* copy header info. */ |
478 |
> |
if (o_header) |
479 |
> |
fputs(s, stdout); /* copy header info. */ |
480 |
|
return(0); |
481 |
|
} |
482 |
|
|
484 |
|
int |
485 |
|
main(int argc, char *argv[]) |
486 |
|
{ |
487 |
< |
int i_header = 1; /* input header? */ |
478 |
< |
int o_header = 1; /* output header? */ |
479 |
< |
int transpose = 0; /* transpose rows & cols? */ |
480 |
< |
int i; |
487 |
> |
int a; |
488 |
|
|
489 |
< |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
490 |
< |
switch (argv[i][1]) { |
489 |
> |
for (a = 1; a < argc && argv[a][0] == '-'; a++) |
490 |
> |
switch (argv[a][1]) { |
491 |
|
case 'i': /* input */ |
492 |
< |
if (argv[i][2] == 'c') /* columns */ |
493 |
< |
ni_columns = atoi(argv[++i]); |
494 |
< |
else if (argv[i][2] == 'r') |
495 |
< |
ni_rows = atoi(argv[++i]); |
492 |
> |
if (argv[a][2] == 'c') /* columns */ |
493 |
> |
ni_columns = atoi(argv[++a]); |
494 |
> |
else if (argv[a][2] == 'r') |
495 |
> |
ni_rows = atoi(argv[++a]); |
496 |
|
else |
497 |
|
goto userr; |
498 |
|
break; |
499 |
|
case 'o': /* output */ |
500 |
< |
if (argv[i][2] == 'c') /* columns */ |
501 |
< |
no_columns = atoi(argv[++i]); |
502 |
< |
else if (argv[i][2] == 'r') |
503 |
< |
no_rows = atoi(argv[++i]); |
500 |
> |
if (argv[a][2] == 'c') /* columns */ |
501 |
> |
no_columns = atoi(argv[++a]); |
502 |
> |
else if (argv[a][2] == 'r') |
503 |
> |
no_rows = atoi(argv[++a]); |
504 |
|
else |
505 |
|
goto userr; |
506 |
|
break; |
507 |
|
case 'h': /* turn off header */ |
508 |
< |
switch (argv[i][2]) { |
508 |
> |
switch (argv[a][2]) { |
509 |
|
case 'i': |
510 |
|
i_header = 0; |
511 |
|
break; |
523 |
|
transpose = !transpose; |
524 |
|
break; |
525 |
|
case 'f': /* format */ |
526 |
< |
switch (argv[i][2]) { |
526 |
> |
switch (argv[a][2]) { |
527 |
|
case 'a': /* ASCII */ |
528 |
|
case 'A': |
529 |
|
fmtid = "ascii"; |
547 |
|
default: |
548 |
|
goto userr; |
549 |
|
} |
550 |
< |
if (argv[i][3]) { |
551 |
< |
if (!isdigit(argv[i][3])) |
550 |
> |
if (argv[a][3]) { |
551 |
> |
if (!isdigit(argv[a][3])) |
552 |
|
goto userr; |
553 |
< |
n_comp = atoi(argv[i]+3); |
554 |
< |
} |
553 |
> |
n_comp = atoi(argv[a]+3); |
554 |
> |
} else |
555 |
> |
n_comp = 1; |
556 |
|
break; |
557 |
|
case 'w': /* warnings on/off */ |
558 |
|
warnings = !warnings; |
560 |
|
default: |
561 |
|
goto userr; |
562 |
|
} |
563 |
< |
if (i < argc-1) /* arg count OK? */ |
563 |
> |
if (a < argc-1) /* arg count OK? */ |
564 |
|
goto userr; |
565 |
|
/* open input file? */ |
566 |
< |
if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
567 |
< |
fprintf(stderr, "%s: cannot open for reading\n", argv[i]); |
566 |
> |
if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) { |
567 |
> |
fprintf(stderr, "%s: cannot open for reading\n", argv[a]); |
568 |
|
return(1); |
569 |
|
} |
570 |
|
if (comp_size) { |
582 |
|
return(0); |
583 |
|
} |
584 |
|
if (i_header) { /* read header */ |
585 |
< |
if (getheader(stdin, &headline, NULL) < 0) |
585 |
> |
if (getheader(stdin, headline, NULL) < 0) |
586 |
|
return(1); |
587 |
|
if (!check_sizes()) |
588 |
|
return(1); |
593 |
|
} else if (!check_sizes()) |
594 |
|
return(1); |
595 |
|
if (o_header) { /* write header */ |
596 |
< |
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 |
< |
} |
596 |
> |
printargs(a, argv, stdout); |
597 |
|
if (no_rows > 0) |
598 |
|
printf("NROWS=%d\n", no_rows); |
599 |
|
if (no_columns > 0) |
604 |
|
} |
605 |
|
if (transpose) { /* transposing rows & columns? */ |
606 |
|
MEMLOAD myMem; /* need to load into memory */ |
607 |
< |
if (i == argc-1) { |
607 |
> |
if (a == argc-1) { |
608 |
|
if (load_file(&myMem, stdin) <= 0) { |
609 |
|
fprintf(stderr, "%s: error loading file into memory\n", |
610 |
< |
argv[i]); |
610 |
> |
argv[a]); |
611 |
|
return(1); |
612 |
|
} |
613 |
|
} else if (load_stream(&myMem, stdin) <= 0) { |
617 |
|
} |
618 |
|
if (!do_transpose(&myMem)) |
619 |
|
return(1); |
620 |
< |
/* free_load(&myMem); */ |
621 |
< |
} else if (!do_resize(stdin)) /* just reshaping input */ |
620 |
> |
/* free_load(&myMem); about to exit, so don't bother */ |
621 |
> |
} else if (comp_size || (no_columns==ni_columns) & (no_rows==ni_rows)) { |
622 |
> |
if (!output_stream(stdin)) /* just changed header */ |
623 |
> |
return(1); |
624 |
> |
} else if (!do_resize(stdin)) /* reshaping input */ |
625 |
|
return(1); |
626 |
|
return(0); |
627 |
|
userr: |