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.15 by greg, Tue Jul 8 16:39:41 2014 UTC vs.
Revision 2.23 by schorsch, Sun Mar 6 01:13:18 2016 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)
# Line 64 | 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 99 | Line 131 | load_file(MEMLOAD *mp, FILE *fp)
131          return(1);
132   }
133  
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
134   /* free a record index */
135   #define free_records(rp)        free(rp)
136  
# Line 274 | Line 271 | int            ni_columns = 0;                 /* number of input columns */
271   int             ni_rows = 0;                    /* number of input rows */
272   int             no_columns = 0;                 /* number of output columns */
273   int             no_rows = 0;                    /* number of output rows */
274 + int             transpose = 0;                  /* transpose rows & cols? */
275 + int             i_header = 1;                   /* input header? */
276 + int             o_header = 1;                   /* output header? */
277  
278   /* check settings and assign defaults */
279   static int
# Line 293 | Line 293 | check_sizes()
293                          return(0);
294                  }
295          }
296 +        if (transpose && (no_rows <= 0) & (no_columns <= 0)) {
297 +                if (ni_rows > 0) no_columns = ni_rows;
298 +                if (ni_columns > 0) no_rows = ni_columns;
299 +        } else if ((no_rows <= 0) & (no_columns > 0) &&
300 +                        !((ni_rows*ni_columns) % no_columns))
301 +                no_rows = ni_rows*ni_columns/no_columns;
302          if (n_comp <= 0)
303                  n_comp = 3;
304          return(1);
# Line 372 | Line 378 | do_resize(FILE *fp)
378          int     columns2go = no_columns;
379          char    word[256];
380                                                  /* sanity checks */
381 <        if (comp_size)
382 <                return(output_stream(fp));      /* binary data -- just copy */
381 >        if (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows))
382 >                return(output_stream(fp));      /* no-op -- just copy */
383          if (no_columns <= 0) {
384                  fprintf(stderr, "Missing -oc specification\n");
385                  return(0);
# Line 466 | Line 472 | headline(char *s, void *p)
472                  n_comp = n;
473                  return(0);
474          }
475 <        fputs(s, stdout);                       /* copy header info. */
475 >        if (o_header)
476 >                fputs(s, stdout);               /* copy header info. */
477          return(0);
478   }
479  
# Line 474 | Line 481 | headline(char *s, void *p)
481   int
482   main(int argc, char *argv[])
483   {
477        int     i_header = 1;                   /* input header? */
478        int     o_header = 1;                   /* output header? */
479        int     transpose = 0;                  /* transpose rows & cols? */
484          int     a;
485  
486          for (a = 1; a < argc && argv[a][0] == '-'; a++)
# Line 565 | Line 569 | main(int argc, char *argv[])
569                  SET_FILE_BINARY(stdout);
570          }
571                                                  /* check for no-op */
572 <        if (!transpose & (i_header == o_header) && (comp_size ||
573 <                        (no_columns == ni_columns) & (no_rows == ni_rows))) {
572 >        if (!transpose & (i_header == o_header) &&
573 >                        (no_columns == ni_columns) & (no_rows == ni_rows)) {
574                  if (warnings)
575                          fprintf(stderr, "%s: no-op -- copying input verbatim\n",
576                                  argv[0]);
# Line 587 | Line 591 | main(int argc, char *argv[])
591                  return(1);
592          if (o_header) {                         /* write header */
593                  printargs(a, argv, stdout);
590                if (transpose && (no_rows <= 0) & (no_columns <= 0)) {
591                        if (ni_rows > 0) no_columns = ni_rows;
592                        if (ni_columns > 0) no_rows = ni_columns;
593                }
594                  if (no_rows > 0)
595                          printf("NROWS=%d\n", no_rows);
596                  if (no_columns > 0)
# Line 600 | Line 600 | main(int argc, char *argv[])
600                  fputc('\n', stdout);            /* finish new header */
601          }
602          if (transpose) {                        /* transposing rows & columns? */
603 <                MEMLOAD myMem;                  /* need to load into memory */
603 >                MEMLOAD myMem;                  /* need to map into memory */
604                  if (a == argc-1) {
605                          if (load_file(&myMem, stdin) <= 0) {
606                                  fprintf(stderr, "%s: error loading file into memory\n",
# Line 614 | Line 614 | main(int argc, char *argv[])
614                  }
615                  if (!do_transpose(&myMem))
616                          return(1);
617 <                /* free_load(&myMem); */
618 <        } else if (!do_resize(stdin))           /* just reshaping input */
617 >                /* free_load(&myMem);   about to exit, so don't bother */
618 >        } else if (!do_resize(stdin))           /* reshaping input */
619                  return(1);
620          return(0);
621   userr:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines