summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qiodevice.cpp
diff options
context:
space:
mode:
authorJoão Abecasis <joao@abecasis.name>2009-11-16 11:48:06 (GMT)
committerJoão Abecasis <joao@abecasis.name>2009-11-17 13:00:46 (GMT)
commitd5eb850f8c2524aa5573bb4e672b195724e5b5ad (patch)
tree53eb6bd9d714d7e1b8f11f8e3b20201ea0f5188f /src/corelib/io/qiodevice.cpp
parent6f5fb0be85b1d7cc9f93b615b5034c882e946301 (diff)
downloadQt-d5eb850f8c2524aa5573bb4e672b195724e5b5ad.zip
Qt-d5eb850f8c2524aa5573bb4e672b195724e5b5ad.tar.gz
Qt-d5eb850f8c2524aa5573bb4e672b195724e5b5ad.tar.bz2
Remove needless loop in QIODevice::seek
Either the buffer has more data than is being skipped and it can be cleared or it doesn't and we must skip it. If the total size of QRingBuffer exceeds INT_MAX there'll be bigger problems (e.g., QRingBuffer::size() will overflow) that can only be handled there and there's no point trying to work around them here. Reviewed-by: Markus Goetz
Diffstat (limited to 'src/corelib/io/qiodevice.cpp')
-rw-r--r--src/corelib/io/qiodevice.cpp19
1 files changed, 7 insertions, 12 deletions
diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp
index 4494d2a..856d737 100644
--- a/src/corelib/io/qiodevice.cpp
+++ b/src/corelib/io/qiodevice.cpp
@@ -619,7 +619,8 @@ qint64 QIODevice::size() const
*/
bool QIODevice::seek(qint64 pos)
{
- if (d_func()->openMode == NotOpen) {
+ Q_D(QIODevice);
+ if (d->openMode == NotOpen) {
qWarning("QIODevice::seek: The device is not open");
return false;
}
@@ -628,7 +629,6 @@ bool QIODevice::seek(qint64 pos)
return false;
}
- Q_D(QIODevice);
#if defined QIODEVICE_DEBUG
printf("%p QIODevice::seek(%d), before: d->pos = %d, d->buffer.size() = %d\n",
this, int(pos), int(d->pos), d->buffer.size());
@@ -640,21 +640,16 @@ bool QIODevice::seek(qint64 pos)
d->devicePos = pos;
}
- if (offset > 0 && !d->buffer.isEmpty()) {
- // When seeking forwards, we need to pop bytes off the front of the
- // buffer.
- do {
- int bytesToSkip = int(qMin<qint64>(offset, INT_MAX));
- d->buffer.skip(bytesToSkip);
- offset -= bytesToSkip;
- } while (offset > 0);
- } else if (offset < 0) {
+ if (offset < 0
+ || offset >= qint64(d->buffer.size()))
// When seeking backwards, an operation that is only allowed for
// random-access devices, the buffer is cleared. The next read
// operation will then refill the buffer. We can optimize this, if we
// find that seeking backwards becomes a significant performance hit.
d->buffer.clear();
- }
+ else if (!d->buffer.isEmpty())
+ d->buffer.skip(int(offset));
+
#if defined QIODEVICE_DEBUG
printf("%p \tafter: d->pos == %d, d->buffer.size() == %d\n", this, int(d->pos),
d->buffer.size());