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.21 by greg, Sun Jun 14 00:33:16 2009 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  itype = 'a';               /* input type (a/f/F/d/D) */
85   int  nbicols = 0;               /* number of binary input columns */
86 < int  bocols = 0;                /* produce binary output columns */
86 > int  otype = 'a';               /* output format (a/f/F/d/D) */
87   char  inpbuf[INBSIZ];           /* input buffer */
88   double  colval[MAXCOL];         /* input column values */
89   unsigned long  colflg = 0;      /* column retrieved flags */
# Line 112 | Line 116 | char  *argv[]
116          biggerlib();
117   #endif
118          varset("PI", ':', 3.14159265358979323846);
119 +        funset("in", 1, '=', &l_in);
120  
121          for (i = 1; i < argc && argv[i][0] == '-'; i++)
122                  switch (argv[i][1]) {
# Line 121 | Line 126 | char  *argv[]
126                  case 'l':
127                          igneol = !igneol;
128                          break;
129 +                case 'p':
130 +                        passive = !passive;
131 +                        break;
132                  case 't':
133                          sepchar = argv[i][2];
134                          break;
# Line 139 | Line 147 | char  *argv[]
147                  case 'i':
148                          switch (argv[i][2]) {
149                          case '\0':
150 +                                itype = 'a';
151                                  nbicols = 0;
152                                  readfmt(argv[++i], 0);
153                                  break;
154                          case 'a':
155 +                                itype = 'a';
156                                  nbicols = 0;
157                                  break;
158                          case 'd':
159 +                        case 'D':
160 +                                itype = argv[i][2];
161                                  if (isdigit(argv[i][3]))
162                                          nbicols = atoi(argv[i]+3);
163                                  else
164                                          nbicols = 1;
165 +                                if (nbicols*sizeof(double) > INBSIZ) {
166 +                                        eputs(argv[0]);
167 +                                        eputs(": too many input columns\n");
168 +                                        quit(1);
169 +                                }
170                                  break;
171                          case 'f':
172 +                        case 'F':
173 +                                itype = argv[i][2];
174                                  if (isdigit(argv[i][3]))
175 <                                        nbicols = -atoi(argv[i]+3);
175 >                                        nbicols = atoi(argv[i]+3);
176                                  else
177 <                                        nbicols = -1;
177 >                                        nbicols = 1;
178 >                                if (nbicols*sizeof(float) > INBSIZ) {
179 >                                        eputs(argv[0]);
180 >                                        eputs(": too many input columns\n");
181 >                                        quit(1);
182 >                                }
183                                  break;
184                          default:
185                                  goto userr;
# Line 164 | Line 188 | char  *argv[]
188                  case 'o':
189                          switch (argv[i][2]) {
190                          case '\0':
191 <                                bocols = 0;
191 >                                otype = 'a';
192                                  readfmt(argv[++i], 1);
193                                  break;
194                          case 'a':
195 <                                bocols = 0;
195 >                                otype = 'a';
196                                  break;
197                          case 'd':
198 <                                bocols = 1;
175 <                                break;
198 >                        case 'D':
199                          case 'f':
200 <                                bocols = -1;
200 >                        case 'F':
201 >                                otype = argv[i][2];
202                                  break;
203 +                        default:
204 +                                goto userr;
205                          }
206                          break;
207                  case 'w':
# Line 188 | Line 214 | char  *argv[]
214                  userr:
215                          eputs("Usage: ");
216                          eputs(argv[0]);
217 < eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
217 > eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
218                          quit(1);
219                  }
220 <
220 >        if (otype != 'a')
221 >                SET_FILE_BINARY(stdout);
222          if (noinput) {          /* produce a single output record */
223 +                if (i < argc) {
224 +                        eputs(argv[0]);
225 +                        eputs(": file argument(s) incompatible with -n\n");
226 +                        quit(1);
227 +                }
228                  eclock++;
229                  putout();
230                  quit(0);
231          }
232 +        if (itype != 'a')
233 +                SET_FILE_BINARY(stdin);
234  
235          if (blnkeq)             /* for efficiency */
236                  nbsynch();
# Line 208 | Line 242 | eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e exp
242                          execute(argv[i]);
243          
244          quit(0);
245 +        return 0; /* pro forma return */
246   }
247  
248  
# Line 223 | Line 258 | nbsynch(void)               /* non-blank starting sync
258   }
259  
260  
261 < int
261 > static int
262   getinputrec(            /* get next input record */
263   FILE  *fp
264   )
265   {
266          if (inpfmt != NULL)
267                  return(getrec());
268 <        if (nbicols > 0)
269 <                return(fread(inpbuf, sizeof(double),
270 <                                        nbicols, fp) == nbicols);
271 <        if (nbicols < 0)
272 <                return(fread(inpbuf, sizeof(float),
273 <                                        -nbicols, fp) == -nbicols);
268 >        if (tolower(itype) == 'd') {
269 >                if (fread(inpbuf, sizeof(double), nbicols, fp) != nbicols)
270 >                        return(0);
271 >                if (itype == 'D')
272 >                        swap64(inpbuf, nbicols);
273 >                return(1);
274 >        }
275 >        if (tolower(itype) == 'f') {
276 >                if (fread(inpbuf, sizeof(float), nbicols, fp) != nbicols)
277 >                        return(0);
278 >                if (itype == 'F')
279 >                        swap32(inpbuf, nbicols);
280 >                return(1);
281 >        }
282          return(fgets(inpbuf, INBSIZ, fp) != NULL);
283   }
284  
# Line 262 | Line 305 | char  *file
305          
306          while (getinputrec(fp)) {
307                  varset("recno", '=', (double)++nrecs);
308 +                varset("outno", '=', (double)(nout+1));
309                  colflg = 0;
310                  eclock++;
311                  if (!conditional || varvalue("cond") > 0.0) {
268                        varset("outno", '=', (double)++nout);
312                          putout();
313 +                        ++nout;
314                  }
315          }
316          fclose(fp);
# Line 280 | Line 324 | putout(void)                /* produce an output recor
324          colpos = 0;
325          if (outfmt != NULL)
326                  putrec();
327 <        else if (bocols)
284 <                chanout(bchanset);
285 <        else
327 >        else if (otype == 'a')
328                  chanout(chanset);
329 <        if (colpos && !bocols)
329 >        else
330 >                chanout(bchanset);
331 >        if (colpos && otype == 'a')
332                  putchar('\n');
333          if (unbuff)
334                  fflush(stdout);
335   }
336  
337  
338 + static double
339 + l_in(char *funame)      /* function call for $channel */
340 + {
341 +        int  n;
342 +        register char  *cp;
343 +                        /* get argument as integer */
344 +        n = (int)(argument(1) + .5);
345 +        if (n != 0)     /* return channel value */
346 +                return(chanvalue(n));
347 +                        /* determine number of channels */
348 +        if (noinput || inpfmt != NULL)
349 +                return(0);
350 +        if (nbicols)
351 +                return(nbicols);
352 +        cp = inpbuf;    /* need to count */
353 +        for (n = 0; *cp; )
354 +                if (blnkeq && isspace(sepchar)) {
355 +                        while (isspace(*cp))
356 +                                cp++;
357 +                        n += *cp != '\0';
358 +                        while (*cp && !isspace(*cp))
359 +                                cp++;
360 +                } else {
361 +                        n += *cp != '\n';
362 +                        while (*cp && *cp++ != sepchar)
363 +                                ;
364 +                }
365 +        return(n);
366 + }
367 +
368   double
369   chanvalue(            /* return value for column n */
370   int  n
# Line 307 | Line 381 | int  n
381                  eputs("illegal channel number\n");
382                  quit(1);
383          }
384 <        if (nbicols > 0) {
384 >        if (nbicols) {
385                  if (n > nbicols)
386                          return(0.0);
387 <                cp = inpbuf + (n-1)*sizeof(double);
388 <                return(*(double *)cp);
389 <        }
390 <        if (nbicols < 0) {
317 <                if (n > -nbicols)
318 <                        return(0.0);
387 >                if (tolower(itype) == 'd') {
388 >                        cp = inpbuf + (n-1)*sizeof(double);
389 >                        return(*(double *)cp);
390 >                }
391                  cp = inpbuf + (n-1)*sizeof(float);
392                  return(*(float *)cp);
393          }
# Line 367 | Line 439 | double  v
439   )
440   {
441          static char     zerobuf[sizeof(double)];
442 +        float   fval = v;
443  
444          while (++colpos < n)
445                  fwrite(zerobuf,
446 <                        bocols>0 ? sizeof(double) : sizeof(float),
446 >                        tolower(otype)=='d' ? sizeof(double) : sizeof(float),
447                          1, stdout);
448 <        if (bocols > 0)
448 >        switch (otype) {
449 >        case 'D':
450 >                swap64((char *)&v, 1);
451 >                /* fall through */
452 >        case 'd':
453                  fwrite(&v, sizeof(double), 1, stdout);
454 <        else {
455 <                float   fval = v;
454 >                break;
455 >        case 'F':
456 >                swap32((char *)&fval, 1);
457 >                /* fall through */
458 >        case 'f':
459                  fwrite(&fval, sizeof(float), 1, stdout);
460 +                break;
461          }
462   }
463  
# Line 404 | Line 485 | int  output
485                          eputs(": cannot open\n");
486                          quit(1);
487                  }
488 <                res = read(fd, inpbuf+1, INBSIZ-1);
488 >                res = read(fd, inpbuf+2, INBSIZ-2);
489                  if (res <= 0 || res >= INBSIZ-1) {
490                          eputs(spec);
491                          if (res < 0)
# Line 416 | Line 497 | int  output
497                          quit(1);
498                  }
499                  close(fd);
500 <                (inptr=inpbuf+1)[res] = '\0';
500 >                (inptr=inpbuf+2)[res] = '\0';
501          }
502          f = &fmt;                               /* get fields */
503          while ((res = readfield(&inptr)) != F_NUL) {
# Line 578 | Line 659 | clearrec(void)                 /* clear input record variables */
659  
660  
661   static int
662 < getrec(void)                                /* get next record from file */
662 > getrec(void)                            /* get next record from file */
663   {
664          int  eatline;
665          register struct field  *f;
666 <        
666 >
667          while (ipb.chr != EOF) {
668 <                eatline = !igneol && ipb.chr != '\n';
588 <                if (blnkeq)             /* beware of nbsynch() */
668 >                if (blnkeq) {           /* beware of nbsynch() */
669                          while (isblnk(ipb.chr))
670 <                                scaninp();
670 >                                resetinp();
671 >                        if (ipb.chr == EOF)
672 >                                return(0);
673 >                }
674 >                eatline = (!igneol && ipb.chr != '\n');
675                  clearrec();             /* start with fresh record */
676                  for (f = inpfmt; f != NULL; f = f->next)
677                          if (getfield(f) == -1)
678                                  break;
679                  if (f == NULL) {
680 <                        advinp();
680 >                        advinp();       /* got one! */
681                          return(1);
682                  }
683 <                resetinp();
683 >                resetinp();             /* eat false start */
684                  if (eatline) {          /* eat rest of line */
685                          while (ipb.chr != '\n') {
686                                  if (ipb.chr == EOF)
687                                          return(0);
688 <                                scaninp();
688 >                                resetinp();
689                          }
690 <                        scaninp();
607 <                        advinp();
690 >                        resetinp();
691                  }
692          }
693          return(0);
# Line 616 | Line 699 | getfield(                             /* get next fiel
699   register struct field  *f
700   )
701   {
702 <        static char  buf[MAXWORD+1];            /* no recursion! */
702 >        static char  buf[RMAXWORD+1];            /* no recursion! */
703          int  delim, inword;
704          double  d;
705          char  *np;
# Line 649 | Line 732 | register struct field  *f
732                          delim = f->next->f.sl[0];
733                  cp = buf;
734                  do {
735 <                        if (ipb.chr == EOF)
735 >                        if (ipb.chr == EOF || ipb.chr == '\n')
736                                  inword = 0;
737                          else if (blnkeq && delim != EOF)
738                                  inword = isblnk(delim) ?
# Line 661 | Line 744 | register struct field  *f
744                                  *cp++ = ipb.chr;
745                                  scaninp();
746                          }
747 <                } while (inword && cp < &buf[MAXWORD]);
747 >                } while (inword && cp < &buf[RMAXWORD]);
748                  *cp = '\0';
749                  if (f->f.sv->val == NULL)
750                          f->f.sv->val = savqstr(buf);    /* first setting */
# Line 690 | Line 773 | register struct field  *f
773                                  *cp++ = ipb.chr;
774                                  scaninp();
775                          }
776 <                } while (inword && cp < &buf[MAXWORD]);
776 >                } while (inword && cp < &buf[RMAXWORD]);
777                  *cp = '\0';
778                  d = np==NULL ? 0. : atof(np);
779                  if (!vardefined(f->f.nv))
# Line 801 | Line 884 | resetinp(void)                      /* rewind position
884                  ipb.beg = ipb.end;
885          ipb.pos = ipb.beg;
886          ipb.chr = *ipb.pos;
887 +        if (passive)                    /* transmit unmatched character? */
888 +                fputc(ipb.chr, stdout);
889          if (++ipb.beg >= &inpbuf[INBSIZ])
890                  ipb.beg = inpbuf;
891          scaninp();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines