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.4 by greg, Fri Sep 6 21:34:39 2013 UTC vs.
Revision 2.23 by schorsch, Sun Mar 6 01:13:18 2016 UTC

# Line 6 | Line 6 | static const char RCSid[] = "$Id$";
6   */
7  
8   #include <stdlib.h>
9 #include <unistd.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>
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  
19 #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
20 #undef getc
21 #undef putc
22 #define getc    getc_unlocked
23 #define putc    putc_unlocked
24 #endif
25
23   typedef struct {
24          void    *base;          /* pointer to base memory */
25          size_t  len;            /* allocated memory length */
# Line 53 | 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 60 | 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 95 | Line 131 | load_file(MEMLOAD *mp, FILE *fp)
131          return(1);
132   }
133  
98 /* load memory from an input stream, starting from current position */
99 static int
100 load_stream(MEMLOAD *mp, FILE *fp)
101 {
102        char    buf[8192];
103        size_t  nr;
104
105        if (mp == NULL)
106                return(-1);
107        mp->base = NULL;
108        mp->len = 0;
109        mp->mapped = 0;
110        if (fp == NULL)
111                return(-1);
112        while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) {
113                if (!mp->len)
114                        mp->base = malloc(nr);
115                else
116                        mp->base = realloc(mp->base, mp->len+nr);
117                if (mp->base == NULL)
118                        return(-1);
119                memcpy((char *)mp->base + mp->len, buf, nr);
120                mp->len += nr;
121        }
122        if (ferror(fp)) {
123                free_load(mp);
124                return(-1);
125        }
126        return(mp->len > 0);
127 }
128
134   /* free a record index */
135   #define free_records(rp)        free(rp)
136  
# Line 229 | Line 234 | output_stream(FILE *fp)
234  
235          if (fp == NULL)
236                  return(0);
237 <        fflush(stdout);                 /* assumes nothing in input buffer */
238 <        while ((n = read(fileno(fp), buf, sizeof(buf))) > 0)
237 >        fflush(stdout);
238 >        while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
239                  if (write(fileno(stdout), buf, n) != n)
240                          return(0);
241 <        return(n >= 0);
241 >        return(!ferror(fp));
242   }
243  
244   /* get next word from stream, leaving stream on EOL or start of next word */
# Line 259 | Line 264 | fget_word(char buf[256], FILE *fp)
264          return(buf);
265   }
266  
267 < char            *fmtid = "ascii";               /* format id */
268 < int             record_width = 3;               /* words/record (<0 binary) */
267 > char            *fmtid = NULL;                  /* format id */
268 > int             comp_size = 0;                  /* binary bytes/channel */
269 > int             n_comp = 0;                     /* components/record */
270   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
280 + check_sizes()
281 + {
282 +        if (fmtid == NULL) {
283 +                fmtid = "ascii";
284 +        } else if (!comp_size) {
285 +                if (!strcmp(fmtid, "float"))
286 +                        comp_size = sizeof(float);
287 +                else if (!strcmp(fmtid, "double"))
288 +                        comp_size = sizeof(double);
289 +                else if (!strcmp(fmtid, "byte"))
290 +                        comp_size = 1;
291 +                else if (strcmp(fmtid, "ascii")) {
292 +                        fprintf(stderr, "Unsupported format: %s\n", fmtid);
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);
305 + }
306 +
307   /* output transposed ASCII or binary data from memory */
308   static int
309   do_transpose(const MEMLOAD *mp)
# Line 274 | Line 312 | do_transpose(const MEMLOAD *mp)
312          RECINDEX                *rp = NULL;
313          long                    nrecords;
314          int                     i, j;
315 +                                                /* propogate sizes */
316 +        if (ni_rows <= 0)
317 +                ni_rows = no_columns;
318 +        if (ni_columns <= 0)
319 +                ni_columns = no_rows;
320                                                  /* get # records (& index) */
321 <        if (record_width > 0) {
322 <                if ((rp = index_records(mp, record_width)) == NULL)
321 >        if (!comp_size) {
322 >                if ((rp = index_records(mp, n_comp)) == NULL)
323                          return(0);
324                  if (ni_columns <= 0)
325                          ni_columns = count_columns(rp);
326                  nrecords = rp->nrecs;
327          } else if ((ni_rows > 0) & (ni_columns > 0)) {
328                  nrecords = ni_rows*ni_columns;
329 <                if (nrecords > mp->len / -record_width) {
329 >                if (nrecords > mp->len/(n_comp*comp_size)) {
330                          fprintf(stderr,
331                              "Input too small for specified size and type\n");
332                          return(0);
333                  }
334          } else
335 <                nrecords = mp->len / -record_width;
335 >                nrecords = mp->len/(n_comp*comp_size);
336                                                  /* check sizes */
294        if (ni_rows <= 0)
295                ni_rows = no_columns;
296        if (ni_columns <= 0)
297                ni_columns = no_rows;
337          if ((ni_rows <= 0) & (ni_columns > 0))
338                  ni_rows = nrecords/ni_columns;
339          if ((ni_columns <= 0) & (ni_rows > 0))
# Line 315 | Line 354 | do_transpose(const MEMLOAD *mp)
354                          putc(tabEOL[j >= no_columns-1], stdout);
355                  } else {                        /* binary output */
356                          fwrite((char *)mp->base +
357 <                                        -record_width*(j*ni_columns + i),
358 <                                        -record_width, 1, stdout);
357 >                                        (n_comp*comp_size)*(j*ni_columns + i),
358 >                                        n_comp*comp_size, 1, stdout);
359                  }
360              if (ferror(stdout)) {
361                  fprintf(stderr, "Error writing to stdout\n");
# Line 339 | Line 378 | do_resize(FILE *fp)
378          int     columns2go = no_columns;
379          char    word[256];
380                                                  /* sanity checks */
381 <        if (record_width <= 0) {
382 <                fprintf(stderr, "Bad call to do_resize (record_width = %d)\n",
344 <                                record_width);
345 <                return(0);
346 <        }
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 359 | Line 395 | do_resize(FILE *fp)
395          do {                                    /* reshape records */
396                  int     n;
397  
398 <                for (n = record_width; n--; ) {
398 >                for (n = n_comp; n--; ) {
399                          if (fget_word(word, fp) == NULL) {
400 <                                if (records2go > 0 || n < record_width-1)
400 >                                if (records2go > 0 || n < n_comp-1)
401                                          break;
402                                  goto done;      /* normal EOD */
403                          }
# Line 396 | Line 432 | done:
432   static int
433   headline(char *s, void *p)
434   {
435 <        char    fmt[32];
435 >        static char     fmt[32];
436 >        int             n;
437  
438          if (formatval(fmt, s)) {
439 +                if (fmtid == NULL) {
440 +                        fmtid = fmt;
441 +                        return(0);
442 +                }
443                  if (!strcmp(fmt, fmtid))
444                          return(0);
445                  fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid);
446                  return(-1);
447          }
448 <        fputs(s, stdout);                       /* copy header info. */
448 >        if (!strncmp(s, "NROWS=", 6)) {
449 >                n = atoi(s+6);
450 >                if ((ni_rows > 0) & (n != ni_rows)) {
451 >                        fputs("Incorrect input row count\n", stderr);
452 >                        return(-1);
453 >                }
454 >                ni_rows = n;
455 >                return(0);
456 >        }
457 >        if (!strncmp(s, "NCOLS=", 6)) {
458 >                n = atoi(s+6);
459 >                if ((ni_columns > 0) & (n != ni_columns)) {
460 >                        fputs("Incorrect input column count\n", stderr);
461 >                        return(-1);
462 >                }
463 >                ni_columns = n;
464 >                return(0);
465 >        }
466 >        if (!strncmp(s, "NCOMP=", 6)) {
467 >                n = atoi(s+6);
468 >                if ((n_comp > 0) & (n != n_comp)) {
469 >                        fputs("Incorrect number of components\n", stderr);
470 >                        return(-1);
471 >                }
472 >                n_comp = n;
473 >                return(0);
474 >        }
475 >        if (o_header)
476 >                fputs(s, stdout);               /* copy header info. */
477          return(0);
478   }
479  
# Line 412 | Line 481 | headline(char *s, void *p)
481   int
482   main(int argc, char *argv[])
483   {
484 <        int     do_header = 1;                  /* header i/o? */
416 <        int     transpose = 0;                  /* transpose rows & cols? */
417 <        int     i;
484 >        int     a;
485  
486 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
487 <                switch (argv[i][1]) {
486 >        for (a = 1; a < argc && argv[a][0] == '-'; a++)
487 >                switch (argv[a][1]) {
488                  case 'i':                       /* input */
489 <                        if (argv[i][2] == 'c')  /* columns */
490 <                                ni_columns = atoi(argv[++i]);
491 <                        else if (argv[i][2] == 'r')
492 <                                ni_rows = atoi(argv[++i]);
489 >                        if (argv[a][2] == 'c')  /* columns */
490 >                                ni_columns = atoi(argv[++a]);
491 >                        else if (argv[a][2] == 'r')
492 >                                ni_rows = atoi(argv[++a]);
493                          else
494                                  goto userr;
495                          break;
496                  case 'o':                       /* output */
497 <                        if (argv[i][2] == 'c')  /* columns */
498 <                                no_columns = atoi(argv[++i]);
499 <                        else if (argv[i][2] == 'r')
500 <                                no_rows = atoi(argv[++i]);
497 >                        if (argv[a][2] == 'c')  /* columns */
498 >                                no_columns = atoi(argv[++a]);
499 >                        else if (argv[a][2] == 'r')
500 >                                no_rows = atoi(argv[++a]);
501                          else
502                                  goto userr;
503                          break;
504 <                case 'h':                       /* header on/off */
505 <                        do_header = !do_header;
504 >                case 'h':                       /* turn off header */
505 >                        switch (argv[a][2]) {
506 >                        case 'i':
507 >                                i_header = 0;
508 >                                break;
509 >                        case 'o':
510 >                                o_header = 0;
511 >                                break;
512 >                        case '\0':
513 >                                i_header = o_header = 0;
514 >                                break;
515 >                        default:
516 >                                goto userr;
517 >                        }
518                          break;
519                  case 't':                       /* transpose on/off */
520                          transpose = !transpose;
521                          break;
522                  case 'f':                       /* format */
523 <                        switch (argv[i][2]) {
523 >                        switch (argv[a][2]) {
524                          case 'a':               /* ASCII */
525                          case 'A':
526                                  fmtid = "ascii";
527 <                                record_width = 1;
527 >                                comp_size = 0;
528                                  break;
529                          case 'f':               /* float */
530                          case 'F':
531                                  fmtid = "float";
532 <                                record_width = -(int)sizeof(float);
532 >                                comp_size = sizeof(float);
533                                  break;
534                          case 'd':               /* double */
535                          case 'D':
536                                  fmtid = "double";
537 <                                record_width = -(int)sizeof(double);
537 >                                comp_size = sizeof(double);
538                                  break;
539                          case 'b':               /* binary (bytes) */
540                          case 'B':
541                                  fmtid = "byte";
542 <                                record_width = -1;
542 >                                comp_size = 1;
543                                  break;
544                          default:
545                                  goto userr;
546                          }
547 <                        if (argv[i][3]) {
548 <                                if (!isdigit(argv[i][3]))
547 >                        if (argv[a][3]) {
548 >                                if (!isdigit(argv[a][3]))
549                                          goto userr;
550 <                                record_width *= atoi(argv[i]+3);
551 <                        }
550 >                                n_comp = atoi(argv[a]+3);
551 >                        } else
552 >                                n_comp = 1;
553                          break;
554                  case 'w':                       /* warnings on/off */
555                          warnings = !warnings;
# Line 477 | Line 557 | main(int argc, char *argv[])
557                  default:
558                          goto userr;
559                  }
560 <        if (!record_width)
560 >        if (a < argc-1)                         /* arg count OK? */
561                  goto userr;
482        if (i < argc-1)                         /* arg count OK? */
483                goto userr;
562                                                  /* open input file? */
563 <        if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
564 <                fprintf(stderr, "%s: cannot open for reading\n", argv[i]);
563 >        if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) {
564 >                fprintf(stderr, "%s: cannot open for reading\n", argv[a]);
565                  return(1);
566          }
567 <        if (record_width < 0) {
567 >        if (comp_size) {
568                  SET_FILE_BINARY(stdin);
569                  SET_FILE_BINARY(stdout);
570          }
571                                                  /* check for no-op */
572 <        if (!transpose && (record_width < 0 ||
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 500 | Line 578 | main(int argc, char *argv[])
578                          return(1);
579                  return(0);
580          }
581 <        if (do_header) {                        /* read/write header */
582 <                if (getheader(stdin, &headline, NULL) < 0)
581 >        if (i_header) {                         /* read header */
582 >                if (getheader(stdin, headline, NULL) < 0)
583                          return(1);
584 <                printargs(argc, argv, stdout);
584 >                if (!check_sizes())
585 >                        return(1);
586 >                if (comp_size) {                /* a little late... */
587 >                        SET_FILE_BINARY(stdin);
588 >                        SET_FILE_BINARY(stdout);
589 >                }
590 >        } else if (!check_sizes())
591 >                return(1);
592 >        if (o_header) {                         /* write header */
593 >                printargs(a, argv, stdout);
594 >                if (no_rows > 0)
595 >                        printf("NROWS=%d\n", no_rows);
596 >                if (no_columns > 0)
597 >                        printf("NCOLS=%d\n", no_columns);
598 >                printf("NCOMP=%d\n", n_comp);
599                  fputformat(fmtid, stdout);
600                  fputc('\n', stdout);            /* finish new header */
601          }
602          if (transpose) {                        /* transposing rows & columns? */
603 <                MEMLOAD myMem;                  /* need to load into memory */
604 <                if (i == argc-1) {
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",
607 <                                                argv[i]);
607 >                                                argv[a]);
608                                  return(1);
609                          }
610                  } else if (load_stream(&myMem, stdin) <= 0) {
# Line 522 | 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:
622          fprintf(stderr,
623 < "Usage: %s [-h][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
623 > "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
624                          argv[0]);
625          return(1);
626   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines