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.4 by greg, Sun May 25 05:27:16 2003 UTC vs.
Revision 1.16 by greg, Fri Dec 10 05:52:14 2004 UTC

# Line 7 | Line 7 | static const char RCSid[] = "$Id$";
7   *     9/11/87
8   */
9  
10 #include  <stdio.h>
11
10   #include  <stdlib.h>
11 <
11 > #include  <fcntl.h>
12 > #include  <stdio.h>
13 > #include  <string.h>
14   #include  <math.h>
15
15   #include  <ctype.h>
16  
17 + #include  "platform.h"
18 + #include  "rterror.h"
19 + #include  "rtmisc.h"
20 + #include  "rtio.h"
21   #include  "calcomp.h"
22  
20 #ifdef  CPM
21 #define  getc           agetc   /* text files only, right? */
22 #endif
23
23   #define  isnum(c)       (isdigit(c) || (c)=='-' || (c)=='.' \
24                                  || (c)=='+' || (c)=='e' || (c)=='E')
25  
26   #define  isblnk(c)      (igneol ? isspace(c) : (c)==' '||(c)=='\t')
27  
28 < #define  INBSIZ         4096    /* longest record */
28 > #define  INBSIZ         16384   /* longest record */
29   #define  MAXCOL         32      /* number of columns recorded */
30  
31                                  /* field type specifications */
# Line 58 | Line 57 | struct field {                  /* record format struc
57   #define  savqstr(s)     strcpy(emalloc(strlen(s)+1),s)
58   #define  freqstr(s)     efree(s)
59  
60 < extern char  *strcpy(), *emalloc(), *savestr();
61 < struct strvar  *getsvar();
60 > static int getinputrec(FILE *fp);
61 > static void scaninp(void), advinp(void), resetinp(void);
62 > static void putrec(void), putout(void), nbsynch(void);
63 > static int getrec(void);
64 > static void execute(char *file);
65 > static void initinp(FILE *fp);
66 > static void svpreset(char *eqn);
67 > static void readfmt(char *spec, int output);
68 > static int readfield(char **pp);
69 > static int getfield(struct field *f);
70 > static void chanset(int n, double v);
71 > static void bchanset(int n, double v);
72 > static struct strvar* getsvar(char *svname);
73 > static double l_in(char *);
74  
75   struct field  *inpfmt = NULL;   /* input record format */
76   struct field  *outfmt = NULL;   /* output record structure */
# Line 67 | Line 78 | struct strvar  *svhead = NULL;  /* string variables */
78  
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? */
84   int  nbicols = 0;               /* number of binary input columns */
# Line 88 | Line 100 | struct {
100   } ipb;                          /* circular lookahead buffer */
101  
102  
103 < main(argc, argv)
104 < int  argc;
105 < char  *argv[];
103 > int
104 > main(
105 > int  argc,
106 > char  *argv[]
107 > )
108   {
109          int  i;
110  
# Line 101 | Line 115 | char  *argv[];
115          biggerlib();
116   #endif
117          varset("PI", ':', 3.14159265358979323846);
118 +        funset("in", 1, '=', &l_in);
119  
120          for (i = 1; i < argc && argv[i][0] == '-'; i++)
121                  switch (argv[i][1]) {
# Line 110 | Line 125 | char  *argv[];
125                  case 'l':
126                          igneol = !igneol;
127                          break;
128 +                case 'p':
129 +                        passive = !passive;
130 +                        break;
131                  case 't':
132                          sepchar = argv[i][2];
133                          break;
# Line 139 | Line 157 | char  *argv[];
157                                          nbicols = atoi(argv[i]+3);
158                                  else
159                                          nbicols = 1;
160 +                                if (nbicols*sizeof(double) > INBSIZ) {
161 +                                        eputs(argv[0]);
162 +                                        eputs(": too many input columns\n");
163 +                                        quit(1);
164 +                                }
165                                  break;
166                          case 'f':
167                                  if (isdigit(argv[i][3]))
168                                          nbicols = -atoi(argv[i]+3);
169                                  else
170                                          nbicols = -1;
171 +                                if (-nbicols*sizeof(float) > INBSIZ) {
172 +                                        eputs(argv[0]);
173 +                                        eputs(": too many input columns\n");
174 +                                        quit(1);
175 +                                }
176                                  break;
177                          default:
178                                  goto userr;
# Line 177 | Line 205 | char  *argv[];
205                  userr:
206                          eputs("Usage: ");
207                          eputs(argv[0]);
208 < eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
208 > eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
209                          quit(1);
210                  }
211  
# Line 197 | Line 225 | eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e exp
225                          execute(argv[i]);
226          
227          quit(0);
228 +        return 0; /* pro forma return */
229   }
230  
231  
232 < nbsynch()               /* non-blank starting synch character */
232 > static void
233 > nbsynch(void)               /* non-blank starting synch character */
234   {
235          if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT)
236                  return;
# Line 211 | Line 241 | nbsynch()               /* non-blank starting synch ch
241   }
242  
243  
244 < int
245 < getinputrec(fp)         /* get next input record */
246 < FILE  *fp;
244 > static int
245 > getinputrec(            /* get next input record */
246 > FILE  *fp
247 > )
248   {
249          if (inpfmt != NULL)
250                  return(getrec());
# Line 227 | Line 258 | FILE  *fp;
258   }
259  
260  
261 < execute(file)           /* process a file */
262 < char  *file;
261 > static void
262 > execute(           /* process a file */
263 > char  *file
264 > )
265   {
266          int  conditional = vardefined("cond");
267          long  nrecs = 0;
# Line 258 | Line 291 | char  *file;
291   }
292  
293  
294 < putout()                /* produce an output record */
294 > static void
295 > putout(void)                /* produce an output record */
296   {
263        extern void  chanset(), bchanset();
297  
298          colpos = 0;
299          if (outfmt != NULL)
# Line 276 | Line 309 | putout()                /* produce an output record */
309   }
310  
311  
312 + static double
313 + l_in(char *funame)      /* function call for $channel */
314 + {
315 +        int  n;
316 +        register char  *cp;
317 +                        /* get argument as integer */
318 +        n = (int)(argument(1) + .5);
319 +        if (n != 0)     /* return channel value */
320 +                return(chanvalue(n));
321 +                        /* determine number of channels */
322 +        if (noinput || inpfmt != NULL)
323 +                return(0);
324 +        if (nbicols > 0)
325 +                return(nbicols);
326 +        if (nbicols < 0)
327 +                return(-nbicols);
328 +        cp = inpbuf;    /* need to count */
329 +        for (n = 0; *cp; )
330 +                if (blnkeq && isspace(sepchar)) {
331 +                        while (isspace(*cp))
332 +                                cp++;
333 +                        n += *cp != '\0';
334 +                        while (*cp && !isspace(*cp))
335 +                                cp++;
336 +                } else {
337 +                        n += *cp != '\n';
338 +                        while (*cp && *cp++ != sepchar)
339 +                                ;
340 +                }
341 +        return(n);
342 + }
343 +
344   double
345 < chanvalue(n)            /* return value for column n */
346 < int  n;
345 > chanvalue(            /* return value for column n */
346 > int  n
347 > )
348   {
349          int  i;
350          register char  *cp;
# Line 329 | Line 395 | int  n;
395  
396  
397   void
398 < chanset(n, v)                   /* output column n */
399 < int  n;
400 < double  v;
398 > chanset(                   /* output column n */
399 > int  n,
400 > double  v
401 > )
402   {
403          if (colpos == 0)                /* no leading separator */
404                  colpos = 1;
# Line 344 | Line 411 | double  v;
411  
412  
413   void
414 < bchanset(n, v)                   /* output binary channel n */
415 < int  n;
416 < double  v;
414 > bchanset(                   /* output binary channel n */
415 > int  n,
416 > double  v
417 > )
418   {
419          static char     zerobuf[sizeof(double)];
420  
# Line 363 | Line 431 | double  v;
431   }
432  
433  
434 < readfmt(spec, output)                   /* read record format */
435 < char  *spec;
436 < int  output;
434 > static void
435 > readfmt(                   /* read record format */
436 > char  *spec,
437 > int  output
438 > )
439   {
440          int  fd;
441          char  *inptr;
# Line 384 | Line 454 | int  output;
454                          eputs(": cannot open\n");
455                          quit(1);
456                  }
457 <                res = read(fd, inpbuf+1, INBSIZ-1);
457 >                res = read(fd, inpbuf+2, INBSIZ-2);
458                  if (res <= 0 || res >= INBSIZ-1) {
459                          eputs(spec);
460                          if (res < 0)
# Line 396 | Line 466 | int  output;
466                          quit(1);
467                  }
468                  close(fd);
469 <                (inptr=inpbuf+1)[res] = '\0';
469 >                (inptr=inpbuf+2)[res] = '\0';
470          }
471          f = &fmt;                               /* get fields */
472          while ((res = readfield(&inptr)) != F_NUL) {
# Line 429 | Line 499 | int  output;
499   }
500  
501  
502 < int
503 < readfield(pp)                   /* get next field in format */
504 < register char  **pp;
502 > static int
503 > readfield(                   /* get next field in format */
504 > register char  **pp
505 > )
506   {
507          int  type = F_NUL;
508          int  width = 0;
# Line 496 | Line 567 | register char  **pp;
567  
568  
569   struct strvar *
570 < getsvar(svname)                         /* get string variable */
571 < char  *svname;
570 > getsvar(                         /* get string variable */
571 > char  *svname
572 > )
573   {
574          register struct strvar  *sv;
575          
# Line 513 | Line 585 | char  *svname;
585   }
586  
587  
588 < svpreset(eqn)                    /* preset a string variable */
589 < char  *eqn;
588 > static void
589 > svpreset(                    /* preset a string variable */
590 > char  *eqn
591 > )
592   {
593          register struct strvar  *sv;
594          register char  *val;
# Line 533 | Line 607 | char  *eqn;
607   }
608  
609  
610 < clearrec()                      /* clear input record variables */
610 > static void
611 > clearrec(void)                  /* clear input record variables */
612   {
613          register struct field  *f;
614  
# Line 552 | Line 627 | clearrec()                     /* clear input record variables */
627   }
628  
629  
630 < getrec()                                /* get next record from file */
630 > static int
631 > getrec(void)                            /* get next record from file */
632   {
633          int  eatline;
634          register struct field  *f;
635 <        
635 >
636          while (ipb.chr != EOF) {
561                eatline = !igneol && ipb.chr != '\n';
637                  if (blnkeq)             /* beware of nbsynch() */
638                          while (isblnk(ipb.chr))
639 <                                scaninp();
639 >                                resetinp();
640 >                eatline = (!igneol && ipb.chr != '\n');
641                  clearrec();             /* start with fresh record */
642                  for (f = inpfmt; f != NULL; f = f->next)
643                          if (getfield(f) == -1)
644                                  break;
645                  if (f == NULL) {
646 <                        advinp();
646 >                        advinp();       /* got one! */
647                          return(1);
648                  }
649 <                resetinp();
649 >                resetinp();             /* eat false start */
650                  if (eatline) {          /* eat rest of line */
651                          while (ipb.chr != '\n') {
652                                  if (ipb.chr == EOF)
653                                          return(0);
654 <                                scaninp();
654 >                                resetinp();
655                          }
656 <                        scaninp();
581 <                        advinp();
656 >                        resetinp();
657                  }
658          }
659          return(0);
660   }
661  
662  
663 < getfield(f)                             /* get next field */
664 < register struct field  *f;
663 > static int
664 > getfield(                             /* get next field */
665 > register struct field  *f
666 > )
667   {
668 <        static char  buf[MAXWORD+1];            /* no recursion! */
668 >        static char  buf[RMAXWORD+1];            /* no recursion! */
669          int  delim, inword;
670          double  d;
671          char  *np;
# Line 621 | Line 698 | register struct field  *f;
698                          delim = f->next->f.sl[0];
699                  cp = buf;
700                  do {
701 <                        if (ipb.chr == EOF)
701 >                        if (ipb.chr == EOF || ipb.chr == '\n')
702                                  inword = 0;
703                          else if (blnkeq && delim != EOF)
704                                  inword = isblnk(delim) ?
# Line 633 | Line 710 | register struct field  *f;
710                                  *cp++ = ipb.chr;
711                                  scaninp();
712                          }
713 <                } while (inword && cp < &buf[MAXWORD]);
713 >                } while (inword && cp < &buf[RMAXWORD]);
714                  *cp = '\0';
715                  if (f->f.sv->val == NULL)
716                          f->f.sv->val = savqstr(buf);    /* first setting */
# Line 662 | Line 739 | register struct field  *f;
739                                  *cp++ = ipb.chr;
740                                  scaninp();
741                          }
742 <                } while (inword && cp < &buf[MAXWORD]);
742 >                } while (inword && cp < &buf[RMAXWORD]);
743                  *cp = '\0';
744                  d = np==NULL ? 0. : atof(np);
745                  if (!vardefined(f->f.nv))
# Line 672 | Line 749 | register struct field  *f;
749                          return(-1);                     /* doesn't match! */
750                  return(0);
751          }
752 +        return -1; /* pro forma return */
753   }
754  
755  
756 < putrec()                                /* output a record */
756 > static void
757 > putrec(void)                                /* output a record */
758   {
759          char  fmt[32];
760          register int  n;
# Line 725 | Line 804 | putrec()                                /* output a re
804   }
805  
806  
807 < initinp(fp)                     /* prepare lookahead buffer */
808 < FILE  *fp;
807 > static void
808 > initinp(FILE  *fp)                     /* prepare lookahead buffer */
809 >
810   {
811          ipb.fin = fp;
812          ipb.beg = ipb.end = inpbuf;
# Line 736 | Line 816 | FILE  *fp;
816   }
817  
818  
819 < scaninp()                       /* scan next character */
819 > static void
820 > scaninp(void)                       /* scan next character */
821   {
822          if (ipb.chr == EOF)
823                  return;
# Line 755 | Line 836 | scaninp()                       /* scan next character
836   }
837  
838  
839 < advinp()                        /* move home to current position */
839 > static void
840 > advinp(void)                        /* move home to current position */
841   {
842          ipb.beg = ipb.pos;
843   }
844  
845  
846 < resetinp()                      /* rewind position and advance 1 */
846 > static void
847 > resetinp(void)                      /* rewind position and advance 1 */
848   {
849          if (ipb.beg == NULL)            /* full */
850                  ipb.beg = ipb.end;
851          ipb.pos = ipb.beg;
852          ipb.chr = *ipb.pos;
853 +        if (passive)                    /* transmit unmatched character? */
854 +                fputc(ipb.chr, stdout);
855          if (++ipb.beg >= &inpbuf[INBSIZ])
856                  ipb.beg = inpbuf;
857          scaninp();
# Line 774 | Line 859 | resetinp()                      /* rewind position and
859  
860  
861   void
862 < eputs(msg)
778 < char  *msg;
862 > eputs(char  *msg)
863   {
864          fputs(msg, stderr);
865   }
866  
867  
868   void
869 < wputs(msg)
786 < char  *msg;
869 > wputs(char  *msg)
870   {
871          if (!nowarn)
872                  eputs(msg);
# Line 791 | Line 874 | char  *msg;
874  
875  
876   void
877 < quit(code)
795 < int  code;
877 > quit(int  code)
878   {
879          exit(code);
880   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines