diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-07-29 13:29:02 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-07-29 13:29:02 -0700 |
commit | c30c31661c0db498b2189faefa288da6ab30dfc0 (patch) | |
tree | d49a69a67ca82d7ea75e5382e995974344377402 /src | |
parent | af62c8a945ff999d4bed28ca01846b740f23d0a5 (diff) |
buffer streams that cannot be rewound in fscanf; fixes #1436
Diffstat (limited to 'src')
-rw-r--r-- | src/library.js | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/library.js b/src/library.js index 9a1f29e5..f7c7a3ba 100644 --- a/src/library.js +++ b/src/library.js @@ -3660,8 +3660,23 @@ LibraryManager.library = { // http://pubs.opengroup.org/onlinepubs/000095399/functions/scanf.html if (FS.streams[stream]) { var i = _ftell(stream), SEEK_SET = 0; - var get = function () { i++; return _fgetc(stream); }; - var unget = function () { _fseek(stream, --i, SEEK_SET); }; + // if the stream does not support seeking backwards (e.g. stdin), buffer it here + var buffer = [], bufferIndex = 0; + var get = function() { + if (bufferIndex < buffer.length) { + return buffer[bufferIndex++]; + } + i++; + bufferIndex++; + var c = _fgetc(stream); + buffer.push(c); + return c; + }; + var unget = function() { + if (_fseek(stream, --i, SEEK_SET) !== 0) { + bufferIndex--; + } + }; return __scanString(format, get, unget, varargs); } else { return -1; |