| 7 |
|
* 5/18/88 |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
– |
#include <stdio.h> |
| 10 |
|
#include <stdlib.h> |
| 11 |
|
#include <ctype.h> |
| 12 |
|
#include <math.h> |
| 13 |
|
#include "platform.h" |
| 14 |
+ |
#include "rtio.h" |
| 15 |
|
|
| 16 |
< |
#define MAXCOL 2048 /* maximum number of columns */ |
| 16 |
> |
#define MAXCOL 8192 /* maximum number of columns */ |
| 17 |
|
|
| 18 |
|
#define ADD 0 /* add numbers */ |
| 19 |
|
#define MULT 1 /* multiply numbers */ |
| 22 |
|
|
| 23 |
|
double init_val[] = {0., 1., -1e12, 1e12}; /* initial values */ |
| 24 |
|
|
| 25 |
+ |
long incnt = 0; /* limit number of input records? */ |
| 26 |
+ |
long outcnt = 0; /* limit number of output records? */ |
| 27 |
+ |
|
| 28 |
|
int func = ADD; /* default function */ |
| 29 |
|
double power = 0.0; /* power for sum */ |
| 30 |
|
int mean = 0; /* compute mean */ |
| 33 |
|
int bocols = 0; /* produce binary output columns */ |
| 34 |
|
int tabc = '\t'; /* default separator */ |
| 35 |
|
int subtotal = 0; /* produce subtotals? */ |
| 36 |
+ |
int nrecsout = 0; /* number of records produced */ |
| 37 |
|
|
| 38 |
|
static int execute(char *fname); |
| 39 |
|
|
| 76 |
|
break; |
| 77 |
|
case 'i': |
| 78 |
|
switch (argv[a][2]) { |
| 79 |
+ |
case 'n': |
| 80 |
+ |
incnt = atol(argv[++a]); |
| 81 |
+ |
break; |
| 82 |
|
case 'a': |
| 83 |
|
nbicols = 0; |
| 84 |
|
break; |
| 112 |
|
break; |
| 113 |
|
case 'o': |
| 114 |
|
switch (argv[a][2]) { |
| 115 |
+ |
case 'n': |
| 116 |
+ |
outcnt = atol(argv[++a]); |
| 117 |
+ |
break; |
| 118 |
|
case 'a': |
| 119 |
|
bocols = 0; |
| 120 |
|
break; |
| 153 |
|
if (a >= argc) |
| 154 |
|
status = execute(NULL) == -1 ? 1 : status; |
| 155 |
|
else |
| 156 |
< |
for ( ; a < argc; a++) |
| 156 |
> |
for ( ; a < argc && (outcnt <= 0 || nrecsout < outcnt); a++) |
| 157 |
|
status = execute(argv[a]) == -1 ? 2 : status; |
| 158 |
|
exit(status); |
| 159 |
|
} |
| 170 |
|
int nf; |
| 171 |
|
/* reading binary input? */ |
| 172 |
|
if (nbicols > 0) |
| 173 |
< |
return(fread(field, sizeof(double), nbicols, fp)); |
| 173 |
> |
return(getbinary(field, sizeof(double), nbicols, fp)); |
| 174 |
|
if (nbicols < 0) { |
| 175 |
|
float *fbuf = (float *)buf; |
| 176 |
|
int i; |
| 177 |
< |
nf = fread(fbuf, sizeof(float), -nbicols, fp); |
| 177 |
> |
nf = getbinary(fbuf, sizeof(float), -nbicols, fp); |
| 178 |
|
for (i = nf; i-- > 0; ) |
| 179 |
|
field[i] = fbuf[i]; |
| 180 |
|
return(nf); |
| 211 |
|
{ |
| 212 |
|
/* binary output? */ |
| 213 |
|
if (bocols > 0) { |
| 214 |
< |
fwrite(field, sizeof(double), n, fp); |
| 214 |
> |
putbinary(field, sizeof(double), n, fp); |
| 215 |
|
return; |
| 216 |
|
} |
| 217 |
|
if (bocols < 0) { |
| 218 |
|
float fv; |
| 219 |
|
while (n-- > 0) { |
| 220 |
|
fv = *field++; |
| 221 |
< |
fwrite(&fv, sizeof(float), 1, fp); |
| 221 |
> |
putbinary(&fv, sizeof(float), 1, fp); |
| 222 |
|
} |
| 223 |
|
return; |
| 224 |
|
} |
| 225 |
|
/* ASCII output */ |
| 226 |
< |
while (n-- > 0) |
| 227 |
< |
fprintf(fp, "%.9g%c", *field++, tabc); |
| 226 |
> |
while (n-- > 0) { |
| 227 |
> |
fprintf(fp, "%.9g", *field++); |
| 228 |
> |
if (n) fputc(tabc, fp); |
| 229 |
> |
} |
| 230 |
|
fputc('\n', fp); |
| 231 |
+ |
if (!subtotal) |
| 232 |
+ |
fflush(fp); /* flush unless -r */ |
| 233 |
|
} |
| 234 |
|
|
| 235 |
|
|
| 240 |
|
{ |
| 241 |
|
double inpval[MAXCOL]; |
| 242 |
|
double tally[MAXCOL]; |
| 243 |
+ |
short rsign[MAXCOL]; |
| 244 |
|
double result[MAXCOL]; |
| 245 |
< |
register int n; |
| 245 |
> |
int n; |
| 246 |
|
int nread, ncol; |
| 247 |
|
long nlin, ltotal; |
| 248 |
|
FILE *fp; |
| 255 |
|
} |
| 256 |
|
if (nbicols) |
| 257 |
|
SET_FILE_BINARY(fp); |
| 258 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 259 |
+ |
flockfile(fp); |
| 260 |
+ |
#endif |
| 261 |
+ |
|
| 262 |
|
ltotal = 0; |
| 263 |
|
while (!feof(fp)) { |
| 264 |
|
if (ltotal == 0) { /* initialize */ |
| 265 |
|
if (func == MULT) /* special case */ |
| 266 |
< |
for (n = 0; n < MAXCOL; n++) |
| 266 |
> |
for (n = 0; n < MAXCOL; n++) { |
| 267 |
|
tally[n] = 0.0; |
| 268 |
+ |
rsign[n] = 1; |
| 269 |
+ |
} |
| 270 |
|
else |
| 271 |
|
for (n = 0; n < MAXCOL; n++) |
| 272 |
|
tally[n] = init_val[func]; |
| 273 |
|
} |
| 274 |
|
ncol = 0; |
| 275 |
|
for (nlin = 0; (count <= 0 || nlin < count) && |
| 276 |
+ |
(incnt <= 0 || nlin < incnt) && |
| 277 |
|
(nread = getrecord(inpval, fp)) > 0; |
| 278 |
|
nlin++) { |
| 279 |
|
/* compute */ |
| 289 |
|
break; |
| 290 |
|
case MULT: |
| 291 |
|
if (inpval[n] == 0.0) |
| 292 |
< |
break; |
| 293 |
< |
tally[n] += log(fabs(inpval[n])); |
| 292 |
> |
rsign[n] = 0; |
| 293 |
> |
else if (inpval[n] < 0.0) { |
| 294 |
> |
rsign[n] = -rsign[n]; |
| 295 |
> |
inpval[n] = -inpval[n]; |
| 296 |
> |
} |
| 297 |
> |
if (rsign[n]) |
| 298 |
> |
tally[n] += log(inpval[n]); |
| 299 |
|
break; |
| 300 |
|
case MAX: |
| 301 |
|
if (inpval[n] > tally[n]) |
| 321 |
|
result[n] = pow(result[n], 1.0/power); |
| 322 |
|
} |
| 323 |
|
if (func == MULT) |
| 324 |
< |
result[n] = exp(result[n]); |
| 324 |
> |
result[n] = rsign[n] * exp(result[n]); |
| 325 |
|
} |
| 326 |
|
putrecord(result, ncol, stdout); |
| 327 |
+ |
++nrecsout; |
| 328 |
+ |
if (outcnt > 0 && nrecsout >= outcnt) |
| 329 |
+ |
break; |
| 330 |
|
if (!subtotal) |
| 331 |
|
ltotal = 0; |
| 332 |
+ |
if (incnt > 0 && nlin >= incnt) |
| 333 |
+ |
break; |
| 334 |
|
} |
| 335 |
|
/* close input */ |
| 336 |
< |
return(fclose(fp)); |
| 336 |
> |
return(fclose(fp) == EOF ? 1 : 0); |
| 337 |
|
} |