diff options
author | Markus Goetz <Markus.Goetz@nokia.com> | 2009-05-13 09:00:40 (GMT) |
---|---|---|
committer | Markus Goetz <Markus.Goetz@nokia.com> | 2009-05-13 11:42:04 (GMT) |
commit | 0076f04c3637aaebb4d70d7c7e5fd0a78527c260 (patch) | |
tree | b65389b3f9511b193db364447a2fe3f331f9cbbf /src | |
parent | 8a7f9857350c50dc77d2a39a864b22cdaffa78ff (diff) | |
download | Qt-0076f04c3637aaebb4d70d7c7e5fd0a78527c260.zip Qt-0076f04c3637aaebb4d70d7c7e5fd0a78527c260.tar.gz Qt-0076f04c3637aaebb4d70d7c7e5fd0a78527c260.tar.bz2 |
QRingBuffer: Enanced with readPointerAtPosition
Ability to read from a QRingBuffer at any position without modifying it.
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qringbuffer_p.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/corelib/tools/qringbuffer_p.h b/src/corelib/tools/qringbuffer_p.h index eed4ba9..02cc497 100644 --- a/src/corelib/tools/qringbuffer_p.h +++ b/src/corelib/tools/qringbuffer_p.h @@ -74,6 +74,52 @@ public: return buffers.isEmpty() ? 0 : (buffers.first().constData() + head); } + // access the bytes at a specified position + // the out-variable length will contain the amount of bytes readable + // from there, e.g. the amount still the same QByteArray + inline const char *readPointerAtPosition(qint64 pos, qint64 &length) const { + if (buffers.isEmpty()) { + length = 0; + return 0; + } + + if (pos >= bufferSize) { + length = 0; + return 0; + } + + // special case: it is in the first buffer + int nextDataBlockSizeValue = nextDataBlockSize(); + if (pos - head < nextDataBlockSizeValue) { + length = nextDataBlockSizeValue - pos; + return buffers.at(0).constData() + head + pos; + } + + // special case: we only had one buffer and tried to read over it + if (buffers.length() == 1) { + length = 0; + return 0; + } + + // skip the first + pos -= nextDataBlockSizeValue; + + // normal case: it is somewhere in the second to the-one-before-the-tailBuffer + for (int i = 1; i < tailBuffer; i++) { + if (pos >= buffers[i].size()) { + pos -= buffers[i].size(); + continue; + } + + length = buffers[i].length() - pos; + return buffers[i].constData() + pos; + } + + // it is in the tail buffer + length = tail - pos; + return buffers[tailBuffer].constData() + pos; + } + inline void free(int bytes) { bufferSize -= bytes; if (bufferSize < 0) |