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.7 by schorsch, Sun Jul 27 22:12:01 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? */
# Line 58 | Line 58 | struct field {                  /* record format struc
58   #define  savqstr(s)     strcpy(emalloc(strlen(s)+1),s)
59   #define  freqstr(s)     efree(s)
60  
61 < extern char  *strcpy(), *emalloc(), *savestr();
62 < struct strvar  *getsvar();
61 > static void scaninp(void), advinp(void), resetinp(void);
62 > static void putrec(void), putout(void), nbsynch(void);
63 > static int getrec(void);
64 > static void execute(char *file);
65 > static void initinp(FILE *fp);
66 > static void svpreset(char *eqn);
67 > static void readfmt(char *spec, int output);
68 > static int readfield(char **pp);
69 > 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  
74   struct field  *inpfmt = NULL;   /* input record format */
75   struct field  *outfmt = NULL;   /* output record structure */
# Line 69 | Line 79 | int  blnkeq = 1;                /* blanks compare equa
79   int  igneol = 0;                /* ignore end of line? */
80   char  sepchar = '\t';           /* input/output separator */
81   int  noinput = 0;               /* no input records? */
82 + int  nbicols = 0;               /* number of binary input columns */
83 + int  bocols = 0;                /* produce binary output columns */
84   char  inpbuf[INBSIZ];           /* input buffer */
85   double  colval[MAXCOL];         /* input column values */
86   unsigned long  colflg = 0;      /* column retrieved flags */
# Line 86 | Line 98 | struct {
98   } ipb;                          /* circular lookahead buffer */
99  
100  
101 < main(argc, argv)
102 < int  argc;
103 < char  *argv[];
101 > int
102 > main(
103 > int  argc,
104 > char  *argv[]
105 > )
106   {
107          int  i;
108  
109 <        esupport |= (E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST);
109 >        esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST;
110 >        esupport &= ~(E_REDEFW);
111  
112   #ifdef  BIGGERLIB
113          biggerlib();
# Line 123 | Line 138 | char  *argv[];
138                          noinput = 1;
139                          break;
140                  case 'i':
141 <                        readfmt(argv[++i], 0);
141 >                        switch (argv[i][2]) {
142 >                        case '\0':
143 >                                nbicols = 0;
144 >                                readfmt(argv[++i], 0);
145 >                                break;
146 >                        case 'a':
147 >                                nbicols = 0;
148 >                                break;
149 >                        case 'd':
150 >                                if (isdigit(argv[i][3]))
151 >                                        nbicols = atoi(argv[i]+3);
152 >                                else
153 >                                        nbicols = 1;
154 >                                break;
155 >                        case 'f':
156 >                                if (isdigit(argv[i][3]))
157 >                                        nbicols = -atoi(argv[i]+3);
158 >                                else
159 >                                        nbicols = -1;
160 >                                break;
161 >                        default:
162 >                                goto userr;
163 >                        }
164                          break;
165                  case 'o':
166 <                        readfmt(argv[++i], 1);
166 >                        switch (argv[i][2]) {
167 >                        case '\0':
168 >                                bocols = 0;
169 >                                readfmt(argv[++i], 1);
170 >                                break;
171 >                        case 'a':
172 >                                bocols = 0;
173 >                                break;
174 >                        case 'd':
175 >                                bocols = 1;
176 >                                break;
177 >                        case 'f':
178 >                                bocols = -1;
179 >                                break;
180 >                        }
181                          break;
182                  case 'w':
183                          nowarn = !nowarn;
# Line 134 | Line 185 | char  *argv[];
185                  case 'u':
186                          unbuff = !unbuff;
187                          break;
188 <                default:
188 >                default:;
189 >                userr:
190                          eputs("Usage: ");
191                          eputs(argv[0]);
192   eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
# Line 160 | Line 212 | eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e exp
212   }
213  
214  
215 < nbsynch()               /* non-blank starting synch character */
215 > static void
216 > nbsynch(void)               /* non-blank starting synch character */
217   {
218          if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT)
219                  return;
# Line 171 | Line 224 | nbsynch()               /* non-blank starting synch ch
224   }
225  
226  
227 < execute(file)           /* process a file */
228 < char  *file;
227 > int
228 > getinputrec(            /* get next input record */
229 > FILE  *fp
230 > )
231   {
232 +        if (inpfmt != NULL)
233 +                return(getrec());
234 +        if (nbicols > 0)
235 +                return(fread(inpbuf, sizeof(double),
236 +                                        nbicols, fp) == nbicols);
237 +        if (nbicols < 0)
238 +                return(fread(inpbuf, sizeof(float),
239 +                                        -nbicols, fp) == -nbicols);
240 +        return(fgets(inpbuf, INBSIZ, fp) != NULL);
241 + }
242 +
243 +
244 + static void
245 + execute(           /* process a file */
246 + char  *file
247 + )
248 + {
249          int  conditional = vardefined("cond");
250          long  nrecs = 0;
251          long  nout = 0;
# Line 188 | Line 260 | char  *file;
260          }
261          if (inpfmt != NULL)
262                  initinp(fp);
263 <                
264 <        while (inpfmt != NULL ? getrec() : fgets(inpbuf, INBSIZ, fp) != NULL) {
263 >        
264 >        while (getinputrec(fp)) {
265                  varset("recno", '=', (double)++nrecs);
266                  colflg = 0;
267                  eclock++;
# Line 202 | Line 274 | char  *file;
274   }
275  
276  
277 < putout()                /* produce an output record */
277 > static void
278 > putout(void)                /* produce an output record */
279   {
207        extern int  chanset();
280  
281          colpos = 0;
282          if (outfmt != NULL)
283                  putrec();
284 +        else if (bocols)
285 +                chanout(bchanset);
286          else
287                  chanout(chanset);
288 <        if (colpos)
288 >        if (colpos && !bocols)
289                  putchar('\n');
290          if (unbuff)
291                  fflush(stdout);
# Line 219 | Line 293 | putout()                /* produce an output record */
293  
294  
295   double
296 < chanvalue(n)            /* return value for column n */
297 < int  n;
296 > chanvalue(            /* return value for column n */
297 > int  n
298 > )
299   {
300          int  i;
301          register char  *cp;
# Line 233 | Line 308 | int  n;
308                  eputs("illegal channel number\n");
309                  quit(1);
310          }
311 +        if (nbicols > 0) {
312 +                if (n > nbicols)
313 +                        return(0.0);
314 +                cp = inpbuf + (n-1)*sizeof(double);
315 +                return(*(double *)cp);
316 +        }
317 +        if (nbicols < 0) {
318 +                if (n > -nbicols)
319 +                        return(0.0);
320 +                cp = inpbuf + (n-1)*sizeof(float);
321 +                return(*(float *)cp);
322 +        }
323          if (n <= MAXCOL && colflg & 1L<<(n-1))
324                  return(colval[n-1]);
325  
# Line 258 | Line 345 | int  n;
345   }
346  
347  
348 < chanset(n, v)                   /* output column n */
349 < int  n;
350 < double  v;
348 > void
349 > chanset(                   /* output column n */
350 > int  n,
351 > double  v
352 > )
353   {
354          if (colpos == 0)                /* no leading separator */
355                  colpos = 1;
# Line 272 | Line 361 | double  v;
361   }
362  
363  
364 < readfmt(spec, output)                   /* read record format */
365 < char  *spec;
366 < int  output;
364 > void
365 > bchanset(                   /* output binary channel n */
366 > int  n,
367 > double  v
368 > )
369   {
370 +        static char     zerobuf[sizeof(double)];
371 +
372 +        while (++colpos < n)
373 +                fwrite(zerobuf,
374 +                        bocols>0 ? sizeof(double) : sizeof(float),
375 +                        1, stdout);
376 +        if (bocols > 0)
377 +                fwrite(&v, sizeof(double), 1, stdout);
378 +        else {
379 +                float   fval = v;
380 +                fwrite(&fval, sizeof(float), 1, stdout);
381 +        }
382 + }
383 +
384 +
385 + static void
386 + readfmt(                   /* read record format */
387 + char  *spec,
388 + int  output
389 + )
390 + {
391          int  fd;
392          char  *inptr;
393          struct field  fmt;
# Line 338 | Line 450 | int  output;
450   }
451  
452  
453 < int
454 < readfield(pp)                   /* get next field in format */
455 < register char  **pp;
453 > static int
454 > readfield(                   /* get next field in format */
455 > register char  **pp
456 > )
457   {
458          int  type = F_NUL;
459          int  width = 0;
# Line 405 | Line 518 | register char  **pp;
518  
519  
520   struct strvar *
521 < getsvar(svname)                         /* get string variable */
522 < char  *svname;
521 > getsvar(                         /* get string variable */
522 > char  *svname
523 > )
524   {
525          register struct strvar  *sv;
526          
# Line 422 | Line 536 | char  *svname;
536   }
537  
538  
539 < svpreset(eqn)                    /* preset a string variable */
540 < char  *eqn;
539 > static void
540 > svpreset(                    /* preset a string variable */
541 > char  *eqn
542 > )
543   {
544          register struct strvar  *sv;
545          register char  *val;
# Line 442 | Line 558 | char  *eqn;
558   }
559  
560  
561 < clearrec()                      /* clear input record variables */
561 > static void
562 > clearrec(void)                  /* clear input record variables */
563   {
564          register struct field  *f;
565  
# Line 461 | Line 578 | clearrec()                     /* clear input record variables */
578   }
579  
580  
581 < getrec()                                /* get next record from file */
581 > static int
582 > getrec(void)                                /* get next record from file */
583   {
584          int  eatline;
585          register struct field  *f;
# Line 494 | Line 612 | getrec()                                /* get next re
612   }
613  
614  
615 < getfield(f)                             /* get next field */
616 < register struct field  *f;
615 > static int
616 > getfield(                             /* get next field */
617 > register struct field  *f
618 > )
619   {
620 <        static char  buf[MAXWORD+1];            /* no recursion! */
620 >        static char  buf[RMAXWORD+1];            /* no recursion! */
621          int  delim, inword;
622          double  d;
623          char  *np;
# Line 542 | Line 662 | register struct field  *f;
662                                  *cp++ = ipb.chr;
663                                  scaninp();
664                          }
665 <                } while (inword && cp < &buf[MAXWORD]);
665 >                } while (inword && cp < &buf[RMAXWORD]);
666                  *cp = '\0';
667                  if (f->f.sv->val == NULL)
668                          f->f.sv->val = savqstr(buf);    /* first setting */
# Line 571 | Line 691 | register struct field  *f;
691                                  *cp++ = ipb.chr;
692                                  scaninp();
693                          }
694 <                } while (inword && cp < &buf[MAXWORD]);
694 >                } while (inword && cp < &buf[RMAXWORD]);
695                  *cp = '\0';
696                  d = np==NULL ? 0. : atof(np);
697                  if (!vardefined(f->f.nv))
# Line 581 | Line 701 | register struct field  *f;
701                          return(-1);                     /* doesn't match! */
702                  return(0);
703          }
704 +        return -1; /* pro forma return */
705   }
706  
707  
708 < putrec()                                /* output a record */
708 > static void
709 > putrec(void)                                /* output a record */
710   {
711          char  fmt[32];
712          register int  n;
# Line 634 | Line 756 | putrec()                                /* output a re
756   }
757  
758  
759 < initinp(fp)                     /* prepare lookahead buffer */
760 < FILE  *fp;
759 > static void
760 > initinp(FILE  *fp)                     /* prepare lookahead buffer */
761 >
762   {
763          ipb.fin = fp;
764          ipb.beg = ipb.end = inpbuf;
# Line 645 | Line 768 | FILE  *fp;
768   }
769  
770  
771 < scaninp()                       /* scan next character */
771 > static void
772 > scaninp(void)                       /* scan next character */
773   {
774          if (ipb.chr == EOF)
775                  return;
# Line 664 | Line 788 | scaninp()                       /* scan next character
788   }
789  
790  
791 < advinp()                        /* move home to current position */
791 > static void
792 > advinp(void)                        /* move home to current position */
793   {
794          ipb.beg = ipb.pos;
795   }
796  
797  
798 < resetinp()                      /* rewind position and advance 1 */
798 > static void
799 > resetinp(void)                      /* rewind position and advance 1 */
800   {
801          if (ipb.beg == NULL)            /* full */
802                  ipb.beg = ipb.end;
# Line 683 | Line 809 | resetinp()                      /* rewind position and
809  
810  
811   void
812 < eputs(msg)
687 < char  *msg;
812 > eputs(char  *msg)
813   {
814          fputs(msg, stderr);
815   }
816  
817  
818   void
819 < wputs(msg)
695 < char  *msg;
819 > wputs(char  *msg)
820   {
821          if (!nowarn)
822                  eputs(msg);
# Line 700 | Line 824 | char  *msg;
824  
825  
826   void
827 < quit(code)
704 < int  code;
827 > quit(int  code)
828   {
829          exit(code);
830   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines