57 |
|
mp->len = 0; |
58 |
|
} |
59 |
|
|
60 |
+ |
/* load memory from an input stream, starting from current position */ |
61 |
+ |
static int |
62 |
+ |
load_stream(MEMLOAD *mp, FILE *fp) |
63 |
+ |
{ |
64 |
+ |
size_t alloced = 0; |
65 |
+ |
char buf[8192]; |
66 |
+ |
size_t nr; |
67 |
+ |
|
68 |
+ |
if (mp == NULL) |
69 |
+ |
return(-1); |
70 |
+ |
mp->base = NULL; |
71 |
+ |
mp->len = 0; |
72 |
+ |
mp->mapped = 0; |
73 |
+ |
if (fp == NULL) |
74 |
+ |
return(-1); |
75 |
+ |
while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { |
76 |
+ |
if (!alloced) |
77 |
+ |
mp->base = malloc(nr); |
78 |
+ |
else if (mp->len+nr > alloced) |
79 |
+ |
mp->base = realloc(mp->base, |
80 |
+ |
alloced = alloced*(2+(nr==sizeof(buf)))/2+nr); |
81 |
+ |
if (mp->base == NULL) |
82 |
+ |
return(-1); |
83 |
+ |
memcpy((char *)mp->base + mp->len, buf, nr); |
84 |
+ |
mp->len += nr; |
85 |
+ |
} |
86 |
+ |
if (ferror(fp)) { |
87 |
+ |
free_load(mp); |
88 |
+ |
return(-1); |
89 |
+ |
} |
90 |
+ |
if (alloced > mp->len*5/4) /* don't waste too much space */ |
91 |
+ |
mp->base = realloc(mp->base, mp->len); |
92 |
+ |
return(mp->len > 0); |
93 |
+ |
} |
94 |
+ |
|
95 |
|
/* load a file into memory */ |
96 |
|
static int |
97 |
|
load_file(MEMLOAD *mp, FILE *fp) |
99 |
|
int fd; |
100 |
|
off_t skip, flen; |
101 |
|
|
102 |
+ |
#ifdef _WIN32 /* too difficult to fix this */ |
103 |
+ |
return load_stream(mp, fp); |
104 |
+ |
#endif |
105 |
|
if (mp == NULL) |
106 |
|
return(-1); |
107 |
|
mp->base = NULL; |
137 |
|
return(1); |
138 |
|
} |
139 |
|
|
102 |
– |
/* load memory from an input stream, starting from current position */ |
103 |
– |
static int |
104 |
– |
load_stream(MEMLOAD *mp, FILE *fp) |
105 |
– |
{ |
106 |
– |
size_t alloced = 0; |
107 |
– |
char buf[8192]; |
108 |
– |
size_t nr; |
109 |
– |
|
110 |
– |
if (mp == NULL) |
111 |
– |
return(-1); |
112 |
– |
mp->base = NULL; |
113 |
– |
mp->len = 0; |
114 |
– |
mp->mapped = 0; |
115 |
– |
if (fp == NULL) |
116 |
– |
return(-1); |
117 |
– |
while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { |
118 |
– |
if (!alloced) |
119 |
– |
mp->base = malloc(nr); |
120 |
– |
else if (mp->len+nr > alloced) |
121 |
– |
mp->base = realloc(mp->base, |
122 |
– |
alloced = alloced*(2+(nr==sizeof(buf)))/2+nr); |
123 |
– |
if (mp->base == NULL) |
124 |
– |
return(-1); |
125 |
– |
memcpy((char *)mp->base + mp->len, buf, nr); |
126 |
– |
mp->len += nr; |
127 |
– |
} |
128 |
– |
if (ferror(fp)) { |
129 |
– |
free_load(mp); |
130 |
– |
return(-1); |
131 |
– |
} |
132 |
– |
if (alloced > mp->len*5/4) /* don't waste too much space */ |
133 |
– |
mp->base = realloc(mp->base, mp->len); |
134 |
– |
return(mp->len > 0); |
135 |
– |
} |
136 |
– |
|
140 |
|
/* free a record index */ |
141 |
|
#define free_records(rp) free(rp) |
142 |
|
|
240 |
|
|
241 |
|
if (fp == NULL) |
242 |
|
return(0); |
243 |
< |
fflush(stdout); /* assumes nothing in input buffer */ |
244 |
< |
while ((n = read(fileno(fp), buf, sizeof(buf))) > 0) |
243 |
> |
fflush(stdout); |
244 |
> |
while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) |
245 |
|
if (write(fileno(stdout), buf, n) != n) |
246 |
|
return(0); |
247 |
< |
return(n >= 0); |
247 |
> |
return(!ferror(fp)); |
248 |
|
} |
249 |
|
|
250 |
|
/* get next word from stream, leaving stream on EOL or start of next word */ |
277 |
|
int ni_rows = 0; /* number of input rows */ |
278 |
|
int no_columns = 0; /* number of output columns */ |
279 |
|
int no_rows = 0; /* number of output rows */ |
280 |
+ |
int transpose = 0; /* transpose rows & cols? */ |
281 |
+ |
int i_header = 1; /* input header? */ |
282 |
+ |
int o_header = 1; /* output header? */ |
283 |
|
|
284 |
|
/* check settings and assign defaults */ |
285 |
< |
static void |
285 |
> |
static int |
286 |
|
check_sizes() |
287 |
|
{ |
288 |
|
if (fmtid == NULL) { |
294 |
|
comp_size = sizeof(double); |
295 |
|
else if (!strcmp(fmtid, "byte")) |
296 |
|
comp_size = 1; |
297 |
+ |
else if (strcmp(fmtid, "ascii")) { |
298 |
+ |
fprintf(stderr, "Unsupported format: %s\n", fmtid); |
299 |
+ |
return(0); |
300 |
+ |
} |
301 |
|
} |
302 |
+ |
if (transpose && (no_rows <= 0) & (no_columns <= 0)) { |
303 |
+ |
if (ni_rows > 0) no_columns = ni_rows; |
304 |
+ |
if (ni_columns > 0) no_rows = ni_columns; |
305 |
+ |
} else if ((no_rows <= 0) & (no_columns > 0) && |
306 |
+ |
!((ni_rows*ni_columns) % no_columns)) |
307 |
+ |
no_rows = ni_rows*ni_columns/no_columns; |
308 |
|
if (n_comp <= 0) |
309 |
|
n_comp = 3; |
310 |
+ |
return(1); |
311 |
|
} |
312 |
|
|
313 |
|
/* output transposed ASCII or binary data from memory */ |
384 |
|
int columns2go = no_columns; |
385 |
|
char word[256]; |
386 |
|
/* sanity checks */ |
387 |
< |
if (comp_size) { |
388 |
< |
fputs("Bad call to do_resize (binary input)\n", stderr); |
372 |
< |
return(0); |
373 |
< |
} |
387 |
> |
if (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows)) |
388 |
> |
return(output_stream(fp)); /* no-op -- just copy */ |
389 |
|
if (no_columns <= 0) { |
390 |
|
fprintf(stderr, "Missing -oc specification\n"); |
391 |
|
return(0); |
472 |
|
if (!strncmp(s, "NCOMP=", 6)) { |
473 |
|
n = atoi(s+6); |
474 |
|
if ((n_comp > 0) & (n != n_comp)) { |
475 |
< |
fputs("Incorrect number of components", stderr); |
475 |
> |
fputs("Incorrect number of components\n", stderr); |
476 |
|
return(-1); |
477 |
|
} |
478 |
|
n_comp = n; |
479 |
|
return(0); |
480 |
|
} |
481 |
< |
fputs(s, stdout); /* copy header info. */ |
481 |
> |
if (o_header) |
482 |
> |
fputs(s, stdout); /* copy header info. */ |
483 |
|
return(0); |
484 |
|
} |
485 |
|
|
487 |
|
int |
488 |
|
main(int argc, char *argv[]) |
489 |
|
{ |
490 |
< |
int do_header = 1; /* header i/o? */ |
475 |
< |
int transpose = 0; /* transpose rows & cols? */ |
476 |
< |
int i; |
490 |
> |
int a; |
491 |
|
|
492 |
< |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
493 |
< |
switch (argv[i][1]) { |
492 |
> |
for (a = 1; a < argc && argv[a][0] == '-'; a++) |
493 |
> |
switch (argv[a][1]) { |
494 |
|
case 'i': /* input */ |
495 |
< |
if (argv[i][2] == 'c') /* columns */ |
496 |
< |
ni_columns = atoi(argv[++i]); |
497 |
< |
else if (argv[i][2] == 'r') |
498 |
< |
ni_rows = atoi(argv[++i]); |
495 |
> |
if (argv[a][2] == 'c') /* columns */ |
496 |
> |
ni_columns = atoi(argv[++a]); |
497 |
> |
else if (argv[a][2] == 'r') |
498 |
> |
ni_rows = atoi(argv[++a]); |
499 |
|
else |
500 |
|
goto userr; |
501 |
|
break; |
502 |
|
case 'o': /* output */ |
503 |
< |
if (argv[i][2] == 'c') /* columns */ |
504 |
< |
no_columns = atoi(argv[++i]); |
505 |
< |
else if (argv[i][2] == 'r') |
506 |
< |
no_rows = atoi(argv[++i]); |
503 |
> |
if (argv[a][2] == 'c') /* columns */ |
504 |
> |
no_columns = atoi(argv[++a]); |
505 |
> |
else if (argv[a][2] == 'r') |
506 |
> |
no_rows = atoi(argv[++a]); |
507 |
|
else |
508 |
|
goto userr; |
509 |
|
break; |
510 |
< |
case 'h': /* header on/off */ |
511 |
< |
do_header = !do_header; |
510 |
> |
case 'h': /* turn off header */ |
511 |
> |
switch (argv[a][2]) { |
512 |
> |
case 'i': |
513 |
> |
i_header = 0; |
514 |
> |
break; |
515 |
> |
case 'o': |
516 |
> |
o_header = 0; |
517 |
> |
break; |
518 |
> |
case '\0': |
519 |
> |
i_header = o_header = 0; |
520 |
> |
break; |
521 |
> |
default: |
522 |
> |
goto userr; |
523 |
> |
} |
524 |
|
break; |
525 |
|
case 't': /* transpose on/off */ |
526 |
|
transpose = !transpose; |
527 |
|
break; |
528 |
|
case 'f': /* format */ |
529 |
< |
switch (argv[i][2]) { |
529 |
> |
switch (argv[a][2]) { |
530 |
|
case 'a': /* ASCII */ |
531 |
|
case 'A': |
532 |
|
fmtid = "ascii"; |
550 |
|
default: |
551 |
|
goto userr; |
552 |
|
} |
553 |
< |
if (argv[i][3]) { |
554 |
< |
if (!isdigit(argv[i][3])) |
553 |
> |
if (argv[a][3]) { |
554 |
> |
if (!isdigit(argv[a][3])) |
555 |
|
goto userr; |
556 |
< |
n_comp = atoi(argv[i]+3); |
557 |
< |
} |
556 |
> |
n_comp = atoi(argv[a]+3); |
557 |
> |
} else |
558 |
> |
n_comp = 1; |
559 |
|
break; |
560 |
|
case 'w': /* warnings on/off */ |
561 |
|
warnings = !warnings; |
563 |
|
default: |
564 |
|
goto userr; |
565 |
|
} |
566 |
< |
if (i < argc-1) /* arg count OK? */ |
566 |
> |
if (a < argc-1) /* arg count OK? */ |
567 |
|
goto userr; |
568 |
|
/* open input file? */ |
569 |
< |
if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
570 |
< |
fprintf(stderr, "%s: cannot open for reading\n", argv[i]); |
569 |
> |
if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) { |
570 |
> |
fprintf(stderr, "%s: cannot open for reading\n", argv[a]); |
571 |
|
return(1); |
572 |
|
} |
573 |
|
if (comp_size) { |
575 |
|
SET_FILE_BINARY(stdout); |
576 |
|
} |
577 |
|
/* check for no-op */ |
578 |
< |
if (!transpose && (comp_size || |
579 |
< |
(no_columns == ni_columns) & (no_rows == ni_rows))) { |
578 |
> |
if (!transpose & (i_header == o_header) && |
579 |
> |
(no_columns == ni_columns) & (no_rows == ni_rows)) { |
580 |
|
if (warnings) |
581 |
|
fprintf(stderr, "%s: no-op -- copying input verbatim\n", |
582 |
|
argv[0]); |
584 |
|
return(1); |
585 |
|
return(0); |
586 |
|
} |
587 |
< |
if (do_header) { /* read/write header */ |
588 |
< |
if (getheader(stdin, &headline, NULL) < 0) |
587 |
> |
if (i_header) { /* read header */ |
588 |
> |
if (getheader(stdin, headline, NULL) < 0) |
589 |
|
return(1); |
590 |
< |
check_sizes(); |
590 |
> |
if (!check_sizes()) |
591 |
> |
return(1); |
592 |
|
if (comp_size) { /* a little late... */ |
593 |
|
SET_FILE_BINARY(stdin); |
594 |
|
SET_FILE_BINARY(stdout); |
595 |
|
} |
596 |
< |
printargs(argc, argv, stdout); |
597 |
< |
if (transpose && (no_rows <= 0) & (no_columns <= 0)) { |
598 |
< |
if (ni_rows > 0) no_columns = ni_rows; |
599 |
< |
if (ni_columns > 0) no_rows = ni_columns; |
572 |
< |
} |
596 |
> |
} else if (!check_sizes()) |
597 |
> |
return(1); |
598 |
> |
if (o_header) { /* write header */ |
599 |
> |
printargs(a, argv, stdout); |
600 |
|
if (no_rows > 0) |
601 |
|
printf("NROWS=%d\n", no_rows); |
602 |
|
if (no_columns > 0) |
604 |
|
printf("NCOMP=%d\n", n_comp); |
605 |
|
fputformat(fmtid, stdout); |
606 |
|
fputc('\n', stdout); /* finish new header */ |
607 |
< |
} else |
581 |
< |
check_sizes(); |
607 |
> |
} |
608 |
|
if (transpose) { /* transposing rows & columns? */ |
609 |
< |
MEMLOAD myMem; /* need to load into memory */ |
610 |
< |
if (i == argc-1) { |
609 |
> |
MEMLOAD myMem; /* need to map into memory */ |
610 |
> |
if (a == argc-1) { |
611 |
|
if (load_file(&myMem, stdin) <= 0) { |
612 |
|
fprintf(stderr, "%s: error loading file into memory\n", |
613 |
< |
argv[i]); |
613 |
> |
argv[a]); |
614 |
|
return(1); |
615 |
|
} |
616 |
|
} else if (load_stream(&myMem, stdin) <= 0) { |
620 |
|
} |
621 |
|
if (!do_transpose(&myMem)) |
622 |
|
return(1); |
623 |
< |
/* free_load(&myMem); */ |
624 |
< |
} else if (!do_resize(stdin)) /* just reshaping input */ |
623 |
> |
/* free_load(&myMem); about to exit, so don't bother */ |
624 |
> |
} else if (!do_resize(stdin)) /* reshaping input */ |
625 |
|
return(1); |
626 |
|
return(0); |
627 |
|
userr: |
628 |
|
fprintf(stderr, |
629 |
< |
"Usage: %s [-h][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", |
629 |
> |
"Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", |
630 |
|
argv[0]); |
631 |
|
return(1); |
632 |
|
} |