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.32 by greg, Fri Nov 8 05:39:05 2019 UTC vs.
Revision 2.39 by greg, Thu Mar 3 03:55:13 2022 UTC

# Line 6 | Line 6 | static const char RCSid[] = "$Id$";
6   */
7  
8   #include <stdlib.h>
9 #include <string.h>
9   #include <ctype.h>
10   #include "platform.h"
11   #include "rtio.h"
# Line 30 | Line 29 | typedef struct {
29  
30   typedef struct {
31          int     nw_rec;         /* number of words per record */
32 <        long    nrecs;          /* number of records we found */
32 >        ssize_t nrecs;          /* number of records we found */
33          char    *rec[1];        /* record array (extends struct) */
34   } RECINDEX;
35  
# Line 88 | Line 87 | load_stream(MEMLOAD *mp, FILE *fp)
87          return(mp->len > 0);
88   }
89  
90 + #if defined(_WIN32) || defined(_WIN64)
91 +                                /* too difficult to fix this */
92 + #define load_file       load_stream
93 + #else
94   /* load a file into memory */
95   static int
96   load_file(MEMLOAD *mp, FILE *fp)
97   {
98          int     fd;
99          off_t   skip, flen, fpos;
97
98 #if defined(_WIN32) || defined(_WIN64)
99                                /* too difficult to fix this */
100        return load_stream(mp, fp);
101 #endif
100          if (mp == NULL)
101                  return(-1);
102          mp->mapped = NULL;
# Line 113 | Line 111 | load_file(MEMLOAD *mp, FILE *fp)
111                  return((int)(flen - skip));
112          mp->len = (size_t)(flen - skip);
113   #ifdef MAP_FILE
114 <        if (mp->len > 1L<<20) {         /* map file if > 1 MByte */
114 >        if (mp->len >= 1L<<20) {        /* map file if >= 1 MByte */
115                  mp->mapped = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0);
116                  if (mp->mapped != MAP_FAILED) {
117                          mp->base = (char *)mp->mapped + skip;
# Line 139 | Line 137 | load_file(MEMLOAD *mp, FILE *fp)
137          }
138          return(1);
139   }
140 + #endif
141  
142   /* free a record index */
143   #define free_records(rp)        free(rp)
# Line 219 | Line 218 | count_columns(const RECINDEX *rp)
218  
219   /* copy nth record from index to stdout */
220   static int
221 < print_record(const RECINDEX *rp, long n)
221 > print_record(const RECINDEX *rp, ssize_t n)
222   {
223          int     words2go = rp->nw_rec;
224          char    *scp;
# Line 294 | Line 293 | int            i_header = 1;                   /* input header? */
293   int             o_header = 1;                   /* output header? */
294   int             outArray[MAXLEVELS][2];         /* output block nesting */
295   int             outLevels = 0;                  /* number of blocking levels */
296 + int             check = 0;                      /* force data check? */
297  
298   /* parse RxCx... string */
299   static int
# Line 360 | Line 360 | check_sizes()
360   }
361  
362   /* call to compute block input position */
363 < static long
363 > static ssize_t
364   get_block_pos(int r, int c, int blklvl[][2], int nlvls)
365   {
366 <        long    n = 0;
366 >        ssize_t n = 0;
367  
368          while (nlvls > 1) {
369                  int     sr = r/blklvl[1][0];
# Line 379 | Line 379 | get_block_pos(int r, int c, int blklvl[][2], int nlvls
379   }
380  
381   /* return input offset based on array ordering and transpose option */
382 < static long
382 > static ssize_t
383   get_input_pos(int r, int c)
384   {
385 <        long    n;
385 >        ssize_t n;
386  
387          if (outLevels > 1) {            /* block reordering */
388                  n = get_block_pos(r, c, outArray, outLevels);
389                  if (transpose) {
390 <                        r = n/no_columns;
391 <                        c = n - r*no_columns;
392 <                        n = (long)r*ni_columns + c;
390 >                        r = n/ni_rows;
391 >                        c = n - r*ni_rows;
392 >                        n = (ssize_t)c*ni_columns + r;
393                  }
394          } else if (transpose)           /* transpose only */
395 <                n = (long)c*ni_columns + r;
395 >                n = (ssize_t)c*ni_columns + r;
396          else                            /* XXX should never happen! */
397 <                n = (long)r*no_columns + c;
397 >                n = (ssize_t)r*no_columns + c;
398          return(n);
399   }
400  
# Line 404 | Line 404 | do_reorder(const MEMLOAD *mp)
404   {
405          static const char       tabEOL[2] = {'\t','\n'};
406          RECINDEX                *rp = NULL;
407 <        long                    nrecords;
407 >        ssize_t                 nrecords;
408          int                     i, j;
409                                                  /* propogate sizes */
410          if (ni_rows <= 0)
411 <                ni_rows = no_columns;
411 >                ni_rows = transpose ? no_columns : no_rows;
412          if (ni_columns <= 0)
413 <                ni_columns = no_rows;
413 >                ni_columns = transpose ? no_rows : no_columns;
414                                                  /* get # records (& index) */
415          if (!comp_size) {
416                  if ((rp = index_records(mp, n_comp)) == NULL)
# Line 419 | Line 419 | do_reorder(const MEMLOAD *mp)
419                          ni_columns = count_columns(rp);
420                  nrecords = rp->nrecs;
421          } else if ((ni_rows > 0) & (ni_columns > 0)) {
422 <                nrecords = ni_rows*ni_columns;
422 >                nrecords = (ssize_t)ni_rows*ni_columns;
423                  if (nrecords > mp->len/(n_comp*comp_size)) {
424 <                        fprintf(stderr,
425 <                            "Input too small for specified size and type\n");
424 >                        fputs("Input too small for specified size and type\n",
425 >                                        stderr);
426                          return(0);
427                  }
428          } else
# Line 430 | Line 430 | do_reorder(const MEMLOAD *mp)
430                                                  /* check sizes */
431          if ((ni_rows <= 0) & (ni_columns > 0))
432                  ni_rows = nrecords/ni_columns;
433 <        if ((ni_columns <= 0) & (ni_rows > 0))
433 >        else if ((ni_columns <= 0) & (ni_rows > 0))
434                  ni_columns = nrecords/ni_rows;
435 <        if (nrecords != ni_rows*ni_columns)
435 >        if (nrecords != (ssize_t)ni_rows*ni_columns)
436                  goto badspec;
437          if (transpose) {
438                  if (no_columns <= 0)
439                          no_columns = ni_rows;
440                  if (no_rows <= 0)
441                          no_rows = ni_columns;
442 <                if ((no_rows != ni_columns) | (no_columns != ni_rows))
442 >                if (outLevels <= 1 &&
443 >                                (no_rows != ni_columns) | (no_columns != ni_rows))
444                          goto badspec;
445 +        } else {
446 +                if (no_columns <= 0)
447 +                        no_columns = ni_columns;
448 +                if (no_rows <= 0)
449 +                        no_rows = ni_rows;
450          }
451 +        if (ni_rows*ni_columns != no_rows*no_columns) {
452 +                fputs("Number of input and output records do not match\n",
453 +                                stderr);
454 +                return(0);
455 +        }
456 +        if (o_header) {                         /* finish header? */
457 +                printf("NROWS=%d\n", no_rows);
458 +                printf("NCOLS=%d\n", no_columns);
459 +                fputformat(fmtid, stdout);
460 +                fputc('\n', stdout);
461 +        }
462                                                  /* reorder records */
463          for (i = 0; i < no_rows; i++) {
464              for (j = 0; j < no_columns; j++) {
465 <                long    n = get_input_pos(i, j);
465 >                ssize_t n = get_input_pos(i, j);
466 >                if (n >= nrecords) {
467 >                        fputs("Index past end-of-file\n", stderr);
468 >                        return(0);
469 >                }
470                  if (rp != NULL) {               /* ASCII output */
471                          print_record(rp, n);
472                          putc(tabEOL[j >= no_columns-1], stdout);
# Line 471 | Line 492 | badspec:
492   static int
493   do_resize(FILE *fp)
494   {
495 <        long    records2go = ni_rows*ni_columns;
495 >        ssize_t records2go = ni_rows*ni_columns;
496          int     columns2go = no_columns;
497          char    word[256];
498 +                        
499 +        if (o_header) {                         /* finish header? */
500 +                if (no_rows > 0)
501 +                        printf("NROWS=%d\n", no_rows);
502 +                if (no_columns > 0)
503 +                        printf("NCOLS=%d\n", no_columns);
504 +                fputformat(fmtid, stdout);
505 +                fputc('\n', stdout);
506 +        }
507                                                  /* sanity checks */
508 <        if (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows))
508 >        if (comp_size || !check &
509 >                        (no_columns == ni_columns) & (no_rows == ni_rows))
510                  return(output_stream(fp));      /* no-op -- just copy */
511          if (no_columns <= 0) {
512                  fprintf(stderr, "Missing -oc specification\n");
# Line 485 | Line 516 | do_resize(FILE *fp)
516                  records2go = no_rows*no_columns;
517          else if (no_rows*no_columns != records2go) {
518                  fprintf(stderr,
519 <                        "Input and output data sizes disagree (%dx%d != %dx%d)\n",
519 >                        "Number of input and output records disagree (%dx%d != %dx%d)\n",
520                                  ni_rows, ni_columns, no_rows, no_columns);
521                  return(0);
522          }
# Line 652 | Line 683 | main(int argc, char *argv[])
683                  case 'w':                       /* warnings on/off */
684                          warnings = !warnings;
685                          break;
686 +                case 'c':                       /* force check operation */
687 +                        check = 1;
688 +                        break;
689                  default:
690                          goto userr;
691                  }
# Line 671 | Line 705 | main(int argc, char *argv[])
705                  SET_FILE_BINARY(stdout);
706          }
707                                                  /* check for no-op */
708 <        if (!transpose & (outLevels <= 1) & (i_header == o_header) &&
708 >        if (!transpose & !check & (outLevels <= 1) & (i_header == o_header) &&
709                          (no_columns == ni_columns) & (no_rows == ni_rows)) {
710                  if (warnings)
711                          fprintf(stderr, "%s: no-op -- copying input verbatim\n",
# Line 695 | Line 729 | main(int argc, char *argv[])
729                  if (!i_header)
730                          newheader("RADIANCE", stdout);
731                  printargs(a, argv, stdout);
698                if (no_rows > 0)
699                        printf("NROWS=%d\n", no_rows);
700                if (no_columns > 0)
701                        printf("NCOLS=%d\n", no_columns);
732                  printf("NCOMP=%d\n", n_comp);
703                fputformat(fmtid, stdout);
704                fputc('\n', stdout);            /* finish new header */
733          }
734 <        if (transpose | (outLevels > 1)) {      /* moving stuff around? */
734 >        if (transpose | check | (outLevels > 1) || (o_header && no_rows <= 0)) {
735                  MEMLOAD myMem;                  /* need to map into memory */
736                  if (a == argc-1) {
737                          if (load_file(&myMem, stdin) <= 0) {
# Line 719 | Line 747 | main(int argc, char *argv[])
747                  if (!do_reorder(&myMem))
748                          return(1);
749                  /* free_load(&myMem);   about to exit, so don't bother */
750 <        } else if (!do_resize(stdin))           /* reshaping input */
750 >        } else if (!do_resize(stdin))           /* just reshaping input */
751                  return(1);
752          return(0);
753   userr:
754          fprintf(stderr,
755 < "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n",
755 > "Usage: %s [-h[io]][-w][-c][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n",
756                          argv[0]);
757          return(1);
758   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines