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.12 by greg, Fri May 30 18:10:20 2014 UTC vs.
Revision 2.27 by greg, Thu Oct 18 22:59:48 2018 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
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
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>
20 >  #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
23   typedef struct {
24          void    *base;          /* pointer to base memory */
25          size_t  len;            /* allocated memory length */
# Line 57 | 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)
91   {
92          int     fd;
93 <        off_t   skip, flen;
93 >        off_t   skip, flen, skipped;
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 79 | Line 111 | load_file(MEMLOAD *mp, FILE *fp)
111          mp->len = (size_t)(flen - skip);
112   #ifdef MAP_FILE
113          if (mp->len > 1L<<20) {         /* map file if > 1 MByte */
114 <                mp->base = mmap(NULL, mp->len, PROT_READ, MAP_PRIVATE, fd, skip);
114 >                mp->base = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0);
115                  if (mp->base != MAP_FAILED) {
116 +                        mp->base = (char *)mp->base + skip;
117                          mp->mapped = 1;
118                          return(1);      /* mmap() success */
119                  }
# Line 92 | Line 125 | load_file(MEMLOAD *mp, FILE *fp)
125                  mp->len = 0;
126                  return(-1);
127          }
128 <        if (read(fd, (char *)mp->base, mp->len) != mp->len) {
129 <                free_load(mp);
130 <                return(-1);
128 >        skipped = skip;                 /* read() fails on really big buffers */
129 >        while (skipped < flen) {
130 >                ssize_t nread = read(fd, (char *)mp->base+(skipped-skip),
131 >                                (flen-skipped <= 1L<<30) ? flen-skipped : 1L<<30);
132 >                if (nread <= 0) {
133 >                        free_load(mp);
134 >                        return(-1);
135 >                }
136 >                skipped += nread;
137          }
138          return(1);
139   }
140  
102 /* load memory from an input stream, starting from current position */
103 static int
104 load_stream(MEMLOAD *mp, FILE *fp)
105 {
106        size_t  alloced = 0;
107        char    buf[8192];
108        size_t  nr;
109
110        if (mp == NULL)
111                return(-1);
112        mp->base = NULL;
113        mp->len = 0;
114        mp->mapped = 0;
115        if (fp == NULL)
116                return(-1);
117        while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) {
118                if (!alloced)
119                        mp->base = malloc(nr);
120                else if (mp->len+nr > alloced)
121                        mp->base = realloc(mp->base,
122                                alloced = alloced*(2+(nr==sizeof(buf)))/2+nr);
123                if (mp->base == NULL)
124                        return(-1);
125                memcpy((char *)mp->base + mp->len, buf, nr);
126                mp->len += nr;
127        }
128        if (ferror(fp)) {
129                free_load(mp);
130                return(-1);
131        }
132        if (alloced > mp->len*5/4)      /* don't waste too much space */
133                mp->base = realloc(mp->base, mp->len);
134        return(mp->len > 0);
135 }
136
141   /* free a record index */
142   #define free_records(rp)        free(rp)
143  
# Line 141 | Line 145 | load_stream(MEMLOAD *mp, FILE *fp)
145   static RECINDEX *
146   index_records(const MEMLOAD *mp, int nw_rec)
147   {
148 +        int             nall = 0;
149          RECINDEX        *rp;
150          char            *cp, *mend;
151          int             n;
# Line 149 | Line 154 | index_records(const MEMLOAD *mp, int nw_rec)
154                  return(NULL);
155          if (nw_rec <= 0)
156                  return(NULL);
157 <        rp = (RECINDEX *)malloc(sizeof(RECINDEX) + mp->len/(2*nw_rec)*sizeof(char *));
157 >        nall = 1000;
158 >        rp = (RECINDEX *)malloc(sizeof(RECINDEX) + nall*sizeof(char *));
159          if (rp == NULL)
160                  return(NULL);
161          rp->nw_rec = nw_rec;
# Line 161 | Line 167 | index_records(const MEMLOAD *mp, int nw_rec)
167                          ++cp;
168                  if (cp >= mend)
169                          break;
170 +                if (rp->nrecs >= nall) {
171 +                        nall += nall>>1;        /* get more record space */
172 +                        rp = (RECINDEX *)realloc(rp,
173 +                                        sizeof(RECINDEX) + nall*sizeof(char *));
174 +                        if (rp == NULL)
175 +                                return(NULL);
176 +                }
177                  rp->rec[rp->nrecs++] = cp;      /* point to first non-white */
178                  n = rp->nw_rec;
179                  while (++cp < mend)             /* find end of record */
# Line 237 | Line 250 | output_stream(FILE *fp)
250  
251          if (fp == NULL)
252                  return(0);
253 <        fflush(stdout);                 /* assumes nothing in input buffer */
254 <        while ((n = read(fileno(fp), buf, sizeof(buf))) > 0)
253 >        fflush(stdout);
254 >        while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
255                  if (write(fileno(stdout), buf, n) != n)
256                          return(0);
257 <        return(n >= 0);
257 >        return(!ferror(fp));
258   }
259  
260   /* get next word from stream, leaving stream on EOL or start of next word */
# Line 274 | Line 287 | int            ni_columns = 0;                 /* number of input columns */
287   int             ni_rows = 0;                    /* number of input rows */
288   int             no_columns = 0;                 /* number of output columns */
289   int             no_rows = 0;                    /* number of output rows */
290 + int             transpose = 0;                  /* transpose rows & cols? */
291 + int             i_header = 1;                   /* input header? */
292 + int             o_header = 1;                   /* output header? */
293  
294   /* check settings and assign defaults */
295   static int
# Line 288 | Line 304 | check_sizes()
304                          comp_size = sizeof(double);
305                  else if (!strcmp(fmtid, "byte"))
306                          comp_size = 1;
307 <                else {
307 >                else if (strcmp(fmtid, "ascii")) {
308                          fprintf(stderr, "Unsupported format: %s\n", fmtid);
309                          return(0);
310                  }
311          }
312 +        if (transpose && (no_rows <= 0) & (no_columns <= 0)) {
313 +                if (ni_rows > 0) no_columns = ni_rows;
314 +                if (ni_columns > 0) no_rows = ni_columns;
315 +        } else if ((no_rows <= 0) & (no_columns > 0) &&
316 +                        !((ni_rows*ni_columns) % no_columns))
317 +                no_rows = ni_rows*ni_columns/no_columns;
318          if (n_comp <= 0)
319                  n_comp = 3;
320          return(1);
# Line 347 | Line 369 | do_transpose(const MEMLOAD *mp)
369                          print_record(rp, j*ni_columns + i);
370                          putc(tabEOL[j >= no_columns-1], stdout);
371                  } else {                        /* binary output */
372 <                        fwrite((char *)mp->base +
373 <                                        (n_comp*comp_size)*(j*ni_columns + i),
374 <                                        n_comp*comp_size, 1, stdout);
372 >                        putbinary((char *)mp->base +
373 >                            (unsigned long)(n_comp*comp_size)*(j*ni_columns + i),
374 >                                        comp_size, n_comp, stdout);
375                  }
376              if (ferror(stdout)) {
377                  fprintf(stderr, "Error writing to stdout\n");
# Line 372 | Line 394 | do_resize(FILE *fp)
394          int     columns2go = no_columns;
395          char    word[256];
396                                                  /* sanity checks */
397 <        if (comp_size)
398 <                return(output_stream(fp));      /* binary data -- just copy */
397 >        if (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows))
398 >                return(output_stream(fp));      /* no-op -- just copy */
399          if (no_columns <= 0) {
400                  fprintf(stderr, "Missing -oc specification\n");
401                  return(0);
# Line 426 | Line 448 | done:
448   static int
449   headline(char *s, void *p)
450   {
451 <        static char     fmt[32];
451 >        static char     fmt[MAXFMTLEN];
452          int             n;
453  
454          if (formatval(fmt, s)) {
# Line 460 | Line 482 | headline(char *s, void *p)
482          if (!strncmp(s, "NCOMP=", 6)) {
483                  n = atoi(s+6);
484                  if ((n_comp > 0) & (n != n_comp)) {
485 <                        fputs("Incorrect number of components", stderr);
485 >                        fputs("Incorrect number of components\n", stderr);
486                          return(-1);
487                  }
488                  n_comp = n;
489                  return(0);
490          }
491 <        fputs(s, stdout);                       /* copy header info. */
491 >        if (o_header)
492 >                fputs(s, stdout);               /* copy header info. */
493          return(0);
494   }
495  
# Line 474 | Line 497 | headline(char *s, void *p)
497   int
498   main(int argc, char *argv[])
499   {
500 <        int     i_header = 1;                   /* input header? */
478 <        int     o_header = 1;                   /* output header? */
479 <        int     transpose = 0;                  /* transpose rows & cols? */
480 <        int     i;
500 >        int     a;
501  
502 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
503 <                switch (argv[i][1]) {
502 >        for (a = 1; a < argc && argv[a][0] == '-'; a++)
503 >                switch (argv[a][1]) {
504                  case 'i':                       /* input */
505 <                        if (argv[i][2] == 'c')  /* columns */
506 <                                ni_columns = atoi(argv[++i]);
507 <                        else if (argv[i][2] == 'r')
508 <                                ni_rows = atoi(argv[++i]);
505 >                        if (argv[a][2] == 'c')  /* columns */
506 >                                ni_columns = atoi(argv[++a]);
507 >                        else if (argv[a][2] == 'r')
508 >                                ni_rows = atoi(argv[++a]);
509                          else
510                                  goto userr;
511                          break;
512                  case 'o':                       /* output */
513 <                        if (argv[i][2] == 'c')  /* columns */
514 <                                no_columns = atoi(argv[++i]);
515 <                        else if (argv[i][2] == 'r')
516 <                                no_rows = atoi(argv[++i]);
513 >                        if (argv[a][2] == 'c')  /* columns */
514 >                                no_columns = atoi(argv[++a]);
515 >                        else if (argv[a][2] == 'r')
516 >                                no_rows = atoi(argv[++a]);
517                          else
518                                  goto userr;
519                          break;
520                  case 'h':                       /* turn off header */
521 <                        switch (argv[i][2]) {
521 >                        switch (argv[a][2]) {
522                          case 'i':
523                                  i_header = 0;
524                                  break;
# Line 516 | Line 536 | main(int argc, char *argv[])
536                          transpose = !transpose;
537                          break;
538                  case 'f':                       /* format */
539 <                        switch (argv[i][2]) {
539 >                        switch (argv[a][2]) {
540                          case 'a':               /* ASCII */
541                          case 'A':
542                                  fmtid = "ascii";
# Line 540 | Line 560 | main(int argc, char *argv[])
560                          default:
561                                  goto userr;
562                          }
563 <                        if (argv[i][3]) {
564 <                                if (!isdigit(argv[i][3]))
563 >                        if (argv[a][3]) {
564 >                                if (!isdigit(argv[a][3]))
565                                          goto userr;
566 <                                n_comp = atoi(argv[i]+3);
567 <                        }
566 >                                n_comp = atoi(argv[a]+3);
567 >                        } else
568 >                                n_comp = 1;
569                          break;
570                  case 'w':                       /* warnings on/off */
571                          warnings = !warnings;
# Line 552 | Line 573 | main(int argc, char *argv[])
573                  default:
574                          goto userr;
575                  }
576 <        if (i < argc-1)                         /* arg count OK? */
576 >        if (a < argc-1)                         /* arg count OK? */
577                  goto userr;
578                                                  /* open input file? */
579 <        if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
580 <                fprintf(stderr, "%s: cannot open for reading\n", argv[i]);
579 >        if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) {
580 >                fprintf(stderr, "%s: cannot open for reading\n", argv[a]);
581                  return(1);
582          }
583          if (comp_size) {
# Line 564 | Line 585 | main(int argc, char *argv[])
585                  SET_FILE_BINARY(stdout);
586          }
587                                                  /* check for no-op */
588 <        if (!transpose & (i_header == o_header) && (comp_size ||
589 <                        (no_columns == ni_columns) & (no_rows == ni_rows))) {
588 >        if (!transpose & (i_header == o_header) &&
589 >                        (no_columns == ni_columns) & (no_rows == ni_rows)) {
590                  if (warnings)
591                          fprintf(stderr, "%s: no-op -- copying input verbatim\n",
592                                  argv[0]);
# Line 574 | Line 595 | main(int argc, char *argv[])
595                  return(0);
596          }
597          if (i_header) {                         /* read header */
598 <                if (getheader(stdin, &headline, NULL) < 0)
598 >                if (getheader(stdin, headline, NULL) < 0)
599                          return(1);
600                  if (!check_sizes())
601                          return(1);
# Line 585 | Line 606 | main(int argc, char *argv[])
606          } else if (!check_sizes())
607                  return(1);
608          if (o_header) {                         /* write header */
609 <                printargs(argc, argv, stdout);
589 <                if (transpose && (no_rows <= 0) & (no_columns <= 0)) {
590 <                        if (ni_rows > 0) no_columns = ni_rows;
591 <                        if (ni_columns > 0) no_rows = ni_columns;
592 <                }
609 >                printargs(a, argv, stdout);
610                  if (no_rows > 0)
611                          printf("NROWS=%d\n", no_rows);
612                  if (no_columns > 0)
# Line 599 | Line 616 | main(int argc, char *argv[])
616                  fputc('\n', stdout);            /* finish new header */
617          }
618          if (transpose) {                        /* transposing rows & columns? */
619 <                MEMLOAD myMem;                  /* need to load into memory */
620 <                if (i == argc-1) {
619 >                MEMLOAD myMem;                  /* need to map into memory */
620 >                if (a == argc-1) {
621                          if (load_file(&myMem, stdin) <= 0) {
622                                  fprintf(stderr, "%s: error loading file into memory\n",
623 <                                                argv[i]);
623 >                                                argv[a]);
624                                  return(1);
625                          }
626                  } else if (load_stream(&myMem, stdin) <= 0) {
# Line 613 | Line 630 | main(int argc, char *argv[])
630                  }
631                  if (!do_transpose(&myMem))
632                          return(1);
633 <                /* free_load(&myMem); */
634 <        } else if (!do_resize(stdin))           /* just reshaping input */
633 >                /* free_load(&myMem);   about to exit, so don't bother */
634 >        } else if (!do_resize(stdin))           /* reshaping input */
635                  return(1);
636          return(0);
637   userr:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines