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.37 by greg, Fri Jan 15 17:22:23 2021 UTC vs.
Revision 2.40 by greg, Wed Mar 16 17:36:45 2022 UTC

# Line 19 | Line 19 | static const char RCSid[] = "$Id$";
19    #include <sys/mman.h>
20   #endif
21  
22 + static char     delims[] = " \t\n\r\f";
23 +
24   #define MAXLEVELS       16      /* max RxC.. block pairs */
25  
26   typedef struct {
# Line 33 | Line 35 | typedef struct {
35          char    *rec[1];        /* record array (extends struct) */
36   } RECINDEX;
37  
38 < int             warnings = 1;   /* report warnings? */
38 > int             warnings = 1;                   /* report warnings? */
39  
40 + char            *fmtid = NULL;                  /* format id */
41 + int             comp_size = 0;                  /* binary bytes/channel */
42 + int             n_comp = 0;                     /* components/record */
43 + int             ni_columns = 0;                 /* number of input columns */
44 + int             ni_rows = 0;                    /* number of input rows */
45 + int             no_columns = 0;                 /* number of output columns */
46 + int             no_rows = 0;                    /* number of output rows */
47 + int             transpose = 0;                  /* transpose rows & cols? */
48 + int             i_header = 1;                   /* input header? */
49 + int             o_header = 1;                   /* output header? */
50 + int             outArray[MAXLEVELS][2];         /* output block nesting */
51 + int             outLevels = 0;                  /* number of blocking levels */
52 + int             check = 0;                      /* force data check? */
53 +
54   /* free loaded file */
55   static void
56   free_load(MEMLOAD *mp)
# Line 87 | Line 103 | load_stream(MEMLOAD *mp, FILE *fp)
103          return(mp->len > 0);
104   }
105  
106 + #if defined(_WIN32) || defined(_WIN64)
107 +                                /* too difficult to fix this */
108 + #define load_file       load_stream
109 + #else
110   /* load a file into memory */
111   static int
112   load_file(MEMLOAD *mp, FILE *fp)
113   {
114          int     fd;
115          off_t   skip, flen, fpos;
96
97 #if defined(_WIN32) || defined(_WIN64)
98                                /* too difficult to fix this */
99        return load_stream(mp, fp);
100 #endif
116          if (mp == NULL)
117                  return(-1);
118          mp->mapped = NULL;
# Line 112 | Line 127 | load_file(MEMLOAD *mp, FILE *fp)
127                  return((int)(flen - skip));
128          mp->len = (size_t)(flen - skip);
129   #ifdef MAP_FILE
130 <        if (mp->len > 1L<<20) {         /* map file if > 1 MByte */
130 >        if (mp->len >= 1L<<20) {        /* map file if >= 1 MByte */
131                  mp->mapped = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0);
132                  if (mp->mapped != MAP_FAILED) {
133                          mp->base = (char *)mp->mapped + skip;
# Line 138 | Line 153 | load_file(MEMLOAD *mp, FILE *fp)
153          }
154          return(1);
155   }
156 + #endif
157  
158   /* free a record index */
159   #define free_records(rp)        free(rp)
# Line 183 | Line 199 | index_records(const MEMLOAD *mp, int nw_rec)
199                                          break;  /* got requisite # words */
200                                  do {            /* else find next word */
201                                          if (*cp == '\n') {
202 <                                                fprintf(stderr,
187 <                                                "Unexpected EOL in record!\n");
202 >                                                fputs("Unexpected EOL in record!\n", stderr);
203                                                  free_records(rp);
204                                                  return(NULL);
205                                          }
# Line 226 | Line 241 | print_record(const RECINDEX *rp, ssize_t n)
241          if ((n < 0) | (n >= rp->nrecs))
242                  return(0);
243          scp = rp->rec[n];
244 +
245 +        if (check && !isfltd(scp, delims))
246 +                goto formerr;
247          do {
248                  putc(*scp++, stdout);
249                  if (!*scp | isspace(*scp)) {
# Line 236 | Line 254 | print_record(const RECINDEX *rp, ssize_t n)
254                                  if (++scp >= rp->rec[n+1])
255                                          break;
256                          while (!*scp | isspace(*scp));
257 +
258 +                        if (check && !isfltd(scp, delims))
259 +                                goto formerr;
260                  }
261          } while (scp < rp->rec[n+1]);
262                                                  /* caller adds record sep. */
263          return(1);
264 + formerr:
265 +        fputs("Badly formed number: ", stderr);
266 +        while (*scp && !isspace(*scp))
267 +                fputc(*scp++, stderr);
268 +        fputc('\n', stderr);
269 +        return(0);
270   }
271  
272   /* copy a stream to stdout */
# Line 281 | Line 308 | fget_word(char buf[256], FILE *fp)
308          return(buf);
309   }
310  
284 char            *fmtid = NULL;                  /* format id */
285 int             comp_size = 0;                  /* binary bytes/channel */
286 int             n_comp = 0;                     /* components/record */
287 int             ni_columns = 0;                 /* number of input columns */
288 int             ni_rows = 0;                    /* number of input rows */
289 int             no_columns = 0;                 /* number of output columns */
290 int             no_rows = 0;                    /* number of output rows */
291 int             transpose = 0;                  /* transpose rows & cols? */
292 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
311   /* parse RxCx... string */
312   static int
313   get_array(const char *spec, int blklvl[][2], int nlvls)
# Line 456 | Line 469 | do_reorder(const MEMLOAD *mp)
469          if (o_header) {                         /* finish header? */
470                  printf("NROWS=%d\n", no_rows);
471                  printf("NCOLS=%d\n", no_columns);
472 +                fputformat(fmtid, stdout);
473                  fputc('\n', stdout);
474          }
475                                                  /* reorder records */
# Line 467 | Line 481 | do_reorder(const MEMLOAD *mp)
481                          return(0);
482                  }
483                  if (rp != NULL) {               /* ASCII output */
484 <                        print_record(rp, n);
484 >                        if (!print_record(rp, n))
485 >                                return(0);
486                          putc(tabEOL[j >= no_columns-1], stdout);
487                  } else {                        /* binary output */
488                          putbinary((char *)mp->base + (n_comp*comp_size)*n,
# Line 475 | Line 490 | do_reorder(const MEMLOAD *mp)
490                  }
491              }
492              if (ferror(stdout)) {
493 <                fprintf(stderr, "Error writing to stdout\n");
493 >                fputs("Error writing to stdout\n", stderr);
494                  return(0);
495              }
496          }
# Line 483 | Line 498 | do_reorder(const MEMLOAD *mp)
498                  free_records(rp);
499          return(1);
500   badspec:
501 <        fprintf(stderr, "Bad dimension(s)\n");
501 >        fputs("Bad dimension(s)\n", stderr);
502          return(0);
503   }
504  
# Line 500 | Line 515 | do_resize(FILE *fp)
515                          printf("NROWS=%d\n", no_rows);
516                  if (no_columns > 0)
517                          printf("NCOLS=%d\n", no_columns);
518 +                fputformat(fmtid, stdout);
519                  fputc('\n', stdout);
520          }
521                                                  /* sanity checks */
# Line 507 | Line 523 | do_resize(FILE *fp)
523                          (no_columns == ni_columns) & (no_rows == ni_rows))
524                  return(output_stream(fp));      /* no-op -- just copy */
525          if (no_columns <= 0) {
526 <                fprintf(stderr, "Missing -oc specification\n");
526 >                fputs("Missing -oc specification\n", stderr);
527                  return(0);
528          }
529          if ((records2go <= 0) & (no_rows > 0))
# Line 527 | Line 543 | do_resize(FILE *fp)
543                                          break;
544                                  goto done;      /* normal EOD */
545                          }
546 +                        if (check && !isfltd(word, delims)) {
547 +                                fputs("Badly formed number: ", stderr);
548 +                                fputs(word, stderr);
549 +                                fputc('\n', stderr);
550 +                                return(0);
551 +                        }
552                          fputs(word, stdout);
553                          if (n) {                /* mid-record? */
554                                  int     c = getc(fp);
# Line 537 | Line 559 | do_resize(FILE *fp)
559                          }
560                  }
561                  if (n >= 0) {
562 <                        fprintf(stderr, "Incomplete record / unexpected EOF\n");
562 >                        fputs("Incomplete record / unexpected EOF\n", stderr);
563                          return(0);
564                  }
565                  if (--columns2go <= 0) {        /* time to end output row? */
# Line 548 | Line 570 | do_resize(FILE *fp)
570          } while (--records2go);                 /* expected EOD? */
571   done:
572          if (warnings && columns2go != no_columns)
573 <                fprintf(stderr, "Warning -- incomplete final row\n");
573 >                fputs("Warning -- incomplete final row\n", stderr);
574          if (warnings && fget_word(word, fp) != NULL)
575 <                fprintf(stderr, "Warning -- characters beyond expected EOD\n");
575 >                fputs("Warning -- characters beyond expected EOD\n", stderr);
576          return(1);
577   }
578  
# Line 702 | Line 724 | main(int argc, char *argv[])
724                  SET_FILE_BINARY(stdin);
725                  SET_FILE_BINARY(stdout);
726          }
727 + #ifdef getc_unlocked                            /* avoid stupid semaphores */
728 +        flockfile(stdin);
729 +        flockfile(stdout);
730 + #endif
731                                                  /* check for no-op */
732          if (!transpose & !check & (outLevels <= 1) & (i_header == o_header) &&
733                          (no_columns == ni_columns) & (no_rows == ni_rows)) {
# Line 728 | Line 754 | main(int argc, char *argv[])
754                          newheader("RADIANCE", stdout);
755                  printargs(a, argv, stdout);
756                  printf("NCOMP=%d\n", n_comp);
731                fputformat(fmtid, stdout);
757          }
758          if (transpose | check | (outLevels > 1) || (o_header && no_rows <= 0)) {
759                  MEMLOAD myMem;                  /* need to map into memory */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines