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.22 by greg, Thu Dec 19 16:38:12 2013 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>
13
11   #include  <math.h>
15
12   #include  <ctype.h>
13  
14 + #include  "platform.h"
15 + #include  "rterror.h"
16 + #include  "rtmisc.h"
17 + #include  "rtio.h"
18   #include  "calcomp.h"
19  
20 #ifdef  CPM
21 #define  getc           agetc   /* text files only, right? */
22 #endif
23
20   #define  isnum(c)       (isdigit(c) || (c)=='-' || (c)=='.' \
21                                  || (c)=='+' || (c)=='e' || (c)=='E')
22  
23   #define  isblnk(c)      (igneol ? isspace(c) : (c)==' '||(c)=='\t')
24  
25 < #define  INBSIZ         4096    /* longest record */
25 > #define  INBSIZ         16384   /* longest record */
26   #define  MAXCOL         32      /* number of columns recorded */
27  
28                                  /* field type specifications */
# Line 58 | Line 54 | struct field {                  /* record format struc
54   #define  savqstr(s)     strcpy(emalloc(strlen(s)+1),s)
55   #define  freqstr(s)     efree(s)
56  
57 < extern char  *strcpy(), *emalloc(), *savestr();
58 < struct strvar  *getsvar();
57 > static int getinputrec(FILE *fp);
58 > static void scaninp(void), advinp(void), resetinp(void);
59 > static void putrec(void), putout(void), nbsynch(void);
60 > static int getrec(void);
61 > static void execute(char *file);
62 > static void initinp(FILE *fp);
63 > static void svpreset(char *eqn);
64 > static void readfmt(char *spec, int output);
65 > static int readfield(char **pp);
66 > static int getfield(struct field *f);
67 > static void chanset(int n, double v);
68 > 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 */
# Line 67 | Line 75 | struct strvar  *svhead = NULL;  /* string variables */
75  
76   int  blnkeq = 1;                /* blanks compare equal? */
77   int  igneol = 0;                /* ignore end of line? */
78 + int  passive = 0;               /* passive mode (transmit unmatched input) */
79   char  sepchar = '\t';           /* input/output separator */
80   int  noinput = 0;               /* no input records? */
81 + int  itype = 'a';               /* input type (a/f/F/d/D) */
82   int  nbicols = 0;               /* number of binary input columns */
83 < int  bocols = 0;                /* produce binary output columns */
83 > int  otype = 'a';               /* output format (a/f/F/d/D) */
84   char  inpbuf[INBSIZ];           /* input buffer */
85   double  colval[MAXCOL];         /* input column values */
86   unsigned long  colflg = 0;      /* column retrieved flags */
# Line 88 | Line 98 | struct {
98   } ipb;                          /* circular lookahead buffer */
99  
100  
101 < main(argc, argv)
102 < int  argc;
103 < char  *argv[];
101 > int
102 > main(
103 > int  argc,
104 > char  *argv[]
105 > )
106   {
107 +        char  *fpath;
108          int  i;
109  
110          esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST;
# Line 101 | Line 114 | char  *argv[];
114          biggerlib();
115   #endif
116          varset("PI", ':', 3.14159265358979323846);
117 +        funset("in", 1, '=', &l_in);
118  
119          for (i = 1; i < argc && argv[i][0] == '-'; i++)
120                  switch (argv[i][1]) {
# Line 110 | Line 124 | char  *argv[];
124                  case 'l':
125                          igneol = !igneol;
126                          break;
127 +                case 'p':
128 +                        passive = !passive;
129 +                        break;
130                  case 't':
131                          sepchar = argv[i][2];
132                          break;
# Line 117 | Line 134 | char  *argv[];
134                          svpreset(argv[++i]);
135                          break;
136                  case 'f':
137 <                        fcompile(argv[++i]);
137 >                        fpath = getpath(argv[++i], getrlibpath(), 0);
138 >                        if (fpath == NULL) {
139 >                                eputs(argv[0]);
140 >                                eputs(": cannot find file '");
141 >                                eputs(argv[i]);
142 >                                eputs("'\n");
143 >                                quit(1);
144 >                        }
145 >                        fcompile(fpath);
146                          break;
147                  case 'e':
148                          scompile(argv[++i], NULL, 0);
# Line 128 | Line 153 | char  *argv[];
153                  case 'i':
154                          switch (argv[i][2]) {
155                          case '\0':
156 +                                itype = 'a';
157                                  nbicols = 0;
158                                  readfmt(argv[++i], 0);
159                                  break;
160                          case 'a':
161 +                                itype = 'a';
162                                  nbicols = 0;
163                                  break;
164                          case 'd':
165 +                        case 'D':
166 +                                itype = argv[i][2];
167                                  if (isdigit(argv[i][3]))
168                                          nbicols = atoi(argv[i]+3);
169                                  else
170                                          nbicols = 1;
171 +                                if (nbicols*sizeof(double) > INBSIZ) {
172 +                                        eputs(argv[0]);
173 +                                        eputs(": too many input columns\n");
174 +                                        quit(1);
175 +                                }
176                                  break;
177                          case 'f':
178 +                        case 'F':
179 +                                itype = argv[i][2];
180                                  if (isdigit(argv[i][3]))
181 <                                        nbicols = -atoi(argv[i]+3);
181 >                                        nbicols = atoi(argv[i]+3);
182                                  else
183 <                                        nbicols = -1;
183 >                                        nbicols = 1;
184 >                                if (nbicols*sizeof(float) > INBSIZ) {
185 >                                        eputs(argv[0]);
186 >                                        eputs(": too many input columns\n");
187 >                                        quit(1);
188 >                                }
189                                  break;
190                          default:
191                                  goto userr;
# Line 153 | Line 194 | char  *argv[];
194                  case 'o':
195                          switch (argv[i][2]) {
196                          case '\0':
197 <                                bocols = 0;
197 >                                otype = 'a';
198                                  readfmt(argv[++i], 1);
199                                  break;
200                          case 'a':
201 <                                bocols = 0;
201 >                                otype = 'a';
202                                  break;
203                          case 'd':
204 <                                bocols = 1;
164 <                                break;
204 >                        case 'D':
205                          case 'f':
206 <                                bocols = -1;
206 >                        case 'F':
207 >                                otype = argv[i][2];
208                                  break;
209 +                        default:
210 +                                goto userr;
211                          }
212                          break;
213                  case 'w':
# Line 177 | Line 220 | char  *argv[];
220                  userr:
221                          eputs("Usage: ");
222                          eputs(argv[0]);
223 < eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
223 > eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
224                          quit(1);
225                  }
226 <
226 >        if (otype != 'a')
227 >                SET_FILE_BINARY(stdout);
228          if (noinput) {          /* produce a single output record */
229 +                if (i < argc) {
230 +                        eputs(argv[0]);
231 +                        eputs(": file argument(s) incompatible with -n\n");
232 +                        quit(1);
233 +                }
234                  eclock++;
235                  putout();
236                  quit(0);
237          }
238 +        if (itype != 'a')
239 +                SET_FILE_BINARY(stdin);
240  
241          if (blnkeq)             /* for efficiency */
242                  nbsynch();
# Line 197 | Line 248 | eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e exp
248                          execute(argv[i]);
249          
250          quit(0);
251 +        return 0; /* pro forma return */
252   }
253  
254  
255 < nbsynch()               /* non-blank starting synch character */
255 > static void
256 > nbsynch(void)               /* non-blank starting synch character */
257   {
258          if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT)
259                  return;
# Line 211 | Line 264 | nbsynch()               /* non-blank starting synch ch
264   }
265  
266  
267 < int
268 < getinputrec(fp)         /* get next input record */
269 < FILE  *fp;
267 > static int
268 > getinputrec(            /* get next input record */
269 > FILE  *fp
270 > )
271   {
272          if (inpfmt != NULL)
273                  return(getrec());
274 <        if (nbicols > 0)
275 <                return(fread(inpbuf, sizeof(double),
276 <                                        nbicols, fp) == nbicols);
277 <        if (nbicols < 0)
278 <                return(fread(inpbuf, sizeof(float),
279 <                                        -nbicols, fp) == -nbicols);
274 >        if (tolower(itype) == 'd') {
275 >                if (fread(inpbuf, sizeof(double), nbicols, fp) != nbicols)
276 >                        return(0);
277 >                if (itype == 'D')
278 >                        swap64(inpbuf, nbicols);
279 >                return(1);
280 >        }
281 >        if (tolower(itype) == 'f') {
282 >                if (fread(inpbuf, sizeof(float), nbicols, fp) != nbicols)
283 >                        return(0);
284 >                if (itype == 'F')
285 >                        swap32(inpbuf, nbicols);
286 >                return(1);
287 >        }
288          return(fgets(inpbuf, INBSIZ, fp) != NULL);
289   }
290  
291  
292 < execute(file)           /* process a file */
293 < char  *file;
292 > static void
293 > execute(           /* process a file */
294 > char  *file
295 > )
296   {
297          int  conditional = vardefined("cond");
298          long  nrecs = 0;
# Line 247 | Line 311 | char  *file;
311          
312          while (getinputrec(fp)) {
313                  varset("recno", '=', (double)++nrecs);
314 +                varset("outno", '=', (double)(nout+1));
315                  colflg = 0;
316                  eclock++;
317                  if (!conditional || varvalue("cond") > 0.0) {
253                        varset("outno", '=', (double)++nout);
318                          putout();
319 +                        ++nout;
320                  }
321          }
322          fclose(fp);
323   }
324  
325  
326 < putout()                /* produce an output record */
326 > static void
327 > putout(void)                /* produce an output record */
328   {
263        extern void  chanset(), bchanset();
329  
330          colpos = 0;
331          if (outfmt != NULL)
332                  putrec();
333 <        else if (bocols)
269 <                chanout(bchanset);
270 <        else
333 >        else if (otype == 'a')
334                  chanout(chanset);
335 <        if (colpos && !bocols)
335 >        else
336 >                chanout(bchanset);
337 >        if (colpos && otype == 'a')
338                  putchar('\n');
339          if (unbuff)
340                  fflush(stdout);
341   }
342  
343  
344 + static double
345 + l_in(char *funame)      /* function call for $channel */
346 + {
347 +        int  n;
348 +        register char  *cp;
349 +                        /* get argument as integer */
350 +        n = (int)(argument(1) + .5);
351 +        if (n != 0)     /* return channel value */
352 +                return(chanvalue(n));
353 +                        /* determine number of channels */
354 +        if (noinput || inpfmt != NULL)
355 +                return(0);
356 +        if (nbicols)
357 +                return(nbicols);
358 +        cp = inpbuf;    /* need to count */
359 +        for (n = 0; *cp; )
360 +                if (blnkeq && isspace(sepchar)) {
361 +                        while (isspace(*cp))
362 +                                cp++;
363 +                        n += *cp != '\0';
364 +                        while (*cp && !isspace(*cp))
365 +                                cp++;
366 +                } else {
367 +                        n += *cp != '\n';
368 +                        while (*cp && *cp++ != sepchar)
369 +                                ;
370 +                }
371 +        return(n);
372 + }
373 +
374   double
375 < chanvalue(n)            /* return value for column n */
376 < int  n;
375 > chanvalue(            /* return value for column n */
376 > int  n
377 > )
378   {
379          int  i;
380          register char  *cp;
# Line 291 | Line 387 | int  n;
387                  eputs("illegal channel number\n");
388                  quit(1);
389          }
390 <        if (nbicols > 0) {
390 >        if (nbicols) {
391                  if (n > nbicols)
392                          return(0.0);
393 <                cp = inpbuf + (n-1)*sizeof(double);
394 <                return(*(double *)cp);
395 <        }
396 <        if (nbicols < 0) {
301 <                if (n > -nbicols)
302 <                        return(0.0);
393 >                if (tolower(itype) == 'd') {
394 >                        cp = inpbuf + (n-1)*sizeof(double);
395 >                        return(*(double *)cp);
396 >                }
397                  cp = inpbuf + (n-1)*sizeof(float);
398                  return(*(float *)cp);
399          }
# Line 329 | Line 423 | int  n;
423  
424  
425   void
426 < chanset(n, v)                   /* output column n */
427 < int  n;
428 < double  v;
426 > chanset(                   /* output column n */
427 > int  n,
428 > double  v
429 > )
430   {
431          if (colpos == 0)                /* no leading separator */
432                  colpos = 1;
# Line 344 | Line 439 | double  v;
439  
440  
441   void
442 < bchanset(n, v)                   /* output binary channel n */
443 < int  n;
444 < double  v;
442 > bchanset(                   /* output binary channel n */
443 > int  n,
444 > double  v
445 > )
446   {
447          static char     zerobuf[sizeof(double)];
448 +        float   fval = v;
449  
450          while (++colpos < n)
451                  fwrite(zerobuf,
452 <                        bocols>0 ? sizeof(double) : sizeof(float),
452 >                        tolower(otype)=='d' ? sizeof(double) : sizeof(float),
453                          1, stdout);
454 <        if (bocols > 0)
454 >        switch (otype) {
455 >        case 'D':
456 >                swap64((char *)&v, 1);
457 >                /* fall through */
458 >        case 'd':
459                  fwrite(&v, sizeof(double), 1, stdout);
460 <        else {
461 <                float   fval = v;
460 >                break;
461 >        case 'F':
462 >                swap32((char *)&fval, 1);
463 >                /* fall through */
464 >        case 'f':
465                  fwrite(&fval, sizeof(float), 1, stdout);
466 +                break;
467          }
468   }
469  
470  
471 < readfmt(spec, output)                   /* read record format */
472 < char  *spec;
473 < int  output;
471 > static void
472 > readfmt(                   /* read record format */
473 > char  *spec,
474 > int  output
475 > )
476   {
477          int  fd;
478          char  *inptr;
# Line 384 | Line 491 | int  output;
491                          eputs(": cannot open\n");
492                          quit(1);
493                  }
494 <                res = read(fd, inpbuf+1, INBSIZ-1);
494 >                res = read(fd, inpbuf+2, INBSIZ-2);
495                  if (res <= 0 || res >= INBSIZ-1) {
496                          eputs(spec);
497                          if (res < 0)
# Line 396 | Line 503 | int  output;
503                          quit(1);
504                  }
505                  close(fd);
506 <                (inptr=inpbuf+1)[res] = '\0';
506 >                (inptr=inpbuf+2)[res] = '\0';
507          }
508          f = &fmt;                               /* get fields */
509          while ((res = readfield(&inptr)) != F_NUL) {
# Line 429 | Line 536 | int  output;
536   }
537  
538  
539 < int
540 < readfield(pp)                   /* get next field in format */
541 < register char  **pp;
539 > static int
540 > readfield(                   /* get next field in format */
541 > register char  **pp
542 > )
543   {
544          int  type = F_NUL;
545          int  width = 0;
# Line 496 | Line 604 | register char  **pp;
604  
605  
606   struct strvar *
607 < getsvar(svname)                         /* get string variable */
608 < char  *svname;
607 > getsvar(                         /* get string variable */
608 > char  *svname
609 > )
610   {
611          register struct strvar  *sv;
612          
# Line 513 | Line 622 | char  *svname;
622   }
623  
624  
625 < svpreset(eqn)                    /* preset a string variable */
626 < char  *eqn;
625 > static void
626 > svpreset(                    /* preset a string variable */
627 > char  *eqn
628 > )
629   {
630          register struct strvar  *sv;
631          register char  *val;
# Line 533 | Line 644 | char  *eqn;
644   }
645  
646  
647 < clearrec()                      /* clear input record variables */
647 > static void
648 > clearrec(void)                  /* clear input record variables */
649   {
650          register struct field  *f;
651  
# Line 552 | Line 664 | clearrec()                     /* clear input record variables */
664   }
665  
666  
667 < getrec()                                /* get next record from file */
667 > static int
668 > getrec(void)                            /* get next record from file */
669   {
670          int  eatline;
671          register struct field  *f;
672 <        
672 >
673          while (ipb.chr != EOF) {
674 <                eatline = !igneol && ipb.chr != '\n';
562 <                if (blnkeq)             /* beware of nbsynch() */
674 >                if (blnkeq) {           /* beware of nbsynch() */
675                          while (isblnk(ipb.chr))
676 <                                scaninp();
676 >                                resetinp();
677 >                        if (ipb.chr == EOF)
678 >                                return(0);
679 >                }
680 >                eatline = (!igneol && ipb.chr != '\n');
681                  clearrec();             /* start with fresh record */
682                  for (f = inpfmt; f != NULL; f = f->next)
683                          if (getfield(f) == -1)
684                                  break;
685                  if (f == NULL) {
686 <                        advinp();
686 >                        advinp();       /* got one! */
687                          return(1);
688                  }
689 <                resetinp();
689 >                resetinp();             /* eat false start */
690                  if (eatline) {          /* eat rest of line */
691                          while (ipb.chr != '\n') {
692                                  if (ipb.chr == EOF)
693                                          return(0);
694 <                                scaninp();
694 >                                resetinp();
695                          }
696 <                        scaninp();
581 <                        advinp();
696 >                        resetinp();
697                  }
698          }
699          return(0);
700   }
701  
702  
703 < getfield(f)                             /* get next field */
704 < register struct field  *f;
703 > static int
704 > getfield(                             /* get next field */
705 > register struct field  *f
706 > )
707   {
708 <        static char  buf[MAXWORD+1];            /* no recursion! */
708 >        static char  buf[RMAXWORD+1];            /* no recursion! */
709          int  delim, inword;
710          double  d;
711          char  *np;
# Line 621 | Line 738 | register struct field  *f;
738                          delim = f->next->f.sl[0];
739                  cp = buf;
740                  do {
741 <                        if (ipb.chr == EOF)
741 >                        if (ipb.chr == EOF || ipb.chr == '\n')
742                                  inword = 0;
743                          else if (blnkeq && delim != EOF)
744                                  inword = isblnk(delim) ?
# Line 633 | Line 750 | register struct field  *f;
750                                  *cp++ = ipb.chr;
751                                  scaninp();
752                          }
753 <                } while (inword && cp < &buf[MAXWORD]);
753 >                } while (inword && cp < &buf[RMAXWORD]);
754                  *cp = '\0';
755                  if (f->f.sv->val == NULL)
756                          f->f.sv->val = savqstr(buf);    /* first setting */
# Line 662 | Line 779 | register struct field  *f;
779                                  *cp++ = ipb.chr;
780                                  scaninp();
781                          }
782 <                } while (inword && cp < &buf[MAXWORD]);
782 >                } while (inword && cp < &buf[RMAXWORD]);
783                  *cp = '\0';
784                  d = np==NULL ? 0. : atof(np);
785                  if (!vardefined(f->f.nv))
# Line 672 | Line 789 | register struct field  *f;
789                          return(-1);                     /* doesn't match! */
790                  return(0);
791          }
792 +        return -1; /* pro forma return */
793   }
794  
795  
796 < putrec()                                /* output a record */
796 > static void
797 > putrec(void)                                /* output a record */
798   {
799          char  fmt[32];
800          register int  n;
# Line 725 | Line 844 | putrec()                                /* output a re
844   }
845  
846  
847 < initinp(fp)                     /* prepare lookahead buffer */
848 < FILE  *fp;
847 > static void
848 > initinp(FILE  *fp)                     /* prepare lookahead buffer */
849 >
850   {
851          ipb.fin = fp;
852          ipb.beg = ipb.end = inpbuf;
# Line 736 | Line 856 | FILE  *fp;
856   }
857  
858  
859 < scaninp()                       /* scan next character */
859 > static void
860 > scaninp(void)                       /* scan next character */
861   {
862          if (ipb.chr == EOF)
863                  return;
# Line 755 | Line 876 | scaninp()                       /* scan next character
876   }
877  
878  
879 < advinp()                        /* move home to current position */
879 > static void
880 > advinp(void)                        /* move home to current position */
881   {
882          ipb.beg = ipb.pos;
883   }
884  
885  
886 < resetinp()                      /* rewind position and advance 1 */
886 > static void
887 > resetinp(void)                      /* rewind position and advance 1 */
888   {
889          if (ipb.beg == NULL)            /* full */
890                  ipb.beg = ipb.end;
891          ipb.pos = ipb.beg;
892          ipb.chr = *ipb.pos;
893 +        if (passive)                    /* transmit unmatched character? */
894 +                fputc(ipb.chr, stdout);
895          if (++ipb.beg >= &inpbuf[INBSIZ])
896                  ipb.beg = inpbuf;
897          scaninp();
# Line 774 | Line 899 | resetinp()                      /* rewind position and
899  
900  
901   void
902 < eputs(msg)
778 < char  *msg;
902 > eputs(char  *msg)
903   {
904          fputs(msg, stderr);
905   }
906  
907  
908   void
909 < wputs(msg)
786 < char  *msg;
909 > wputs(char  *msg)
910   {
911          if (!nowarn)
912                  eputs(msg);
# Line 791 | Line 914 | char  *msg;
914  
915  
916   void
917 < quit(code)
795 < int  code;
917 > quit(int  code)
918   {
919          exit(code);
920   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines