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.9 by greg, Wed Nov 5 19:03:03 2003 UTC vs.
Revision 1.18 by greg, Thu Jun 2 04:47:27 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"
17 #include  "calcomp.h"
18   #include  "rterror.h"
19 + #include  "rtmisc.h"
20 + #include  "rtio.h"
21 + #include  "calcomp.h"
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 54 | 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 66 | 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 73 | 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 109 | 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 118 | 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 179 | 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 195 | 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 215 | 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 230 | 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 298 | 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 411 | 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 423 | 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 585 | 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) {
594                eatline = !igneol && ipb.chr != '\n';
649                  if (blnkeq)             /* beware of nbsynch() */
650                          while (isblnk(ipb.chr))
651 <                                scaninp();
651 >                                resetinp();
652 >                eatline = (!igneol && ipb.chr != '\n');
653                  clearrec();             /* start with fresh record */
654                  for (f = inpfmt; f != NULL; f = f->next)
655                          if (getfield(f) == -1)
656                                  break;
657                  if (f == NULL) {
658 <                        advinp();
658 >                        advinp();       /* got one! */
659                          return(1);
660                  }
661 <                resetinp();
661 >                resetinp();             /* eat false start */
662                  if (eatline) {          /* eat rest of line */
663                          while (ipb.chr != '\n') {
664                                  if (ipb.chr == EOF)
665                                          return(0);
666 <                                scaninp();
666 >                                resetinp();
667                          }
668 <                        scaninp();
614 <                        advinp();
668 >                        resetinp();
669                  }
670          }
671          return(0);
# Line 808 | Line 862 | resetinp(void)                      /* rewind position
862                  ipb.beg = ipb.end;
863          ipb.pos = ipb.beg;
864          ipb.chr = *ipb.pos;
865 +        if (passive)                    /* transmit unmatched character? */
866 +                fputc(ipb.chr, stdout);
867          if (++ipb.beg >= &inpbuf[INBSIZ])
868                  ipb.beg = inpbuf;
869          scaninp();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines