ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rcollate.c
Revision: 2.6
Committed: Mon Nov 18 18:07:16 2013 UTC (10 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +1 -2 lines
Log Message:
Fixes for Windows

File Contents

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