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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines