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.3 by greg, Sun Apr 27 06:08:03 2003 UTC vs.
Revision 1.11 by schorsch, Fri Nov 14 17:31:24 2003 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>
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 88 | 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  
# Line 101 | Line 112 | char  *argv[];
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 139 | Line 151 | char  *argv[];
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;
# Line 197 | 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 211 | Line 235 | nbsynch()               /* non-blank starting synch ch
235   }
236  
237  
238 < int
239 < getinputrec(fp)         /* get next input record */
240 < FILE  *fp;
238 > static int
239 > getinputrec(            /* get next input record */
240 > FILE  *fp
241 > )
242   {
243          if (inpfmt != NULL)
244                  return(getrec());
# Line 227 | Line 252 | FILE  *fp;
252   }
253  
254  
255 < execute(file)           /* process a file */
256 < char  *file;
255 > static void
256 > execute(           /* process a file */
257 > char  *file
258 > )
259   {
260          int  conditional = vardefined("cond");
261          long  nrecs = 0;
# Line 258 | Line 285 | char  *file;
285   }
286  
287  
288 < putout()                /* produce an output record */
288 > static void
289 > putout(void)                /* produce an output record */
290   {
263        extern void  chanset(), bchanset();
291  
292          colpos = 0;
293          if (outfmt != NULL)
# Line 269 | Line 296 | putout()                /* produce an output record */
296                  chanout(bchanset);
297          else
298                  chanout(chanset);
299 <        if (colpos && !bchanset)
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 329 | Line 389 | int  n;
389  
390  
391   void
392 < chanset(n, v)                   /* output column n */
393 < int  n;
394 < double  v;
392 > chanset(                   /* output column n */
393 > int  n,
394 > double  v
395 > )
396   {
397          if (colpos == 0)                /* no leading separator */
398                  colpos = 1;
# Line 344 | Line 405 | double  v;
405  
406  
407   void
408 < bchanset(n, v)                   /* output binary channel n */
409 < int  n;
410 < double  v;
408 > bchanset(                   /* output binary channel n */
409 > int  n,
410 > double  v
411 > )
412   {
413          static char     zerobuf[sizeof(double)];
414  
# Line 363 | Line 425 | double  v;
425   }
426  
427  
428 < readfmt(spec, output)                   /* read record format */
429 < char  *spec;
430 < int  output;
428 > static void
429 > readfmt(                   /* read record format */
430 > char  *spec,
431 > int  output
432 > )
433   {
434          int  fd;
435          char  *inptr;
# Line 429 | 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 496 | 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 513 | 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 533 | 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 552 | 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 585 | 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 621 | 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 633 | 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 662 | 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 672 | 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 725 | 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 736 | 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 755 | 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 774 | Line 852 | resetinp()                      /* rewind position and
852  
853  
854   void
855 < eputs(msg)
778 < char  *msg;
855 > eputs(char  *msg)
856   {
857          fputs(msg, stderr);
858   }
859  
860  
861   void
862 < wputs(msg)
786 < char  *msg;
862 > wputs(char  *msg)
863   {
864          if (!nowarn)
865                  eputs(msg);
# Line 791 | Line 867 | char  *msg;
867  
868  
869   void
870 < quit(code)
795 < int  code;
870 > quit(int  code)
871   {
872          exit(code);
873   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines