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.31 by greg, Mon Feb 21 23:00:55 2022 UTC vs.
Revision 1.38 by greg, Fri Mar 10 17:38:01 2023 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), 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 69 | Line 69 | static void bchanset(int n, double v);
69   static struct strvar* getsvar(char *svname);
70   static double l_in(char *);
71  
72 < struct field *inpfmt = NULL; /* input record format */
73 < struct field *outfmt = NULL; /* output record structure */
74 < struct strvar *svhead = NULL; /* string variables */
72 > struct field *inpfmt = NULL;    /* input record format */
73 > struct field *outfmt = NULL;    /* output record structure */
74 > struct strvar *svhead = NULL;   /* string variables */
75  
76 < long incnt = 0;         /* limit number of input records? */
76 > long incnt = 0;                 /* limit number of input records? */
77   long outcnt = 0;                /* limit number of output records? */
78  
79 < int blnkeq = 1;         /* blanks compare equal? */
80 < int igneol = 0;         /* ignore end of line? */
79 > int blnkeq = 1;                 /* blanks compare equal? */
80 > int igneol = 0;                 /* ignore end of line? */
81   int passive = 0;                /* passive mode (transmit unmatched input) */
82 < char sepchar = '\t';     /* input/output separator */
83 < int noinput = 0;         /* no input records? */
82 > char sepchar = '\t';            /* input/output separator */
83 > int noinput = 0;                /* no input records? */
84   int itype = 'a';                /* input type (a/f/F/d/D) */
85   int nbicols = 0;                /* number of binary input columns */
86   int otype = 'a';                /* output format (a/f/F/d/D) */
87 < char inpbuf[INBSIZ];     /* input buffer */
88 < double colval[MAXCOL];   /* input column values */
89 < unsigned long colflg = 0; /* column retrieved flags */
90 < int colpos;              /* output column position */
87 > char inpbuf[INBSIZ];            /* input buffer */
88 > double colval[MAXCOL];          /* input column values */
89 > unsigned long colflg = 0;       /* column retrieved flags */
90 > int colpos;                     /* output column position */
91  
92 < int nowarn = 0;         /* non-fatal diagnostic output */
93 < int unbuff = 0;         /* unbuffered output (flush each record) */
92 > 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 128 | Line 128 | char *argv[]
128                          igneol = !igneol;
129                          break;
130                  case 'p':
131 <                        passive = !passive;
131 >                        passive = 1;
132                          break;
133 +                case 'P':
134 +                        passive = -1;
135 +                        break;
136                  case 't':
137                          sepchar = argv[i][2];
138                          break;
# Line 152 | 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 229 | Line 233 | char *argv[]
233                  userr:
234                          eputs("Usage: ");
235                          eputs(argv[0]);
236 < eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
236 > eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
237                          quit(1);
238                  }
239          if (otype != 'a')
# Line 237 | Line 241 | eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e
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 247 | Line 251 | eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e
251                  putout();
252                  quit(0);
253          }
254 <        if (blnkeq)      /* for efficiency */
254 >        if (passive && (inpfmt == NULL) | (outfmt == NULL)) {
255 >                eputs(argv[0]);
256 >                eputs(": options -p and -P require -i and -o formats\n");
257 >                quit(1);
258 >        }
259 >        if (blnkeq)     /* for efficiency */
260                  nbsynch();
261  
262 <        if (i == argc)   /* from stdin */
262 >        if (i == argc)  /* from stdin */
263                  execute(NULL);
264 <        else             /* from one or more files */
264 >        else            /* from one or more files */
265                  for ( ; i < argc; i++)
266                          execute(argv[i]);
267          
# Line 262 | Line 271 | eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e
271  
272  
273   static void
274 < nbsynch(void)    /* non-blank starting synch character */
274 > nbsynch(void)   /* non-blank starting synch character */
275   {
276          if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT)
277                  return;
# Line 278 | Line 287 | getinputrec(           /* get next input record */
287   FILE *fp
288   )
289   {
281        if (inpfmt != NULL)
282                return(getrec());
290          if ((itype == 'd') | (itype == 'D')) {
291                  if (getbinary(inpbuf, sizeof(double), nbicols, fp) != nbicols)
292                          return(0);
# Line 299 | Line 306 | FILE *fp
306  
307  
308   static void
309 < execute(         /* process a file */
309 > execute(        /* process a file */
310   char *file
311   )
312   {
313 <        const int conditional = vardefined("cond");
314 <        const int set_recno = (varlookup("recno") != NULL);
315 <        const int set_outno = (varlookup("outno") != NULL);
316 <        long nrecs = 0;
317 <        long nout = 0;
318 <        FILE *fp;
313 >        static char     condVN[] = "cond";
314 >        static char     recnoVN[] = "recno";
315 >        static char     outnoVN[] = "outno";
316 >        const int       conditional = vardefined(condVN);
317 >        const int       set_recno = (varlookup(recnoVN) != NULL);
318 >        const int       set_outno = (varlookup(outnoVN) != NULL);
319 >        long            nrecs = 0;
320 >        long            nout = 0;
321 >        FILE            *fp;
322          
323          if (file == NULL)
324                  fp = stdin;
# Line 322 | Line 332 | char *file
332   #ifdef getc_unlocked            /* avoid lock/unlock overhead */
333          flockfile(fp);
334   #endif
335 +        if (conditional == ':') {
336 +                eputs(condVN);
337 +                eputs(": defined as constant\n");
338 +                quit(1);
339 +        }
340          if (inpfmt != NULL)
341                  initinp(fp);
342          
343 <        while (getinputrec(fp)) {
343 >        while (inpfmt != NULL ? getrec() : getinputrec(fp)) {
344                  ++nrecs;
345                  if (set_recno)
346 <                        varset("recno", '=', (double)nrecs);
346 >                        varset(recnoVN, '=', (double)nrecs);
347                  if (set_outno)
348 <                        varset("outno", '=', (double)(nout+1));
348 >                        varset(outnoVN, '=', (double)(nout+1));
349                  colflg = 0;
350                  eclock++;
351 <                if (!conditional || varvalue("cond") > 0.0) {
351 >                if (!conditional || varvalue(condVN) > 0.0) {
352 >                        if (inpfmt != NULL)
353 >                                advinp(0);
354                          putout();
355                          ++nout;
356 +                } else if (inpfmt != NULL) {
357 +                        advinp(1);
358                  }
359                  if (incnt && nrecs >= incnt)
360                          break;
# Line 395 | Line 414 | l_in(char *funame)     /* function call for $channel */
414   }
415  
416   double
417 < chanvalue(       /* return value for column n */
417 > chanvalue(      /* return value for column n */
418   int n
419   )
420   {
# Line 434 | Line 453 | int n
453                          while (*cp && *cp++ != sepchar)
454                                  ;
455  
456 <        while (isspace(*cp))     /* some atof()'s don't like tabs */
456 >        while (isspace(*cp))    /* some atof()'s don't like tabs */
457                  cp++;
458  
459          if (n <= MAXCOL) {
# Line 446 | Line 465 | int n
465  
466  
467   void
468 < chanset(                 /* output column n */
468 > chanset(                /* output column n */
469   int n,
470   double v
471   )
# Line 462 | Line 481 | double v
481  
482  
483   void
484 < bchanset(                /* output binary channel n */
484 > bchanset(               /* output binary channel n */
485   int n,
486   double v
487   )
# Line 492 | Line 511 | double v
511  
512  
513   static void
514 < readfmt(                 /* read record format */
514 > readfmt(                /* read record format */
515   char *spec,
516   int output
517   )
# Line 506 | Line 525 | int output
525          for (inptr = spec; *inptr; inptr++)
526                  if (*inptr == '$')
527                          break;
528 <        if (*inptr)                      /* inline */
528 >        if (*inptr)                     /* inline */
529                  inptr = spec;
530 <        else {                           /* from file */
530 >        else {                          /* from file */
531                  if ((fd = open(spec, 0)) == -1) {
532                          eputs(spec);
533                          eputs(": cannot open\n");
# Line 528 | Line 547 | int output
547                  close(fd);
548                  (inptr=inpbuf+2)[res] = '\0';
549          }
550 <        f = &fmt;                        /* get fields */
550 >        f = &fmt;                       /* get fields */
551          while ((res = readfield(&inptr)) != F_NUL) {
552                  f->next = (struct field *)emalloc(sizeof(struct field));
553                  f = f->next;
# Line 560 | Line 579 | int output
579  
580  
581   static int
582 < readfield(               /* get next field in format */
582 > readfield(              /* get next field in format */
583   char **pp
584   )
585   {
# Line 627 | Line 646 | char **pp
646  
647  
648   struct strvar *
649 < getsvar(                         /* get string variable */
649 > getsvar(                        /* get string variable */
650   char *svname
651   )
652   {
# Line 646 | Line 665 | char *svname
665  
666  
667   static void
668 < svpreset(                /* preset a string variable */
668 > svpreset(               /* preset a string variable */
669   char *eqn
670   )
671   {
# Line 688 | Line 707 | clearrec(void)                 /* clear input record variables */
707  
708  
709   static int
710 < getrec(void)                            /* get next record from file */
710 > getrec(void)                    /* get next record from file */
711   {
712          int eatline;
713          struct field *f;
# Line 705 | Line 724 | getrec(void)                           /* get next record from file */
724                  for (f = inpfmt; f != NULL; f = f->next)
725                          if (!getfield(f))
726                                  break;
727 <                if (f == NULL) {
709 <                        advinp(); /* got one! */
727 >                if (f == NULL)          /* got one? */
728                          return(1);
729 <                }
730 <                skipinp();              /* eat false start */
713 <                if (eatline) {   /* eat rest of line */
729 >                skipinp();              /* else eat false start */
730 >                if (eatline) {          /* eat rest of line */
731                          while (ipb.chr != '\n') {
732                                  if (ipb.chr == EOF)
733                                          return(0);
# Line 724 | Line 741 | getrec(void)                           /* get next record from file */
741  
742  
743   static int
744 < getfield(                        /* get next field */
744 > getfield(                       /* get next field */
745   struct field *f
746   )
747   {
748 <        static char buf[RMAXWORD+1];     /* no recursion! */
748 >        static char buf[RMAXWORD+1];    /* no recursion! */
749          int delim, inword;
750          double d;
751          char *np;
# Line 880 | Line 897 | putrec(void)                           /* output a record */
897  
898  
899   static void
900 < initinp(FILE *fp)                /* prepare lookahead buffer */
900 > initinp(FILE *fp)               /* prepare lookahead buffer */
901  
902   {
903          ipb.fin = fp;
# Line 898 | Line 915 | scaninp(void)                  /* scan next character */
915                  return;
916          if (++ipb.pos >= &inpbuf[INBSIZ])
917                  ipb.pos = inpbuf;
918 <        if (ipb.pos == ipb.end) {        /* new character */
918 >        if (ipb.pos == ipb.end) {       /* new character */
919                  if ((ipb.chr = getc(ipb.fin)) != EOF) {
920                          *ipb.end = ipb.chr;
921                          if (++ipb.end >= &inpbuf[INBSIZ])
# Line 912 | Line 929 | scaninp(void)                  /* scan next character */
929  
930  
931   static void
932 < advinp(void)                    /* move home to current position */
932 > skipinp(void)                   /* rewind position and advance 1 */
933   {
934 <        ipb.beg = ipb.pos;
918 < }
919 <
920 <
921 < static void
922 < skipinp(void)            /* rewind position and advance 1 */
923 < {
924 <        if (ipb.beg == NULL)    /* full */
934 >        if (ipb.beg == NULL)            /* can't fully rewind? */
935                  ipb.beg = ipb.end;
936          ipb.pos = ipb.beg;
937          ipb.chr = *ipb.pos;
# Line 933 | Line 943 | skipinp(void)           /* rewind position and advance 1 */
943   }
944  
945  
946 + static void
947 + advinp(int skip)                /* advance home to current position */
948 + {
949 +        if (!skip | (passive >= 0)) {
950 +                ipb.beg = ipb.pos;      /* no need to copy input */
951 +                return;
952 +        }
953 +        if (ipb.beg == NULL) {          /* buffer overflowed a bit? */
954 +                wputs("buffer overflow\n");
955 +                puts("\n*** MISSING DATA ***");
956 +                ipb.beg = ipb.end;
957 +        }
958 +        while (ipb.beg != ipb.pos) {    /* copy buffer to current */
959 +                putchar(*ipb.beg);
960 +                if (++ipb.beg >= &inpbuf[INBSIZ])
961 +                        ipb.beg = inpbuf;
962 +        }
963 + }
964 +
965 +
966   void
967 < eputs(char *msg)
967 > eputs(const char *msg)
968   {
969          fputs(msg, stderr);
970   }
971  
972  
973   void
974 < wputs(char *msg)
974 > wputs(const char *msg)
975   {
976          if (!nowarn)
977                  eputs(msg);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines