--- ray/src/cal/rcalc.c 2003/11/14 17:31:24 1.11 +++ ray/src/cal/rcalc.c 2009/06/14 00:33:16 1.21 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcalc.c,v 1.11 2003/11/14 17:31:24 schorsch Exp $"; +static const char RCSid[] = "$Id: rcalc.c,v 1.21 2009/06/14 00:33:16 greg Exp $"; #endif /* * rcalc.c - record calculator program. @@ -15,15 +15,17 @@ static const char RCSid[] = "$Id: rcalc.c,v 1.11 2003/ #include #include "platform.h" -#include "calcomp.h" #include "rterror.h" +#include "rtmisc.h" +#include "rtio.h" +#include "calcomp.h" #define isnum(c) (isdigit(c) || (c)=='-' || (c)=='.' \ || (c)=='+' || (c)=='e' || (c)=='E') #define isblnk(c) (igneol ? isspace(c) : (c)==' '||(c)=='\t') -#define INBSIZ 4096 /* longest record */ +#define INBSIZ 16384 /* longest record */ #define MAXCOL 32 /* number of columns recorded */ /* field type specifications */ @@ -76,10 +78,12 @@ struct strvar *svhead = NULL; /* string variables */ int blnkeq = 1; /* blanks compare equal? */ int igneol = 0; /* ignore end of line? */ +int passive = 0; /* passive mode (transmit unmatched input) */ char sepchar = '\t'; /* input/output separator */ int noinput = 0; /* no input records? */ +int itype = 'a'; /* input type (a/f/F/d/D) */ int nbicols = 0; /* number of binary input columns */ -int bocols = 0; /* produce binary output columns */ +int otype = 'a'; /* output format (a/f/F/d/D) */ char inpbuf[INBSIZ]; /* input buffer */ double colval[MAXCOL]; /* input column values */ unsigned long colflg = 0; /* column retrieved flags */ @@ -122,6 +126,9 @@ char *argv[] case 'l': igneol = !igneol; break; + case 'p': + passive = !passive; + break; case 't': sepchar = argv[i][2]; break; @@ -140,13 +147,17 @@ char *argv[] case 'i': switch (argv[i][2]) { case '\0': + itype = 'a'; nbicols = 0; readfmt(argv[++i], 0); break; case 'a': + itype = 'a'; nbicols = 0; break; case 'd': + case 'D': + itype = argv[i][2]; if (isdigit(argv[i][3])) nbicols = atoi(argv[i]+3); else @@ -158,11 +169,13 @@ char *argv[] } break; case 'f': + case 'F': + itype = argv[i][2]; if (isdigit(argv[i][3])) - nbicols = -atoi(argv[i]+3); + nbicols = atoi(argv[i]+3); else - nbicols = -1; - if (-nbicols*sizeof(float) > INBSIZ) { + nbicols = 1; + if (nbicols*sizeof(float) > INBSIZ) { eputs(argv[0]); eputs(": too many input columns\n"); quit(1); @@ -175,18 +188,20 @@ char *argv[] case 'o': switch (argv[i][2]) { case '\0': - bocols = 0; + otype = 'a'; readfmt(argv[++i], 1); break; case 'a': - bocols = 0; + otype = 'a'; break; case 'd': - bocols = 1; - break; + case 'D': case 'f': - bocols = -1; + case 'F': + otype = argv[i][2]; break; + default: + goto userr; } break; case 'w': @@ -199,15 +214,23 @@ char *argv[] userr: eputs("Usage: "); eputs(argv[0]); -eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); +eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); quit(1); } - + if (otype != 'a') + SET_FILE_BINARY(stdout); if (noinput) { /* produce a single output record */ + if (i < argc) { + eputs(argv[0]); + eputs(": file argument(s) incompatible with -n\n"); + quit(1); + } eclock++; putout(); quit(0); } + if (itype != 'a') + SET_FILE_BINARY(stdin); if (blnkeq) /* for efficiency */ nbsynch(); @@ -242,12 +265,20 @@ FILE *fp { if (inpfmt != NULL) return(getrec()); - if (nbicols > 0) - return(fread(inpbuf, sizeof(double), - nbicols, fp) == nbicols); - if (nbicols < 0) - return(fread(inpbuf, sizeof(float), - -nbicols, fp) == -nbicols); + if (tolower(itype) == 'd') { + if (fread(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) + return(0); + if (itype == 'F') + swap32(inpbuf, nbicols); + return(1); + } return(fgets(inpbuf, INBSIZ, fp) != NULL); } @@ -274,11 +305,12 @@ char *file while (getinputrec(fp)) { varset("recno", '=', (double)++nrecs); + varset("outno", '=', (double)(nout+1)); colflg = 0; eclock++; if (!conditional || varvalue("cond") > 0.0) { - varset("outno", '=', (double)++nout); putout(); + ++nout; } } fclose(fp); @@ -292,11 +324,11 @@ putout(void) /* produce an output recor colpos = 0; if (outfmt != NULL) putrec(); - else if (bocols) - chanout(bchanset); - else + else if (otype == 'a') chanout(chanset); - if (colpos && !bocols) + else + chanout(bchanset); + if (colpos && otype == 'a') putchar('\n'); if (unbuff) fflush(stdout); @@ -315,10 +347,8 @@ l_in(char *funame) /* function call for $channel */ /* determine number of channels */ if (noinput || inpfmt != NULL) return(0); - if (nbicols > 0) + if (nbicols) return(nbicols); - if (nbicols < 0) - return(-nbicols); cp = inpbuf; /* need to count */ for (n = 0; *cp; ) if (blnkeq && isspace(sepchar)) { @@ -351,15 +381,13 @@ int n eputs("illegal channel number\n"); quit(1); } - if (nbicols > 0) { + if (nbicols) { if (n > nbicols) return(0.0); - cp = inpbuf + (n-1)*sizeof(double); - return(*(double *)cp); - } - if (nbicols < 0) { - if (n > -nbicols) - return(0.0); + if (tolower(itype) == 'd') { + cp = inpbuf + (n-1)*sizeof(double); + return(*(double *)cp); + } cp = inpbuf + (n-1)*sizeof(float); return(*(float *)cp); } @@ -411,16 +439,25 @@ double v ) { static char zerobuf[sizeof(double)]; + float fval = v; while (++colpos < n) fwrite(zerobuf, - bocols>0 ? sizeof(double) : sizeof(float), + tolower(otype)=='d' ? sizeof(double) : sizeof(float), 1, stdout); - if (bocols > 0) + switch (otype) { + case 'D': + swap64((char *)&v, 1); + /* fall through */ + case 'd': fwrite(&v, sizeof(double), 1, stdout); - else { - float fval = v; + break; + case 'F': + swap32((char *)&fval, 1); + /* fall through */ + case 'f': fwrite(&fval, sizeof(float), 1, stdout); + break; } } @@ -448,7 +485,7 @@ int output eputs(": cannot open\n"); quit(1); } - res = read(fd, inpbuf+1, INBSIZ-1); + res = read(fd, inpbuf+2, INBSIZ-2); if (res <= 0 || res >= INBSIZ-1) { eputs(spec); if (res < 0) @@ -460,7 +497,7 @@ int output quit(1); } close(fd); - (inptr=inpbuf+1)[res] = '\0'; + (inptr=inpbuf+2)[res] = '\0'; } f = &fmt; /* get fields */ while ((res = readfield(&inptr)) != F_NUL) { @@ -622,33 +659,35 @@ clearrec(void) /* clear input record variables */ static int -getrec(void) /* get next record from file */ +getrec(void) /* get next record from file */ { int eatline; register struct field *f; - + while (ipb.chr != EOF) { - eatline = !igneol && ipb.chr != '\n'; - if (blnkeq) /* beware of nbsynch() */ + if (blnkeq) { /* beware of nbsynch() */ while (isblnk(ipb.chr)) - scaninp(); + resetinp(); + if (ipb.chr == EOF) + return(0); + } + eatline = (!igneol && ipb.chr != '\n'); clearrec(); /* start with fresh record */ for (f = inpfmt; f != NULL; f = f->next) if (getfield(f) == -1) break; if (f == NULL) { - advinp(); + advinp(); /* got one! */ return(1); } - resetinp(); + resetinp(); /* eat false start */ if (eatline) { /* eat rest of line */ while (ipb.chr != '\n') { if (ipb.chr == EOF) return(0); - scaninp(); + resetinp(); } - scaninp(); - advinp(); + resetinp(); } } return(0); @@ -845,6 +884,8 @@ resetinp(void) /* rewind position ipb.beg = ipb.end; ipb.pos = ipb.beg; ipb.chr = *ipb.pos; + if (passive) /* transmit unmatched character? */ + fputc(ipb.chr, stdout); if (++ipb.beg >= &inpbuf[INBSIZ]) ipb.beg = inpbuf; scaninp();