ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/rcalc.c
(Generate patch)

Comparing ray/src/cal/rcalc.c (file contents):
Revision 1.33 by greg, Sat Mar 12 15:50:13 2022 UTC vs.
Revision 1.39 by greg, Fri Feb 23 03:47:57 2024 UTC

# Line 22 | Line 22 | static const char RCSid[] = "$Id$";
22  
23   #define isblnk(c) (igneol ? isspace(c) : ((c)==' ')|((c)=='\t'))
24  
25 < #define INBSIZ   16384  /* longest record */
26 < #define MAXCOL   32 /* number of columns recorded */
25 > #define INBSIZ  16384   /* longest record */
26 > #define MAXCOL  32      /* number of columns recorded */
27  
28 <                                /* field type specifications */
29 < #define F_NUL    0       /* empty */
30 < #define F_TYP    0x7000  /* mask for type */
31 < #define F_WID    0x0fff  /* mask for width */
32 < #define T_LIT    0x1000  /* string literal */
33 < #define T_STR    0x2000  /* string variable */
34 < #define T_NUM    0x3000  /* numeric value */
28 >                        /* field type specifications */
29 > #define F_NUL   0               /* empty */
30 > #define F_TYP   0x7000          /* mask for type */
31 > #define F_WID   0x0fff          /* mask for width */
32 > #define T_LIT   0x1000          /* string literal */
33 > #define T_STR   0x2000          /* string variable */
34 > #define T_NUM   0x3000          /* numeric value */
35  
36 < struct strvar {          /* string variable */
36 > struct strvar {         /* string variable */
37          char *name;
38          char *val;
39          char *preset;
40          struct strvar *next;
41   };
42  
43 < struct field {           /* record format structure */
44 <        int type;                /* type of field (& width) */
43 > struct field {          /* record format structure */
44 >        int type;               /* type of field (& width) */
45          union {
46 <                char *sl;                /* string literal */
47 <                struct strvar *sv;       /* string variable */
48 <                char *nv;                /* numeric variable */
49 <                EPNODE *ne;              /* numeric expression */
50 <        } f;                     /* field contents */
51 <        struct field *next;      /* next field in record */
46 >                char *sl;               /* string literal */
47 >                struct strvar *sv;      /* string variable */
48 >                char *nv;               /* numeric variable */
49 >                EPNODE *ne;             /* numeric expression */
50 >        } f;                    /* field contents */
51 >        struct field *next;     /* next field in record */
52   };
53  
54   #define savqstr(s) strcpy(emalloc(strlen(s)+1),s)
55   #define freqstr(s) efree(s)
56  
57   static int getinputrec(FILE *fp);
58 < static void scaninp(void), advinp(void), passinp(void), skipinp(void);
58 > static void scaninp(void), skipinp(void), advinp(int skip);
59   static void putrec(void), putout(void), nbsynch(void);
60   static int getrec(void);
61   static void execute(char *file);
# Line 93 | Line 93 | int nowarn = 0;                        /* non-fatal diagnostic output */
93   int unbuff = 0;                 /* unbuffered output (flush each record) */
94  
95   struct {
96 <        FILE *fin;               /* input file */
97 <        int chr;                 /* next character */
98 <        char *beg;               /* home position */
99 <        char *pos;               /* scan position */
100 <        char *end;               /* read position */
101 < } ipb;                   /* circular lookahead buffer */
96 >        FILE *fin;              /* input file */
97 >        int chr;                /* next character */
98 >        char *beg;              /* home position */
99 >        char *pos;              /* scan position */
100 >        char *end;              /* read position */
101 > } ipb;                  /* circular lookahead buffer */
102  
103  
104   int
# Line 155 | Line 155 | char *argv[]
155                          break;
156                  case 'n':
157                          noinput = 1;
158 +                        esupport &= ~E_RCONST;
159                          break;
160                  case 'i':
161                          switch (argv[i][2]) {
# Line 240 | Line 241 | eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval]
241   #ifdef getc_unlocked            /* avoid lock/unlock overhead */
242          flockfile(stdout);
243   #endif
244 <        if (noinput) {   /* produce a single output record */
244 >        if (noinput) {  /* produce a single output record */
245                  if (i < argc) {
246                          eputs(argv[0]);
247                          eputs(": file argument(s) incompatible with -n\n");
# Line 255 | Line 256 | eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval]
256                  eputs(": options -p and -P require -i and -o formats\n");
257                  quit(1);
258          }
259 <        if (blnkeq)      /* for efficiency */
259 >        if (blnkeq)     /* for efficiency */
260                  nbsynch();
261  
262 <        if (i == argc)   /* from stdin */
262 >        doptimize(1);   /* optimize definitions */
263 >
264 >        if (i == argc)  /* from stdin */
265                  execute(NULL);
266 <        else             /* from one or more files */
266 >        else            /* from one or more files */
267                  for ( ; i < argc; i++)
268                          execute(argv[i]);
269          
# Line 270 | Line 273 | eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval]
273  
274  
275   static void
276 < nbsynch(void)    /* non-blank starting synch character */
276 > nbsynch(void)   /* non-blank starting synch character */
277   {
278          if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT)
279                  return;
# Line 305 | Line 308 | FILE *fp
308  
309  
310   static void
311 < execute(         /* process a file */
311 > execute(        /* process a file */
312   char *file
313   )
314   {
# Line 348 | Line 351 | char *file
351                  colflg = 0;
352                  eclock++;
353                  if (!conditional || varvalue(condVN) > 0.0) {
354 +                        if (inpfmt != NULL)
355 +                                advinp(0);
356                          putout();
357                          ++nout;
353                        advinp();
358                  } else if (inpfmt != NULL) {
359 <                        if (passive < 0)
356 <                                passinp();
357 <                        else
358 <                                advinp();
359 >                        advinp(1);
360                  }
361                  if (incnt && nrecs >= incnt)
362                          break;
# Line 415 | Line 416 | l_in(char *funame)     /* function call for $channel */
416   }
417  
418   double
419 < chanvalue(       /* return value for column n */
419 > chanvalue(      /* return value for column n */
420   int n
421   )
422   {
# Line 454 | Line 455 | int n
455                          while (*cp && *cp++ != sepchar)
456                                  ;
457  
458 <        while (isspace(*cp))     /* some atof()'s don't like tabs */
458 >        while (isspace(*cp))    /* some atof()'s don't like tabs */
459                  cp++;
460  
461          if (n <= MAXCOL) {
# Line 466 | Line 467 | int n
467  
468  
469   void
470 < chanset(                 /* output column n */
470 > chanset(                /* output column n */
471   int n,
472   double v
473   )
# Line 482 | Line 483 | double v
483  
484  
485   void
486 < bchanset(                /* output binary channel n */
486 > bchanset(               /* output binary channel n */
487   int n,
488   double v
489   )
# Line 512 | Line 513 | double v
513  
514  
515   static void
516 < readfmt(                 /* read record format */
516 > readfmt(                /* read record format */
517   char *spec,
518   int output
519   )
# Line 526 | Line 527 | int output
527          for (inptr = spec; *inptr; inptr++)
528                  if (*inptr == '$')
529                          break;
530 <        if (*inptr)                      /* inline */
530 >        if (*inptr)                     /* inline */
531                  inptr = spec;
532 <        else {                           /* from file */
532 >        else {                          /* from file */
533                  if ((fd = open(spec, 0)) == -1) {
534                          eputs(spec);
535                          eputs(": cannot open\n");
# Line 548 | Line 549 | int output
549                  close(fd);
550                  (inptr=inpbuf+2)[res] = '\0';
551          }
552 <        f = &fmt;                        /* get fields */
552 >        f = &fmt;                       /* get fields */
553          while ((res = readfield(&inptr)) != F_NUL) {
554                  f->next = (struct field *)emalloc(sizeof(struct field));
555                  f = f->next;
# Line 580 | Line 581 | int output
581  
582  
583   static int
584 < readfield(               /* get next field in format */
584 > readfield(              /* get next field in format */
585   char **pp
586   )
587   {
# Line 647 | Line 648 | char **pp
648  
649  
650   struct strvar *
651 < getsvar(                         /* get string variable */
651 > getsvar(                        /* get string variable */
652   char *svname
653   )
654   {
# Line 666 | Line 667 | char *svname
667  
668  
669   static void
670 < svpreset(                /* preset a string variable */
670 > svpreset(               /* preset a string variable */
671   char *eqn
672   )
673   {
# Line 742 | Line 743 | getrec(void)                   /* get next record from file */
743  
744  
745   static int
746 < getfield(                        /* get next field */
746 > getfield(                       /* get next field */
747   struct field *f
748   )
749   {
750 <        static char buf[RMAXWORD+1];     /* no recursion! */
750 >        static char buf[RMAXWORD+1];    /* no recursion! */
751          int delim, inword;
752          double d;
753          char *np;
# Line 898 | Line 899 | putrec(void)                           /* output a record */
899  
900  
901   static void
902 < initinp(FILE *fp)                /* prepare lookahead buffer */
902 > initinp(FILE *fp)               /* prepare lookahead buffer */
903  
904   {
905          ipb.fin = fp;
# Line 916 | Line 917 | scaninp(void)                  /* scan next character */
917                  return;
918          if (++ipb.pos >= &inpbuf[INBSIZ])
919                  ipb.pos = inpbuf;
920 <        if (ipb.pos == ipb.end) {        /* new character */
920 >        if (ipb.pos == ipb.end) {       /* new character */
921                  if ((ipb.chr = getc(ipb.fin)) != EOF) {
922                          *ipb.end = ipb.chr;
923                          if (++ipb.end >= &inpbuf[INBSIZ])
# Line 930 | Line 931 | scaninp(void)                  /* scan next character */
931  
932  
933   static void
934 < advinp(void)                    /* move home to current position */
934 > skipinp(void)                   /* rewind position and advance 1 */
935   {
936 <        ipb.beg = ipb.pos;
936 >        if (ipb.beg == NULL)            /* can't fully rewind? */
937 >                ipb.beg = ipb.end;
938 >        ipb.pos = ipb.beg;
939 >        ipb.chr = *ipb.pos;
940 >        if (passive)                    /* transmit unmatched character? */
941 >                putchar(ipb.chr);
942 >        if (++ipb.beg >= &inpbuf[INBSIZ])
943 >                ipb.beg = inpbuf;
944 >        scaninp();
945   }
946  
947  
948   static void
949 < passinp(void)                   /* pass beginning to current position */
949 > advinp(int skip)                /* advance home to current position */
950   {
951 <        if (!passive | (ipb.beg == NULL)) {
952 <                advinp();
951 >        if (!skip | (passive >= 0)) {
952 >                ipb.beg = ipb.pos;      /* no need to copy input */
953                  return;
954          }
955 <        while (ipb.beg != ipb.pos) {    /* transfer buffer unaltered */
955 >        if (ipb.beg == NULL) {          /* buffer overflowed a bit? */
956 >                wputs("buffer overflow\n");
957 >                puts("\n*** MISSING DATA ***");
958 >                ipb.beg = ipb.end;
959 >        }
960 >        while (ipb.beg != ipb.pos) {    /* copy buffer to current */
961                  putchar(*ipb.beg);
962                  if (++ipb.beg >= &inpbuf[INBSIZ])
963                          ipb.beg = inpbuf;
# Line 951 | Line 965 | passinp(void)                  /* pass beginning to current position
965   }
966  
967  
954 static void
955 skipinp(void)                   /* rewind position and advance 1 */
956 {
957        if (ipb.beg == NULL)            /* full */
958                ipb.beg = ipb.end;
959        ipb.pos = ipb.beg;
960        ipb.chr = *ipb.pos;
961        if (passive)                    /* transmit unmatched character? */
962                putchar(ipb.chr);
963        if (++ipb.beg >= &inpbuf[INBSIZ])
964                ipb.beg = inpbuf;
965        scaninp();
966 }
967
968
968   void
969 < eputs(char *msg)
969 > eputs(const char *msg)
970   {
971          fputs(msg, stderr);
972   }
973  
974  
975   void
976 < wputs(char *msg)
976 > wputs(const char *msg)
977   {
978          if (!nowarn)
979                  eputs(msg);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines