6 |
|
*/ |
7 |
|
|
8 |
|
#include <stdlib.h> |
9 |
– |
#include <unistd.h> |
9 |
|
#include <string.h> |
10 |
|
#include <ctype.h> |
11 |
|
#include "platform.h" |
12 |
|
#include "rtio.h" |
13 |
|
#include "resolu.h" |
14 |
< |
#ifndef _WIN32 |
14 |
> |
#ifdef _WIN32 |
15 |
> |
#undef ftello |
16 |
> |
#define ftello ftell |
17 |
> |
#undef ssize_t |
18 |
> |
#define ssize_t size_t |
19 |
> |
#else |
20 |
|
#include <sys/mman.h> |
21 |
|
#endif |
22 |
|
|
39 |
|
char *rec[1]; /* record array (extends struct) */ |
40 |
|
} RECINDEX; |
41 |
|
|
42 |
+ |
int warnings = 1; /* report warnings? */ |
43 |
+ |
|
44 |
|
/* free loaded file */ |
45 |
|
static void |
46 |
|
free_load(MEMLOAD *mp) |
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 |
|
|
115 |
|
if (fp == NULL) |
116 |
|
return(-1); |
117 |
|
while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { |
118 |
< |
if (!mp->len) |
118 |
> |
if (!alloced) |
119 |
|
mp->base = malloc(nr); |
120 |
< |
else |
121 |
< |
mp->base = realloc(mp->base, mp->len+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); |
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 |
|
|
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 void |
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 |
+ |
} |
292 |
+ |
if (n_comp <= 0) |
293 |
+ |
n_comp = 3; |
294 |
+ |
} |
295 |
+ |
|
296 |
|
/* output transposed ASCII or binary data from memory */ |
297 |
|
static int |
298 |
|
do_transpose(const MEMLOAD *mp) |
301 |
|
RECINDEX *rp = NULL; |
302 |
|
long nrecords; |
303 |
|
int i, j; |
304 |
+ |
/* propogate sizes */ |
305 |
+ |
if (ni_rows <= 0) |
306 |
+ |
ni_rows = no_columns; |
307 |
+ |
if (ni_columns <= 0) |
308 |
+ |
ni_columns = no_rows; |
309 |
|
/* get # records (& index) */ |
310 |
< |
if (record_width > 0) { |
311 |
< |
if ((rp = index_records(mp, record_width)) == NULL) |
310 |
> |
if (!comp_size) { |
311 |
> |
if ((rp = index_records(mp, n_comp)) == NULL) |
312 |
|
return(0); |
313 |
|
if (ni_columns <= 0) |
314 |
|
ni_columns = count_columns(rp); |
315 |
|
nrecords = rp->nrecs; |
316 |
< |
} else if ((ni_rows > 0) & (ni_columns > 0)) |
316 |
> |
} else if ((ni_rows > 0) & (ni_columns > 0)) { |
317 |
|
nrecords = ni_rows*ni_columns; |
318 |
< |
else |
319 |
< |
nrecords = mp->len / -record_width; |
318 |
> |
if (nrecords > mp->len/(n_comp*comp_size)) { |
319 |
> |
fprintf(stderr, |
320 |
> |
"Input too small for specified size and type\n"); |
321 |
> |
return(0); |
322 |
> |
} |
323 |
> |
} else |
324 |
> |
nrecords = mp->len/(n_comp*comp_size); |
325 |
|
/* check sizes */ |
287 |
– |
if (ni_rows <= 0) |
288 |
– |
ni_rows = no_columns; |
289 |
– |
if (ni_columns <= 0) |
290 |
– |
ni_columns = no_rows; |
326 |
|
if ((ni_rows <= 0) & (ni_columns > 0)) |
327 |
|
ni_rows = nrecords/ni_columns; |
328 |
|
if ((ni_columns <= 0) & (ni_rows > 0)) |
343 |
|
putc(tabEOL[j >= no_columns-1], stdout); |
344 |
|
} else { /* binary output */ |
345 |
|
fwrite((char *)mp->base + |
346 |
< |
-record_width*(j*ni_columns + i), |
347 |
< |
-record_width, 1, stdout); |
346 |
> |
(n_comp*comp_size)*(j*ni_columns + i), |
347 |
> |
n_comp*comp_size, 1, stdout); |
348 |
|
} |
349 |
|
if (ferror(stdout)) { |
350 |
|
fprintf(stderr, "Error writing to stdout\n"); |
367 |
|
int columns2go = no_columns; |
368 |
|
char word[256]; |
369 |
|
/* sanity checks */ |
370 |
< |
if (record_width <= 0) { |
371 |
< |
fprintf(stderr, "Bad call to do_resize (record_width = %d)\n", |
337 |
< |
record_width); |
370 |
> |
if (comp_size) { |
371 |
> |
fputs("Bad call to do_resize (binary input)\n", stderr); |
372 |
|
return(0); |
373 |
|
} |
374 |
|
if (no_columns <= 0) { |
386 |
|
do { /* reshape records */ |
387 |
|
int n; |
388 |
|
|
389 |
< |
for (n = record_width; n--; ) { |
389 |
> |
for (n = n_comp; n--; ) { |
390 |
|
if (fget_word(word, fp) == NULL) { |
391 |
< |
if (records2go > 0 || n < record_width-1) |
391 |
> |
if (records2go > 0 || n < n_comp-1) |
392 |
|
break; |
393 |
|
goto done; /* normal EOD */ |
394 |
|
} |
412 |
|
putc('\t', stdout); |
413 |
|
} while (--records2go); /* expected EOD? */ |
414 |
|
done: |
415 |
< |
if (columns2go != no_columns) |
415 |
> |
if (warnings && columns2go != no_columns) |
416 |
|
fprintf(stderr, "Warning -- incomplete final row\n"); |
417 |
< |
if (fget_word(word, fp) != NULL) |
418 |
< |
fprintf(stderr, "Warning -- data beyond expected EOF\n"); |
417 |
> |
if (warnings && fget_word(word, fp) != NULL) |
418 |
> |
fprintf(stderr, "Warning -- characters beyond expected EOD\n"); |
419 |
|
return(1); |
420 |
|
} |
421 |
|
|
423 |
|
static int |
424 |
|
headline(char *s, void *p) |
425 |
|
{ |
426 |
< |
char fmt[32]; |
426 |
> |
static char fmt[32]; |
427 |
> |
int n; |
428 |
|
|
429 |
|
if (formatval(fmt, s)) { |
430 |
+ |
if (fmtid == NULL) { |
431 |
+ |
fmtid = fmt; |
432 |
+ |
return(0); |
433 |
+ |
} |
434 |
|
if (!strcmp(fmt, fmtid)) |
435 |
|
return(0); |
436 |
|
fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid); |
437 |
|
return(-1); |
438 |
|
} |
439 |
+ |
if (!strncmp(s, "NROWS=", 6)) { |
440 |
+ |
n = atoi(s+6); |
441 |
+ |
if ((ni_rows > 0) & (n != ni_rows)) { |
442 |
+ |
fputs("Incorrect input row count\n", stderr); |
443 |
+ |
return(-1); |
444 |
+ |
} |
445 |
+ |
ni_rows = n; |
446 |
+ |
return(0); |
447 |
+ |
} |
448 |
+ |
if (!strncmp(s, "NCOLS=", 6)) { |
449 |
+ |
n = atoi(s+6); |
450 |
+ |
if ((ni_columns > 0) & (n != ni_columns)) { |
451 |
+ |
fputs("Incorrect input column count\n", stderr); |
452 |
+ |
return(-1); |
453 |
+ |
} |
454 |
+ |
ni_columns = n; |
455 |
+ |
return(0); |
456 |
+ |
} |
457 |
+ |
if (!strncmp(s, "NCOMP=", 6)) { |
458 |
+ |
n = atoi(s+6); |
459 |
+ |
if ((n_comp > 0) & (n != n_comp)) { |
460 |
+ |
fputs("Incorrect number of components", stderr); |
461 |
+ |
return(-1); |
462 |
+ |
} |
463 |
+ |
n_comp = n; |
464 |
+ |
return(0); |
465 |
+ |
} |
466 |
|
fputs(s, stdout); /* copy header info. */ |
467 |
|
return(0); |
468 |
|
} |
471 |
|
int |
472 |
|
main(int argc, char *argv[]) |
473 |
|
{ |
474 |
< |
int do_header = 1; /* header i/o? */ |
474 |
> |
int i_header = 1; /* input header? */ |
475 |
> |
int o_header = 1; /* output header? */ |
476 |
|
int transpose = 0; /* transpose rows & cols? */ |
477 |
|
int i; |
478 |
|
|
494 |
|
else |
495 |
|
goto userr; |
496 |
|
break; |
497 |
< |
case 'h': /* header on/off */ |
498 |
< |
do_header = !do_header; |
497 |
> |
case 'h': /* turn off header */ |
498 |
> |
switch (argv[i][2]) { |
499 |
> |
case 'i': |
500 |
> |
i_header = 0; |
501 |
> |
break; |
502 |
> |
case 'o': |
503 |
> |
o_header = 0; |
504 |
> |
break; |
505 |
> |
case '\0': |
506 |
> |
i_header = o_header = 0; |
507 |
> |
break; |
508 |
> |
default: |
509 |
> |
goto userr; |
510 |
> |
} |
511 |
|
break; |
512 |
|
case 't': /* transpose on/off */ |
513 |
|
transpose = !transpose; |
517 |
|
case 'a': /* ASCII */ |
518 |
|
case 'A': |
519 |
|
fmtid = "ascii"; |
520 |
< |
record_width = 1; |
520 |
> |
comp_size = 0; |
521 |
|
break; |
522 |
|
case 'f': /* float */ |
523 |
|
case 'F': |
524 |
|
fmtid = "float"; |
525 |
< |
record_width = -(int)sizeof(float); |
525 |
> |
comp_size = sizeof(float); |
526 |
|
break; |
527 |
|
case 'd': /* double */ |
528 |
|
case 'D': |
529 |
|
fmtid = "double"; |
530 |
< |
record_width = -(int)sizeof(double); |
530 |
> |
comp_size = sizeof(double); |
531 |
|
break; |
532 |
|
case 'b': /* binary (bytes) */ |
533 |
|
case 'B': |
534 |
|
fmtid = "byte"; |
535 |
< |
record_width = -1; |
535 |
> |
comp_size = 1; |
536 |
|
break; |
537 |
|
default: |
538 |
|
goto userr; |
540 |
|
if (argv[i][3]) { |
541 |
|
if (!isdigit(argv[i][3])) |
542 |
|
goto userr; |
543 |
< |
record_width *= atoi(argv[i]+3); |
543 |
> |
n_comp = atoi(argv[i]+3); |
544 |
|
} |
545 |
|
break; |
546 |
+ |
case 'w': /* warnings on/off */ |
547 |
+ |
warnings = !warnings; |
548 |
+ |
break; |
549 |
|
default: |
550 |
|
goto userr; |
551 |
|
} |
470 |
– |
if (!record_width) |
471 |
– |
goto userr; |
552 |
|
if (i < argc-1) /* arg count OK? */ |
553 |
|
goto userr; |
554 |
|
/* open input file? */ |
556 |
|
fprintf(stderr, "%s: cannot open for reading\n", argv[i]); |
557 |
|
return(1); |
558 |
|
} |
559 |
< |
if (record_width < 0) { |
559 |
> |
if (comp_size) { |
560 |
|
SET_FILE_BINARY(stdin); |
561 |
|
SET_FILE_BINARY(stdout); |
562 |
|
} |
563 |
|
/* check for no-op */ |
564 |
< |
if (!transpose && (record_width < 0 || |
564 |
> |
if (!transpose && (comp_size || |
565 |
|
(no_columns == ni_columns) & (no_rows == ni_rows))) { |
566 |
< |
fprintf(stderr, "%s: no-op -- copying input verbatim\n", |
566 |
> |
if (warnings) |
567 |
> |
fprintf(stderr, "%s: no-op -- copying input verbatim\n", |
568 |
|
argv[0]); |
569 |
|
if (!output_stream(stdin)) |
570 |
|
return(1); |
571 |
|
return(0); |
572 |
|
} |
573 |
< |
if (do_header) { /* read/write header */ |
573 |
> |
if (i_header) { /* read header */ |
574 |
|
if (getheader(stdin, &headline, NULL) < 0) |
575 |
|
return(1); |
576 |
+ |
check_sizes(); |
577 |
+ |
if (comp_size) { /* a little late... */ |
578 |
+ |
SET_FILE_BINARY(stdin); |
579 |
+ |
SET_FILE_BINARY(stdout); |
580 |
+ |
} |
581 |
+ |
} else |
582 |
+ |
check_sizes(); |
583 |
+ |
if (o_header) { /* write header */ |
584 |
|
printargs(argc, argv, stdout); |
585 |
+ |
if (transpose && (no_rows <= 0) & (no_columns <= 0)) { |
586 |
+ |
if (ni_rows > 0) no_columns = ni_rows; |
587 |
+ |
if (ni_columns > 0) no_rows = ni_columns; |
588 |
+ |
} |
589 |
+ |
if (no_rows > 0) |
590 |
+ |
printf("NROWS=%d\n", no_rows); |
591 |
+ |
if (no_columns > 0) |
592 |
+ |
printf("NCOLS=%d\n", no_columns); |
593 |
+ |
printf("NCOMP=%d\n", n_comp); |
594 |
|
fputformat(fmtid, stdout); |
595 |
|
fputc('\n', stdout); /* finish new header */ |
596 |
|
} |
615 |
|
return(0); |
616 |
|
userr: |
617 |
|
fprintf(stderr, |
618 |
< |
"Usage: %s [-h][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", |
618 |
> |
"Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", |
619 |
|
argv[0]); |
620 |
|
return(1); |
621 |
|
} |