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.9 by greg, Wed Nov 5 19:03:03 2003 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  <stdio.h>
12 > #include  <string.h>
13   #include  <math.h>
15
14   #include  <ctype.h>
15  
16 + #include  "platform.h"
17   #include  "calcomp.h"
18 + #include  "rterror.h"
19  
20 #ifdef  CPM
21 #define  getc           agetc   /* text files only, right? */
22 #endif
23
20   #define  isnum(c)       (isdigit(c) || (c)=='-' || (c)=='.' \
21                                  || (c)=='+' || (c)=='e' || (c)=='E')
22  
# Line 58 | Line 54 | struct field {                  /* record format struc
54   #define  savqstr(s)     strcpy(emalloc(strlen(s)+1),s)
55   #define  freqstr(s)     efree(s)
56  
57 < extern char  *strcpy(), *emalloc(), *savestr();
58 < struct strvar  *getsvar();
57 > static void scaninp(void), advinp(void), resetinp(void);
58 > static void putrec(void), putout(void), nbsynch(void);
59 > static int getrec(void);
60 > static void execute(char *file);
61 > static void initinp(FILE *fp);
62 > static void svpreset(char *eqn);
63 > static void readfmt(char *spec, int output);
64 > static int readfield(char **pp);
65 > static int getfield(struct field *f);
66 > static void chanset(int n, double v);
67 > static void bchanset(int n, double v);
68 > static struct strvar* getsvar(char *svname);
69  
70   struct field  *inpfmt = NULL;   /* input record format */
71   struct field  *outfmt = NULL;   /* output record structure */
# Line 69 | Line 75 | int  blnkeq = 1;                /* blanks compare equa
75   int  igneol = 0;                /* ignore end of line? */
76   char  sepchar = '\t';           /* input/output separator */
77   int  noinput = 0;               /* no input records? */
78 + int  nbicols = 0;               /* number of binary input columns */
79 + int  bocols = 0;                /* produce binary output columns */
80   char  inpbuf[INBSIZ];           /* input buffer */
81   double  colval[MAXCOL];         /* input column values */
82   unsigned long  colflg = 0;      /* column retrieved flags */
# Line 86 | Line 94 | struct {
94   } ipb;                          /* circular lookahead buffer */
95  
96  
97 < main(argc, argv)
98 < int  argc;
99 < char  *argv[];
97 > int
98 > main(
99 > int  argc,
100 > char  *argv[]
101 > )
102   {
103          int  i;
104  
105 <        esupport |= (E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST);
105 >        esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST;
106 >        esupport &= ~(E_REDEFW);
107  
108   #ifdef  BIGGERLIB
109          biggerlib();
# Line 123 | Line 134 | char  *argv[];
134                          noinput = 1;
135                          break;
136                  case 'i':
137 <                        readfmt(argv[++i], 0);
137 >                        switch (argv[i][2]) {
138 >                        case '\0':
139 >                                nbicols = 0;
140 >                                readfmt(argv[++i], 0);
141 >                                break;
142 >                        case 'a':
143 >                                nbicols = 0;
144 >                                break;
145 >                        case 'd':
146 >                                if (isdigit(argv[i][3]))
147 >                                        nbicols = atoi(argv[i]+3);
148 >                                else
149 >                                        nbicols = 1;
150 >                                if (nbicols*sizeof(double) > INBSIZ) {
151 >                                        eputs(argv[0]);
152 >                                        eputs(": too many input columns\n");
153 >                                        quit(1);
154 >                                }
155 >                                break;
156 >                        case 'f':
157 >                                if (isdigit(argv[i][3]))
158 >                                        nbicols = -atoi(argv[i]+3);
159 >                                else
160 >                                        nbicols = -1;
161 >                                if (-nbicols*sizeof(float) > INBSIZ) {
162 >                                        eputs(argv[0]);
163 >                                        eputs(": too many input columns\n");
164 >                                        quit(1);
165 >                                }
166 >                                break;
167 >                        default:
168 >                                goto userr;
169 >                        }
170                          break;
171                  case 'o':
172 <                        readfmt(argv[++i], 1);
172 >                        switch (argv[i][2]) {
173 >                        case '\0':
174 >                                bocols = 0;
175 >                                readfmt(argv[++i], 1);
176 >                                break;
177 >                        case 'a':
178 >                                bocols = 0;
179 >                                break;
180 >                        case 'd':
181 >                                bocols = 1;
182 >                                break;
183 >                        case 'f':
184 >                                bocols = -1;
185 >                                break;
186 >                        }
187                          break;
188                  case 'w':
189                          nowarn = !nowarn;
# Line 134 | Line 191 | char  *argv[];
191                  case 'u':
192                          unbuff = !unbuff;
193                          break;
194 <                default:
194 >                default:;
195 >                userr:
196                          eputs("Usage: ");
197                          eputs(argv[0]);
198   eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
# Line 160 | Line 218 | eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e exp
218   }
219  
220  
221 < nbsynch()               /* non-blank starting synch character */
221 > static void
222 > nbsynch(void)               /* non-blank starting synch character */
223   {
224          if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT)
225                  return;
# Line 171 | Line 230 | nbsynch()               /* non-blank starting synch ch
230   }
231  
232  
233 < execute(file)           /* process a file */
234 < char  *file;
233 > int
234 > getinputrec(            /* get next input record */
235 > FILE  *fp
236 > )
237   {
238 +        if (inpfmt != NULL)
239 +                return(getrec());
240 +        if (nbicols > 0)
241 +                return(fread(inpbuf, sizeof(double),
242 +                                        nbicols, fp) == nbicols);
243 +        if (nbicols < 0)
244 +                return(fread(inpbuf, sizeof(float),
245 +                                        -nbicols, fp) == -nbicols);
246 +        return(fgets(inpbuf, INBSIZ, fp) != NULL);
247 + }
248 +
249 +
250 + static void
251 + execute(           /* process a file */
252 + char  *file
253 + )
254 + {
255          int  conditional = vardefined("cond");
256          long  nrecs = 0;
257          long  nout = 0;
# Line 188 | Line 266 | char  *file;
266          }
267          if (inpfmt != NULL)
268                  initinp(fp);
269 <                
270 <        while (inpfmt != NULL ? getrec() : fgets(inpbuf, INBSIZ, fp) != NULL) {
269 >        
270 >        while (getinputrec(fp)) {
271                  varset("recno", '=', (double)++nrecs);
272                  colflg = 0;
273                  eclock++;
# Line 202 | Line 280 | char  *file;
280   }
281  
282  
283 < putout()                /* produce an output record */
283 > static void
284 > putout(void)                /* produce an output record */
285   {
207        extern int  chanset();
286  
287          colpos = 0;
288          if (outfmt != NULL)
289                  putrec();
290 +        else if (bocols)
291 +                chanout(bchanset);
292          else
293                  chanout(chanset);
294 <        if (colpos)
294 >        if (colpos && !bocols)
295                  putchar('\n');
296          if (unbuff)
297                  fflush(stdout);
# Line 219 | Line 299 | putout()                /* produce an output record */
299  
300  
301   double
302 < chanvalue(n)            /* return value for column n */
303 < int  n;
302 > chanvalue(            /* return value for column n */
303 > int  n
304 > )
305   {
306          int  i;
307          register char  *cp;
# Line 233 | Line 314 | int  n;
314                  eputs("illegal channel number\n");
315                  quit(1);
316          }
317 +        if (nbicols > 0) {
318 +                if (n > nbicols)
319 +                        return(0.0);
320 +                cp = inpbuf + (n-1)*sizeof(double);
321 +                return(*(double *)cp);
322 +        }
323 +        if (nbicols < 0) {
324 +                if (n > -nbicols)
325 +                        return(0.0);
326 +                cp = inpbuf + (n-1)*sizeof(float);
327 +                return(*(float *)cp);
328 +        }
329          if (n <= MAXCOL && colflg & 1L<<(n-1))
330                  return(colval[n-1]);
331  
# Line 258 | Line 351 | int  n;
351   }
352  
353  
354 < chanset(n, v)                   /* output column n */
355 < int  n;
356 < double  v;
354 > void
355 > chanset(                   /* output column n */
356 > int  n,
357 > double  v
358 > )
359   {
360          if (colpos == 0)                /* no leading separator */
361                  colpos = 1;
# Line 272 | Line 367 | double  v;
367   }
368  
369  
370 < readfmt(spec, output)                   /* read record format */
371 < char  *spec;
372 < int  output;
370 > void
371 > bchanset(                   /* output binary channel n */
372 > int  n,
373 > double  v
374 > )
375   {
376 +        static char     zerobuf[sizeof(double)];
377 +
378 +        while (++colpos < n)
379 +                fwrite(zerobuf,
380 +                        bocols>0 ? sizeof(double) : sizeof(float),
381 +                        1, stdout);
382 +        if (bocols > 0)
383 +                fwrite(&v, sizeof(double), 1, stdout);
384 +        else {
385 +                float   fval = v;
386 +                fwrite(&fval, sizeof(float), 1, stdout);
387 +        }
388 + }
389 +
390 +
391 + static void
392 + readfmt(                   /* read record format */
393 + char  *spec,
394 + int  output
395 + )
396 + {
397          int  fd;
398          char  *inptr;
399          struct field  fmt;
# Line 338 | Line 456 | int  output;
456   }
457  
458  
459 < int
460 < readfield(pp)                   /* get next field in format */
461 < register char  **pp;
459 > static int
460 > readfield(                   /* get next field in format */
461 > register char  **pp
462 > )
463   {
464          int  type = F_NUL;
465          int  width = 0;
# Line 405 | Line 524 | register char  **pp;
524  
525  
526   struct strvar *
527 < getsvar(svname)                         /* get string variable */
528 < char  *svname;
527 > getsvar(                         /* get string variable */
528 > char  *svname
529 > )
530   {
531          register struct strvar  *sv;
532          
# Line 422 | Line 542 | char  *svname;
542   }
543  
544  
545 < svpreset(eqn)                    /* preset a string variable */
546 < char  *eqn;
545 > static void
546 > svpreset(                    /* preset a string variable */
547 > char  *eqn
548 > )
549   {
550          register struct strvar  *sv;
551          register char  *val;
# Line 442 | Line 564 | char  *eqn;
564   }
565  
566  
567 < clearrec()                      /* clear input record variables */
567 > static void
568 > clearrec(void)                  /* clear input record variables */
569   {
570          register struct field  *f;
571  
# Line 461 | Line 584 | clearrec()                     /* clear input record variables */
584   }
585  
586  
587 < getrec()                                /* get next record from file */
587 > static int
588 > getrec(void)                                /* get next record from file */
589   {
590          int  eatline;
591          register struct field  *f;
# Line 494 | Line 618 | getrec()                                /* get next re
618   }
619  
620  
621 < getfield(f)                             /* get next field */
622 < register struct field  *f;
621 > static int
622 > getfield(                             /* get next field */
623 > register struct field  *f
624 > )
625   {
626 <        static char  buf[MAXWORD+1];            /* no recursion! */
626 >        static char  buf[RMAXWORD+1];            /* no recursion! */
627          int  delim, inword;
628          double  d;
629          char  *np;
# Line 530 | Line 656 | register struct field  *f;
656                          delim = f->next->f.sl[0];
657                  cp = buf;
658                  do {
659 <                        if (ipb.chr == EOF)
659 >                        if (ipb.chr == EOF || ipb.chr == '\n')
660                                  inword = 0;
661                          else if (blnkeq && delim != EOF)
662                                  inword = isblnk(delim) ?
# Line 542 | Line 668 | register struct field  *f;
668                                  *cp++ = ipb.chr;
669                                  scaninp();
670                          }
671 <                } while (inword && cp < &buf[MAXWORD]);
671 >                } while (inword && cp < &buf[RMAXWORD]);
672                  *cp = '\0';
673                  if (f->f.sv->val == NULL)
674                          f->f.sv->val = savqstr(buf);    /* first setting */
# Line 571 | Line 697 | register struct field  *f;
697                                  *cp++ = ipb.chr;
698                                  scaninp();
699                          }
700 <                } while (inword && cp < &buf[MAXWORD]);
700 >                } while (inword && cp < &buf[RMAXWORD]);
701                  *cp = '\0';
702                  d = np==NULL ? 0. : atof(np);
703                  if (!vardefined(f->f.nv))
# Line 581 | Line 707 | register struct field  *f;
707                          return(-1);                     /* doesn't match! */
708                  return(0);
709          }
710 +        return -1; /* pro forma return */
711   }
712  
713  
714 < putrec()                                /* output a record */
714 > static void
715 > putrec(void)                                /* output a record */
716   {
717          char  fmt[32];
718          register int  n;
# Line 634 | Line 762 | putrec()                                /* output a re
762   }
763  
764  
765 < initinp(fp)                     /* prepare lookahead buffer */
766 < FILE  *fp;
765 > static void
766 > initinp(FILE  *fp)                     /* prepare lookahead buffer */
767 >
768   {
769          ipb.fin = fp;
770          ipb.beg = ipb.end = inpbuf;
# Line 645 | Line 774 | FILE  *fp;
774   }
775  
776  
777 < scaninp()                       /* scan next character */
777 > static void
778 > scaninp(void)                       /* scan next character */
779   {
780          if (ipb.chr == EOF)
781                  return;
# Line 664 | Line 794 | scaninp()                       /* scan next character
794   }
795  
796  
797 < advinp()                        /* move home to current position */
797 > static void
798 > advinp(void)                        /* move home to current position */
799   {
800          ipb.beg = ipb.pos;
801   }
802  
803  
804 < resetinp()                      /* rewind position and advance 1 */
804 > static void
805 > resetinp(void)                      /* rewind position and advance 1 */
806   {
807          if (ipb.beg == NULL)            /* full */
808                  ipb.beg = ipb.end;
# Line 683 | Line 815 | resetinp()                      /* rewind position and
815  
816  
817   void
818 < eputs(msg)
687 < char  *msg;
818 > eputs(char  *msg)
819   {
820          fputs(msg, stderr);
821   }
822  
823  
824   void
825 < wputs(msg)
695 < char  *msg;
825 > wputs(char  *msg)
826   {
827          if (!nowarn)
828                  eputs(msg);
# Line 700 | Line 830 | char  *msg;
830  
831  
832   void
833 < quit(code)
704 < int  code;
833 > quit(int  code)
834   {
835          exit(code);
836   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines