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 8192 /* maximum number of columns */ |
17 |
|
|
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 |
|
|
59 |
|
func = MULT; |
60 |
|
break; |
61 |
|
case 's': |
58 |
– |
func = ADD; |
62 |
|
power = atof(argv[a]+2); |
63 |
|
break; |
64 |
|
case 'u': |
75 |
|
break; |
76 |
|
case 'i': |
77 |
|
switch (argv[a][2]) { |
78 |
+ |
case 'n': |
79 |
+ |
incnt = atol(argv[++a]); |
80 |
+ |
break; |
81 |
|
case 'a': |
82 |
|
nbicols = 0; |
83 |
|
break; |
111 |
|
break; |
112 |
|
case 'o': |
113 |
|
switch (argv[a][2]) { |
114 |
+ |
case 'n': |
115 |
+ |
outcnt = atol(argv[++a]); |
116 |
+ |
break; |
117 |
|
case 'a': |
118 |
|
bocols = 0; |
119 |
|
break; |
136 |
|
} |
137 |
|
else |
138 |
|
break; |
139 |
+ |
if ((power != 0.0) & (func != ADD)) { |
140 |
+ |
fprintf(stderr, "%s: -sE option requires summation\n", argv[0]); |
141 |
+ |
exit(1); |
142 |
+ |
} |
143 |
|
if (mean) { |
144 |
|
if (func == MAX) { |
145 |
|
fprintf(stderr, "%s: average maximum?!\n", argv[0]); |
156 |
|
if (a >= argc) |
157 |
|
status = execute(NULL) == -1 ? 1 : status; |
158 |
|
else |
159 |
< |
for ( ; a < argc; a++) |
159 |
> |
for ( ; a < argc && (outcnt <= 0 || nrecsout < outcnt); a++) |
160 |
|
status = execute(argv[a]) == -1 ? 2 : status; |
161 |
|
exit(status); |
162 |
|
} |
173 |
|
int nf; |
174 |
|
/* reading binary input? */ |
175 |
|
if (nbicols > 0) |
176 |
< |
return(fread(field, sizeof(double), nbicols, fp)); |
176 |
> |
return(getbinary(field, sizeof(double), nbicols, fp)); |
177 |
|
if (nbicols < 0) { |
178 |
|
float *fbuf = (float *)buf; |
179 |
|
int i; |
180 |
< |
nf = fread(fbuf, sizeof(float), -nbicols, fp); |
180 |
> |
nf = getbinary(fbuf, sizeof(float), -nbicols, fp); |
181 |
|
for (i = nf; i-- > 0; ) |
182 |
|
field[i] = fbuf[i]; |
183 |
|
return(nf); |
214 |
|
{ |
215 |
|
/* binary output? */ |
216 |
|
if (bocols > 0) { |
217 |
< |
fwrite(field, sizeof(double), n, fp); |
217 |
> |
putbinary(field, sizeof(double), n, fp); |
218 |
|
return; |
219 |
|
} |
220 |
|
if (bocols < 0) { |
221 |
|
float fv; |
222 |
|
while (n-- > 0) { |
223 |
|
fv = *field++; |
224 |
< |
fwrite(&fv, sizeof(float), 1, fp); |
224 |
> |
putbinary(&fv, sizeof(float), 1, fp); |
225 |
|
} |
226 |
|
return; |
227 |
|
} |
228 |
|
/* ASCII output */ |
229 |
< |
while (n-- > 0) |
230 |
< |
fprintf(fp, "%.9g%c", *field++, tabc); |
229 |
> |
while (n-- > 0) { |
230 |
> |
fprintf(fp, "%.9g", *field++); |
231 |
> |
if (n) fputc(tabc, fp); |
232 |
> |
} |
233 |
|
fputc('\n', fp); |
234 |
+ |
if (!subtotal) |
235 |
+ |
fflush(fp); /* flush unless -r */ |
236 |
|
} |
237 |
|
|
238 |
|
|
276 |
|
} |
277 |
|
ncol = 0; |
278 |
|
for (nlin = 0; (count <= 0 || nlin < count) && |
279 |
+ |
(incnt <= 0 || nlin < incnt) && |
280 |
|
(nread = getrecord(inpval, fp)) > 0; |
281 |
|
nlin++) { |
282 |
|
/* compute */ |
285 |
|
case ADD: |
286 |
|
if (inpval[n] == 0.0) |
287 |
|
break; |
288 |
< |
if (power != 0.0) |
271 |
< |
tally[n] += pow(fabs(inpval[n]),power); |
272 |
< |
else |
288 |
> |
if (power == 0.0) |
289 |
|
tally[n] += inpval[n]; |
290 |
+ |
else if (power == 1.0) |
291 |
+ |
tally[n] += fabs(inpval[n]); |
292 |
+ |
else if (power == -1.0) |
293 |
+ |
tally[n] += 1.0/fabs(inpval[n]); |
294 |
+ |
else |
295 |
+ |
tally[n] += pow(fabs(inpval[n]), power); |
296 |
|
break; |
297 |
|
case MULT: |
298 |
|
if (inpval[n] == 0.0) |
331 |
|
result[n] = rsign[n] * exp(result[n]); |
332 |
|
} |
333 |
|
putrecord(result, ncol, stdout); |
334 |
+ |
++nrecsout; |
335 |
+ |
if (outcnt > 0 && nrecsout >= outcnt) |
336 |
+ |
break; |
337 |
|
if (!subtotal) |
338 |
|
ltotal = 0; |
339 |
+ |
if (incnt > 0 && nlin >= incnt) |
340 |
+ |
break; |
341 |
|
} |
342 |
|
/* close input */ |
343 |
< |
return(fclose(fp)); |
343 |
> |
return(fclose(fp) == EOF ? 1 : 0); |
344 |
|
} |