25 |
|
|
26 |
|
#define isblnk(c) (igneol ? isspace(c) : (c)==' '||(c)=='\t') |
27 |
|
|
28 |
< |
#define INBSIZ 4096 /* longest record */ |
28 |
> |
#define INBSIZ 16384 /* longest record */ |
29 |
|
#define MAXCOL 32 /* number of columns recorded */ |
30 |
|
|
31 |
|
/* field type specifications */ |
81 |
|
int passive = 0; /* passive mode (transmit unmatched input) */ |
82 |
|
char sepchar = '\t'; /* input/output separator */ |
83 |
|
int noinput = 0; /* no input records? */ |
84 |
+ |
int itype = 'a'; /* input type (a/f/F/d/D) */ |
85 |
|
int nbicols = 0; /* number of binary input columns */ |
86 |
< |
int bocols = 0; /* produce binary output columns */ |
86 |
> |
int otype = 'a'; /* output format (a/f/F/d/D) */ |
87 |
|
char inpbuf[INBSIZ]; /* input buffer */ |
88 |
|
double colval[MAXCOL]; /* input column values */ |
89 |
|
unsigned long colflg = 0; /* column retrieved flags */ |
147 |
|
case 'i': |
148 |
|
switch (argv[i][2]) { |
149 |
|
case '\0': |
150 |
+ |
itype = 'a'; |
151 |
|
nbicols = 0; |
152 |
|
readfmt(argv[++i], 0); |
153 |
|
break; |
154 |
|
case 'a': |
155 |
+ |
itype = 'a'; |
156 |
|
nbicols = 0; |
157 |
|
break; |
158 |
|
case 'd': |
159 |
+ |
case 'D': |
160 |
+ |
itype = argv[i][2]; |
161 |
|
if (isdigit(argv[i][3])) |
162 |
|
nbicols = atoi(argv[i]+3); |
163 |
|
else |
169 |
|
} |
170 |
|
break; |
171 |
|
case 'f': |
172 |
+ |
case 'F': |
173 |
+ |
itype = argv[i][2]; |
174 |
|
if (isdigit(argv[i][3])) |
175 |
< |
nbicols = -atoi(argv[i]+3); |
175 |
> |
nbicols = atoi(argv[i]+3); |
176 |
|
else |
177 |
< |
nbicols = -1; |
178 |
< |
if (-nbicols*sizeof(float) > INBSIZ) { |
177 |
> |
nbicols = 1; |
178 |
> |
if (nbicols*sizeof(float) > INBSIZ) { |
179 |
|
eputs(argv[0]); |
180 |
|
eputs(": too many input columns\n"); |
181 |
|
quit(1); |
188 |
|
case 'o': |
189 |
|
switch (argv[i][2]) { |
190 |
|
case '\0': |
191 |
< |
bocols = 0; |
191 |
> |
otype = 'a'; |
192 |
|
readfmt(argv[++i], 1); |
193 |
|
break; |
194 |
|
case 'a': |
195 |
< |
bocols = 0; |
195 |
> |
otype = 'a'; |
196 |
|
break; |
197 |
|
case 'd': |
198 |
< |
bocols = 1; |
192 |
< |
break; |
198 |
> |
case 'D': |
199 |
|
case 'f': |
200 |
< |
bocols = -1; |
200 |
> |
case 'F': |
201 |
> |
otype = argv[i][2]; |
202 |
|
break; |
203 |
+ |
default: |
204 |
+ |
goto userr; |
205 |
|
} |
206 |
|
break; |
207 |
|
case 'w': |
217 |
|
eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
218 |
|
quit(1); |
219 |
|
} |
220 |
< |
|
220 |
> |
if (otype != 'a') |
221 |
> |
SET_FILE_BINARY(stdout); |
222 |
|
if (noinput) { /* produce a single output record */ |
223 |
+ |
if (i < argc) { |
224 |
+ |
eputs(argv[0]); |
225 |
+ |
eputs(": file argument(s) incompatible with -n\n"); |
226 |
+ |
quit(1); |
227 |
+ |
} |
228 |
|
eclock++; |
229 |
|
putout(); |
230 |
|
quit(0); |
231 |
|
} |
232 |
+ |
if (itype != 'a') |
233 |
+ |
SET_FILE_BINARY(stdin); |
234 |
|
|
235 |
|
if (blnkeq) /* for efficiency */ |
236 |
|
nbsynch(); |
265 |
|
{ |
266 |
|
if (inpfmt != NULL) |
267 |
|
return(getrec()); |
268 |
< |
if (nbicols > 0) |
269 |
< |
return(fread(inpbuf, sizeof(double), |
270 |
< |
nbicols, fp) == nbicols); |
271 |
< |
if (nbicols < 0) |
272 |
< |
return(fread(inpbuf, sizeof(float), |
273 |
< |
-nbicols, fp) == -nbicols); |
268 |
> |
if (tolower(itype) == 'd') { |
269 |
> |
if (fread(inpbuf, sizeof(double), nbicols, fp) != nbicols) |
270 |
> |
return(0); |
271 |
> |
if (itype == 'D') |
272 |
> |
swap64(inpbuf, nbicols); |
273 |
> |
return(1); |
274 |
> |
} |
275 |
> |
if (tolower(itype) == 'f') { |
276 |
> |
if (fread(inpbuf, sizeof(float), nbicols, fp) != nbicols) |
277 |
> |
return(0); |
278 |
> |
if (itype == 'F') |
279 |
> |
swap32(inpbuf, nbicols); |
280 |
> |
return(1); |
281 |
> |
} |
282 |
|
return(fgets(inpbuf, INBSIZ, fp) != NULL); |
283 |
|
} |
284 |
|
|
305 |
|
|
306 |
|
while (getinputrec(fp)) { |
307 |
|
varset("recno", '=', (double)++nrecs); |
308 |
+ |
varset("outno", '=', (double)(nout+1)); |
309 |
|
colflg = 0; |
310 |
|
eclock++; |
311 |
|
if (!conditional || varvalue("cond") > 0.0) { |
286 |
– |
varset("outno", '=', (double)++nout); |
312 |
|
putout(); |
313 |
+ |
++nout; |
314 |
|
} |
315 |
|
} |
316 |
|
fclose(fp); |
324 |
|
colpos = 0; |
325 |
|
if (outfmt != NULL) |
326 |
|
putrec(); |
327 |
< |
else if (bocols) |
302 |
< |
chanout(bchanset); |
303 |
< |
else |
327 |
> |
else if (otype == 'a') |
328 |
|
chanout(chanset); |
329 |
< |
if (colpos && !bocols) |
329 |
> |
else |
330 |
> |
chanout(bchanset); |
331 |
> |
if (colpos && otype == 'a') |
332 |
|
putchar('\n'); |
333 |
|
if (unbuff) |
334 |
|
fflush(stdout); |
347 |
|
/* determine number of channels */ |
348 |
|
if (noinput || inpfmt != NULL) |
349 |
|
return(0); |
350 |
< |
if (nbicols > 0) |
350 |
> |
if (nbicols) |
351 |
|
return(nbicols); |
326 |
– |
if (nbicols < 0) |
327 |
– |
return(-nbicols); |
352 |
|
cp = inpbuf; /* need to count */ |
353 |
|
for (n = 0; *cp; ) |
354 |
|
if (blnkeq && isspace(sepchar)) { |
381 |
|
eputs("illegal channel number\n"); |
382 |
|
quit(1); |
383 |
|
} |
384 |
< |
if (nbicols > 0) { |
384 |
> |
if (nbicols) { |
385 |
|
if (n > nbicols) |
386 |
|
return(0.0); |
387 |
< |
cp = inpbuf + (n-1)*sizeof(double); |
388 |
< |
return(*(double *)cp); |
389 |
< |
} |
390 |
< |
if (nbicols < 0) { |
367 |
< |
if (n > -nbicols) |
368 |
< |
return(0.0); |
387 |
> |
if (tolower(itype) == 'd') { |
388 |
> |
cp = inpbuf + (n-1)*sizeof(double); |
389 |
> |
return(*(double *)cp); |
390 |
> |
} |
391 |
|
cp = inpbuf + (n-1)*sizeof(float); |
392 |
|
return(*(float *)cp); |
393 |
|
} |
439 |
|
) |
440 |
|
{ |
441 |
|
static char zerobuf[sizeof(double)]; |
442 |
+ |
float fval = v; |
443 |
|
|
444 |
|
while (++colpos < n) |
445 |
|
fwrite(zerobuf, |
446 |
< |
bocols>0 ? sizeof(double) : sizeof(float), |
446 |
> |
tolower(otype)=='d' ? sizeof(double) : sizeof(float), |
447 |
|
1, stdout); |
448 |
< |
if (bocols > 0) |
448 |
> |
switch (otype) { |
449 |
> |
case 'D': |
450 |
> |
swap64((char *)&v, 1); |
451 |
> |
/* fall through */ |
452 |
> |
case 'd': |
453 |
|
fwrite(&v, sizeof(double), 1, stdout); |
454 |
< |
else { |
455 |
< |
float fval = v; |
454 |
> |
break; |
455 |
> |
case 'F': |
456 |
> |
swap32((char *)&fval, 1); |
457 |
> |
/* fall through */ |
458 |
> |
case 'f': |
459 |
|
fwrite(&fval, sizeof(float), 1, stdout); |
460 |
+ |
break; |
461 |
|
} |
462 |
|
} |
463 |
|
|
665 |
|
register struct field *f; |
666 |
|
|
667 |
|
while (ipb.chr != EOF) { |
668 |
< |
if (blnkeq) /* beware of nbsynch() */ |
668 |
> |
if (blnkeq) { /* beware of nbsynch() */ |
669 |
|
while (isblnk(ipb.chr)) |
670 |
|
resetinp(); |
671 |
+ |
if (ipb.chr == EOF) |
672 |
+ |
return(0); |
673 |
+ |
} |
674 |
|
eatline = (!igneol && ipb.chr != '\n'); |
675 |
|
clearrec(); /* start with fresh record */ |
676 |
|
for (f = inpfmt; f != NULL; f = f->next) |