diff options
author | Ritt Konstantin <ritt.ks@gmail.com> | 2011-06-10 09:10:49 (GMT) |
---|---|---|
committer | Peter Hartmann <peter.hartmann@nokia.com> | 2011-06-10 09:11:48 (GMT) |
commit | 6b91affb9a355e668bc9d06dee580d95230ac63a (patch) | |
tree | 341248589b104d4f329ee75e7a1188e3e00d277c | |
parent | f2bfd1f7913e21f8946ea9aa2f96ee4e38a942eb (diff) | |
download | Qt-6b91affb9a355e668bc9d06dee580d95230ac63a.zip Qt-6b91affb9a355e668bc9d06dee580d95230ac63a.tar.gz Qt-6b91affb9a355e668bc9d06dee580d95230ac63a.tar.bz2 |
fix an incorrect OpenMode flags handling in QBuffer::open()
which leads to absurd statement (QBuffer::open() == !QBuffer::isOpen()) to be true.
also treat Truncate as (Truncate | WriteOnly) to satisfy lazy ones
Reviewed-by: Joao
Merge-request: 2612
-rw-r--r-- | src/corelib/io/qbuffer.cpp | 17 | ||||
-rw-r--r-- | tests/auto/qbuffer/tst_qbuffer.cpp | 50 |
2 files changed, 56 insertions, 11 deletions
diff --git a/src/corelib/io/qbuffer.cpp b/src/corelib/io/qbuffer.cpp index 38e406b..35e7b68 100644 --- a/src/corelib/io/qbuffer.cpp +++ b/src/corelib/io/qbuffer.cpp @@ -333,23 +333,18 @@ bool QBuffer::open(OpenMode flags) { Q_D(QBuffer); - if ((flags & Append) == Append) + if ((flags & (Append | Truncate)) != 0) flags |= WriteOnly; - setOpenMode(flags); - if (!(isReadable() || isWritable())) { - qWarning("QFile::open: File access not specified"); + if ((flags & (ReadOnly | WriteOnly)) == 0) { + qWarning("QBuffer::open: Buffer access not specified"); return false; } - if ((flags & QIODevice::Truncate) == QIODevice::Truncate) { + if ((flags & Truncate) == Truncate) d->buf->resize(0); - } - if ((flags & QIODevice::Append) == QIODevice::Append) // append to end of buffer - seek(d->buf->size()); - else - seek(0); + d->ioIndex = (flags & Append) == Append ? d->buf->size() : 0; - return true; + return QIODevice::open(flags); } /*! diff --git a/tests/auto/qbuffer/tst_qbuffer.cpp b/tests/auto/qbuffer/tst_qbuffer.cpp index 776935d..bf4842f 100644 --- a/tests/auto/qbuffer/tst_qbuffer.cpp +++ b/tests/auto/qbuffer/tst_qbuffer.cpp @@ -56,6 +56,7 @@ public: tst_QBuffer(); private slots: + void open(); void getSetCheck(); void readBlock(); void readBlockPastEnd(); @@ -104,6 +105,55 @@ tst_QBuffer::tst_QBuffer() { } +void tst_QBuffer::open() +{ + QByteArray data(10, 'f'); + + QBuffer b; + + QTest::ignoreMessage(QtWarningMsg, "QBuffer::open: Buffer access not specified"); + QVERIFY(!b.open(QIODevice::NotOpen)); + QVERIFY(!b.isOpen()); + b.close(); + + QTest::ignoreMessage(QtWarningMsg, "QBuffer::open: Buffer access not specified"); + QVERIFY(!b.open(QIODevice::Text)); + QVERIFY(!b.isOpen()); + b.close(); + + QTest::ignoreMessage(QtWarningMsg, "QBuffer::open: Buffer access not specified"); + QVERIFY(!b.open(QIODevice::Unbuffered)); + QVERIFY(!b.isOpen()); + b.close(); + + QVERIFY(b.open(QIODevice::ReadOnly)); + QVERIFY(b.isReadable()); + b.close(); + + QVERIFY(b.open(QIODevice::WriteOnly)); + QVERIFY(b.isWritable()); + b.close(); + + b.setData(data); + QVERIFY(b.open(QIODevice::Append)); + QVERIFY(b.isWritable()); + QCOMPARE(b.size(), qint64(10)); + QCOMPARE(b.pos(), b.size()); + b.close(); + + b.setData(data); + QVERIFY(b.open(QIODevice::Truncate)); + QVERIFY(b.isWritable()); + QCOMPARE(b.size(), qint64(0)); + QCOMPARE(b.pos(), qint64(0)); + b.close(); + + QVERIFY(b.open(QIODevice::ReadWrite)); + QVERIFY(b.isReadable()); + QVERIFY(b.isWritable()); + b.close(); +} + // some status() tests, too void tst_QBuffer::readBlock() { |