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.22 by greg, Fri Mar 4 00:21:21 2016 UTC

# Line 20 | Line 20 | static const char RCSid[] = "$Id$";
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)
# Line 64 | Line 92 | load_file(MEMLOAD *mp, FILE *fp)
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;
# Line 99 | Line 130 | load_file(MEMLOAD *mp, FILE *fp)
130          return(1);
131   }
132  
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
133   /* free a record index */
134   #define free_records(rp)        free(rp)
135  
# Line 237 | Line 233 | output_stream(FILE *fp)
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)
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(n >= 0);
240 >        return(!ferror(fp));
241   }
242  
243   /* get next word from stream, leaving stream on EOL or start of next word */
# Line 274 | Line 270 | 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
# Line 288 | Line 287 | check_sizes()
287                          comp_size = sizeof(double);
288                  else if (!strcmp(fmtid, "byte"))
289                          comp_size = 1;
290 <                else {
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);
# Line 372 | Line 377 | do_resize(FILE *fp)
377          int     columns2go = no_columns;
378          char    word[256];
379                                                  /* sanity checks */
380 <        if (comp_size)
381 <                return(output_stream(fp));      /* binary data -- just copy */
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);
# Line 460 | Line 465 | headline(char *s, void *p)
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", stderr);
468 >                        fputs("Incorrect number of components\n", stderr);
469                          return(-1);
470                  }
471                  n_comp = n;
472                  return(0);
473          }
474 <        fputs(s, stdout);                       /* copy header info. */
474 >        if (o_header)
475 >                fputs(s, stdout);               /* copy header info. */
476          return(0);
477   }
478  
# Line 474 | Line 480 | headline(char *s, void *p)
480   int
481   main(int argc, char *argv[])
482   {
483 <        int     i_header = 1;                   /* input header? */
478 <        int     o_header = 1;                   /* output header? */
479 <        int     transpose = 0;                  /* transpose rows & cols? */
480 <        int     i;
483 >        int     a;
484  
485 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
486 <                switch (argv[i][1]) {
485 >        for (a = 1; a < argc && argv[a][0] == '-'; a++)
486 >                switch (argv[a][1]) {
487                  case 'i':                       /* input */
488 <                        if (argv[i][2] == 'c')  /* columns */
489 <                                ni_columns = atoi(argv[++i]);
490 <                        else if (argv[i][2] == 'r')
491 <                                ni_rows = atoi(argv[++i]);
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[i][2] == 'c')  /* columns */
497 <                                no_columns = atoi(argv[++i]);
498 <                        else if (argv[i][2] == 'r')
499 <                                no_rows = atoi(argv[++i]);
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[i][2]) {
504 >                        switch (argv[a][2]) {
505                          case 'i':
506                                  i_header = 0;
507                                  break;
# Line 516 | Line 519 | main(int argc, char *argv[])
519                          transpose = !transpose;
520                          break;
521                  case 'f':                       /* format */
522 <                        switch (argv[i][2]) {
522 >                        switch (argv[a][2]) {
523                          case 'a':               /* ASCII */
524                          case 'A':
525                                  fmtid = "ascii";
# Line 540 | Line 543 | main(int argc, char *argv[])
543                          default:
544                                  goto userr;
545                          }
546 <                        if (argv[i][3]) {
547 <                                if (!isdigit(argv[i][3]))
546 >                        if (argv[a][3]) {
547 >                                if (!isdigit(argv[a][3]))
548                                          goto userr;
549 <                                n_comp = atoi(argv[i]+3);
550 <                        }
549 >                                n_comp = atoi(argv[a]+3);
550 >                        } else
551 >                                n_comp = 1;
552                          break;
553                  case 'w':                       /* warnings on/off */
554                          warnings = !warnings;
# Line 552 | Line 556 | main(int argc, char *argv[])
556                  default:
557                          goto userr;
558                  }
559 <        if (i < argc-1)                         /* arg count OK? */
559 >        if (a < argc-1)                         /* arg count OK? */
560                  goto userr;
561                                                  /* open input file? */
562 <        if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
563 <                fprintf(stderr, "%s: cannot open for reading\n", argv[i]);
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) {
# Line 564 | Line 568 | main(int argc, char *argv[])
568                  SET_FILE_BINARY(stdout);
569          }
570                                                  /* check for no-op */
571 <        if (!transpose & (i_header == o_header) && (comp_size ||
572 <                        (no_columns == ni_columns) & (no_rows == ni_rows))) {
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]);
# Line 574 | Line 578 | main(int argc, char *argv[])
578                  return(0);
579          }
580          if (i_header) {                         /* read header */
581 <                if (getheader(stdin, &headline, NULL) < 0)
581 >                if (getheader(stdin, headline, NULL) < 0)
582                          return(1);
583                  if (!check_sizes())
584                          return(1);
# Line 585 | Line 589 | main(int argc, char *argv[])
589          } else if (!check_sizes())
590                  return(1);
591          if (o_header) {                         /* write header */
592 <                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 <                }
592 >                printargs(a, argv, stdout);
593                  if (no_rows > 0)
594                          printf("NROWS=%d\n", no_rows);
595                  if (no_columns > 0)
# Line 599 | Line 599 | main(int argc, char *argv[])
599                  fputc('\n', stdout);            /* finish new header */
600          }
601          if (transpose) {                        /* transposing rows & columns? */
602 <                MEMLOAD myMem;                  /* need to load into memory */
603 <                if (i == argc-1) {
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[i]);
606 >                                                argv[a]);
607                                  return(1);
608                          }
609                  } else if (load_stream(&myMem, stdin) <= 0) {
# Line 613 | Line 613 | main(int argc, char *argv[])
613                  }
614                  if (!do_transpose(&myMem))
615                          return(1);
616 <                /* free_load(&myMem); */
617 <        } else if (!do_resize(stdin))           /* just reshaping input */
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:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines