ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rcollate.c
Revision: 2.22
Committed: Fri Mar 4 00:21:21 2016 UTC (8 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.21: +1 -8 lines
Log Message:
Eliminated redundant #define's for getc and putc

File Contents

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