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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines