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.5 by schorsch, Sun Jun 8 12:03:09 2003 UTC vs.
Revision 1.19 by greg, Tue Jun 14 01:25:02 2005 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   */
9  
10   #include  <stdlib.h>
11 + #include  <fcntl.h>
12   #include  <stdio.h>
13   #include  <string.h>
14   #include  <math.h>
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  
19 #ifdef  CPM
20 #define  getc           agetc   /* text files only, right? */
21 #endif
22
23   #define  isnum(c)       (isdigit(c) || (c)=='-' || (c)=='.' \
24                                  || (c)=='+' || (c)=='e' || (c)=='E')
25  
26   #define  isblnk(c)      (igneol ? isspace(c) : (c)==' '||(c)=='\t')
27  
28 < #define  INBSIZ         4096    /* longest record */
28 > #define  INBSIZ         16384   /* longest record */
29   #define  MAXCOL         32      /* number of columns recorded */
30  
31                                  /* field type specifications */
# Line 57 | 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 + 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);
# Line 69 | Line 70 | 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 76 | Line 78 | struct strvar  *svhead = NULL;  /* string variables */
78  
79   int  blnkeq = 1;                /* blanks compare equal? */
80   int  igneol = 0;                /* ignore end of line? */
81 + int  passive = 0;               /* passive mode (transmit unmatched input) */
82   char  sepchar = '\t';           /* input/output separator */
83   int  noinput = 0;               /* no input records? */
84   int  nbicols = 0;               /* number of binary input columns */
# Line 112 | Line 115 | char  *argv[]
115          biggerlib();
116   #endif
117          varset("PI", ':', 3.14159265358979323846);
118 +        funset("in", 1, '=', &l_in);
119  
120          for (i = 1; i < argc && argv[i][0] == '-'; i++)
121                  switch (argv[i][1]) {
# Line 121 | Line 125 | char  *argv[]
125                  case 'l':
126                          igneol = !igneol;
127                          break;
128 +                case 'p':
129 +                        passive = !passive;
130 +                        break;
131                  case 't':
132                          sepchar = argv[i][2];
133                          break;
# Line 150 | Line 157 | char  *argv[]
157                                          nbicols = atoi(argv[i]+3);
158                                  else
159                                          nbicols = 1;
160 +                                if (nbicols*sizeof(double) > INBSIZ) {
161 +                                        eputs(argv[0]);
162 +                                        eputs(": too many input columns\n");
163 +                                        quit(1);
164 +                                }
165                                  break;
166                          case 'f':
167                                  if (isdigit(argv[i][3]))
168                                          nbicols = -atoi(argv[i]+3);
169                                  else
170                                          nbicols = -1;
171 +                                if (-nbicols*sizeof(float) > INBSIZ) {
172 +                                        eputs(argv[0]);
173 +                                        eputs(": too many input columns\n");
174 +                                        quit(1);
175 +                                }
176                                  break;
177                          default:
178                                  goto userr;
# Line 172 | Line 189 | char  *argv[]
189                                  break;
190                          case 'd':
191                                  bocols = 1;
192 +                                SET_FILE_BINARY(stdout);
193                                  break;
194                          case 'f':
195                                  bocols = -1;
196 +                                SET_FILE_BINARY(stdout);
197                                  break;
198 +                        default:
199 +                                goto userr;
200                          }
201                          break;
202                  case 'w':
# Line 188 | Line 209 | char  *argv[]
209                  userr:
210                          eputs("Usage: ");
211                          eputs(argv[0]);
212 < eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
212 > eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
213                          quit(1);
214                  }
215 <
215 >        if (bocols)
216 >                SET_FILE_BINARY(stdout);
217          if (noinput) {          /* produce a single output record */
218 +                if (i < argc) {
219 +                        eputs(argv[0]);
220 +                        eputs(": file argument(s) incompatible with -n\n");
221 +                        quit(1);
222 +                }
223                  eclock++;
224                  putout();
225                  quit(0);
226          }
227 +        if (nbicols)
228 +                SET_FILE_BINARY(stdin);
229  
230          if (blnkeq)             /* for efficiency */
231                  nbsynch();
# Line 208 | Line 237 | eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e exp
237                          execute(argv[i]);
238          
239          quit(0);
240 +        return 0; /* pro forma return */
241   }
242  
243  
# Line 223 | Line 253 | nbsynch(void)               /* non-blank starting sync
253   }
254  
255  
256 < int
256 > static int
257   getinputrec(            /* get next input record */
258   FILE  *fp
259   )
# Line 291 | Line 321 | putout(void)                /* produce an output recor
321   }
322  
323  
324 + static double
325 + l_in(char *funame)      /* function call for $channel */
326 + {
327 +        int  n;
328 +        register char  *cp;
329 +                        /* get argument as integer */
330 +        n = (int)(argument(1) + .5);
331 +        if (n != 0)     /* return channel value */
332 +                return(chanvalue(n));
333 +                        /* determine number of channels */
334 +        if (noinput || inpfmt != NULL)
335 +                return(0);
336 +        if (nbicols > 0)
337 +                return(nbicols);
338 +        if (nbicols < 0)
339 +                return(-nbicols);
340 +        cp = inpbuf;    /* need to count */
341 +        for (n = 0; *cp; )
342 +                if (blnkeq && isspace(sepchar)) {
343 +                        while (isspace(*cp))
344 +                                cp++;
345 +                        n += *cp != '\0';
346 +                        while (*cp && !isspace(*cp))
347 +                                cp++;
348 +                } else {
349 +                        n += *cp != '\n';
350 +                        while (*cp && *cp++ != sepchar)
351 +                                ;
352 +                }
353 +        return(n);
354 + }
355 +
356   double
357   chanvalue(            /* return value for column n */
358   int  n
# Line 404 | Line 466 | int  output
466                          eputs(": cannot open\n");
467                          quit(1);
468                  }
469 <                res = read(fd, inpbuf+1, INBSIZ-1);
469 >                res = read(fd, inpbuf+2, INBSIZ-2);
470                  if (res <= 0 || res >= INBSIZ-1) {
471                          eputs(spec);
472                          if (res < 0)
# Line 416 | Line 478 | int  output
478                          quit(1);
479                  }
480                  close(fd);
481 <                (inptr=inpbuf+1)[res] = '\0';
481 >                (inptr=inpbuf+2)[res] = '\0';
482          }
483          f = &fmt;                               /* get fields */
484          while ((res = readfield(&inptr)) != F_NUL) {
# Line 578 | Line 640 | clearrec(void)                 /* clear input record variables */
640  
641  
642   static int
643 < getrec(void)                                /* get next record from file */
643 > getrec(void)                            /* get next record from file */
644   {
645          int  eatline;
646          register struct field  *f;
647 <        
647 >
648          while (ipb.chr != EOF) {
649 <                eatline = !igneol && ipb.chr != '\n';
588 <                if (blnkeq)             /* beware of nbsynch() */
649 >                if (blnkeq) {           /* beware of nbsynch() */
650                          while (isblnk(ipb.chr))
651 <                                scaninp();
651 >                                resetinp();
652 >                        if (ipb.chr == EOF)
653 >                                return(0);
654 >                }
655 >                eatline = (!igneol && ipb.chr != '\n');
656                  clearrec();             /* start with fresh record */
657                  for (f = inpfmt; f != NULL; f = f->next)
658                          if (getfield(f) == -1)
659                                  break;
660                  if (f == NULL) {
661 <                        advinp();
661 >                        advinp();       /* got one! */
662                          return(1);
663                  }
664 <                resetinp();
664 >                resetinp();             /* eat false start */
665                  if (eatline) {          /* eat rest of line */
666                          while (ipb.chr != '\n') {
667                                  if (ipb.chr == EOF)
668                                          return(0);
669 <                                scaninp();
669 >                                resetinp();
670                          }
671 <                        scaninp();
607 <                        advinp();
671 >                        resetinp();
672                  }
673          }
674          return(0);
# Line 616 | Line 680 | getfield(                             /* get next fiel
680   register struct field  *f
681   )
682   {
683 <        static char  buf[MAXWORD+1];            /* no recursion! */
683 >        static char  buf[RMAXWORD+1];            /* no recursion! */
684          int  delim, inword;
685          double  d;
686          char  *np;
# Line 649 | Line 713 | register struct field  *f
713                          delim = f->next->f.sl[0];
714                  cp = buf;
715                  do {
716 <                        if (ipb.chr == EOF)
716 >                        if (ipb.chr == EOF || ipb.chr == '\n')
717                                  inword = 0;
718                          else if (blnkeq && delim != EOF)
719                                  inword = isblnk(delim) ?
# Line 661 | Line 725 | register struct field  *f
725                                  *cp++ = ipb.chr;
726                                  scaninp();
727                          }
728 <                } while (inword && cp < &buf[MAXWORD]);
728 >                } while (inword && cp < &buf[RMAXWORD]);
729                  *cp = '\0';
730                  if (f->f.sv->val == NULL)
731                          f->f.sv->val = savqstr(buf);    /* first setting */
# Line 690 | Line 754 | register struct field  *f
754                                  *cp++ = ipb.chr;
755                                  scaninp();
756                          }
757 <                } while (inword && cp < &buf[MAXWORD]);
757 >                } while (inword && cp < &buf[RMAXWORD]);
758                  *cp = '\0';
759                  d = np==NULL ? 0. : atof(np);
760                  if (!vardefined(f->f.nv))
# Line 801 | Line 865 | resetinp(void)                      /* rewind position
865                  ipb.beg = ipb.end;
866          ipb.pos = ipb.beg;
867          ipb.chr = *ipb.pos;
868 +        if (passive)                    /* transmit unmatched character? */
869 +                fputc(ipb.chr, stdout);
870          if (++ipb.beg >= &inpbuf[INBSIZ])
871                  ipb.beg = inpbuf;
872          scaninp();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines