1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rcollate.c,v 2.35 2019/12/28 18:05:14 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Utility to re-order records in a binary or ASCII data file (matrix) |
6 |
*/ |
7 |
|
8 |
#include <stdlib.h> |
9 |
#include <ctype.h> |
10 |
#include "platform.h" |
11 |
#include "rtio.h" |
12 |
#include "resolu.h" |
13 |
#if defined(_WIN32) || defined(_WIN64) |
14 |
#undef ftello |
15 |
#define ftello ftell |
16 |
#undef ssize_t |
17 |
#define ssize_t size_t |
18 |
#else |
19 |
#include <sys/mman.h> |
20 |
#endif |
21 |
|
22 |
#define MAXLEVELS 16 /* max RxC.. block pairs */ |
23 |
|
24 |
typedef struct { |
25 |
void *mapped; /* memory-mapped pointer */ |
26 |
void *base; /* pointer to base memory */ |
27 |
size_t len; /* allocated memory length */ |
28 |
} MEMLOAD; /* file loaded/mapped into memory */ |
29 |
|
30 |
typedef struct { |
31 |
int nw_rec; /* number of words per record */ |
32 |
long nrecs; /* number of records we found */ |
33 |
char *rec[1]; /* record array (extends struct) */ |
34 |
} RECINDEX; |
35 |
|
36 |
int warnings = 1; /* report warnings? */ |
37 |
|
38 |
/* free loaded file */ |
39 |
static void |
40 |
free_load(MEMLOAD *mp) |
41 |
{ |
42 |
if (mp == NULL || (mp->base == NULL) | (mp->len <= 0)) |
43 |
return; |
44 |
#ifdef MAP_FILE |
45 |
if (mp->mapped) |
46 |
munmap(mp->mapped, mp->len); |
47 |
else |
48 |
#endif |
49 |
free(mp->base); |
50 |
mp->mapped = NULL; |
51 |
mp->base = NULL; |
52 |
mp->len = 0; |
53 |
} |
54 |
|
55 |
/* load memory from an input stream, starting from current position */ |
56 |
static int |
57 |
load_stream(MEMLOAD *mp, FILE *fp) |
58 |
{ |
59 |
size_t alloced = 0; |
60 |
char buf[8192]; |
61 |
size_t nr; |
62 |
|
63 |
if (mp == NULL) |
64 |
return(-1); |
65 |
mp->mapped = NULL; |
66 |
mp->base = NULL; |
67 |
mp->len = 0; |
68 |
if (fp == NULL) |
69 |
return(-1); |
70 |
while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { |
71 |
if (!alloced) |
72 |
mp->base = malloc(alloced = nr); |
73 |
else if (mp->len+nr > alloced) |
74 |
mp->base = realloc(mp->base, |
75 |
alloced = alloced*(2+(nr==sizeof(buf)))/2+nr); |
76 |
if (mp->base == NULL) |
77 |
return(-1); |
78 |
memcpy((char *)mp->base + mp->len, buf, nr); |
79 |
mp->len += nr; |
80 |
} |
81 |
if (ferror(fp)) { |
82 |
free_load(mp); |
83 |
return(-1); |
84 |
} |
85 |
if (alloced > mp->len*5/4) /* don't waste too much space */ |
86 |
mp->base = realloc(mp->base, mp->len); |
87 |
return(mp->len > 0); |
88 |
} |
89 |
|
90 |
/* load a file into memory */ |
91 |
static int |
92 |
load_file(MEMLOAD *mp, FILE *fp) |
93 |
{ |
94 |
int fd; |
95 |
off_t skip, flen, fpos; |
96 |
|
97 |
#if defined(_WIN32) || defined(_WIN64) |
98 |
/* too difficult to fix this */ |
99 |
return load_stream(mp, fp); |
100 |
#endif |
101 |
if (mp == NULL) |
102 |
return(-1); |
103 |
mp->mapped = NULL; |
104 |
mp->base = NULL; |
105 |
mp->len = 0; |
106 |
if (fp == NULL) |
107 |
return(-1); |
108 |
fd = fileno(fp); |
109 |
skip = ftello(fp); |
110 |
flen = lseek(fd, 0, SEEK_END); |
111 |
if (flen <= skip) |
112 |
return((int)(flen - skip)); |
113 |
mp->len = (size_t)(flen - skip); |
114 |
#ifdef MAP_FILE |
115 |
if (mp->len > 1L<<20) { /* map file if > 1 MByte */ |
116 |
mp->mapped = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0); |
117 |
if (mp->mapped != MAP_FAILED) { |
118 |
mp->base = (char *)mp->mapped + skip; |
119 |
return(1); /* mmap() success */ |
120 |
} |
121 |
mp->mapped = NULL; /* else fall back to reading it in... */ |
122 |
} |
123 |
#endif |
124 |
if (lseek(fd, skip, SEEK_SET) != skip || |
125 |
(mp->base = malloc(mp->len)) == NULL) { |
126 |
mp->len = 0; |
127 |
return(-1); |
128 |
} |
129 |
fpos = skip; |
130 |
while (fpos < flen) { /* read() fails if n > 2 GBytes */ |
131 |
ssize_t nread = read(fd, (char *)mp->base+(fpos-skip), |
132 |
(flen-fpos < 1L<<24) ? flen-fpos : 1L<<24); |
133 |
if (nread <= 0) { |
134 |
free_load(mp); |
135 |
return(-1); |
136 |
} |
137 |
fpos += nread; |
138 |
} |
139 |
return(1); |
140 |
} |
141 |
|
142 |
/* free a record index */ |
143 |
#define free_records(rp) free(rp) |
144 |
|
145 |
/* compute record index */ |
146 |
static RECINDEX * |
147 |
index_records(const MEMLOAD *mp, int nw_rec) |
148 |
{ |
149 |
int nall = 0; |
150 |
RECINDEX *rp; |
151 |
char *cp, *mend; |
152 |
int n; |
153 |
|
154 |
if (mp == NULL || (mp->base == NULL) | (mp->len <= 0)) |
155 |
return(NULL); |
156 |
if (nw_rec <= 0) |
157 |
return(NULL); |
158 |
nall = 1000; |
159 |
rp = (RECINDEX *)malloc(sizeof(RECINDEX) + nall*sizeof(char *)); |
160 |
if (rp == NULL) |
161 |
return(NULL); |
162 |
rp->nw_rec = nw_rec; |
163 |
rp->nrecs = 0; |
164 |
cp = (char *)mp->base; |
165 |
mend = cp + mp->len; |
166 |
for ( ; ; ) { /* whitespace-separated words */ |
167 |
while (cp < mend && !*cp | isspace(*cp)) |
168 |
++cp; |
169 |
if (cp >= mend) |
170 |
break; |
171 |
if (rp->nrecs >= nall) { |
172 |
nall += nall>>1; /* get more record space */ |
173 |
rp = (RECINDEX *)realloc(rp, |
174 |
sizeof(RECINDEX) + nall*sizeof(char *)); |
175 |
if (rp == NULL) |
176 |
return(NULL); |
177 |
} |
178 |
rp->rec[rp->nrecs++] = cp; /* point to first non-white */ |
179 |
n = rp->nw_rec; |
180 |
while (++cp < mend) /* find end of record */ |
181 |
if (!*cp | isspace(*cp)) { |
182 |
if (--n <= 0) |
183 |
break; /* got requisite # words */ |
184 |
do { /* else find next word */ |
185 |
if (*cp == '\n') { |
186 |
fprintf(stderr, |
187 |
"Unexpected EOL in record!\n"); |
188 |
free_records(rp); |
189 |
return(NULL); |
190 |
} |
191 |
if (++cp >= mend) |
192 |
break; |
193 |
} while (!*cp | isspace(*cp)); |
194 |
} |
195 |
} |
196 |
rp->rec[rp->nrecs] = mend; /* reallocate to save space */ |
197 |
rp = (RECINDEX *)realloc(rp, |
198 |
sizeof(RECINDEX) + rp->nrecs*sizeof(char *)); |
199 |
return(rp); |
200 |
} |
201 |
|
202 |
/* count number of columns based on first EOL */ |
203 |
static int |
204 |
count_columns(const RECINDEX *rp) |
205 |
{ |
206 |
char *cp = rp->rec[0]; |
207 |
char *mend = rp->rec[rp->nrecs]; |
208 |
int i; |
209 |
|
210 |
while (*cp != '\n') |
211 |
if (++cp >= mend) |
212 |
return(0); |
213 |
for (i = 0; i < rp->nrecs; i++) |
214 |
if (rp->rec[i] >= cp) |
215 |
break; |
216 |
return(i); |
217 |
} |
218 |
|
219 |
/* copy nth record from index to stdout */ |
220 |
static int |
221 |
print_record(const RECINDEX *rp, long n) |
222 |
{ |
223 |
int words2go = rp->nw_rec; |
224 |
char *scp; |
225 |
|
226 |
if ((n < 0) | (n >= rp->nrecs)) |
227 |
return(0); |
228 |
scp = rp->rec[n]; |
229 |
do { |
230 |
putc(*scp++, stdout); |
231 |
if (!*scp | isspace(*scp)) { |
232 |
if (--words2go <= 0) |
233 |
break; |
234 |
putc(' ', stdout); /* single space btwn. words */ |
235 |
do |
236 |
if (++scp >= rp->rec[n+1]) |
237 |
break; |
238 |
while (!*scp | isspace(*scp)); |
239 |
} |
240 |
} while (scp < rp->rec[n+1]); |
241 |
/* caller adds record sep. */ |
242 |
return(1); |
243 |
} |
244 |
|
245 |
/* copy a stream to stdout */ |
246 |
static int |
247 |
output_stream(FILE *fp) |
248 |
{ |
249 |
char buf[8192]; |
250 |
ssize_t n; |
251 |
|
252 |
if (fp == NULL) |
253 |
return(0); |
254 |
fflush(stdout); |
255 |
while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) |
256 |
if (write(fileno(stdout), buf, n) != n) |
257 |
return(0); |
258 |
return(!ferror(fp)); |
259 |
} |
260 |
|
261 |
/* get next word from stream, leaving stream on EOL or start of next word */ |
262 |
static char * |
263 |
fget_word(char buf[256], FILE *fp) |
264 |
{ |
265 |
int c; |
266 |
char *cp; |
267 |
/* skip nul's and white space */ |
268 |
while (!(c = getc(fp)) || isspace(c)) |
269 |
; |
270 |
if (c == EOF) |
271 |
return(NULL); |
272 |
cp = buf; |
273 |
do |
274 |
*cp++ = c; |
275 |
while ((c = getc(fp)) != EOF && !isspace(c) && cp < buf+255); |
276 |
*cp = '\0'; |
277 |
while (isspace(c) & (c != '\n')) |
278 |
c = getc(fp); |
279 |
if (c != EOF) |
280 |
ungetc(c, fp); |
281 |
return(buf); |
282 |
} |
283 |
|
284 |
char *fmtid = NULL; /* format id */ |
285 |
int comp_size = 0; /* binary bytes/channel */ |
286 |
int n_comp = 0; /* components/record */ |
287 |
int ni_columns = 0; /* number of input columns */ |
288 |
int ni_rows = 0; /* number of input rows */ |
289 |
int no_columns = 0; /* number of output columns */ |
290 |
int no_rows = 0; /* number of output rows */ |
291 |
int transpose = 0; /* transpose rows & cols? */ |
292 |
int i_header = 1; /* input header? */ |
293 |
int o_header = 1; /* output header? */ |
294 |
int outArray[MAXLEVELS][2]; /* output block nesting */ |
295 |
int outLevels = 0; /* number of blocking levels */ |
296 |
int check = 0; /* force data check? */ |
297 |
|
298 |
/* parse RxCx... string */ |
299 |
static int |
300 |
get_array(const char *spec, int blklvl[][2], int nlvls) |
301 |
{ |
302 |
int n; |
303 |
|
304 |
if (nlvls <= 0) { |
305 |
fputs("Too many block levels!\n", stderr); |
306 |
exit(1); |
307 |
} |
308 |
if (sscanf(spec, "%dx%d", &blklvl[0][0], &blklvl[0][1]) != 2) { |
309 |
fputs("Bad block specification!\n", stderr); |
310 |
exit(1); |
311 |
} |
312 |
while (isdigit(*spec)) |
313 |
spec++; |
314 |
spec++; /* 'x' */ |
315 |
while (isdigit(*spec)) |
316 |
spec++; |
317 |
if ((*spec != 'x') & (*spec != 'X')) { |
318 |
if (*spec) { |
319 |
fputs("Blocks must be separated by 'x' or 'X'\n", stderr); |
320 |
exit(1); |
321 |
} |
322 |
return(1); |
323 |
} |
324 |
spec++; |
325 |
n = get_array(spec, blklvl+1, nlvls-1); |
326 |
if (!n) |
327 |
return(0); |
328 |
blklvl[0][0] *= blklvl[1][0]; |
329 |
blklvl[0][1] *= blklvl[1][1]; |
330 |
return(n+1); |
331 |
} |
332 |
|
333 |
/* check settings and assign defaults */ |
334 |
static int |
335 |
check_sizes() |
336 |
{ |
337 |
if (fmtid == NULL) { |
338 |
fmtid = "ascii"; |
339 |
} else if (!comp_size) { |
340 |
if (!strcmp(fmtid, "float")) |
341 |
comp_size = sizeof(float); |
342 |
else if (!strcmp(fmtid, "double")) |
343 |
comp_size = sizeof(double); |
344 |
else if (!strcmp(fmtid, "byte")) |
345 |
comp_size = 1; |
346 |
else if (strcmp(fmtid, "ascii")) { |
347 |
fprintf(stderr, "Unsupported format: %s\n", fmtid); |
348 |
return(0); |
349 |
} |
350 |
} |
351 |
if (transpose && (no_rows <= 0) & (no_columns <= 0)) { |
352 |
if (ni_rows > 0) no_columns = ni_rows; |
353 |
if (ni_columns > 0) no_rows = ni_columns; |
354 |
} else if ((no_rows <= 0) & (no_columns > 0) && |
355 |
!((ni_rows*ni_columns) % no_columns)) |
356 |
no_rows = ni_rows*ni_columns/no_columns; |
357 |
if (n_comp <= 0) |
358 |
n_comp = 3; |
359 |
return(1); |
360 |
} |
361 |
|
362 |
/* call to compute block input position */ |
363 |
static long |
364 |
get_block_pos(int r, int c, int blklvl[][2], int nlvls) |
365 |
{ |
366 |
long n = 0; |
367 |
|
368 |
while (nlvls > 1) { |
369 |
int sr = r/blklvl[1][0]; |
370 |
int sc = c/blklvl[1][1]; |
371 |
r -= sr*blklvl[1][0]; |
372 |
c -= sc*blklvl[1][1]; |
373 |
n += sr*blklvl[1][0]*blklvl[0][1] + sc*blklvl[1][0]*blklvl[1][1]; |
374 |
blklvl++; |
375 |
nlvls--; |
376 |
} |
377 |
n += r*blklvl[0][1] + c; |
378 |
return(n); |
379 |
} |
380 |
|
381 |
/* return input offset based on array ordering and transpose option */ |
382 |
static long |
383 |
get_input_pos(int r, int c) |
384 |
{ |
385 |
long n; |
386 |
|
387 |
if (outLevels > 1) { /* block reordering */ |
388 |
n = get_block_pos(r, c, outArray, outLevels); |
389 |
if (transpose) { |
390 |
r = n/ni_rows; |
391 |
c = n - r*ni_rows; |
392 |
n = (long)c*ni_columns + r; |
393 |
} |
394 |
} else if (transpose) /* transpose only */ |
395 |
n = (long)c*ni_columns + r; |
396 |
else /* XXX should never happen! */ |
397 |
n = (long)r*no_columns + c; |
398 |
return(n); |
399 |
} |
400 |
|
401 |
/* output reordered ASCII or binary data from memory */ |
402 |
static int |
403 |
do_reorder(const MEMLOAD *mp) |
404 |
{ |
405 |
static const char tabEOL[2] = {'\t','\n'}; |
406 |
RECINDEX *rp = NULL; |
407 |
long nrecords; |
408 |
int i, j; |
409 |
/* propogate sizes */ |
410 |
if (ni_rows <= 0) |
411 |
ni_rows = transpose ? no_columns : no_rows; |
412 |
if (ni_columns <= 0) |
413 |
ni_columns = transpose ? no_rows : no_columns; |
414 |
/* get # records (& index) */ |
415 |
if (!comp_size) { |
416 |
if ((rp = index_records(mp, n_comp)) == NULL) |
417 |
return(0); |
418 |
if (ni_columns <= 0) |
419 |
ni_columns = count_columns(rp); |
420 |
nrecords = rp->nrecs; |
421 |
} else if ((ni_rows > 0) & (ni_columns > 0)) { |
422 |
nrecords = ni_rows*ni_columns; |
423 |
if (nrecords > mp->len/(n_comp*comp_size)) { |
424 |
fputs("Input too small for specified size and type\n", |
425 |
stderr); |
426 |
return(0); |
427 |
} |
428 |
} else |
429 |
nrecords = mp->len/(n_comp*comp_size); |
430 |
/* check sizes */ |
431 |
if ((ni_rows <= 0) & (ni_columns > 0)) |
432 |
ni_rows = nrecords/ni_columns; |
433 |
else if ((ni_columns <= 0) & (ni_rows > 0)) |
434 |
ni_columns = nrecords/ni_rows; |
435 |
if (nrecords != ni_rows*ni_columns) |
436 |
goto badspec; |
437 |
if (transpose) { |
438 |
if (no_columns <= 0) |
439 |
no_columns = ni_rows; |
440 |
if (no_rows <= 0) |
441 |
no_rows = ni_columns; |
442 |
if (outLevels <= 1 && |
443 |
(no_rows != ni_columns) | (no_columns != ni_rows)) |
444 |
goto badspec; |
445 |
} else { |
446 |
if (no_columns <= 0) |
447 |
no_columns = ni_columns; |
448 |
if (no_rows <= 0) |
449 |
no_rows = ni_rows; |
450 |
} |
451 |
if (ni_rows*ni_columns != no_rows*no_columns) { |
452 |
fputs("Number of input and output records do not match\n", |
453 |
stderr); |
454 |
return(0); |
455 |
} |
456 |
if (o_header) { /* finish header? */ |
457 |
printf("NROWS=%d\n", no_rows); |
458 |
printf("NCOLS=%d\n", no_columns); |
459 |
fputc('\n', stdout); |
460 |
} |
461 |
/* reorder records */ |
462 |
for (i = 0; i < no_rows; i++) { |
463 |
for (j = 0; j < no_columns; j++) { |
464 |
long n = get_input_pos(i, j); |
465 |
if (n >= nrecords) { |
466 |
fputs("Index past end-of-file\n", stderr); |
467 |
return(0); |
468 |
} |
469 |
if (rp != NULL) { /* ASCII output */ |
470 |
print_record(rp, n); |
471 |
putc(tabEOL[j >= no_columns-1], stdout); |
472 |
} else { /* binary output */ |
473 |
putbinary((char *)mp->base + (n_comp*comp_size)*n, |
474 |
comp_size, n_comp, stdout); |
475 |
} |
476 |
} |
477 |
if (ferror(stdout)) { |
478 |
fprintf(stderr, "Error writing to stdout\n"); |
479 |
return(0); |
480 |
} |
481 |
} |
482 |
if (rp != NULL) |
483 |
free_records(rp); |
484 |
return(1); |
485 |
badspec: |
486 |
fprintf(stderr, "Bad dimension(s)\n"); |
487 |
return(0); |
488 |
} |
489 |
|
490 |
/* resize ASCII stream input by ignoring EOLs between records */ |
491 |
static int |
492 |
do_resize(FILE *fp) |
493 |
{ |
494 |
long records2go = ni_rows*ni_columns; |
495 |
int columns2go = no_columns; |
496 |
char word[256]; |
497 |
|
498 |
if (o_header) { /* finish header? */ |
499 |
if (no_rows > 0) |
500 |
printf("NROWS=%d\n", no_rows); |
501 |
if (no_columns > 0) |
502 |
printf("NCOLS=%d\n", no_columns); |
503 |
fputc('\n', stdout); |
504 |
} |
505 |
/* sanity checks */ |
506 |
if (comp_size || !check & |
507 |
(no_columns == ni_columns) & (no_rows == ni_rows)) |
508 |
return(output_stream(fp)); /* no-op -- just copy */ |
509 |
if (no_columns <= 0) { |
510 |
fprintf(stderr, "Missing -oc specification\n"); |
511 |
return(0); |
512 |
} |
513 |
if ((records2go <= 0) & (no_rows > 0)) |
514 |
records2go = no_rows*no_columns; |
515 |
else if (no_rows*no_columns != records2go) { |
516 |
fprintf(stderr, |
517 |
"Number of input and output records disagree (%dx%d != %dx%d)\n", |
518 |
ni_rows, ni_columns, no_rows, no_columns); |
519 |
return(0); |
520 |
} |
521 |
do { /* reshape records */ |
522 |
int n; |
523 |
|
524 |
for (n = n_comp; n--; ) { |
525 |
if (fget_word(word, fp) == NULL) { |
526 |
if (records2go > 0 || n < n_comp-1) |
527 |
break; |
528 |
goto done; /* normal EOD */ |
529 |
} |
530 |
fputs(word, stdout); |
531 |
if (n) { /* mid-record? */ |
532 |
int c = getc(fp); |
533 |
if ((c == '\n') | (c == EOF)) |
534 |
break; |
535 |
ungetc(c, fp); |
536 |
putc(' ', stdout); |
537 |
} |
538 |
} |
539 |
if (n >= 0) { |
540 |
fprintf(stderr, "Incomplete record / unexpected EOF\n"); |
541 |
return(0); |
542 |
} |
543 |
if (--columns2go <= 0) { /* time to end output row? */ |
544 |
putc('\n', stdout); |
545 |
columns2go = no_columns; |
546 |
} else /* else separate records */ |
547 |
putc('\t', stdout); |
548 |
} while (--records2go); /* expected EOD? */ |
549 |
done: |
550 |
if (warnings && columns2go != no_columns) |
551 |
fprintf(stderr, "Warning -- incomplete final row\n"); |
552 |
if (warnings && fget_word(word, fp) != NULL) |
553 |
fprintf(stderr, "Warning -- characters beyond expected EOD\n"); |
554 |
return(1); |
555 |
} |
556 |
|
557 |
/* process a header line and copy to stdout */ |
558 |
static int |
559 |
headline(char *s, void *p) |
560 |
{ |
561 |
static char fmt[MAXFMTLEN]; |
562 |
int n; |
563 |
|
564 |
if (formatval(fmt, s)) { |
565 |
if (fmtid == NULL) { |
566 |
fmtid = fmt; |
567 |
return(0); |
568 |
} |
569 |
if (!strcmp(fmt, fmtid)) |
570 |
return(0); |
571 |
fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid); |
572 |
return(-1); |
573 |
} |
574 |
if (!strncmp(s, "NROWS=", 6)) { |
575 |
n = atoi(s+6); |
576 |
if ((ni_rows > 0) & (n != ni_rows)) { |
577 |
fputs("Incorrect input row count\n", stderr); |
578 |
return(-1); |
579 |
} |
580 |
ni_rows = n; |
581 |
return(0); |
582 |
} |
583 |
if (!strncmp(s, "NCOLS=", 6)) { |
584 |
n = atoi(s+6); |
585 |
if ((ni_columns > 0) & (n != ni_columns)) { |
586 |
fputs("Incorrect input column count\n", stderr); |
587 |
return(-1); |
588 |
} |
589 |
ni_columns = n; |
590 |
return(0); |
591 |
} |
592 |
if (!strncmp(s, "NCOMP=", 6)) { |
593 |
n = atoi(s+6); |
594 |
if ((n_comp > 0) & (n != n_comp)) { |
595 |
fputs("Incorrect number of components\n", stderr); |
596 |
return(-1); |
597 |
} |
598 |
n_comp = n; |
599 |
return(0); |
600 |
} |
601 |
if (o_header) |
602 |
fputs(s, stdout); /* copy header info. */ |
603 |
return(0); |
604 |
} |
605 |
|
606 |
/* main routine for converting rows/columns in data file */ |
607 |
int |
608 |
main(int argc, char *argv[]) |
609 |
{ |
610 |
int a; |
611 |
|
612 |
for (a = 1; a < argc && argv[a][0] == '-'; a++) |
613 |
switch (argv[a][1]) { |
614 |
case 'i': /* input */ |
615 |
if (argv[a][2] == 'c') /* columns */ |
616 |
ni_columns = atoi(argv[++a]); |
617 |
else if (argv[a][2] == 'r') |
618 |
ni_rows = atoi(argv[++a]); |
619 |
else |
620 |
goto userr; |
621 |
break; |
622 |
case 'o': /* output */ |
623 |
if (argv[a][2] == 'c') /* columns */ |
624 |
no_columns = atoi(argv[++a]); |
625 |
else if (argv[a][2] == 'r') |
626 |
no_rows = atoi(argv[++a]); |
627 |
else if (argv[a][2] || |
628 |
!(outLevels=get_array(argv[++a], outArray, MAXLEVELS))) |
629 |
goto userr; |
630 |
break; |
631 |
case 'h': /* turn off header */ |
632 |
switch (argv[a][2]) { |
633 |
case 'i': |
634 |
i_header = 0; |
635 |
break; |
636 |
case 'o': |
637 |
o_header = 0; |
638 |
break; |
639 |
case '\0': |
640 |
i_header = o_header = 0; |
641 |
break; |
642 |
default: |
643 |
goto userr; |
644 |
} |
645 |
break; |
646 |
case 't': /* transpose on/off */ |
647 |
transpose = !transpose; |
648 |
break; |
649 |
case 'f': /* format */ |
650 |
switch (argv[a][2]) { |
651 |
case 'a': /* ASCII */ |
652 |
case 'A': |
653 |
fmtid = "ascii"; |
654 |
comp_size = 0; |
655 |
break; |
656 |
case 'f': /* float */ |
657 |
case 'F': |
658 |
fmtid = "float"; |
659 |
comp_size = sizeof(float); |
660 |
break; |
661 |
case 'd': /* double */ |
662 |
case 'D': |
663 |
fmtid = "double"; |
664 |
comp_size = sizeof(double); |
665 |
break; |
666 |
case 'b': /* binary (bytes) */ |
667 |
case 'B': |
668 |
fmtid = "byte"; |
669 |
comp_size = 1; |
670 |
break; |
671 |
default: |
672 |
goto userr; |
673 |
} |
674 |
if (argv[a][3]) { |
675 |
if (!isdigit(argv[a][3])) |
676 |
goto userr; |
677 |
n_comp = atoi(argv[a]+3); |
678 |
} else |
679 |
n_comp = 1; |
680 |
break; |
681 |
case 'w': /* warnings on/off */ |
682 |
warnings = !warnings; |
683 |
break; |
684 |
case 'c': /* force check operation */ |
685 |
check = 1; |
686 |
break; |
687 |
default: |
688 |
goto userr; |
689 |
} |
690 |
if (a < argc-1) /* arg count OK? */ |
691 |
goto userr; |
692 |
if (outLevels) { /* should check consistency? */ |
693 |
no_rows = outArray[0][0]; |
694 |
no_columns = outArray[0][1]; |
695 |
} |
696 |
/* open input file? */ |
697 |
if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) { |
698 |
fprintf(stderr, "%s: cannot open for reading\n", argv[a]); |
699 |
return(1); |
700 |
} |
701 |
if (comp_size) { |
702 |
SET_FILE_BINARY(stdin); |
703 |
SET_FILE_BINARY(stdout); |
704 |
} |
705 |
/* check for no-op */ |
706 |
if (!transpose & !check & (outLevels <= 1) & (i_header == o_header) && |
707 |
(no_columns == ni_columns) & (no_rows == ni_rows)) { |
708 |
if (warnings) |
709 |
fprintf(stderr, "%s: no-op -- copying input verbatim\n", |
710 |
argv[0]); |
711 |
if (!output_stream(stdin)) |
712 |
return(1); |
713 |
return(0); |
714 |
} |
715 |
if (i_header) { /* read header */ |
716 |
if (getheader(stdin, headline, NULL) < 0) |
717 |
return(1); |
718 |
if (!check_sizes()) |
719 |
return(1); |
720 |
if (comp_size) { /* a little late... */ |
721 |
SET_FILE_BINARY(stdin); |
722 |
SET_FILE_BINARY(stdout); |
723 |
} |
724 |
} else if (!check_sizes()) |
725 |
return(1); |
726 |
if (o_header) { /* write/add to header */ |
727 |
if (!i_header) |
728 |
newheader("RADIANCE", stdout); |
729 |
printargs(a, argv, stdout); |
730 |
printf("NCOMP=%d\n", n_comp); |
731 |
fputformat(fmtid, stdout); |
732 |
} |
733 |
if (transpose | check | (outLevels > 1) || (o_header && no_rows <= 0)) { |
734 |
MEMLOAD myMem; /* need to map into memory */ |
735 |
if (a == argc-1) { |
736 |
if (load_file(&myMem, stdin) <= 0) { |
737 |
fprintf(stderr, "%s: error loading file into memory\n", |
738 |
argv[a]); |
739 |
return(1); |
740 |
} |
741 |
} else if (load_stream(&myMem, stdin) <= 0) { |
742 |
fprintf(stderr, "%s: error loading stdin into memory\n", |
743 |
argv[0]); |
744 |
return(1); |
745 |
} |
746 |
if (!do_reorder(&myMem)) |
747 |
return(1); |
748 |
/* free_load(&myMem); about to exit, so don't bother */ |
749 |
} else if (!do_resize(stdin)) /* just reshaping input */ |
750 |
return(1); |
751 |
return(0); |
752 |
userr: |
753 |
fprintf(stderr, |
754 |
"Usage: %s [-h[io]][-w][-c][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n", |
755 |
argv[0]); |
756 |
return(1); |
757 |
} |