From edfcd768ba394150df34d59f08765c3e061e6c72 Mon Sep 17 00:00:00 2001 From: Giulio Camuffo Date: Sat, 26 Jan 2013 19:00:22 +0100 Subject: COMMON: Make BufferedSeekableReadStream use the buffer with SEEK_SET and SEEK_END --- common/stream.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'common/stream.cpp') diff --git a/common/stream.cpp b/common/stream.cpp index 85647bfe3a..d3cfce4066 100644 --- a/common/stream.cpp +++ b/common/stream.cpp @@ -379,12 +379,25 @@ BufferedSeekableReadStream::BufferedSeekableReadStream(SeekableReadStream *paren bool BufferedSeekableReadStream::seek(int32 offset, int whence) { // If it is a "local" seek, we may get away with "seeking" around // in the buffer only. - // Note: We could try to handle SEEK_END and SEEK_SET, too, but - // since they are rarely used, it seems not worth the effort. _eos = false; // seeking always cancels EOS - if (whence == SEEK_CUR && (int)_pos + offset >= 0 && _pos + offset <= _bufSize) { - _pos += offset; + int relOffset = 0; + switch (whence) { + case SEEK_SET: + relOffset = offset - pos(); + break; + case SEEK_CUR: + relOffset = offset; + break; + case SEEK_END: + relOffset = (size() + offset) - pos(); + break; + default: + break; + } + + if ((int)_pos + relOffset >= 0 && _pos + relOffset <= _bufSize) { + _pos += relOffset; // Note: we do not need to reset parent's eos flag here. It is // sufficient that it is reset when actually seeking in the parent. -- cgit v1.2.3 From b4d0a8ba66e2c99949d1fa14d801c7de77db76ba Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sat, 26 Jan 2013 19:33:27 +0100 Subject: JANITORIAL: Enforce "} // End of namespace" with a single space after }. --- common/stream.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'common/stream.cpp') diff --git a/common/stream.cpp b/common/stream.cpp index 85647bfe3a..a240d4e790 100644 --- a/common/stream.cpp +++ b/common/stream.cpp @@ -342,7 +342,7 @@ uint32 BufferedReadStream::read(void *dataPtr, uint32 dataSize) { return alreadyRead + dataSize; } -} // End of nameless namespace +} // End of nameless namespace ReadStream *wrapBufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream) { @@ -400,7 +400,7 @@ bool BufferedSeekableReadStream::seek(int32 offset, int whence) { return true; } -} // End of nameless namespace +} // End of nameless namespace SeekableReadStream *wrapBufferedSeekableReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream) { if (parentStream) @@ -482,7 +482,7 @@ public: }; -} // End of nameless namespace +} // End of nameless namespace WriteStream *wrapBufferedWriteStream(WriteStream *parentStream, uint32 bufSize) { if (parentStream) @@ -490,4 +490,4 @@ WriteStream *wrapBufferedWriteStream(WriteStream *parentStream, uint32 bufSize) return 0; } -} // End of namespace Common +} // End of namespace Common -- cgit v1.2.3 From 354aa0f5f34d33e32b6f35bcbf4e6f18e2bac783 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sat, 26 Jan 2013 19:36:12 +0100 Subject: JANITORIAL: Use "End of anonymous namespace" as comment for anonymous namespaces. --- common/stream.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'common/stream.cpp') diff --git a/common/stream.cpp b/common/stream.cpp index a240d4e790..57b1b8ba93 100644 --- a/common/stream.cpp +++ b/common/stream.cpp @@ -342,7 +342,7 @@ uint32 BufferedReadStream::read(void *dataPtr, uint32 dataSize) { return alreadyRead + dataSize; } -} // End of nameless namespace +} // End of anonymous namespace ReadStream *wrapBufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream) { @@ -400,7 +400,7 @@ bool BufferedSeekableReadStream::seek(int32 offset, int whence) { return true; } -} // End of nameless namespace +} // End of anonymous namespace SeekableReadStream *wrapBufferedSeekableReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream) { if (parentStream) @@ -482,7 +482,7 @@ public: }; -} // End of nameless namespace +} // End of anonymous namespace WriteStream *wrapBufferedWriteStream(WriteStream *parentStream, uint32 bufSize) { if (parentStream) -- cgit v1.2.3 From 5bfd2f675620f69e23cefbaf8553cb341a9f992c Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Mon, 28 Jan 2013 17:30:57 +0100 Subject: COMMON: Fix successive seeks in BufferedSeekableReadStream. This fixes the failing test case added in da8eeb9dbed2102764b3ca0697d6882bae0402cc. Thanks to wjp for his input on this. --- common/stream.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'common/stream.cpp') diff --git a/common/stream.cpp b/common/stream.cpp index 57b1b8ba93..2851b3a320 100644 --- a/common/stream.cpp +++ b/common/stream.cpp @@ -393,7 +393,14 @@ bool BufferedSeekableReadStream::seek(int32 offset, int whence) { // just seek normally in the parent stream. if (whence == SEEK_CUR) offset -= (_bufSize - _pos); - _pos = _bufSize; + // We invalidate the buffer here. This assures that successive seeks + // do not have the chance to incorrectly think they seeked back into + // the buffer. + // Note: This does not take full advantage of the buffer. But it is + // a simple way to prevent nasty errors. It would be possible to take + // full advantage of the buffer by saving its actual start position. + // This seems not worth the effort for this seemingly uncommon use. + _pos = _bufSize = 0; _parentStream->seek(offset, whence); } -- cgit v1.2.3