diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2015-03-23 14:36:19 +0200 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2015-04-10 05:37:11 +0000 |
commit | 853cba729bc2bbac254ee36be6d42241eb92fd21 (patch) | |
tree | edd86f3f1e1b34bf1247ec598a55ddd3eb2c482a /src/corelib/tools/qringbuffer_p.h | |
parent | 921b22e5cee1be54b96dcd1eebbb3db48301bc9a (diff) |
QRingBuffer: allow to peek bytes from any position
Add an ordinary peek() function which also allows retrieving data from
a specified position. We need this functionality in several places.
Change-Id: Ia4a1b6fe1d7f76cb8f6f1ea34b3e4b89e05a2a68
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qringbuffer_p.h')
-rw-r--r-- | src/corelib/tools/qringbuffer_p.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/corelib/tools/qringbuffer_p.h b/src/corelib/tools/qringbuffer_p.h index 6f4ebcf420..9ca14d2987 100644 --- a/src/corelib/tools/qringbuffer_p.h +++ b/src/corelib/tools/qringbuffer_p.h @@ -293,6 +293,30 @@ public: return qba; } + // peek the bytes from a specified position + inline qint64 peek(char *data, qint64 maxLength, qint64 pos = 0) const + { + qint64 readSoFar = 0; + + if (pos >= 0) { + pos += head; + for (int i = 0; readSoFar < maxLength && i < buffers.size(); ++i) { + qint64 blockLength = (i == tailBuffer ? tail : buffers[i].size()); + + if (pos < blockLength) { + blockLength = qMin(blockLength - pos, maxLength - readSoFar); + memcpy(data + readSoFar, buffers[i].constData() + pos, blockLength); + readSoFar += blockLength; + pos = 0; + } else { + pos -= blockLength; + } + } + } + + return readSoFar; + } + // append a new buffer to the end inline void append(const QByteArray &qba) { if (tail == 0) { |