ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rcollate.c
Revision: 2.7
Committed: Mon Nov 18 22:02:12 2013 UTC (10 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +7 -2 lines
Log Message:
More Windows fixes (someone should come up with an acrylic version of Windows)

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.7 static const char RCSid[] = "$Id: rcollate.c,v 2.6 2013/11/18 18:07:16 greg Exp $";
3 greg 2.1 #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 greg 2.7 #ifdef _WIN32
15     #undef ftello
16     #define ftello ftell
17     #undef ssize_t
18     #define ssize_t size_t
19     #else
20 greg 2.1 #include <sys/mman.h>
21     #endif
22    
23     #ifdef getc_unlocked /* avoid horrendous overhead of flockfile */
24     #undef getc
25     #undef putc
26     #define getc getc_unlocked
27     #define putc putc_unlocked
28     #endif
29    
30     typedef struct {
31     void *base; /* pointer to base memory */
32     size_t len; /* allocated memory length */
33     int mapped; /* memory-mapped file? */
34     } MEMLOAD; /* file loaded/mapped into memory */
35    
36     typedef struct {
37     int nw_rec; /* number of words per record */
38     int nrecs; /* number of records we found */
39     char *rec[1]; /* record array (extends struct) */
40     } RECINDEX;
41    
42 greg 2.4 int warnings = 1; /* report warnings? */
43    
44 greg 2.1 /* free loaded file */
45     static void
46     free_load(MEMLOAD *mp)
47     {
48     if (mp == NULL || (mp->base == NULL) | (mp->len <= 0))
49     return;
50     #ifdef MAP_FILE
51     if (mp->mapped)
52     munmap(mp->base, mp->len);
53     else
54     #endif
55     free(mp->base);
56     mp->base = NULL;
57     mp->len = 0;
58     }
59    
60     /* load a file into memory */
61     static int
62     load_file(MEMLOAD *mp, FILE *fp)
63     {
64     int fd;
65     off_t skip, flen;
66    
67     if (mp == NULL)
68     return(-1);
69     mp->base = NULL;
70     mp->len = 0;
71     mp->mapped = 0;
72     if (fp == NULL)
73     return(-1);
74     fd = fileno(fp);
75     skip = ftello(fp);
76     flen = lseek(fd, 0, SEEK_END);
77     if (flen <= skip)
78     return((int)(flen - skip));
79     mp->len = (size_t)(flen - skip);
80     #ifdef MAP_FILE
81     if (mp->len > 1L<<20) { /* map file if > 1 MByte */
82 greg 2.2 mp->base = mmap(NULL, mp->len, PROT_READ, MAP_PRIVATE, fd, skip);
83 greg 2.1 if (mp->base != MAP_FAILED) {
84     mp->mapped = 1;
85     return(1); /* mmap() success */
86     }
87     mp->base = NULL; /* fall back to reading it in... */
88     }
89     #endif
90     if (lseek(fd, skip, SEEK_SET) != skip ||
91     (mp->base = malloc(mp->len)) == NULL) {
92     mp->len = 0;
93     return(-1);
94     }
95     if (read(fd, (char *)mp->base, mp->len) != mp->len) {
96     free_load(mp);
97     return(-1);
98     }
99     return(1);
100     }
101    
102     /* load memory from an input stream, starting from current position */
103     static int
104     load_stream(MEMLOAD *mp, FILE *fp)
105     {
106     char buf[8192];
107     size_t nr;
108    
109     if (mp == NULL)
110     return(-1);
111     mp->base = NULL;
112     mp->len = 0;
113     mp->mapped = 0;
114     if (fp == NULL)
115     return(-1);
116     while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) {
117     if (!mp->len)
118     mp->base = malloc(nr);
119     else
120     mp->base = realloc(mp->base, mp->len+nr);
121     if (mp->base == NULL)
122     return(-1);
123     memcpy((char *)mp->base + mp->len, buf, nr);
124     mp->len += nr;
125     }
126     if (ferror(fp)) {
127     free_load(mp);
128     return(-1);
129     }
130     return(mp->len > 0);
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); /* assumes nothing in input buffer */
237     while ((n = read(fileno(fp), buf, sizeof(buf))) > 0)
238     if (write(fileno(stdout), buf, n) != n)
239     return(0);
240     return(n >= 0);
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 = "ascii"; /* format id */
267     int record_width = 3; /* words/record (<0 binary) */
268     int ni_columns = 0; /* number of input columns */
269     int ni_rows = 0; /* number of input rows */
270     int no_columns = 0; /* number of output columns */
271     int no_rows = 0; /* number of output rows */
272    
273     /* output transposed ASCII or binary data from memory */
274     static int
275     do_transpose(const MEMLOAD *mp)
276     {
277     static const char tabEOL[2] = {'\t','\n'};
278     RECINDEX *rp = NULL;
279     long nrecords;
280     int i, j;
281 greg 2.5 /* propogate sizes */
282     if (ni_rows <= 0)
283     ni_rows = no_columns;
284     if (ni_columns <= 0)
285     ni_columns = no_rows;
286 greg 2.1 /* get # records (& index) */
287     if (record_width > 0) {
288     if ((rp = index_records(mp, record_width)) == NULL)
289     return(0);
290     if (ni_columns <= 0)
291     ni_columns = count_columns(rp);
292     nrecords = rp->nrecs;
293 greg 2.3 } else if ((ni_rows > 0) & (ni_columns > 0)) {
294 greg 2.1 nrecords = ni_rows*ni_columns;
295 greg 2.3 if (nrecords > mp->len / -record_width) {
296     fprintf(stderr,
297     "Input too small for specified size and type\n");
298     return(0);
299     }
300     } else
301 greg 2.1 nrecords = mp->len / -record_width;
302     /* check sizes */
303     if ((ni_rows <= 0) & (ni_columns > 0))
304     ni_rows = nrecords/ni_columns;
305     if ((ni_columns <= 0) & (ni_rows > 0))
306     ni_columns = nrecords/ni_rows;
307     if (nrecords != ni_rows*ni_columns)
308     goto badspec;
309     if (no_columns <= 0)
310     no_columns = ni_rows;
311     if (no_rows <= 0)
312     no_rows = ni_columns;
313     if ((no_rows != ni_columns) | (no_columns != ni_rows))
314     goto badspec;
315     /* transpose records */
316     for (i = 0; i < no_rows; i++) {
317     for (j = 0; j < no_columns; j++)
318     if (rp != NULL) { /* ASCII output */
319     print_record(rp, j*ni_columns + i);
320     putc(tabEOL[j >= no_columns-1], stdout);
321     } else { /* binary output */
322     fwrite((char *)mp->base +
323     -record_width*(j*ni_columns + i),
324     -record_width, 1, stdout);
325     }
326     if (ferror(stdout)) {
327     fprintf(stderr, "Error writing to stdout\n");
328     return(0);
329     }
330     }
331     if (rp != NULL)
332     free_records(rp);
333     return(1);
334     badspec:
335     fprintf(stderr, "Bad transpose specification -- check dimension(s)\n");
336     return(0);
337     }
338    
339     /* resize ASCII stream input by ignoring EOLs between records */
340     static int
341     do_resize(FILE *fp)
342     {
343     long records2go = ni_rows*ni_columns;
344     int columns2go = no_columns;
345     char word[256];
346     /* sanity checks */
347     if (record_width <= 0) {
348     fprintf(stderr, "Bad call to do_resize (record_width = %d)\n",
349     record_width);
350     return(0);
351     }
352     if (no_columns <= 0) {
353     fprintf(stderr, "Missing -oc specification\n");
354     return(0);
355     }
356     if ((records2go <= 0) & (no_rows > 0))
357     records2go = no_rows*no_columns;
358     else if (no_rows*no_columns != records2go) {
359     fprintf(stderr,
360     "Input and output data sizes disagree (%dx%d != %dx%d)\n",
361     ni_rows, ni_columns, no_rows, no_columns);
362     return(0);
363     }
364     do { /* reshape records */
365     int n;
366    
367     for (n = record_width; n--; ) {
368     if (fget_word(word, fp) == NULL) {
369     if (records2go > 0 || n < record_width-1)
370     break;
371     goto done; /* normal EOD */
372     }
373     fputs(word, stdout);
374     if (n) { /* mid-record? */
375     int c = getc(fp);
376     if ((c == '\n') | (c == EOF))
377     break;
378     ungetc(c, fp);
379     putc(' ', stdout);
380     }
381     }
382     if (n >= 0) {
383     fprintf(stderr, "Incomplete record / unexpected EOF\n");
384     return(0);
385     }
386     if (--columns2go <= 0) { /* time to end output row? */
387     putc('\n', stdout);
388     columns2go = no_columns;
389     } else /* else separate records */
390     putc('\t', stdout);
391     } while (--records2go); /* expected EOD? */
392     done:
393 greg 2.4 if (warnings && columns2go != no_columns)
394 greg 2.1 fprintf(stderr, "Warning -- incomplete final row\n");
395 greg 2.4 if (warnings && fget_word(word, fp) != NULL)
396     fprintf(stderr, "Warning -- characters beyond expected EOD\n");
397 greg 2.1 return(1);
398     }
399    
400     /* process a header line and copy to stdout */
401     static int
402     headline(char *s, void *p)
403     {
404     char fmt[32];
405    
406     if (formatval(fmt, s)) {
407     if (!strcmp(fmt, fmtid))
408     return(0);
409     fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid);
410     return(-1);
411     }
412     fputs(s, stdout); /* copy header info. */
413     return(0);
414     }
415    
416     /* main routine for converting rows/columns in data file */
417     int
418     main(int argc, char *argv[])
419     {
420     int do_header = 1; /* header i/o? */
421     int transpose = 0; /* transpose rows & cols? */
422     int i;
423    
424     for (i = 1; i < argc && argv[i][0] == '-'; i++)
425     switch (argv[i][1]) {
426     case 'i': /* input */
427     if (argv[i][2] == 'c') /* columns */
428     ni_columns = atoi(argv[++i]);
429     else if (argv[i][2] == 'r')
430     ni_rows = atoi(argv[++i]);
431     else
432     goto userr;
433     break;
434     case 'o': /* output */
435     if (argv[i][2] == 'c') /* columns */
436     no_columns = atoi(argv[++i]);
437     else if (argv[i][2] == 'r')
438     no_rows = atoi(argv[++i]);
439     else
440     goto userr;
441     break;
442     case 'h': /* header on/off */
443     do_header = !do_header;
444     break;
445     case 't': /* transpose on/off */
446     transpose = !transpose;
447     break;
448     case 'f': /* format */
449     switch (argv[i][2]) {
450     case 'a': /* ASCII */
451     case 'A':
452     fmtid = "ascii";
453     record_width = 1;
454     break;
455     case 'f': /* float */
456     case 'F':
457     fmtid = "float";
458     record_width = -(int)sizeof(float);
459     break;
460     case 'd': /* double */
461     case 'D':
462     fmtid = "double";
463     record_width = -(int)sizeof(double);
464     break;
465     case 'b': /* binary (bytes) */
466     case 'B':
467     fmtid = "byte";
468     record_width = -1;
469     break;
470     default:
471     goto userr;
472     }
473     if (argv[i][3]) {
474     if (!isdigit(argv[i][3]))
475     goto userr;
476     record_width *= atoi(argv[i]+3);
477     }
478     break;
479 greg 2.4 case 'w': /* warnings on/off */
480     warnings = !warnings;
481     break;
482 greg 2.1 default:
483     goto userr;
484     }
485     if (!record_width)
486     goto userr;
487     if (i < argc-1) /* arg count OK? */
488     goto userr;
489     /* open input file? */
490     if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
491     fprintf(stderr, "%s: cannot open for reading\n", argv[i]);
492     return(1);
493     }
494     if (record_width < 0) {
495     SET_FILE_BINARY(stdin);
496     SET_FILE_BINARY(stdout);
497     }
498     /* check for no-op */
499     if (!transpose && (record_width < 0 ||
500     (no_columns == ni_columns) & (no_rows == ni_rows))) {
501 greg 2.4 if (warnings)
502     fprintf(stderr, "%s: no-op -- copying input verbatim\n",
503 greg 2.1 argv[0]);
504     if (!output_stream(stdin))
505     return(1);
506     return(0);
507     }
508     if (do_header) { /* read/write header */
509     if (getheader(stdin, &headline, NULL) < 0)
510     return(1);
511     printargs(argc, argv, stdout);
512     fputformat(fmtid, stdout);
513     fputc('\n', stdout); /* finish new header */
514     }
515     if (transpose) { /* transposing rows & columns? */
516     MEMLOAD myMem; /* need to load into memory */
517     if (i == argc-1) {
518     if (load_file(&myMem, stdin) <= 0) {
519     fprintf(stderr, "%s: error loading file into memory\n",
520     argv[i]);
521     return(1);
522     }
523     } else if (load_stream(&myMem, stdin) <= 0) {
524     fprintf(stderr, "%s: error loading stdin into memory\n",
525     argv[0]);
526     return(1);
527     }
528     if (!do_transpose(&myMem))
529     return(1);
530     /* free_load(&myMem); */
531     } else if (!do_resize(stdin)) /* just reshaping input */
532     return(1);
533     return(0);
534     userr:
535     fprintf(stderr,
536 greg 2.4 "Usage: %s [-h][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
537 greg 2.1 argv[0]);
538     return(1);
539     }