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.24 by greg, Sat May 10 01:27:14 2014 UTC vs.
Revision 1.30 by greg, Tue Dec 10 19:00:26 2019 UTC

# Line 17 | Line 17 | static const char RCSid[] = "$Id$";
17   #include  "rtio.h"
18   #include  "calcomp.h"
19  
20 < #define  isnum(c)       (isdigit(c) || (c)=='-' || (c)=='.' \
21 <                                || (c)=='+' || (c)=='e' || (c)=='E')
20 > #define  isnum(c)       (isdigit(c) || ((c)=='-') | ((c)=='.') \
21 >                                | ((c)=='+') | ((c)=='e') | ((c)=='E'))
22  
23 < #define  isblnk(c)      (igneol ? isspace(c) : (c)==' '||(c)=='\t')
23 > #define  isblnk(c)      (igneol ? isspace(c) : ((c)==' ')|((c)=='\t'))
24  
25   #define  INBSIZ         16384   /* longest record */
26   #define  MAXCOL         32      /* number of columns recorded */
# Line 73 | Line 73 | struct field  *inpfmt = NULL;   /* input record format
73   struct field  *outfmt = NULL;   /* output record structure */
74   struct strvar  *svhead = NULL;  /* string variables */
75  
76 + long  incnt = 0;                /* limit number of input records? */
77 + long  outcnt = 0;               /* limit number of output records? */
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) */
# Line 157 | Line 160 | char  *argv[]
160                                  nbicols = 0;
161                                  readfmt(argv[++i], 0);
162                                  break;
163 +                        case 'n':
164 +                                incnt = atol(argv[++i]);
165 +                                break;
166                          case 'a':
167                                  itype = 'a';
168                                  nbicols = 0;
# Line 197 | Line 203 | char  *argv[]
203                                  otype = 'a';
204                                  readfmt(argv[++i], 1);
205                                  break;
206 +                        case 'n':
207 +                                outcnt = atol(argv[++i]);
208 +                                break;
209                          case 'a':
210                                  otype = 'a';
211                                  break;
# Line 271 | Line 280 | FILE  *fp
280   {
281          if (inpfmt != NULL)
282                  return(getrec());
283 <        if (tolower(itype) == 'd') {
284 <                if (fread(inpbuf, sizeof(double), nbicols, fp) != nbicols)
283 >        if ((itype == 'd') | (itype == 'D')) {
284 >                if (getbinary(inpbuf, sizeof(double), nbicols, fp) != nbicols)
285                          return(0);
286                  if (itype == 'D')
287                          swap64(inpbuf, nbicols);
288                  return(1);
289          }
290 <        if (tolower(itype) == 'f') {
291 <                if (fread(inpbuf, sizeof(float), nbicols, fp) != nbicols)
290 >        if ((itype == 'f') | (itype == 'F')) {
291 >                if (getbinary(inpbuf, sizeof(float), nbicols, fp) != nbicols)
292                          return(0);
293                  if (itype == 'F')
294                          swap32(inpbuf, nbicols);
# Line 294 | Line 303 | execute(           /* process a file */
303   char  *file
304   )
305   {
306 <        int  conditional = vardefined("cond");
306 >        const int  conditional = vardefined("cond");
307 >        const int  set_recno = (varlookup("recno") != NULL);
308 >        const int  set_outno = (varlookup("outno") != NULL);
309          long  nrecs = 0;
310          long  nout = 0;
311          FILE  *fp;
# Line 315 | Line 326 | char  *file
326                  initinp(fp);
327          
328          while (getinputrec(fp)) {
329 <                varset("recno", '=', (double)++nrecs);
330 <                varset("outno", '=', (double)(nout+1));
329 >                ++nrecs;
330 >                if (set_recno)
331 >                        varset("recno", '=', (double)nrecs);
332 >                if (set_outno)
333 >                        varset("outno", '=', (double)(nout+1));
334                  colflg = 0;
335                  eclock++;
336                  if (!conditional || varvalue("cond") > 0.0) {
337                          putout();
338                          ++nout;
339                  }
340 +                if (incnt && nrecs >= incnt)
341 +                        break;
342 +                if (outcnt && nout >= outcnt)
343 +                        break;
344          }
345          fclose(fp);
346   }
# Line 395 | Line 413 | int  n
413          if (nbicols) {
414                  if (n > nbicols)
415                          return(0.0);
416 <                if (tolower(itype) == 'd') {
416 >                if ((itype == 'd') | (itype == 'D')) {
417                          cp = inpbuf + (n-1)*sizeof(double);
418                          return(*(double *)cp);
419                  }
# Line 450 | Line 468 | double  v
468   )
469   {
470          static char     zerobuf[sizeof(double)];
471 +        const int       otlen = ((otype == 'd') | (otype == 'D')) ?
472 +                                        sizeof(double) : sizeof(float);
473          float   fval = v;
474  
475          while (++colpos < n)
476 <                fwrite(zerobuf,
457 <                        tolower(otype)=='d' ? sizeof(double) : sizeof(float),
458 <                        1, stdout);
476 >                putbinary(zerobuf, otlen, 1, stdout);
477          switch (otype) {
478          case 'D':
479                  swap64((char *)&v, 1);
480                  /* fall through */
481          case 'd':
482 <                fwrite(&v, sizeof(double), 1, stdout);
482 >                putbinary(&v, sizeof(double), 1, stdout);
483                  break;
484          case 'F':
485                  swap32((char *)&fval, 1);
486                  /* fall through */
487          case 'f':
488 <                fwrite(&fval, sizeof(float), 1, stdout);
488 >                putbinary(&fval, sizeof(float), 1, stdout);
489                  break;
490          }
491   }
# Line 682 | Line 700 | getrec(void)                           /* get next record from file */
700                          if (ipb.chr == EOF)
701                                  return(0);
702                  }
703 <                eatline = (!igneol && ipb.chr != '\n');
703 >                eatline = !igneol & (ipb.chr != '\n');
704                  clearrec();             /* start with fresh record */
705                  for (f = inpfmt; f != NULL; f = f->next)
706 <                        if (getfield(f) == -1)
706 >                        if (!getfield(f))
707                                  break;
708                  if (f == NULL) {
709                          advinp();       /* got one! */
# Line 722 | Line 740 | struct field  *f
740                  do {
741                          if (blnkeq && isblnk(*cp)) {
742                                  if (!isblnk(ipb.chr))
743 <                                        return(-1);
743 >                                        return(0);
744                                  do
745                                          cp++;
746                                  while (isblnk(*cp));
# Line 733 | Line 751 | struct field  *f
751                                  cp++;
752                                  scaninp();
753                          } else
754 <                                return(-1);
754 >                                return(0);
755                  } while (*cp);
756 <                return(0);
756 >                break;
757          case T_STR:
758                  if (f->next == NULL || (f->next->type & F_TYP) != T_LIT)
759                          delim = EOF;
# Line 743 | Line 761 | struct field  *f
761                          delim = f->next->f.sl[0];
762                  cp = buf;
763                  do {
764 <                        if (ipb.chr == EOF || ipb.chr == '\n')
764 >                        if ((ipb.chr == EOF) | (ipb.chr == '\n'))
765                                  inword = 0;
766                          else if (blnkeq && delim != EOF)
767                                  inword = isblnk(delim) ?
# Line 760 | Line 778 | struct field  *f
778                  if (f->f.sv->val == NULL)
779                          f->f.sv->val = savqstr(buf);    /* first setting */
780                  else if (strcmp(f->f.sv->val, buf))
781 <                        return(-1);                     /* doesn't match! */
782 <                return(0);
781 >                        return(0);                      /* doesn't match! */
782 >                break;
783          case T_NUM:
784                  if (f->next == NULL || (f->next->type & F_TYP) != T_LIT)
785                          delim = EOF;
# Line 791 | Line 809 | struct field  *f
809                          varset(f->f.nv, '=', d);        /* first setting */
810                  else if ((d = (varvalue(f->f.nv)-d)/(d==0.?1.:d)) > .001
811                                  || d < -.001)
812 <                        return(-1);                     /* doesn't match! */
813 <                return(0);
812 >                        return(0);                      /* doesn't match! */
813 >                break;
814          }
815 <        return -1; /* pro forma return */
815 >        return(1);      /* success! */
816   }
817  
818  
819   static void
820   putrec(void)                                /* output a record */
821   {
822 <        char  fmt[32], typ[32];
822 >        char  fmt[32], typ[16];
823          int  n;
824          struct field  *f;
825          int  adlast, adnext;
# Line 836 | Line 854 | putrec(void)                                /* output
854                          break;
855                  case T_NUM:
856                          n = f->type & F_WID;
839                        typ[0] = (n <= 6) ? 'g' : 'e';
840                        typ[1] = '\0';
857                          dv = evalue(f->f.ne);
858 <                        if ((av = fabs(dv)) < 1L<<31) {
858 >                        av = fabs(dv);
859 >                        if (n <= 9)
860 >                                strcpy(typ, "g");
861 >                        else
862 >                                sprintf(typ, ".%de", n-5);
863 >                        if (av < 1L<<31) {
864                                  long    iv = (int)(av + .5);
865                                  if (iv && fabs(av-iv) <= av*1e-14)
866                                          strcpy(typ, ".0f");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines