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 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) |
306 |
|
RECINDEX *rp = NULL; |
307 |
|
long nrecords; |
308 |
|
int i, j; |
309 |
+ |
/* propogate sizes */ |
310 |
+ |
if (ni_rows <= 0) |
311 |
+ |
ni_rows = no_columns; |
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 */ |
292 |
– |
if (ni_rows <= 0) |
293 |
– |
ni_rows = no_columns; |
294 |
– |
if (ni_columns <= 0) |
295 |
– |
ni_columns = no_rows; |
331 |
|
if ((ni_rows <= 0) & (ni_columns > 0)) |
332 |
|
ni_rows = nrecords/ni_columns; |
333 |
|
if ((ni_columns <= 0) & (ni_rows > 0)) |
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", |
342 |
< |
record_width); |
375 |
> |
if (comp_size) { |
376 |
> |
fputs("Bad call to do_resize (binary input)\n", stderr); |
377 |
|
return(0); |
378 |
|
} |
379 |
|
if (no_columns <= 0) { |
391 |
|
do { /* reshape records */ |
392 |
|
int n; |
393 |
|
|
394 |
< |
for (n = record_width; n--; ) { |
394 |
> |
for (n = n_comp; n--; ) { |
395 |
|
if (fget_word(word, fp) == NULL) { |
396 |
< |
if (records2go > 0 || n < record_width-1) |
396 |
> |
if (records2go > 0 || n < n_comp-1) |
397 |
|
break; |
398 |
|
goto done; /* normal EOD */ |
399 |
|
} |
417 |
|
putc('\t', stdout); |
418 |
|
} while (--records2go); /* expected EOD? */ |
419 |
|
done: |
420 |
< |
if (columns2go != no_columns) |
420 |
> |
if (warnings && columns2go != no_columns) |
421 |
|
fprintf(stderr, "Warning -- incomplete final row\n"); |
422 |
< |
if (fget_word(word, fp) != NULL) |
423 |
< |
fprintf(stderr, "Warning -- data beyond expected EOF\n"); |
422 |
> |
if (warnings && fget_word(word, fp) != NULL) |
423 |
> |
fprintf(stderr, "Warning -- characters beyond expected EOD\n"); |
424 |
|
return(1); |
425 |
|
} |
426 |
|
|
428 |
|
static int |
429 |
|
headline(char *s, void *p) |
430 |
|
{ |
431 |
< |
char fmt[32]; |
431 |
> |
static char fmt[32]; |
432 |
> |
int n; |
433 |
|
|
434 |
|
if (formatval(fmt, s)) { |
435 |
+ |
if (fmtid == NULL) { |
436 |
+ |
fmtid = fmt; |
437 |
+ |
return(0); |
438 |
+ |
} |
439 |
|
if (!strcmp(fmt, fmtid)) |
440 |
|
return(0); |
441 |
|
fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid); |
442 |
|
return(-1); |
443 |
|
} |
444 |
+ |
if (!strncmp(s, "NROWS=", 6)) { |
445 |
+ |
n = atoi(s+6); |
446 |
+ |
if ((ni_rows > 0) & (n != ni_rows)) { |
447 |
+ |
fputs("Incorrect input row count\n", stderr); |
448 |
+ |
return(-1); |
449 |
+ |
} |
450 |
+ |
ni_rows = n; |
451 |
+ |
return(0); |
452 |
+ |
} |
453 |
+ |
if (!strncmp(s, "NCOLS=", 6)) { |
454 |
+ |
n = atoi(s+6); |
455 |
+ |
if ((ni_columns > 0) & (n != ni_columns)) { |
456 |
+ |
fputs("Incorrect input column count\n", stderr); |
457 |
+ |
return(-1); |
458 |
+ |
} |
459 |
+ |
ni_columns = n; |
460 |
+ |
return(0); |
461 |
+ |
} |
462 |
+ |
if (!strncmp(s, "NCOMP=", 6)) { |
463 |
+ |
n = atoi(s+6); |
464 |
+ |
if ((n_comp > 0) & (n != n_comp)) { |
465 |
+ |
fputs("Incorrect number of components", stderr); |
466 |
+ |
return(-1); |
467 |
+ |
} |
468 |
+ |
n_comp = n; |
469 |
+ |
return(0); |
470 |
+ |
} |
471 |
|
fputs(s, stdout); /* copy header info. */ |
472 |
|
return(0); |
473 |
|
} |
476 |
|
int |
477 |
|
main(int argc, char *argv[]) |
478 |
|
{ |
479 |
< |
int do_header = 1; /* header i/o? */ |
479 |
> |
int i_header = 1; /* input header? */ |
480 |
> |
int o_header = 1; /* output header? */ |
481 |
|
int transpose = 0; /* transpose rows & cols? */ |
482 |
|
int i; |
483 |
|
|
499 |
|
else |
500 |
|
goto userr; |
501 |
|
break; |
502 |
< |
case 'h': /* header on/off */ |
503 |
< |
do_header = !do_header; |
502 |
> |
case 'h': /* turn off header */ |
503 |
> |
switch (argv[i][2]) { |
504 |
> |
case 'i': |
505 |
> |
i_header = 0; |
506 |
> |
break; |
507 |
> |
case 'o': |
508 |
> |
o_header = 0; |
509 |
> |
break; |
510 |
> |
case '\0': |
511 |
> |
i_header = o_header = 0; |
512 |
> |
break; |
513 |
> |
default: |
514 |
> |
goto userr; |
515 |
> |
} |
516 |
|
break; |
517 |
|
case 't': /* transpose on/off */ |
518 |
|
transpose = !transpose; |
522 |
|
case 'a': /* ASCII */ |
523 |
|
case 'A': |
524 |
|
fmtid = "ascii"; |
525 |
< |
record_width = 1; |
525 |
> |
comp_size = 0; |
526 |
|
break; |
527 |
|
case 'f': /* float */ |
528 |
|
case 'F': |
529 |
|
fmtid = "float"; |
530 |
< |
record_width = -(int)sizeof(float); |
530 |
> |
comp_size = sizeof(float); |
531 |
|
break; |
532 |
|
case 'd': /* double */ |
533 |
|
case 'D': |
534 |
|
fmtid = "double"; |
535 |
< |
record_width = -(int)sizeof(double); |
535 |
> |
comp_size = sizeof(double); |
536 |
|
break; |
537 |
|
case 'b': /* binary (bytes) */ |
538 |
|
case 'B': |
539 |
|
fmtid = "byte"; |
540 |
< |
record_width = -1; |
540 |
> |
comp_size = 1; |
541 |
|
break; |
542 |
|
default: |
543 |
|
goto userr; |
545 |
|
if (argv[i][3]) { |
546 |
|
if (!isdigit(argv[i][3])) |
547 |
|
goto userr; |
548 |
< |
record_width *= atoi(argv[i]+3); |
548 |
> |
n_comp = atoi(argv[i]+3); |
549 |
|
} |
550 |
|
break; |
551 |
+ |
case 'w': /* warnings on/off */ |
552 |
+ |
warnings = !warnings; |
553 |
+ |
break; |
554 |
|
default: |
555 |
|
goto userr; |
556 |
|
} |
475 |
– |
if (!record_width) |
476 |
– |
goto userr; |
557 |
|
if (i < argc-1) /* arg count OK? */ |
558 |
|
goto userr; |
559 |
|
/* open input file? */ |
561 |
|
fprintf(stderr, "%s: cannot open for reading\n", argv[i]); |
562 |
|
return(1); |
563 |
|
} |
564 |
< |
if (record_width < 0) { |
564 |
> |
if (comp_size) { |
565 |
|
SET_FILE_BINARY(stdin); |
566 |
|
SET_FILE_BINARY(stdout); |
567 |
|
} |
568 |
|
/* check for no-op */ |
569 |
< |
if (!transpose && (record_width < 0 || |
569 |
> |
if (!transpose && (comp_size || |
570 |
|
(no_columns == ni_columns) & (no_rows == ni_rows))) { |
571 |
< |
fprintf(stderr, "%s: no-op -- copying input verbatim\n", |
571 |
> |
if (warnings) |
572 |
> |
fprintf(stderr, "%s: no-op -- copying input verbatim\n", |
573 |
|
argv[0]); |
574 |
|
if (!output_stream(stdin)) |
575 |
|
return(1); |
576 |
|
return(0); |
577 |
|
} |
578 |
< |
if (do_header) { /* read/write header */ |
578 |
> |
if (i_header) { /* read header */ |
579 |
|
if (getheader(stdin, &headline, NULL) < 0) |
580 |
|
return(1); |
581 |
+ |
if (!check_sizes()) |
582 |
+ |
return(1); |
583 |
+ |
if (comp_size) { /* a little late... */ |
584 |
+ |
SET_FILE_BINARY(stdin); |
585 |
+ |
SET_FILE_BINARY(stdout); |
586 |
+ |
} |
587 |
+ |
} else if (!check_sizes()) |
588 |
+ |
return(1); |
589 |
+ |
if (o_header) { /* write header */ |
590 |
|
printargs(argc, argv, stdout); |
591 |
+ |
if (transpose && (no_rows <= 0) & (no_columns <= 0)) { |
592 |
+ |
if (ni_rows > 0) no_columns = ni_rows; |
593 |
+ |
if (ni_columns > 0) no_rows = ni_columns; |
594 |
+ |
} |
595 |
+ |
if (no_rows > 0) |
596 |
+ |
printf("NROWS=%d\n", no_rows); |
597 |
+ |
if (no_columns > 0) |
598 |
+ |
printf("NCOLS=%d\n", no_columns); |
599 |
+ |
printf("NCOMP=%d\n", n_comp); |
600 |
|
fputformat(fmtid, stdout); |
601 |
|
fputc('\n', stdout); /* finish new header */ |
602 |
|
} |
621 |
|
return(0); |
622 |
|
userr: |
623 |
|
fprintf(stderr, |
624 |
< |
"Usage: %s [-h][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", |
624 |
> |
"Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", |
625 |
|
argv[0]); |
626 |
|
return(1); |
627 |
|
} |