summaryrefslogtreecommitdiffstats
path: root/src/corelib/io
diff options
context:
space:
mode:
authorGeir Vattekar <geir.vattekar@nokia.com>2010-06-29 13:13:40 (GMT)
committerGeir Vattekar <geir.vattekar@nokia.com>2010-06-29 13:13:40 (GMT)
commitc2c720cf948f2779aa82e003a2db4d951f94e77f (patch)
tree0f3662fcf8a336f6cd7d5090184f7ead2336691e /src/corelib/io
parent497fd44414dea6314731b96869e2d49bb8b439b5 (diff)
parent1209a398c3b59b21b38036fffd4fb2442320e5c3 (diff)
downloadQt-c2c720cf948f2779aa82e003a2db4d951f94e77f.zip
Qt-c2c720cf948f2779aa82e003a2db4d951f94e77f.tar.gz
Qt-c2c720cf948f2779aa82e003a2db4d951f94e77f.tar.bz2
Merge branch '4.7' of git@scm.dev.nokia.troll.no:qt/oslo-staging-2 into 4.7
Diffstat (limited to 'src/corelib/io')
-rw-r--r--src/corelib/io/qbuffer.cpp18
-rw-r--r--src/corelib/io/qiodevice.cpp42
-rw-r--r--src/corelib/io/qiodevice_p.h13
3 files changed, 61 insertions, 12 deletions
diff --git a/src/corelib/io/qbuffer.cpp b/src/corelib/io/qbuffer.cpp
index c1ff353..73c71ea 100644
--- a/src/corelib/io/qbuffer.cpp
+++ b/src/corelib/io/qbuffer.cpp
@@ -62,6 +62,9 @@ public:
QByteArray defaultBuf;
int ioIndex;
+ virtual qint64 peek(char *data, qint64 maxSize);
+ virtual QByteArray peek(qint64 maxSize);
+
#ifndef QT_NO_QOBJECT
// private slots
void _q_emitSignals();
@@ -83,6 +86,21 @@ void QBufferPrivate::_q_emitSignals()
}
#endif
+qint64 QBufferPrivate::peek(char *data, qint64 maxSize)
+{
+ qint64 readBytes = qMin(maxSize, static_cast<qint64>(buf->size()) - pos);
+ memcpy(data, buf->constData() + pos, readBytes);
+ return readBytes;
+}
+
+QByteArray QBufferPrivate::peek(qint64 maxSize)
+{
+ qint64 readBytes = qMin(maxSize, static_cast<qint64>(buf->size()) - pos);
+ if (pos == 0 && maxSize >= buf->size())
+ return *buf;
+ return QByteArray(buf->constData() + pos, readBytes);
+}
+
/*!
\class QBuffer
\reentrant
diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp
index 223df9b..26e587d 100644
--- a/src/corelib/io/qiodevice.cpp
+++ b/src/corelib/io/qiodevice.cpp
@@ -1442,6 +1442,35 @@ bool QIODevicePrivate::putCharHelper(char c)
return q_func()->write(&c, 1) == 1;
}
+/*!
+ \internal
+*/
+qint64 QIODevicePrivate::peek(char *data, qint64 maxSize)
+{
+ qint64 readBytes = q_func()->read(data, maxSize);
+ if (readBytes <= 0)
+ return readBytes;
+
+ buffer.ungetBlock(data, readBytes);
+ *pPos -= readBytes;
+ return readBytes;
+}
+
+/*!
+ \internal
+*/
+QByteArray QIODevicePrivate::peek(qint64 maxSize)
+{
+ QByteArray result = q_func()->read(maxSize);
+
+ if (result.isEmpty())
+ return result;
+
+ buffer.ungetBlock(result.constData(), result.size());
+ *pPos -= result.size();
+ return result;
+}
+
/*! \fn bool QIODevice::getChar(char *c)
Reads one character from the device and stores it in \a c. If \a c
@@ -1476,11 +1505,7 @@ bool QIODevice::getChar(char *c)
*/
qint64 QIODevice::peek(char *data, qint64 maxSize)
{
- qint64 readBytes = read(data, maxSize);
- int i = readBytes;
- while (i > 0)
- ungetChar(data[i-- - 1]);
- return readBytes;
+ return d_func()->peek(data, maxSize);
}
/*!
@@ -1502,12 +1527,7 @@ qint64 QIODevice::peek(char *data, qint64 maxSize)
*/
QByteArray QIODevice::peek(qint64 maxSize)
{
- QByteArray result = read(maxSize);
- int i = result.size();
- const char *data = result.constData();
- while (i > 0)
- ungetChar(data[i-- - 1]);
- return result;
+ return d_func()->peek(maxSize);
}
/*!
diff --git a/src/corelib/io/qiodevice_p.h b/src/corelib/io/qiodevice_p.h
index 94dadca..4a25562 100644
--- a/src/corelib/io/qiodevice_p.h
+++ b/src/corelib/io/qiodevice_p.h
@@ -151,6 +151,15 @@ public:
len++;
*first = c;
}
+ void ungetBlock(const char* block, int size) {
+ if ((first - buf) < size) {
+ // underflow, the existing valid data needs to move to the end of the (potentially bigger) buffer
+ makeSpace(len + size, freeSpaceAtStart);
+ memcpy(first - size, block, size);
+ }
+ first -= size;
+ len += size;
+ }
private:
enum FreeSpacePos {freeSpaceAtStart, freeSpaceAtEnd};
@@ -222,7 +231,9 @@ public:
accessMode = q_func()->isSequential() ? Sequential : RandomAccess;
return accessMode == Sequential;
}
-
+
+ virtual qint64 peek(char *data, qint64 maxSize);
+ virtual QByteArray peek(qint64 maxSize);
#ifdef QT_NO_QOBJECT
QIODevice *q_ptr;