ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rcollate.c
(Generate patch)

Comparing ray/src/util/rcollate.c (file contents):
Revision 2.6 by greg, Mon Nov 18 18:07:16 2013 UTC vs.
Revision 2.24 by greg, Sat Apr 16 00:42:16 2016 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   #include "platform.h"
12   #include "rtio.h"
13   #include "resolu.h"
14 < #ifndef _WIN32
15 < #include <sys/mman.h>
14 > #if defined(_WIN32) || defined(_WIN64)
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  
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
23   typedef struct {
24          void    *base;          /* pointer to base memory */
25          size_t  len;            /* allocated memory length */
# Line 52 | Line 50 | free_load(MEMLOAD *mp)
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)
# Line 59 | Line 92 | load_file(MEMLOAD *mp, FILE *fp)
92          int     fd;
93          off_t   skip, flen;
94  
95 + #if defined(_WIN32) || defined(_WIN64)
96 +                                /* too difficult to fix this */
97 +        return load_stream(mp, fp);
98 + #endif
99          if (mp == NULL)
100                  return(-1);
101          mp->base = NULL;
# Line 94 | Line 131 | load_file(MEMLOAD *mp, FILE *fp)
131          return(1);
132   }
133  
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
134   /* free a record index */
135   #define free_records(rp)        free(rp)
136  
# Line 132 | Line 138 | load_stream(MEMLOAD *mp, FILE *fp)
138   static RECINDEX *
139   index_records(const MEMLOAD *mp, int nw_rec)
140   {
141 +        int             nall = 0;
142          RECINDEX        *rp;
143          char            *cp, *mend;
144          int             n;
# Line 140 | Line 147 | index_records(const MEMLOAD *mp, int nw_rec)
147                  return(NULL);
148          if (nw_rec <= 0)
149                  return(NULL);
150 <        rp = (RECINDEX *)malloc(sizeof(RECINDEX) + mp->len/(2*nw_rec)*sizeof(char *));
150 >        nall = 1000;
151 >        rp = (RECINDEX *)malloc(sizeof(RECINDEX) + nall*sizeof(char *));
152          if (rp == NULL)
153                  return(NULL);
154          rp->nw_rec = nw_rec;
# Line 152 | Line 160 | index_records(const MEMLOAD *mp, int nw_rec)
160                          ++cp;
161                  if (cp >= mend)
162                          break;
163 +                if (rp->nrecs >= nall) {
164 +                        nall += nall>>1;        /* get more record space */
165 +                        rp = (RECINDEX *)realloc(rp,
166 +                                        sizeof(RECINDEX) + nall*sizeof(char *));
167 +                        if (rp == NULL)
168 +                                return(NULL);
169 +                }
170                  rp->rec[rp->nrecs++] = cp;      /* point to first non-white */
171                  n = rp->nw_rec;
172                  while (++cp < mend)             /* find end of record */
# Line 228 | Line 243 | output_stream(FILE *fp)
243  
244          if (fp == NULL)
245                  return(0);
246 <        fflush(stdout);                 /* assumes nothing in input buffer */
247 <        while ((n = read(fileno(fp), buf, sizeof(buf))) > 0)
246 >        fflush(stdout);
247 >        while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
248                  if (write(fileno(stdout), buf, n) != n)
249                          return(0);
250 <        return(n >= 0);
250 >        return(!ferror(fp));
251   }
252  
253   /* get next word from stream, leaving stream on EOL or start of next word */
# Line 258 | Line 273 | fget_word(char buf[256], FILE *fp)
273          return(buf);
274   }
275  
276 < char            *fmtid = "ascii";               /* format id */
277 < int             record_width = 3;               /* words/record (<0 binary) */
276 > char            *fmtid = NULL;                  /* format id */
277 > int             comp_size = 0;                  /* binary bytes/channel */
278 > int             n_comp = 0;                     /* components/record */
279   int             ni_columns = 0;                 /* number of input columns */
280   int             ni_rows = 0;                    /* number of input rows */
281   int             no_columns = 0;                 /* number of output columns */
282   int             no_rows = 0;                    /* number of output rows */
283 + int             transpose = 0;                  /* transpose rows & cols? */
284 + int             i_header = 1;                   /* input header? */
285 + int             o_header = 1;                   /* output header? */
286  
287 + /* check settings and assign defaults */
288 + static int
289 + check_sizes()
290 + {
291 +        if (fmtid == NULL) {
292 +                fmtid = "ascii";
293 +        } else if (!comp_size) {
294 +                if (!strcmp(fmtid, "float"))
295 +                        comp_size = sizeof(float);
296 +                else if (!strcmp(fmtid, "double"))
297 +                        comp_size = sizeof(double);
298 +                else if (!strcmp(fmtid, "byte"))
299 +                        comp_size = 1;
300 +                else if (strcmp(fmtid, "ascii")) {
301 +                        fprintf(stderr, "Unsupported format: %s\n", fmtid);
302 +                        return(0);
303 +                }
304 +        }
305 +        if (transpose && (no_rows <= 0) & (no_columns <= 0)) {
306 +                if (ni_rows > 0) no_columns = ni_rows;
307 +                if (ni_columns > 0) no_rows = ni_columns;
308 +        } else if ((no_rows <= 0) & (no_columns > 0) &&
309 +                        !((ni_rows*ni_columns) % no_columns))
310 +                no_rows = ni_rows*ni_columns/no_columns;
311 +        if (n_comp <= 0)
312 +                n_comp = 3;
313 +        return(1);
314 + }
315 +
316   /* output transposed ASCII or binary data from memory */
317   static int
318   do_transpose(const MEMLOAD *mp)
# Line 279 | Line 327 | do_transpose(const MEMLOAD *mp)
327          if (ni_columns <= 0)
328                  ni_columns = no_rows;
329                                                  /* get # records (& index) */
330 <        if (record_width > 0) {
331 <                if ((rp = index_records(mp, record_width)) == NULL)
330 >        if (!comp_size) {
331 >                if ((rp = index_records(mp, n_comp)) == NULL)
332                          return(0);
333                  if (ni_columns <= 0)
334                          ni_columns = count_columns(rp);
335                  nrecords = rp->nrecs;
336          } else if ((ni_rows > 0) & (ni_columns > 0)) {
337                  nrecords = ni_rows*ni_columns;
338 <                if (nrecords > mp->len / -record_width) {
338 >                if (nrecords > mp->len/(n_comp*comp_size)) {
339                          fprintf(stderr,
340                              "Input too small for specified size and type\n");
341                          return(0);
342                  }
343          } else
344 <                nrecords = mp->len / -record_width;
344 >                nrecords = mp->len/(n_comp*comp_size);
345                                                  /* check sizes */
346          if ((ni_rows <= 0) & (ni_columns > 0))
347                  ni_rows = nrecords/ni_columns;
# Line 315 | Line 363 | do_transpose(const MEMLOAD *mp)
363                          putc(tabEOL[j >= no_columns-1], stdout);
364                  } else {                        /* binary output */
365                          fwrite((char *)mp->base +
366 <                                        -record_width*(j*ni_columns + i),
367 <                                        -record_width, 1, stdout);
366 >                                        (n_comp*comp_size)*(j*ni_columns + i),
367 >                                        n_comp*comp_size, 1, stdout);
368                  }
369              if (ferror(stdout)) {
370                  fprintf(stderr, "Error writing to stdout\n");
# Line 339 | Line 387 | do_resize(FILE *fp)
387          int     columns2go = no_columns;
388          char    word[256];
389                                                  /* sanity checks */
390 <        if (record_width <= 0) {
391 <                fprintf(stderr, "Bad call to do_resize (record_width = %d)\n",
344 <                                record_width);
345 <                return(0);
346 <        }
390 >        if (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows))
391 >                return(output_stream(fp));      /* no-op -- just copy */
392          if (no_columns <= 0) {
393                  fprintf(stderr, "Missing -oc specification\n");
394                  return(0);
# Line 359 | Line 404 | do_resize(FILE *fp)
404          do {                                    /* reshape records */
405                  int     n;
406  
407 <                for (n = record_width; n--; ) {
407 >                for (n = n_comp; n--; ) {
408                          if (fget_word(word, fp) == NULL) {
409 <                                if (records2go > 0 || n < record_width-1)
409 >                                if (records2go > 0 || n < n_comp-1)
410                                          break;
411                                  goto done;      /* normal EOD */
412                          }
# Line 396 | Line 441 | done:
441   static int
442   headline(char *s, void *p)
443   {
444 <        char    fmt[32];
444 >        static char     fmt[32];
445 >        int             n;
446  
447          if (formatval(fmt, s)) {
448 +                if (fmtid == NULL) {
449 +                        fmtid = fmt;
450 +                        return(0);
451 +                }
452                  if (!strcmp(fmt, fmtid))
453                          return(0);
454                  fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid);
455                  return(-1);
456          }
457 <        fputs(s, stdout);                       /* copy header info. */
457 >        if (!strncmp(s, "NROWS=", 6)) {
458 >                n = atoi(s+6);
459 >                if ((ni_rows > 0) & (n != ni_rows)) {
460 >                        fputs("Incorrect input row count\n", stderr);
461 >                        return(-1);
462 >                }
463 >                ni_rows = n;
464 >                return(0);
465 >        }
466 >        if (!strncmp(s, "NCOLS=", 6)) {
467 >                n = atoi(s+6);
468 >                if ((ni_columns > 0) & (n != ni_columns)) {
469 >                        fputs("Incorrect input column count\n", stderr);
470 >                        return(-1);
471 >                }
472 >                ni_columns = n;
473 >                return(0);
474 >        }
475 >        if (!strncmp(s, "NCOMP=", 6)) {
476 >                n = atoi(s+6);
477 >                if ((n_comp > 0) & (n != n_comp)) {
478 >                        fputs("Incorrect number of components\n", stderr);
479 >                        return(-1);
480 >                }
481 >                n_comp = n;
482 >                return(0);
483 >        }
484 >        if (o_header)
485 >                fputs(s, stdout);               /* copy header info. */
486          return(0);
487   }
488  
# Line 412 | Line 490 | headline(char *s, void *p)
490   int
491   main(int argc, char *argv[])
492   {
493 <        int     do_header = 1;                  /* header i/o? */
416 <        int     transpose = 0;                  /* transpose rows & cols? */
417 <        int     i;
493 >        int     a;
494  
495 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
496 <                switch (argv[i][1]) {
495 >        for (a = 1; a < argc && argv[a][0] == '-'; a++)
496 >                switch (argv[a][1]) {
497                  case 'i':                       /* input */
498 <                        if (argv[i][2] == 'c')  /* columns */
499 <                                ni_columns = atoi(argv[++i]);
500 <                        else if (argv[i][2] == 'r')
501 <                                ni_rows = atoi(argv[++i]);
498 >                        if (argv[a][2] == 'c')  /* columns */
499 >                                ni_columns = atoi(argv[++a]);
500 >                        else if (argv[a][2] == 'r')
501 >                                ni_rows = atoi(argv[++a]);
502                          else
503                                  goto userr;
504                          break;
505                  case 'o':                       /* output */
506 <                        if (argv[i][2] == 'c')  /* columns */
507 <                                no_columns = atoi(argv[++i]);
508 <                        else if (argv[i][2] == 'r')
509 <                                no_rows = atoi(argv[++i]);
506 >                        if (argv[a][2] == 'c')  /* columns */
507 >                                no_columns = atoi(argv[++a]);
508 >                        else if (argv[a][2] == 'r')
509 >                                no_rows = atoi(argv[++a]);
510                          else
511                                  goto userr;
512                          break;
513 <                case 'h':                       /* header on/off */
514 <                        do_header = !do_header;
513 >                case 'h':                       /* turn off header */
514 >                        switch (argv[a][2]) {
515 >                        case 'i':
516 >                                i_header = 0;
517 >                                break;
518 >                        case 'o':
519 >                                o_header = 0;
520 >                                break;
521 >                        case '\0':
522 >                                i_header = o_header = 0;
523 >                                break;
524 >                        default:
525 >                                goto userr;
526 >                        }
527                          break;
528                  case 't':                       /* transpose on/off */
529                          transpose = !transpose;
530                          break;
531                  case 'f':                       /* format */
532 <                        switch (argv[i][2]) {
532 >                        switch (argv[a][2]) {
533                          case 'a':               /* ASCII */
534                          case 'A':
535                                  fmtid = "ascii";
536 <                                record_width = 1;
536 >                                comp_size = 0;
537                                  break;
538                          case 'f':               /* float */
539                          case 'F':
540                                  fmtid = "float";
541 <                                record_width = -(int)sizeof(float);
541 >                                comp_size = sizeof(float);
542                                  break;
543                          case 'd':               /* double */
544                          case 'D':
545                                  fmtid = "double";
546 <                                record_width = -(int)sizeof(double);
546 >                                comp_size = sizeof(double);
547                                  break;
548                          case 'b':               /* binary (bytes) */
549                          case 'B':
550                                  fmtid = "byte";
551 <                                record_width = -1;
551 >                                comp_size = 1;
552                                  break;
553                          default:
554                                  goto userr;
555                          }
556 <                        if (argv[i][3]) {
557 <                                if (!isdigit(argv[i][3]))
556 >                        if (argv[a][3]) {
557 >                                if (!isdigit(argv[a][3]))
558                                          goto userr;
559 <                                record_width *= atoi(argv[i]+3);
560 <                        }
559 >                                n_comp = atoi(argv[a]+3);
560 >                        } else
561 >                                n_comp = 1;
562                          break;
563                  case 'w':                       /* warnings on/off */
564                          warnings = !warnings;
# Line 477 | Line 566 | main(int argc, char *argv[])
566                  default:
567                          goto userr;
568                  }
569 <        if (!record_width)
569 >        if (a < argc-1)                         /* arg count OK? */
570                  goto userr;
482        if (i < argc-1)                         /* arg count OK? */
483                goto userr;
571                                                  /* open input file? */
572 <        if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
573 <                fprintf(stderr, "%s: cannot open for reading\n", argv[i]);
572 >        if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) {
573 >                fprintf(stderr, "%s: cannot open for reading\n", argv[a]);
574                  return(1);
575          }
576 <        if (record_width < 0) {
576 >        if (comp_size) {
577                  SET_FILE_BINARY(stdin);
578                  SET_FILE_BINARY(stdout);
579          }
580                                                  /* check for no-op */
581 <        if (!transpose && (record_width < 0 ||
582 <                        (no_columns == ni_columns) & (no_rows == ni_rows))) {
581 >        if (!transpose & (i_header == o_header) &&
582 >                        (no_columns == ni_columns) & (no_rows == ni_rows)) {
583                  if (warnings)
584                          fprintf(stderr, "%s: no-op -- copying input verbatim\n",
585                                  argv[0]);
# Line 500 | Line 587 | main(int argc, char *argv[])
587                          return(1);
588                  return(0);
589          }
590 <        if (do_header) {                        /* read/write header */
591 <                if (getheader(stdin, &headline, NULL) < 0)
590 >        if (i_header) {                         /* read header */
591 >                if (getheader(stdin, headline, NULL) < 0)
592                          return(1);
593 <                printargs(argc, argv, stdout);
593 >                if (!check_sizes())
594 >                        return(1);
595 >                if (comp_size) {                /* a little late... */
596 >                        SET_FILE_BINARY(stdin);
597 >                        SET_FILE_BINARY(stdout);
598 >                }
599 >        } else if (!check_sizes())
600 >                return(1);
601 >        if (o_header) {                         /* write header */
602 >                printargs(a, argv, stdout);
603 >                if (no_rows > 0)
604 >                        printf("NROWS=%d\n", no_rows);
605 >                if (no_columns > 0)
606 >                        printf("NCOLS=%d\n", no_columns);
607 >                printf("NCOMP=%d\n", n_comp);
608                  fputformat(fmtid, stdout);
609                  fputc('\n', stdout);            /* finish new header */
610          }
611          if (transpose) {                        /* transposing rows & columns? */
612 <                MEMLOAD myMem;                  /* need to load into memory */
613 <                if (i == argc-1) {
612 >                MEMLOAD myMem;                  /* need to map into memory */
613 >                if (a == argc-1) {
614                          if (load_file(&myMem, stdin) <= 0) {
615                                  fprintf(stderr, "%s: error loading file into memory\n",
616 <                                                argv[i]);
616 >                                                argv[a]);
617                                  return(1);
618                          }
619                  } else if (load_stream(&myMem, stdin) <= 0) {
# Line 522 | Line 623 | main(int argc, char *argv[])
623                  }
624                  if (!do_transpose(&myMem))
625                          return(1);
626 <                /* free_load(&myMem); */
627 <        } else if (!do_resize(stdin))           /* just reshaping input */
626 >                /* free_load(&myMem);   about to exit, so don't bother */
627 >        } else if (!do_resize(stdin))           /* reshaping input */
628                  return(1);
629          return(0);
630   userr:
631          fprintf(stderr,
632 < "Usage: %s [-h][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
632 > "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
633                          argv[0]);
634          return(1);
635   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines