summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qiodevice.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/io/qiodevice.cpp')
-rw-r--r--src/corelib/io/qiodevice.cpp147
1 files changed, 55 insertions, 92 deletions
diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp
index e362535852..b0029e2af7 100644
--- a/src/corelib/io/qiodevice.cpp
+++ b/src/corelib/io/qiodevice.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
//#define QIODEVICE_DEBUG
@@ -45,48 +9,42 @@
#include "qfile.h"
#include "qstringlist.h"
#include "qdir.h"
-#include "private/qbytearray_p.h"
+#include "private/qtools_p.h"
#include <algorithm>
-#ifdef QIODEVICE_DEBUG
-# include <ctype.h>
-#endif
-
QT_BEGIN_NAMESPACE
-#ifdef QIODEVICE_DEBUG
-void debugBinaryString(const QByteArray &input)
+using namespace Qt::StringLiterals;
+using namespace QtMiscUtils;
+
+[[maybe_unused]]
+static void debugBinaryString(const char *input, qint64 maxlen)
{
QByteArray tmp;
- int startOffset = 0;
- for (int i = 0; i < input.size(); ++i) {
+ qlonglong startOffset = 0;
+ for (qint64 i = 0; i < maxlen; ++i) {
tmp += input[i];
- if ((i % 16) == 15 || i == (input.size() - 1)) {
- printf("\n%15d:", startOffset);
+ if ((i % 16) == 15 || i == (maxlen - 1)) {
+ printf("\n%15lld:", startOffset);
startOffset += tmp.size();
- for (int j = 0; j < tmp.size(); ++j)
+ for (qsizetype j = 0; j < tmp.size(); ++j)
printf(" %02x", int(uchar(tmp[j])));
- for (int j = tmp.size(); j < 16 + 1; ++j)
+ for (qsizetype j = tmp.size(); j < 16 + 1; ++j)
printf(" ");
- for (int j = 0; j < tmp.size(); ++j)
- printf("%c", isprint(int(uchar(tmp[j]))) ? tmp[j] : '.');
+ for (qsizetype j = 0; j < tmp.size(); ++j)
+ printf("%c", isAsciiPrintable(tmp[j]) ? tmp[j] : '.');
tmp.clear();
}
}
printf("\n\n");
}
-void debugBinaryString(const char *data, qint64 maxlen)
-{
- debugBinaryString(QByteArray(data, maxlen));
-}
-#endif
-
#define Q_VOID
+Q_DECL_COLD_FUNCTION
static void checkWarnMessage(const QIODevice *device, const char *function, const char *what)
{
#ifndef QT_NO_WARNING_OUTPUT
@@ -130,9 +88,9 @@ static void checkWarnMessage(const QIODevice *device, const char *function, cons
#define CHECK_MAXBYTEARRAYSIZE(function) \
do { \
- if (maxSize >= MaxByteArraySize) { \
+ if (maxSize >= QByteArray::max_size()) { \
checkWarnMessage(this, #function, "maxSize argument exceeds QByteArray size limit"); \
- maxSize = MaxByteArraySize - 1; \
+ maxSize = QByteArray::max_size() - 1; \
} \
} while (0)
@@ -703,15 +661,18 @@ void QIODevice::setCurrentReadChannel(int channel)
void QIODevicePrivate::setReadChannelCount(int count)
{
if (count > readBuffers.size()) {
+ readBuffers.reserve(count);
+
// If readBufferChunkSize is zero, we should bypass QIODevice's
// read buffers, even if the QIODeviceBase::Unbuffered flag is not
// set when opened. However, if a read transaction is started or
// ungetChar() is called, we still have to use the internal buffer.
// To support these cases, pass a default value to the QRingBuffer
// constructor.
- readBuffers.insert(readBuffers.end(), count - readBuffers.size(),
- QRingBuffer(readBufferChunkSize != 0 ? readBufferChunkSize
- : QIODEVICE_BUFFERSIZE));
+
+ while (readBuffers.size() < count)
+ readBuffers.emplace_back(readBufferChunkSize != 0 ? readBufferChunkSize
+ : QIODEVICE_BUFFERSIZE);
} else {
readBuffers.resize(count);
}
@@ -762,8 +723,9 @@ void QIODevicePrivate::setWriteChannelCount(int count)
// If writeBufferChunkSize is zero (default value), we don't use
// QIODevice's write buffers.
if (writeBufferChunkSize != 0) {
- writeBuffers.insert(writeBuffers.end(), count - writeBuffers.size(),
- QRingBuffer(writeBufferChunkSize));
+ writeBuffers.reserve(count);
+ while (writeBuffers.size() < count)
+ writeBuffers.emplace_back(writeBufferChunkSize);
}
} else {
writeBuffers.resize(count);
@@ -1047,7 +1009,7 @@ qint64 QIODevice::read(char *data, qint64 maxSize)
*data = c;
#if defined QIODEVICE_DEBUG
printf("%p \tread 0x%hhx (%c) returning 1 (shortcut)\n", this,
- int(c), isprint(c) ? c : '?');
+ int(c), isAsciiPrintable(c) ? c : '?');
#endif
if (d->buffer.isEmpty())
readData(data, 0);
@@ -1240,13 +1202,13 @@ QByteArray QIODevice::read(qint64 maxSize)
CHECK_MAXLEN(read, result);
CHECK_MAXBYTEARRAYSIZE(read);
- result.resize(int(maxSize));
+ result.resize(qsizetype(maxSize));
qint64 readBytes = d->read(result.data(), result.size());
if (readBytes <= 0)
result.clear();
else
- result.resize(int(readBytes));
+ result.resize(qsizetype(readBytes));
return result;
}
@@ -1257,7 +1219,9 @@ QByteArray QIODevice::read(qint64 maxSize)
This function has no way of reporting errors; returning an empty
QByteArray can mean either that no data was currently available
- for reading, or that an error occurred.
+ for reading, or that an error occurred. This function also has no
+ way of indicating that more data may have been available and
+ couldn't be read.
*/
QByteArray QIODevice::readAll()
{
@@ -1278,7 +1242,7 @@ QByteArray QIODevice::readAll()
: d->buffer.size());
qint64 readResult;
do {
- if (readBytes + readChunkSize >= MaxByteArraySize) {
+ if (readBytes + readChunkSize >= QByteArray::max_size()) {
// If resize would fail, don't read more, return what we have.
break;
}
@@ -1291,10 +1255,9 @@ QByteArray QIODevice::readAll()
} while (readResult > 0);
} else {
// Read it all in one go.
- // If resize fails, don't read anything.
readBytes -= d->pos;
- if (readBytes >= MaxByteArraySize)
- return QByteArray();
+ if (readBytes >= QByteArray::max_size())
+ readBytes = QByteArray::max_size();
result.resize(readBytes);
readBytes = d->read(result.data(), readBytes);
}
@@ -1302,7 +1265,7 @@ QByteArray QIODevice::readAll()
if (readBytes <= 0)
result.clear();
else
- result.resize(int(readBytes));
+ result.resize(qsizetype(readBytes));
return result;
}
@@ -1363,7 +1326,7 @@ qint64 QIODevice::readLine(char *data, qint64 maxSize)
#if defined QIODEVICE_DEBUG
printf("%p \treturning %lld, d->pos = %lld, d->buffer.size() = %lld, size() = %lld\n",
this, readBytes, d->pos, d->buffer.size(), size());
- debugBinaryString(data, int(readBytes));
+ debugBinaryString(data, readBytes);
#endif
return readBytes;
@@ -1407,7 +1370,7 @@ qint64 QIODevicePrivate::readLine(char *data, qint64 maxSize)
#if defined QIODEVICE_DEBUG
printf("%p \tread from buffer: %lld bytes, last character read: %hhx\n", q,
readSoFar, data[readSoFar - 1]);
- debugBinaryString(data, int(readSoFar));
+ debugBinaryString(data, readSoFar);
#endif
if (data[readSoFar - 1] == '\n') {
if (openMode & QIODevice::Text) {
@@ -1434,7 +1397,7 @@ qint64 QIODevicePrivate::readLine(char *data, qint64 maxSize)
printf("%p \tread from readLineData: %lld bytes, readSoFar = %lld bytes\n", q,
readBytes, readSoFar);
if (readBytes > 0) {
- debugBinaryString(data, int(readSoFar + readBytes));
+ debugBinaryString(data, readSoFar + readBytes);
}
#endif
if (readBytes < 0) {
@@ -1485,19 +1448,19 @@ QByteArray QIODevice::readLine(qint64 maxSize)
qint64 readBytes = 0;
if (maxSize == 0) {
// Size is unknown, read incrementally.
- maxSize = MaxByteArraySize - 1;
+ maxSize = QByteArray::max_size() - 1;
// The first iteration needs to leave an extra byte for the terminating null
result.resize(1);
qint64 readResult;
do {
- result.resize(int(qMin(maxSize, qint64(result.size() + d->buffer.chunkSize()))));
+ result.resize(qsizetype(qMin(maxSize, qint64(result.size() + d->buffer.chunkSize()))));
readResult = d->readLine(result.data() + readBytes, result.size() - readBytes);
if (readResult > 0 || readBytes == 0)
readBytes += readResult;
} while (readResult == d->buffer.chunkSize()
- && result[int(readBytes - 1)] != '\n');
+ && result[qsizetype(readBytes - 1)] != '\n');
} else {
CHECK_LINEMAXLEN(readLine, result);
CHECK_MAXBYTEARRAYSIZE(readLine);
@@ -1537,7 +1500,7 @@ qint64 QIODevice::readLineData(char *data, qint64 maxSize)
Q_D(QIODevice);
qint64 readSoFar = 0;
char c;
- int lastReadReturn = 0;
+ qint64 lastReadReturn = 0;
d->baseReadLineDataCalled = true;
while (readSoFar < maxSize && (lastReadReturn = read(&c, 1)) == 1) {
@@ -1827,7 +1790,7 @@ void QIODevice::ungetChar(char c)
}
#if defined QIODEVICE_DEBUG
- printf("%p QIODevice::ungetChar(0x%hhx '%c')\n", this, c, isprint(c) ? c : '?');
+ printf("%p QIODevice::ungetChar(0x%hhx '%c')\n", this, c, isAsciiPrintable(c) ? c : '?');
#endif
d->buffer.ungetChar(c);
@@ -2166,7 +2129,7 @@ QString QIODevice::errorString() const
Q_D(const QIODevice);
if (d->errorString.isEmpty()) {
#ifdef QT_NO_QOBJECT
- return QLatin1String(QT_TRANSLATE_NOOP(QIODevice, "Unknown error"));
+ return QLatin1StringView(QT_TRANSLATE_NOOP(QIODevice, "Unknown error"));
#else
return tr("Unknown error");
#endif
@@ -2241,23 +2204,23 @@ QDebug operator<<(QDebug debug, QIODevice::OpenMode modes)
debug << "OpenMode(";
QStringList modeList;
if (modes == QIODevice::NotOpen) {
- modeList << QLatin1String("NotOpen");
+ modeList << "NotOpen"_L1;
} else {
if (modes & QIODevice::ReadOnly)
- modeList << QLatin1String("ReadOnly");
+ modeList << "ReadOnly"_L1;
if (modes & QIODevice::WriteOnly)
- modeList << QLatin1String("WriteOnly");
+ modeList << "WriteOnly"_L1;
if (modes & QIODevice::Append)
- modeList << QLatin1String("Append");
+ modeList << "Append"_L1;
if (modes & QIODevice::Truncate)
- modeList << QLatin1String("Truncate");
+ modeList << "Truncate"_L1;
if (modes & QIODevice::Text)
- modeList << QLatin1String("Text");
+ modeList << "Text"_L1;
if (modes & QIODevice::Unbuffered)
- modeList << QLatin1String("Unbuffered");
+ modeList << "Unbuffered"_L1;
}
std::sort(modeList.begin(), modeList.end());
- debug << modeList.join(QLatin1Char('|'));
+ debug << modeList.join(u'|');
debug << ')';
return debug;
}