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 |
|
|
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) |
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)) { |
317 |
|
nrecords = ni_rows*ni_columns; |
318 |
< |
if (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 / -record_width; |
324 |
> |
nrecords = mp->len/(n_comp*comp_size); |
325 |
|
/* check sizes */ |
326 |
|
if ((ni_rows <= 0) & (ni_columns > 0)) |
327 |
|
ni_rows = nrecords/ni_columns; |
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", |
344 |
< |
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 |
|
} |
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 |
|
} |
504 |
|
case 'a': /* ASCII */ |
505 |
|
case 'A': |
506 |
|
fmtid = "ascii"; |
507 |
< |
record_width = 1; |
507 |
> |
comp_size = 0; |
508 |
|
break; |
509 |
|
case 'f': /* float */ |
510 |
|
case 'F': |
511 |
|
fmtid = "float"; |
512 |
< |
record_width = -(int)sizeof(float); |
512 |
> |
comp_size = sizeof(float); |
513 |
|
break; |
514 |
|
case 'd': /* double */ |
515 |
|
case 'D': |
516 |
|
fmtid = "double"; |
517 |
< |
record_width = -(int)sizeof(double); |
517 |
> |
comp_size = sizeof(double); |
518 |
|
break; |
519 |
|
case 'b': /* binary (bytes) */ |
520 |
|
case 'B': |
521 |
|
fmtid = "byte"; |
522 |
< |
record_width = -1; |
522 |
> |
comp_size = 1; |
523 |
|
break; |
524 |
|
default: |
525 |
|
goto userr; |
527 |
|
if (argv[i][3]) { |
528 |
|
if (!isdigit(argv[i][3])) |
529 |
|
goto userr; |
530 |
< |
record_width *= atoi(argv[i]+3); |
530 |
> |
n_comp = atoi(argv[i]+3); |
531 |
|
} |
532 |
|
break; |
533 |
|
case 'w': /* warnings on/off */ |
536 |
|
default: |
537 |
|
goto userr; |
538 |
|
} |
480 |
– |
if (!record_width) |
481 |
– |
goto userr; |
539 |
|
if (i < argc-1) /* arg count OK? */ |
540 |
|
goto userr; |
541 |
|
/* open input file? */ |
543 |
|
fprintf(stderr, "%s: cannot open for reading\n", argv[i]); |
544 |
|
return(1); |
545 |
|
} |
546 |
< |
if (record_width < 0) { |
546 |
> |
if (comp_size) { |
547 |
|
SET_FILE_BINARY(stdin); |
548 |
|
SET_FILE_BINARY(stdout); |
549 |
|
} |
550 |
|
/* check for no-op */ |
551 |
< |
if (!transpose && (record_width < 0 || |
551 |
> |
if (!transpose && (comp_size || |
552 |
|
(no_columns == ni_columns) & (no_rows == ni_rows))) { |
553 |
|
if (warnings) |
554 |
|
fprintf(stderr, "%s: no-op -- copying input verbatim\n", |
560 |
|
if (do_header) { /* read/write header */ |
561 |
|
if (getheader(stdin, &headline, NULL) < 0) |
562 |
|
return(1); |
563 |
+ |
check_sizes(); |
564 |
+ |
if (comp_size) { /* a little late... */ |
565 |
+ |
SET_FILE_BINARY(stdin); |
566 |
+ |
SET_FILE_BINARY(stdout); |
567 |
+ |
} |
568 |
|
printargs(argc, argv, stdout); |
569 |
+ |
if (transpose && (no_rows <= 0) & (no_columns <= 0)) { |
570 |
+ |
if (ni_rows > 0) no_columns = ni_rows; |
571 |
+ |
if (ni_columns > 0) no_rows = ni_columns; |
572 |
+ |
} |
573 |
+ |
if (no_rows > 0) |
574 |
+ |
printf("NROWS=%d\n", no_rows); |
575 |
+ |
if (no_columns > 0) |
576 |
+ |
printf("NCOLS=%d\n", no_columns); |
577 |
+ |
printf("NCOMP=%d\n", n_comp); |
578 |
|
fputformat(fmtid, stdout); |
579 |
|
fputc('\n', stdout); /* finish new header */ |
580 |
< |
} |
580 |
> |
} else |
581 |
> |
check_sizes(); |
582 |
|
if (transpose) { /* transposing rows & columns? */ |
583 |
|
MEMLOAD myMem; /* need to load into memory */ |
584 |
|
if (i == argc-1) { |