--- ray/src/cal/rcalc.c 2014/05/10 01:48:14 1.25 +++ ray/src/cal/rcalc.c 2019/12/10 19:00:26 1.30 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcalc.c,v 1.25 2014/05/10 01:48:14 greg Exp $"; +static const char RCSid[] = "$Id: rcalc.c,v 1.30 2019/12/10 19:00:26 greg Exp $"; #endif /* * rcalc.c - record calculator program. @@ -17,10 +17,10 @@ static const char RCSid[] = "$Id: rcalc.c,v 1.25 2014/ #include "rtio.h" #include "calcomp.h" -#define isnum(c) (isdigit(c) || (c)=='-' || (c)=='.' \ - || (c)=='+' || (c)=='e' || (c)=='E') +#define isnum(c) (isdigit(c) || ((c)=='-') | ((c)=='.') \ + | ((c)=='+') | ((c)=='e') | ((c)=='E')) -#define isblnk(c) (igneol ? isspace(c) : (c)==' '||(c)=='\t') +#define isblnk(c) (igneol ? isspace(c) : ((c)==' ')|((c)=='\t')) #define INBSIZ 16384 /* longest record */ #define MAXCOL 32 /* number of columns recorded */ @@ -73,6 +73,9 @@ struct field *inpfmt = NULL; /* input record format struct field *outfmt = NULL; /* output record structure */ struct strvar *svhead = NULL; /* string variables */ +long incnt = 0; /* limit number of input records? */ +long outcnt = 0; /* limit number of output records? */ + int blnkeq = 1; /* blanks compare equal? */ int igneol = 0; /* ignore end of line? */ int passive = 0; /* passive mode (transmit unmatched input) */ @@ -157,6 +160,9 @@ char *argv[] nbicols = 0; readfmt(argv[++i], 0); break; + case 'n': + incnt = atol(argv[++i]); + break; case 'a': itype = 'a'; nbicols = 0; @@ -197,6 +203,9 @@ char *argv[] otype = 'a'; readfmt(argv[++i], 1); break; + case 'n': + outcnt = atol(argv[++i]); + break; case 'a': otype = 'a'; break; @@ -271,15 +280,15 @@ FILE *fp { if (inpfmt != NULL) return(getrec()); - if (tolower(itype) == 'd') { - if (fread(inpbuf, sizeof(double), nbicols, fp) != nbicols) + if ((itype == 'd') | (itype == 'D')) { + if (getbinary(inpbuf, sizeof(double), nbicols, fp) != nbicols) return(0); if (itype == 'D') swap64(inpbuf, nbicols); return(1); } - if (tolower(itype) == 'f') { - if (fread(inpbuf, sizeof(float), nbicols, fp) != nbicols) + if ((itype == 'f') | (itype == 'F')) { + if (getbinary(inpbuf, sizeof(float), nbicols, fp) != nbicols) return(0); if (itype == 'F') swap32(inpbuf, nbicols); @@ -294,7 +303,9 @@ execute( /* process a file */ char *file ) { - int conditional = vardefined("cond"); + const int conditional = vardefined("cond"); + const int set_recno = (varlookup("recno") != NULL); + const int set_outno = (varlookup("outno") != NULL); long nrecs = 0; long nout = 0; FILE *fp; @@ -315,14 +326,21 @@ char *file initinp(fp); while (getinputrec(fp)) { - varset("recno", '=', (double)++nrecs); - varset("outno", '=', (double)(nout+1)); + ++nrecs; + if (set_recno) + varset("recno", '=', (double)nrecs); + if (set_outno) + varset("outno", '=', (double)(nout+1)); colflg = 0; eclock++; if (!conditional || varvalue("cond") > 0.0) { putout(); ++nout; } + if (incnt && nrecs >= incnt) + break; + if (outcnt && nout >= outcnt) + break; } fclose(fp); } @@ -395,7 +413,7 @@ int n if (nbicols) { if (n > nbicols) return(0.0); - if (tolower(itype) == 'd') { + if ((itype == 'd') | (itype == 'D')) { cp = inpbuf + (n-1)*sizeof(double); return(*(double *)cp); } @@ -450,24 +468,24 @@ double v ) { static char zerobuf[sizeof(double)]; + const int otlen = ((otype == 'd') | (otype == 'D')) ? + sizeof(double) : sizeof(float); float fval = v; while (++colpos < n) - fwrite(zerobuf, - tolower(otype)=='d' ? sizeof(double) : sizeof(float), - 1, stdout); + putbinary(zerobuf, otlen, 1, stdout); switch (otype) { case 'D': swap64((char *)&v, 1); /* fall through */ case 'd': - fwrite(&v, sizeof(double), 1, stdout); + putbinary(&v, sizeof(double), 1, stdout); break; case 'F': swap32((char *)&fval, 1); /* fall through */ case 'f': - fwrite(&fval, sizeof(float), 1, stdout); + putbinary(&fval, sizeof(float), 1, stdout); break; } } @@ -682,10 +700,10 @@ getrec(void) /* get next record from file */ if (ipb.chr == EOF) return(0); } - eatline = (!igneol && ipb.chr != '\n'); + eatline = !igneol & (ipb.chr != '\n'); clearrec(); /* start with fresh record */ for (f = inpfmt; f != NULL; f = f->next) - if (getfield(f) == -1) + if (!getfield(f)) break; if (f == NULL) { advinp(); /* got one! */ @@ -722,7 +740,7 @@ struct field *f do { if (blnkeq && isblnk(*cp)) { if (!isblnk(ipb.chr)) - return(-1); + return(0); do cp++; while (isblnk(*cp)); @@ -733,9 +751,9 @@ struct field *f cp++; scaninp(); } else - return(-1); + return(0); } while (*cp); - return(0); + break; case T_STR: if (f->next == NULL || (f->next->type & F_TYP) != T_LIT) delim = EOF; @@ -743,7 +761,7 @@ struct field *f delim = f->next->f.sl[0]; cp = buf; do { - if (ipb.chr == EOF || ipb.chr == '\n') + if ((ipb.chr == EOF) | (ipb.chr == '\n')) inword = 0; else if (blnkeq && delim != EOF) inword = isblnk(delim) ? @@ -760,8 +778,8 @@ struct field *f if (f->f.sv->val == NULL) f->f.sv->val = savqstr(buf); /* first setting */ else if (strcmp(f->f.sv->val, buf)) - return(-1); /* doesn't match! */ - return(0); + return(0); /* doesn't match! */ + break; case T_NUM: if (f->next == NULL || (f->next->type & F_TYP) != T_LIT) delim = EOF; @@ -791,10 +809,10 @@ struct field *f varset(f->f.nv, '=', d); /* first setting */ else if ((d = (varvalue(f->f.nv)-d)/(d==0.?1.:d)) > .001 || d < -.001) - return(-1); /* doesn't match! */ - return(0); + return(0); /* doesn't match! */ + break; } - return -1; /* pro forma return */ + return(1); /* success! */ }