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.39 by greg, Thu Mar 3 03:55:13 2022 UTC vs.
Revision 2.42 by greg, Wed Mar 16 19:25:25 2022 UTC

# Line 29 | Line 29 | typedef struct {
29  
30   typedef struct {
31          int     nw_rec;         /* number of words per record */
32 <        ssize_t nrecs;          /* number of records we found */
32 >        size_t  nrecs;          /* number of records we found */
33          char    *rec[1];        /* record array (extends struct) */
34   } RECINDEX;
35  
36 < int             warnings = 1;   /* report warnings? */
36 > int             warnings = 1;                   /* report warnings? */
37  
38 + char            *fmtid = NULL;                  /* format id */
39 + int             comp_size = 0;                  /* binary bytes/channel */
40 + int             n_comp = 0;                     /* components/record */
41 + int             ni_columns = 0;                 /* number of input columns */
42 + int             ni_rows = 0;                    /* number of input rows */
43 + int             no_columns = 0;                 /* number of output columns */
44 + int             no_rows = 0;                    /* number of output rows */
45 + int             transpose = 0;                  /* transpose rows & cols? */
46 + int             i_header = 1;                   /* input header? */
47 + int             o_header = 1;                   /* output header? */
48 + int             outArray[MAXLEVELS][2];         /* output block nesting */
49 + int             outLevels = 0;                  /* number of blocking levels */
50 + int             check = 0;                      /* force data check? */
51 +
52   /* free loaded file */
53   static void
54   free_load(MEMLOAD *mp)
# Line 183 | Line 197 | index_records(const MEMLOAD *mp, int nw_rec)
197                                          break;  /* got requisite # words */
198                                  do {            /* else find next word */
199                                          if (*cp == '\n') {
200 <                                                fprintf(stderr,
187 <                                                "Unexpected EOL in record!\n");
200 >                                                fputs("Unexpected EOL in record!\n", stderr);
201                                                  free_records(rp);
202                                                  return(NULL);
203                                          }
# Line 218 | Line 231 | count_columns(const RECINDEX *rp)
231  
232   /* copy nth record from index to stdout */
233   static int
234 < print_record(const RECINDEX *rp, ssize_t n)
234 > print_record(const RECINDEX *rp, size_t n)
235   {
236 <        int     words2go = rp->nw_rec;
237 <        char    *scp;
236 >        static char     delims[] = " \t\n\r\f";
237 >        int             words2go = rp->nw_rec;
238 >        char            *scp;
239  
240 <        if ((n < 0) | (n >= rp->nrecs))
240 >        if (n >= rp->nrecs)
241                  return(0);
242          scp = rp->rec[n];
243 +
244 +        if (check && !isfltd(scp, delims))
245 +                goto formerr;
246          do {
247                  putc(*scp++, stdout);
248                  if (!*scp | isspace(*scp)) {
# Line 236 | Line 253 | print_record(const RECINDEX *rp, ssize_t n)
253                                  if (++scp >= rp->rec[n+1])
254                                          break;
255                          while (!*scp | isspace(*scp));
256 +
257 +                        if (check && !isfltd(scp, delims))
258 +                                goto formerr;
259                  }
260          } while (scp < rp->rec[n+1]);
261                                                  /* caller adds record sep. */
262          return(1);
263 + formerr:
264 +        fputs("Badly formed number: ", stderr);
265 +        while (*scp && !isspace(*scp))
266 +                fputc(*scp++, stderr);
267 +        fputc('\n', stderr);
268 +        return(0);
269   }
270  
271 < /* copy a stream to stdout */
272 < static int
271 > /* copy a stream to stdout, return bytes written or 0 on error */
272 > static size_t
273   output_stream(FILE *fp)
274   {
275 +        size_t  ntot = 0;
276          char    buf[8192];
277 <        ssize_t n;
277 >        size_t  n;
278  
279          if (fp == NULL)
280                  return(0);
281          fflush(stdout);
282 <        while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
282 >        while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
283                  if (write(fileno(stdout), buf, n) != n)
284                          return(0);
285 <        return(!ferror(fp));
285 >                ntot += n;
286 >        }
287 >        return(!ferror(fp) * ntot);
288   }
289  
290   /* get next word from stream, leaving stream on EOL or start of next word */
# Line 281 | Line 310 | fget_word(char buf[256], FILE *fp)
310          return(buf);
311   }
312  
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
313   /* parse RxCx... string */
314   static int
315   get_array(const char *spec, int blklvl[][2], int nlvls)
# Line 351 | Line 366 | check_sizes()
366          if (transpose && (no_rows <= 0) & (no_columns <= 0)) {
367                  if (ni_rows > 0) no_columns = ni_rows;
368                  if (ni_columns > 0) no_rows = ni_columns;
369 <        } else if ((no_rows <= 0) & (no_columns > 0) &&
370 <                        !((ni_rows*ni_columns) % no_columns))
371 <                no_rows = ni_rows*ni_columns/no_columns;
369 >        } else {
370 >                if (no_columns <= 0)
371 >                        no_columns = ni_columns;
372 >                if ((no_rows <= 0) & (no_columns > 0) &&
373 >                                !((ni_rows*ni_columns) % no_columns))
374 >                        no_rows = ni_rows*ni_columns/no_columns;
375 >        }
376          if (n_comp <= 0)
377                  n_comp = 3;
378          return(1);
379   }
380  
381   /* call to compute block input position */
382 < static ssize_t
382 > static size_t
383   get_block_pos(int r, int c, int blklvl[][2], int nlvls)
384   {
385 <        ssize_t n = 0;
385 >        size_t  n = 0;
386  
387          while (nlvls > 1) {
388                  int     sr = r/blklvl[1][0];
# Line 379 | Line 398 | get_block_pos(int r, int c, int blklvl[][2], int nlvls
398   }
399  
400   /* return input offset based on array ordering and transpose option */
401 < static ssize_t
401 > static size_t
402   get_input_pos(int r, int c)
403   {
404 <        ssize_t n;
404 >        size_t  n;
405  
406          if (outLevels > 1) {            /* block reordering */
407                  n = get_block_pos(r, c, outArray, outLevels);
408                  if (transpose) {
409                          r = n/ni_rows;
410                          c = n - r*ni_rows;
411 <                        n = (ssize_t)c*ni_columns + r;
411 >                        n = (size_t)c*ni_columns + r;
412                  }
413          } else if (transpose)           /* transpose only */
414 <                n = (ssize_t)c*ni_columns + r;
414 >                n = (size_t)c*ni_columns + r;
415          else                            /* XXX should never happen! */
416 <                n = (ssize_t)r*no_columns + c;
416 >                n = (size_t)r*no_columns + c;
417          return(n);
418   }
419  
# Line 404 | Line 423 | do_reorder(const MEMLOAD *mp)
423   {
424          static const char       tabEOL[2] = {'\t','\n'};
425          RECINDEX                *rp = NULL;
426 <        ssize_t                 nrecords;
426 >        size_t                  nrecords;
427          int                     i, j;
428                                                  /* propogate sizes */
429          if (ni_rows <= 0)
# Line 419 | Line 438 | do_reorder(const MEMLOAD *mp)
438                          ni_columns = count_columns(rp);
439                  nrecords = rp->nrecs;
440          } else if ((ni_rows > 0) & (ni_columns > 0)) {
441 <                nrecords = (ssize_t)ni_rows*ni_columns;
441 >                nrecords = (size_t)ni_rows*ni_columns;
442                  if (nrecords > mp->len/(n_comp*comp_size)) {
443                          fputs("Input too small for specified size and type\n",
444                                          stderr);
# Line 432 | Line 451 | do_reorder(const MEMLOAD *mp)
451                  ni_rows = nrecords/ni_columns;
452          else if ((ni_columns <= 0) & (ni_rows > 0))
453                  ni_columns = nrecords/ni_rows;
454 <        if (nrecords != (ssize_t)ni_rows*ni_columns)
454 >        if (nrecords != (size_t)ni_rows*ni_columns)
455                  goto badspec;
456          if (transpose) {
457                  if (no_columns <= 0)
# Line 462 | Line 481 | do_reorder(const MEMLOAD *mp)
481                                                  /* reorder records */
482          for (i = 0; i < no_rows; i++) {
483              for (j = 0; j < no_columns; j++) {
484 <                ssize_t n = get_input_pos(i, j);
484 >                size_t  n = get_input_pos(i, j);
485                  if (n >= nrecords) {
486                          fputs("Index past end-of-file\n", stderr);
487                          return(0);
488                  }
489                  if (rp != NULL) {               /* ASCII output */
490 <                        print_record(rp, n);
490 >                        if (!print_record(rp, n))
491 >                                return(0);
492                          putc(tabEOL[j >= no_columns-1], stdout);
493                  } else {                        /* binary output */
494                          putbinary((char *)mp->base + (n_comp*comp_size)*n,
# Line 476 | Line 496 | do_reorder(const MEMLOAD *mp)
496                  }
497              }
498              if (ferror(stdout)) {
499 <                fprintf(stderr, "Error writing to stdout\n");
499 >                fputs("Error writing to stdout\n", stderr);
500                  return(0);
501              }
502          }
# Line 484 | Line 504 | do_reorder(const MEMLOAD *mp)
504                  free_records(rp);
505          return(1);
506   badspec:
507 <        fprintf(stderr, "Bad dimension(s)\n");
507 >        fputs("Bad dimension(s)\n", stderr);
508          return(0);
509   }
510  
511 < /* resize ASCII stream input by ignoring EOLs between records */
511 > /* resize stream input by ignoring EOLs between ASCII records */
512   static int
513   do_resize(FILE *fp)
514   {
515 <        ssize_t records2go = ni_rows*ni_columns;
515 >        size_t  records2go = ni_rows*ni_columns;
516          int     columns2go = no_columns;
517          char    word[256];
518                          
# Line 504 | Line 524 | do_resize(FILE *fp)
524                  fputformat(fmtid, stdout);
525                  fputc('\n', stdout);
526          }
527 <                                                /* sanity checks */
528 <        if (comp_size || !check &
529 <                        (no_columns == ni_columns) & (no_rows == ni_rows))
530 <                return(output_stream(fp));      /* no-op -- just copy */
527 >        if (comp_size) {                        /* just copy binary data? */
528 >                size_t  nwritten = output_stream(fp);
529 >                if ((no_rows > 0) & (no_columns > 0) &&
530 >                                nwritten != (size_t)no_rows*no_columns*n_comp*comp_size) {
531 >                        if (check) {
532 >                                fputs("Incorrect binary file size\n", stderr);
533 >                                return(0);      /* fatal if check is true */
534 >                        }
535 >                        if (warnings)
536 >                                fputs("Warning -- unexpected binary file size\n", stderr);
537 >                }
538 >                return(nwritten > 0);
539 >        }                                       /* else straight ASCII data copy? */
540 >        if (!check & (no_columns == ni_columns) & (no_rows == ni_rows))
541 >                return(output_stream(fp) > 0);
542 >                                                /* need to reshape (& check?) input */
543          if (no_columns <= 0) {
544 <                fprintf(stderr, "Missing -oc specification\n");
544 >                fputs("Missing -oc specification\n", stderr);
545                  return(0);
546          }
547          if ((records2go <= 0) & (no_rows > 0))
# Line 521 | Line 553 | do_resize(FILE *fp)
553                  return(0);
554          }
555          do {                                    /* reshape records */
556 <                int     n;
557 <
526 <                for (n = n_comp; n--; ) {
556 >                int     n = n_comp;
557 >                while (n--) {
558                          if (fget_word(word, fp) == NULL) {
559 <                                if (records2go > 0 || n < n_comp-1)
559 >                                if ((records2go > 0) | (n < n_comp-1))
560                                          break;
561                                  goto done;      /* normal EOD */
562                          }
563 +                        if (check && !isflt(word)) {
564 +                                fputs("Badly formed number: ", stderr);
565 +                                fputs(word, stderr);
566 +                                fputc('\n', stderr);
567 +                                return(0);
568 +                        }
569                          fputs(word, stdout);
570                          if (n) {                /* mid-record? */
571                                  int     c = getc(fp);
# Line 539 | Line 576 | do_resize(FILE *fp)
576                          }
577                  }
578                  if (n >= 0) {
579 <                        fprintf(stderr, "Incomplete record / unexpected EOF\n");
579 >                        fputs("Incomplete record / unexpected EOF\n", stderr);
580                          return(0);
581                  }
582                  if (--columns2go <= 0) {        /* time to end output row? */
# Line 549 | Line 586 | do_resize(FILE *fp)
586                          putc('\t', stdout);
587          } while (--records2go);                 /* expected EOD? */
588   done:
589 <        if (warnings && columns2go != no_columns)
590 <                fprintf(stderr, "Warning -- incomplete final row\n");
591 <        if (warnings && fget_word(word, fp) != NULL)
592 <                fprintf(stderr, "Warning -- characters beyond expected EOD\n");
589 >        if (columns2go != no_columns) {
590 >                if (check) {
591 >                        fputs("Incomplete final row\n", stderr);
592 >                        return(0);
593 >                }
594 >                if (warnings)
595 >                        fputs("Warning -- incomplete final row\n", stderr);
596 >        }
597 >        if (fget_word(word, fp) != NULL) {
598 >                if (check) {
599 >                        fputs("Characters beyond expected EOD\n", stderr);
600 >                        return(0);
601 >                }
602 >                if (warnings)
603 >                        fputs("Warning -- characters beyond expected EOD\n", stderr);
604 >        }
605          return(1);
606   }
607  
# Line 696 | Line 745 | main(int argc, char *argv[])
745                  no_columns = outArray[0][1];
746          }
747                                                  /* open input file? */
748 <        if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) {
748 >        if (a == argc-1 && freopen(argv[a], "rb", stdin) == NULL) {
749                  fprintf(stderr, "%s: cannot open for reading\n", argv[a]);
750                  return(1);
751 <        }
703 <        if (comp_size) {
751 >        } else
752                  SET_FILE_BINARY(stdin);
753 <                SET_FILE_BINARY(stdout);
754 <        }
753 >        SET_FILE_BINARY(stdout);
754 > #ifdef getc_unlocked                            /* avoid stupid semaphores */
755 >        flockfile(stdin);
756 >        flockfile(stdout);
757 > #endif
758                                                  /* check for no-op */
759          if (!transpose & !check & (outLevels <= 1) & (i_header == o_header) &&
760                          (no_columns == ni_columns) & (no_rows == ni_rows)) {
761                  if (warnings)
762                          fprintf(stderr, "%s: no-op -- copying input verbatim\n",
763                                  argv[0]);
764 <                if (!output_stream(stdin))
714 <                        return(1);
715 <                return(0);
764 >                return(!output_stream(stdin));
765          }
766          if (i_header) {                         /* read header */
767                  if (getheader(stdin, headline, NULL) < 0)
768                          return(1);
769                  if (!check_sizes())
770                          return(1);
722                if (comp_size) {                /* a little late... */
723                        SET_FILE_BINARY(stdin);
724                        SET_FILE_BINARY(stdout);
725                }
771          } else if (!check_sizes())
772                  return(1);
773          if (o_header) {                         /* write/add to header */
# Line 731 | Line 776 | main(int argc, char *argv[])
776                  printargs(a, argv, stdout);
777                  printf("NCOMP=%d\n", n_comp);
778          }
779 <        if (transpose | check | (outLevels > 1) || (o_header && no_rows <= 0)) {
779 >        if (!comp_size) {                       /* a little late... */
780 >                SET_FILE_TEXT(stdin);
781 >                SET_FILE_TEXT(stdout);
782 >        }
783 >        if (transpose | (outLevels > 1) || (o_header && no_rows <= 0)) {
784                  MEMLOAD myMem;                  /* need to map into memory */
785                  if (a == argc-1) {
786                          if (load_file(&myMem, stdin) <= 0) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines