ref: 55a37fc705b5c65ee69084ebd8e39108721a669e
parent: 19a10cb2b8a643e7d96a3f322cb94a49827c16a4
author: Ulrich Klauer <ulrich@chirlu.de>
date: Mon Dec 26 10:39:33 EST 2011
fir: improve file parsing 1. In certain cases, fir would try to read again from a coefficient file after having reached end-of-file. If the file is the standard input, this would force the user to press CTRL-D a second time. - Also, since the order of execution of "fscanf(...) + fscanf(...)" is not guaranteed, this is a potential bug. 2. Return to the documented behaviour that no arguments means reading coefficients from standard input.
--- a/src/fir.c
+++ b/src/fir.c
@@ -34,7 +34,9 @@
b->filter_ptr = &b->filter;
--argc, ++argv;
- if (argc == 1)
+ if (!argc)
+ p->filename = "-"; /* default to stdin */
+ else if (argc == 1)
p->filename = argv[0], --argc;
else for (; argc && sscanf(*argv, "%lf%c", &d, &c) == 1; --argc, ++argv) {
p->n++;
@@ -57,12 +59,16 @@
FILE * file = lsx_open_input_file(effp, p->filename);
if (!file)
return SOX_EOF;
- while (fscanf(file, " #%*[^\n]%c", &c) + (i = fscanf(file, "%lf", &d)) >0)
- if (i > 0) {
+ while ((i = fscanf(file, " #%*[^\n]%c", &c)) >= 0) {
+ if (i >= 1) continue; /* found and skipped a comment */
+ if ((i = fscanf(file, "%lf", &d)) > 0) {
+ /* found a coefficient value */
p->n++;
p->h = lsx_realloc(p->h, p->n * sizeof(*p->h));
p->h[p->n - 1] = d;
- }
+ } else break; /* either EOF, or something went wrong
+ (read or syntax error) */
+ }
if (!feof(file)) {
lsx_fail("error reading coefficient file");
if (file != stdin) fclose(file);