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.1 by greg, Sat Feb 22 02:07:20 2003 UTC vs.
Revision 1.14 by greg, Mon Oct 11 10:02:15 2004 UTC

# Line 1 | Line 1
1   #ifndef lint
2 < static const char       RCSid[] = "$Id$";
2 > static const char RCSid[] = "$Id$";
3   #endif
4   /*
5   *  rcalc.c - record calculator program.
# 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  
# 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 */
85 + int  bocols = 0;                /* produce binary output columns */
86   char  inpbuf[INBSIZ];           /* input buffer */
87   double  colval[MAXCOL];         /* input column values */
88   unsigned long  colflg = 0;      /* column retrieved flags */
# Line 86 | 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  
111 <        esupport |= (E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST);
111 >        esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST;
112 >        esupport &= ~(E_REDEFW);
113  
114   #ifdef  BIGGERLIB
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 107 | 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 123 | Line 144 | char  *argv[];
144                          noinput = 1;
145                          break;
146                  case 'i':
147 <                        readfmt(argv[++i], 0);
147 >                        switch (argv[i][2]) {
148 >                        case '\0':
149 >                                nbicols = 0;
150 >                                readfmt(argv[++i], 0);
151 >                                break;
152 >                        case 'a':
153 >                                nbicols = 0;
154 >                                break;
155 >                        case 'd':
156 >                                if (isdigit(argv[i][3]))
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;
179 >                        }
180                          break;
181                  case 'o':
182 <                        readfmt(argv[++i], 1);
182 >                        switch (argv[i][2]) {
183 >                        case '\0':
184 >                                bocols = 0;
185 >                                readfmt(argv[++i], 1);
186 >                                break;
187 >                        case 'a':
188 >                                bocols = 0;
189 >                                break;
190 >                        case 'd':
191 >                                bocols = 1;
192 >                                break;
193 >                        case 'f':
194 >                                bocols = -1;
195 >                                break;
196 >                        }
197                          break;
198                  case 'w':
199                          nowarn = !nowarn;
# Line 134 | Line 201 | char  *argv[];
201                  case 'u':
202                          unbuff = !unbuff;
203                          break;
204 <                default:
204 >                default:;
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 157 | 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 171 | Line 241 | nbsynch()               /* non-blank starting synch ch
241   }
242  
243  
244 < execute(file)           /* process a file */
245 < char  *file;
244 > static int
245 > getinputrec(            /* get next input record */
246 > FILE  *fp
247 > )
248   {
249 +        if (inpfmt != NULL)
250 +                return(getrec());
251 +        if (nbicols > 0)
252 +                return(fread(inpbuf, sizeof(double),
253 +                                        nbicols, fp) == nbicols);
254 +        if (nbicols < 0)
255 +                return(fread(inpbuf, sizeof(float),
256 +                                        -nbicols, fp) == -nbicols);
257 +        return(fgets(inpbuf, INBSIZ, fp) != NULL);
258 + }
259 +
260 +
261 + static void
262 + execute(           /* process a file */
263 + char  *file
264 + )
265 + {
266          int  conditional = vardefined("cond");
267          long  nrecs = 0;
268          long  nout = 0;
# Line 188 | Line 277 | char  *file;
277          }
278          if (inpfmt != NULL)
279                  initinp(fp);
280 <                
281 <        while (inpfmt != NULL ? getrec() : fgets(inpbuf, INBSIZ, fp) != NULL) {
280 >        
281 >        while (getinputrec(fp)) {
282                  varset("recno", '=', (double)++nrecs);
283                  colflg = 0;
284                  eclock++;
# Line 202 | Line 291 | char  *file;
291   }
292  
293  
294 < putout()                /* produce an output record */
294 > static void
295 > putout(void)                /* produce an output record */
296   {
207        extern int  chanset();
297  
298          colpos = 0;
299          if (outfmt != NULL)
300                  putrec();
301 +        else if (bocols)
302 +                chanout(bchanset);
303          else
304                  chanout(chanset);
305 <        if (colpos)
305 >        if (colpos && !bocols)
306                  putchar('\n');
307          if (unbuff)
308                  fflush(stdout);
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 233 | Line 357 | int  n;
357                  eputs("illegal channel number\n");
358                  quit(1);
359          }
360 +        if (nbicols > 0) {
361 +                if (n > nbicols)
362 +                        return(0.0);
363 +                cp = inpbuf + (n-1)*sizeof(double);
364 +                return(*(double *)cp);
365 +        }
366 +        if (nbicols < 0) {
367 +                if (n > -nbicols)
368 +                        return(0.0);
369 +                cp = inpbuf + (n-1)*sizeof(float);
370 +                return(*(float *)cp);
371 +        }
372          if (n <= MAXCOL && colflg & 1L<<(n-1))
373                  return(colval[n-1]);
374  
# Line 258 | Line 394 | int  n;
394   }
395  
396  
397 < chanset(n, v)                   /* output column n */
398 < int  n;
399 < double  v;
397 > void
398 > chanset(                   /* output column n */
399 > int  n,
400 > double  v
401 > )
402   {
403          if (colpos == 0)                /* no leading separator */
404                  colpos = 1;
# Line 272 | Line 410 | double  v;
410   }
411  
412  
413 < readfmt(spec, output)                   /* read record format */
414 < char  *spec;
415 < int  output;
413 > void
414 > bchanset(                   /* output binary channel n */
415 > int  n,
416 > double  v
417 > )
418   {
419 +        static char     zerobuf[sizeof(double)];
420 +
421 +        while (++colpos < n)
422 +                fwrite(zerobuf,
423 +                        bocols>0 ? sizeof(double) : sizeof(float),
424 +                        1, stdout);
425 +        if (bocols > 0)
426 +                fwrite(&v, sizeof(double), 1, stdout);
427 +        else {
428 +                float   fval = v;
429 +                fwrite(&fval, sizeof(float), 1, stdout);
430 +        }
431 + }
432 +
433 +
434 + static void
435 + readfmt(                   /* read record format */
436 + char  *spec,
437 + int  output
438 + )
439 + {
440          int  fd;
441          char  *inptr;
442          struct field  fmt;
# Line 293 | 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 305 | 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 338 | 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 405 | 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 422 | 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 442 | 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 461 | 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) {
637                  eatline = !igneol && ipb.chr != '\n';
638                  if (blnkeq)             /* beware of nbsynch() */
639                          while (isblnk(ipb.chr))
640 <                                scaninp();
640 >                                resetinp();
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();
490 <                        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 530 | 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 542 | 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 571 | 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 581 | 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 634 | 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 645 | 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 664 | 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 683 | Line 859 | resetinp()                      /* rewind position and
859  
860  
861   void
862 < eputs(msg)
687 < char  *msg;
862 > eputs(char  *msg)
863   {
864          fputs(msg, stderr);
865   }
866  
867  
868   void
869 < wputs(msg)
695 < char  *msg;
869 > wputs(char  *msg)
870   {
871          if (!nowarn)
872                  eputs(msg);
# Line 700 | Line 874 | char  *msg;
874  
875  
876   void
877 < quit(code)
704 < int  code;
877 > quit(int  code)
878   {
879          exit(code);
880   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines