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.10 by greg, Thu Nov 6 05:39:33 2003 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  <stdio.h>
12 > #include  <string.h>
13   #include  <math.h>
15
14   #include  <ctype.h>
15  
16 + #include  "platform.h"
17   #include  "calcomp.h"
18 + #include  "rterror.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  
# 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 69 | Line 77 | int  blnkeq = 1;                /* blanks compare equa
77   int  igneol = 0;                /* ignore end of line? */
78   char  sepchar = '\t';           /* input/output separator */
79   int  noinput = 0;               /* no input records? */
80 + int  nbicols = 0;               /* number of binary input columns */
81 + int  bocols = 0;                /* produce binary output columns */
82   char  inpbuf[INBSIZ];           /* input buffer */
83   double  colval[MAXCOL];         /* input column values */
84   unsigned long  colflg = 0;      /* column retrieved flags */
# Line 86 | Line 96 | struct {
96   } ipb;                          /* circular lookahead buffer */
97  
98  
99 < main(argc, argv)
100 < int  argc;
101 < char  *argv[];
99 > int
100 > main(
101 > int  argc,
102 > char  *argv[]
103 > )
104   {
105          int  i;
106  
107 <        esupport |= (E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST);
107 >        esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST;
108 >        esupport &= ~(E_REDEFW);
109  
110   #ifdef  BIGGERLIB
111          biggerlib();
112   #endif
113          varset("PI", ':', 3.14159265358979323846);
114 +        funset("in", 1, '=', &l_in);
115  
116          for (i = 1; i < argc && argv[i][0] == '-'; i++)
117                  switch (argv[i][1]) {
# Line 123 | Line 137 | char  *argv[];
137                          noinput = 1;
138                          break;
139                  case 'i':
140 <                        readfmt(argv[++i], 0);
140 >                        switch (argv[i][2]) {
141 >                        case '\0':
142 >                                nbicols = 0;
143 >                                readfmt(argv[++i], 0);
144 >                                break;
145 >                        case 'a':
146 >                                nbicols = 0;
147 >                                break;
148 >                        case 'd':
149 >                                if (isdigit(argv[i][3]))
150 >                                        nbicols = atoi(argv[i]+3);
151 >                                else
152 >                                        nbicols = 1;
153 >                                if (nbicols*sizeof(double) > INBSIZ) {
154 >                                        eputs(argv[0]);
155 >                                        eputs(": too many input columns\n");
156 >                                        quit(1);
157 >                                }
158 >                                break;
159 >                        case 'f':
160 >                                if (isdigit(argv[i][3]))
161 >                                        nbicols = -atoi(argv[i]+3);
162 >                                else
163 >                                        nbicols = -1;
164 >                                if (-nbicols*sizeof(float) > INBSIZ) {
165 >                                        eputs(argv[0]);
166 >                                        eputs(": too many input columns\n");
167 >                                        quit(1);
168 >                                }
169 >                                break;
170 >                        default:
171 >                                goto userr;
172 >                        }
173                          break;
174                  case 'o':
175 <                        readfmt(argv[++i], 1);
175 >                        switch (argv[i][2]) {
176 >                        case '\0':
177 >                                bocols = 0;
178 >                                readfmt(argv[++i], 1);
179 >                                break;
180 >                        case 'a':
181 >                                bocols = 0;
182 >                                break;
183 >                        case 'd':
184 >                                bocols = 1;
185 >                                break;
186 >                        case 'f':
187 >                                bocols = -1;
188 >                                break;
189 >                        }
190                          break;
191                  case 'w':
192                          nowarn = !nowarn;
# Line 134 | Line 194 | char  *argv[];
194                  case 'u':
195                          unbuff = !unbuff;
196                          break;
197 <                default:
197 >                default:;
198 >                userr:
199                          eputs("Usage: ");
200                          eputs(argv[0]);
201   eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
# Line 160 | Line 221 | eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e exp
221   }
222  
223  
224 < nbsynch()               /* non-blank starting synch character */
224 > static void
225 > nbsynch(void)               /* non-blank starting synch character */
226   {
227          if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT)
228                  return;
# Line 171 | Line 233 | nbsynch()               /* non-blank starting synch ch
233   }
234  
235  
236 < execute(file)           /* process a file */
237 < char  *file;
236 > static int
237 > getinputrec(            /* get next input record */
238 > FILE  *fp
239 > )
240   {
241 +        if (inpfmt != NULL)
242 +                return(getrec());
243 +        if (nbicols > 0)
244 +                return(fread(inpbuf, sizeof(double),
245 +                                        nbicols, fp) == nbicols);
246 +        if (nbicols < 0)
247 +                return(fread(inpbuf, sizeof(float),
248 +                                        -nbicols, fp) == -nbicols);
249 +        return(fgets(inpbuf, INBSIZ, fp) != NULL);
250 + }
251 +
252 +
253 + static void
254 + execute(           /* process a file */
255 + char  *file
256 + )
257 + {
258          int  conditional = vardefined("cond");
259          long  nrecs = 0;
260          long  nout = 0;
# Line 188 | Line 269 | char  *file;
269          }
270          if (inpfmt != NULL)
271                  initinp(fp);
272 <                
273 <        while (inpfmt != NULL ? getrec() : fgets(inpbuf, INBSIZ, fp) != NULL) {
272 >        
273 >        while (getinputrec(fp)) {
274                  varset("recno", '=', (double)++nrecs);
275                  colflg = 0;
276                  eclock++;
# Line 202 | Line 283 | char  *file;
283   }
284  
285  
286 < putout()                /* produce an output record */
286 > static void
287 > putout(void)                /* produce an output record */
288   {
207        extern int  chanset();
289  
290          colpos = 0;
291          if (outfmt != NULL)
292                  putrec();
293 +        else if (bocols)
294 +                chanout(bchanset);
295          else
296                  chanout(chanset);
297 <        if (colpos)
297 >        if (colpos && !bocols)
298                  putchar('\n');
299          if (unbuff)
300                  fflush(stdout);
301   }
302  
303  
304 + static double
305 + l_in(char *funame)      /* function call for $channel */
306 + {
307 +        int  n;
308 +        register char  *cp;
309 +                        /* get argument as integer */
310 +        n = (int)(argument(1) + .5);
311 +        if (n != 0)     /* return channel value */
312 +                return(chanvalue(n));
313 +                        /* determine number of channels */
314 +        if (noinput || inpfmt != NULL)
315 +                return(0);
316 +        if (nbicols > 0)
317 +                return(nbicols);
318 +        if (nbicols < 0)
319 +                return(-nbicols);
320 +        cp = inpbuf;    /* need to count */
321 +        for (n = 0; *cp; )
322 +                if (blnkeq && isspace(sepchar)) {
323 +                        while (isspace(*cp))
324 +                                cp++;
325 +                        n += *cp != '\0';
326 +                        while (*cp && !isspace(*cp))
327 +                                cp++;
328 +                } else {
329 +                        n += *cp != '\n';
330 +                        while (*cp && *cp++ != sepchar)
331 +                                ;
332 +                }
333 +        return(n);
334 + }
335 +
336   double
337 < chanvalue(n)            /* return value for column n */
338 < int  n;
337 > chanvalue(            /* return value for column n */
338 > int  n
339 > )
340   {
341          int  i;
342          register char  *cp;
# Line 233 | Line 349 | int  n;
349                  eputs("illegal channel number\n");
350                  quit(1);
351          }
352 +        if (nbicols > 0) {
353 +                if (n > nbicols)
354 +                        return(0.0);
355 +                cp = inpbuf + (n-1)*sizeof(double);
356 +                return(*(double *)cp);
357 +        }
358 +        if (nbicols < 0) {
359 +                if (n > -nbicols)
360 +                        return(0.0);
361 +                cp = inpbuf + (n-1)*sizeof(float);
362 +                return(*(float *)cp);
363 +        }
364          if (n <= MAXCOL && colflg & 1L<<(n-1))
365                  return(colval[n-1]);
366  
# Line 258 | Line 386 | int  n;
386   }
387  
388  
389 < chanset(n, v)                   /* output column n */
390 < int  n;
391 < double  v;
389 > void
390 > chanset(                   /* output column n */
391 > int  n,
392 > double  v
393 > )
394   {
395          if (colpos == 0)                /* no leading separator */
396                  colpos = 1;
# Line 272 | Line 402 | double  v;
402   }
403  
404  
405 < readfmt(spec, output)                   /* read record format */
406 < char  *spec;
407 < int  output;
405 > void
406 > bchanset(                   /* output binary channel n */
407 > int  n,
408 > double  v
409 > )
410   {
411 +        static char     zerobuf[sizeof(double)];
412 +
413 +        while (++colpos < n)
414 +                fwrite(zerobuf,
415 +                        bocols>0 ? sizeof(double) : sizeof(float),
416 +                        1, stdout);
417 +        if (bocols > 0)
418 +                fwrite(&v, sizeof(double), 1, stdout);
419 +        else {
420 +                float   fval = v;
421 +                fwrite(&fval, sizeof(float), 1, stdout);
422 +        }
423 + }
424 +
425 +
426 + static void
427 + readfmt(                   /* read record format */
428 + char  *spec,
429 + int  output
430 + )
431 + {
432          int  fd;
433          char  *inptr;
434          struct field  fmt;
# Line 338 | Line 491 | int  output;
491   }
492  
493  
494 < int
495 < readfield(pp)                   /* get next field in format */
496 < register char  **pp;
494 > static int
495 > readfield(                   /* get next field in format */
496 > register char  **pp
497 > )
498   {
499          int  type = F_NUL;
500          int  width = 0;
# Line 405 | Line 559 | register char  **pp;
559  
560  
561   struct strvar *
562 < getsvar(svname)                         /* get string variable */
563 < char  *svname;
562 > getsvar(                         /* get string variable */
563 > char  *svname
564 > )
565   {
566          register struct strvar  *sv;
567          
# Line 422 | Line 577 | char  *svname;
577   }
578  
579  
580 < svpreset(eqn)                    /* preset a string variable */
581 < char  *eqn;
580 > static void
581 > svpreset(                    /* preset a string variable */
582 > char  *eqn
583 > )
584   {
585          register struct strvar  *sv;
586          register char  *val;
# Line 442 | Line 599 | char  *eqn;
599   }
600  
601  
602 < clearrec()                      /* clear input record variables */
602 > static void
603 > clearrec(void)                  /* clear input record variables */
604   {
605          register struct field  *f;
606  
# Line 461 | Line 619 | clearrec()                     /* clear input record variables */
619   }
620  
621  
622 < getrec()                                /* get next record from file */
622 > static int
623 > getrec(void)                                /* get next record from file */
624   {
625          int  eatline;
626          register struct field  *f;
# Line 494 | Line 653 | getrec()                                /* get next re
653   }
654  
655  
656 < getfield(f)                             /* get next field */
657 < register struct field  *f;
656 > static int
657 > getfield(                             /* get next field */
658 > register struct field  *f
659 > )
660   {
661 <        static char  buf[MAXWORD+1];            /* no recursion! */
661 >        static char  buf[RMAXWORD+1];            /* no recursion! */
662          int  delim, inword;
663          double  d;
664          char  *np;
# Line 530 | Line 691 | register struct field  *f;
691                          delim = f->next->f.sl[0];
692                  cp = buf;
693                  do {
694 <                        if (ipb.chr == EOF)
694 >                        if (ipb.chr == EOF || ipb.chr == '\n')
695                                  inword = 0;
696                          else if (blnkeq && delim != EOF)
697                                  inword = isblnk(delim) ?
# Line 542 | Line 703 | register struct field  *f;
703                                  *cp++ = ipb.chr;
704                                  scaninp();
705                          }
706 <                } while (inword && cp < &buf[MAXWORD]);
706 >                } while (inword && cp < &buf[RMAXWORD]);
707                  *cp = '\0';
708                  if (f->f.sv->val == NULL)
709                          f->f.sv->val = savqstr(buf);    /* first setting */
# Line 571 | Line 732 | register struct field  *f;
732                                  *cp++ = ipb.chr;
733                                  scaninp();
734                          }
735 <                } while (inword && cp < &buf[MAXWORD]);
735 >                } while (inword && cp < &buf[RMAXWORD]);
736                  *cp = '\0';
737                  d = np==NULL ? 0. : atof(np);
738                  if (!vardefined(f->f.nv))
# Line 581 | Line 742 | register struct field  *f;
742                          return(-1);                     /* doesn't match! */
743                  return(0);
744          }
745 +        return -1; /* pro forma return */
746   }
747  
748  
749 < putrec()                                /* output a record */
749 > static void
750 > putrec(void)                                /* output a record */
751   {
752          char  fmt[32];
753          register int  n;
# Line 634 | Line 797 | putrec()                                /* output a re
797   }
798  
799  
800 < initinp(fp)                     /* prepare lookahead buffer */
801 < FILE  *fp;
800 > static void
801 > initinp(FILE  *fp)                     /* prepare lookahead buffer */
802 >
803   {
804          ipb.fin = fp;
805          ipb.beg = ipb.end = inpbuf;
# Line 645 | Line 809 | FILE  *fp;
809   }
810  
811  
812 < scaninp()                       /* scan next character */
812 > static void
813 > scaninp(void)                       /* scan next character */
814   {
815          if (ipb.chr == EOF)
816                  return;
# Line 664 | Line 829 | scaninp()                       /* scan next character
829   }
830  
831  
832 < advinp()                        /* move home to current position */
832 > static void
833 > advinp(void)                        /* move home to current position */
834   {
835          ipb.beg = ipb.pos;
836   }
837  
838  
839 < resetinp()                      /* rewind position and advance 1 */
839 > static void
840 > resetinp(void)                      /* rewind position and advance 1 */
841   {
842          if (ipb.beg == NULL)            /* full */
843                  ipb.beg = ipb.end;
# Line 683 | Line 850 | resetinp()                      /* rewind position and
850  
851  
852   void
853 < eputs(msg)
687 < char  *msg;
853 > eputs(char  *msg)
854   {
855          fputs(msg, stderr);
856   }
857  
858  
859   void
860 < wputs(msg)
695 < char  *msg;
860 > wputs(char  *msg)
861   {
862          if (!nowarn)
863                  eputs(msg);
# Line 700 | Line 865 | char  *msg;
865  
866  
867   void
868 < quit(code)
704 < int  code;
868 > quit(int  code)
869   {
870          exit(code);
871   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines