12#include "private/qbytearray_p.h"
13#include "private/qtools_p.h"
30 if ((
i % 16) == 15 ||
i == (maxlen - 1)) {
31 printf(
"\n%15lld:", startOffset);
32 startOffset += tmp.
size();
35 printf(
" %02x",
int(
uchar(tmp[
j])));
50#ifndef QT_NO_WARNING_OUTPUT
56 d <<
" (" <<
device->metaObject()->className();
57 if (!
device->objectName().isEmpty())
58 d <<
", \"" <<
device->objectName() <<
'"';
59 if (
const QFile *
f = qobject_cast<const QFile *>(
device))
73#define CHECK_MAXLEN(function, returnType) \
76 checkWarnMessage(this, #function, "Called with maxSize < 0"); \
81#define CHECK_LINEMAXLEN(function, returnType) \
84 checkWarnMessage(this, #function, "Called with maxSize < 2"); \
89#define CHECK_MAXBYTEARRAYSIZE(function) \
91 if (maxSize >= MaxByteArraySize) { \
92 checkWarnMessage(this, #function, "maxSize argument exceeds QByteArray size limit"); \
93 maxSize = MaxByteArraySize - 1; \
97#define CHECK_WRITABLE(function, returnType) \
99 if ((d->openMode & WriteOnly) == 0) { \
100 if (d->openMode == NotOpen) { \
101 checkWarnMessage(this, #function, "device not open"); \
104 checkWarnMessage(this, #function, "ReadOnly device"); \
109#define CHECK_READABLE(function, returnType) \
111 if ((d->openMode & ReadOnly) == 0) { \
112 if (d->openMode == NotOpen) { \
113 checkWarnMessage(this, #function, "device not open"); \
116 checkWarnMessage(this, #function, "WriteOnly device"); \
425#if defined QIODEVICE_DEBUG
426 QFile *
file = qobject_cast<QFile *>(
this);
439#if defined QIODEVICE_DEBUG
462#if defined QIODEVICE_DEBUG
463 printf(
"%p QIODevice::~QIODevice()\n",
this);
498 return d_func()->openMode;
511#if defined QIODEVICE_DEBUG
512 printf(
"%p QIODevice::setOpenMode(0x%x)\n",
this,
openMode.toInt());
539 d->openMode &= ~Text;
549 return d_func()->openMode.testAnyFlag(
Text);
562 return d_func()->openMode !=
NotOpen;
603 return d_func()->readChannelCount;
616 return d_func()->writeChannelCount;
628 return d_func()->currentReadChannel;
645 if (
d->transactionStarted) {
646 checkWarnMessage(
this,
"setReadChannel",
"Failed due to read transaction being in progress");
650#if defined QIODEVICE_DEBUG
651 qDebug(
"%p QIODevice::setCurrentReadChannel(%d), d->currentReadChannel = %d, d->readChannelCount = %d\n",
652 this,
channel,
d->currentReadChannel,
d->readChannelCount);
692 return d_func()->currentWriteChannel;
709#if defined QIODEVICE_DEBUG
710 qDebug(
"%p QIODevice::setCurrentWriteChannel(%d), d->currentWriteChannel = %d, d->writeChannelCount = %d\n",
711 this,
channel,
d->currentWriteChannel,
d->writeChannelCount);
743 if (!ringBuffer.isEmpty())
762 d->readBuffers.clear();
763 d->writeBuffers.clear();
766 d->errorString.clear();
767#if defined QIODEVICE_DEBUG
768 printf(
"%p QIODevice::open(0x%x)\n",
this,
mode.toInt());
785#if defined QIODEVICE_DEBUG
786 printf(
"%p QIODevice::close()\n",
this);
794 d->transactionStarted =
false;
795 d->transactionPos = 0;
796 d->setReadChannelCount(0);
798 d->writeChannelCount = 0;
817#if defined QIODEVICE_DEBUG
818 printf(
"%p QIODevice::pos() == %lld\n",
this,
d->pos);
852 if (
d->isSequential()) {
861 qWarning(
"QIODevice::seek: Invalid pos: %lld",
pos);
865#if defined QIODEVICE_DEBUG
866 printf(
"%p QIODevice::seek(%lld), before: d->pos = %lld, d->buffer.size() = %lld\n",
867 this,
pos,
d->pos,
d->buffer.size());
873#if defined QIODEVICE_DEBUG
874 printf(
"%p \tafter: d->pos == %lld, d->buffer.size() == %lld\n",
this,
d->pos,
888 if (offset < 0 || offset >=
buffer.size()) {
915#if defined QIODEVICE_DEBUG
916 printf(
"%p QIODevice::atEnd() returns %s, d->openMode == %d, d->pos == %lld\n",
this,
917 result ?
"true" :
"false",
d->openMode.toInt(),
d->pos);
935#if defined QIODEVICE_DEBUG
936 printf(
"%p QIODevice::reset()\n",
this);
956 if (!
d->isSequential())
958 return d->buffer.size() -
d->transactionPos;
972 return d_func()->writeBuffer.size();
991#if defined QIODEVICE_DEBUG
992 printf(
"%p QIODevice::read(%p, %lld), d->pos = %lld, d->buffer.size() = %lld\n",
993 this,
data, maxSize,
d->pos,
d->buffer.size());
997 const bool sequential =
d->isSequential();
1000 if (maxSize == 1 && !(sequential &&
d->transactionStarted)) {
1002 while ((chint =
d->buffer.getChar()) != -1) {
1006 char c = char(
uchar(chint));
1007 if (
c ==
'\r' && (
d->openMode &
Text))
1010#if defined QIODEVICE_DEBUG
1011 printf(
"%p \tread 0x%hhx (%c) returning 1 (shortcut)\n",
this,
1014 if (
d->buffer.isEmpty())
1023#if defined QIODEVICE_DEBUG
1024 printf(
"%p \treturning %lld, d->pos == %lld, d->buffer.size() == %lld\n",
this,
1025 readBytes,
d->pos,
d->buffer.size());
1042 const bool keepDataInBuffer = sequential
1044 : peeking && buffered;
1047 bool madeBufferReadsOnly =
true;
1048 bool deviceAtEof =
false;
1049 char *readPtr =
data;
1053 qint64 bufferReadChunkSize = keepDataInBuffer
1056 if (bufferReadChunkSize > 0) {
1057 bufferPos += bufferReadChunkSize;
1059 pos += bufferReadChunkSize;
1060#if defined QIODEVICE_DEBUG
1061 printf(
"%p \treading %lld bytes from buffer into position %lld\n",
q,
1062 bufferReadChunkSize, readSoFar);
1064 readSoFar += bufferReadChunkSize;
1065 data += bufferReadChunkSize;
1066 maxSize -= bufferReadChunkSize;
1069 if (maxSize > 0 && !deviceAtEof) {
1073 madeBufferReadsOnly =
false;
1078#if defined QIODEVICE_DEBUG
1079 printf(
"%p \treading %lld bytes from device (total %lld)\n",
q,
1093 const qint64 bytesToBuffer = (!buffered && maxSize <
buffer.chunkSize())
1103#if defined QIODEVICE_DEBUG
1104 printf(
"%p \treading %lld from device into buffer\n",
q,
1121 const char *endPtr =
data;
1124 while (*readPtr !=
'\r') {
1125 if (++readPtr == endPtr)
1129 char *writePtr = readPtr;
1131 while (readPtr < endPtr) {
1132 char ch = *readPtr++;
1153 if (keepDataInBuffer) {
1158 }
else if (peeking) {
1163 q->readData(
data, 0);
1182#if defined QIODEVICE_DEBUG
1183 printf(
"%p QIODevice::read(%lld), d->pos = %lld, d->buffer.size() = %lld\n",
1184 this, maxSize,
d->pos,
d->buffer.size());
1192 if (maxSize ==
d->buffer.nextDataBlockSize() && !
d->transactionStarted
1195 if (!
d->isSequential())
1197 if (
d->buffer.isEmpty())
1229#if defined QIODEVICE_DEBUG
1230 printf(
"%p QIODevice::readAll(), d->pos = %lld, d->buffer.size() = %lld\n",
1231 this,
d->pos,
d->buffer.size());
1238 if (readBytes == 0) {
1241 d->isSequential() ? (
d->buffer.size() -
d->transactionPos)
1242 :
d->buffer.size());
1249 result.resize(readBytes + readChunkSize);
1250 readResult =
d->read(
result.data() + readBytes, readChunkSize);
1251 if (readResult > 0 || readBytes == 0) {
1252 readBytes += readResult;
1253 readChunkSize =
d->buffer.chunkSize();
1255 }
while (readResult > 0);
1258 readBytes -=
d->pos;
1261 result.resize(readBytes);
1262 readBytes =
d->read(
result.data(), readBytes);
1317#if defined QIODEVICE_DEBUG
1318 printf(
"%p QIODevice::readLine(%p, %lld), d->pos = %lld, d->buffer.size() = %lld\n",
1319 this,
data, maxSize,
d->pos,
d->buffer.size());
1324 const qint64 readBytes =
d->readLine(
data, maxSize);
1326#if defined QIODEVICE_DEBUG
1327 printf(
"%p \treturning %lld, d->pos = %lld, d->buffer.size() = %lld, size() = %lld\n",
1328 this, readBytes,
d->pos,
d->buffer.size(),
size());
1350 if (keepDataInBuffer) {
1358 q->readData(
data, 0);
1360 }
else if (!
buffer.isEmpty()) {
1362 readSoFar =
buffer.readLine(
data, maxSize + 1);
1364 q->readData(
data, 0);
1370#if defined QIODEVICE_DEBUG
1371 printf(
"%p \tread from buffer: %lld bytes, last character read: %hhx\n",
q,
1372 readSoFar,
data[readSoFar - 1]);
1375 if (
data[readSoFar - 1] ==
'\n') {
1378 if (readSoFar > 1 &&
data[readSoFar - 2] ==
'\r') {
1380 data[readSoFar - 1] =
'\n';
1383 data[readSoFar] =
'\0';
1393 qint64 readBytes = keepDataInBuffer
1394 ?
q->QIODevice::readLineData(
data + readSoFar, maxSize - readSoFar)
1395 :
q->readLineData(
data + readSoFar, maxSize - readSoFar);
1396#if defined QIODEVICE_DEBUG
1397 printf(
"%p \tread from readLineData: %lld bytes, readSoFar = %lld bytes\n",
q,
1398 readBytes, readSoFar);
1399 if (readBytes > 0) {
1403 if (readBytes < 0) {
1404 data[readSoFar] =
'\0';
1405 return readSoFar ? readSoFar : -1;
1407 readSoFar += readBytes;
1414 data[readSoFar] =
'\0';
1417 if (readSoFar > 1 &&
data[readSoFar - 1] ==
'\n' &&
data[readSoFar - 2] ==
'\r') {
1418 data[readSoFar - 2] =
'\n';
1419 data[readSoFar - 1] =
'\0';
1440#if defined QIODEVICE_DEBUG
1441 printf(
"%p QIODevice::readLine(%lld), d->pos = %lld, d->buffer.size() = %lld\n",
1442 this, maxSize,
d->pos,
d->buffer.size());
1459 readResult =
d->readLine(
result.data() + readBytes,
result.size() - readBytes);
1460 if (readResult > 0 || readBytes == 0)
1461 readBytes += readResult;
1462 }
while (readResult ==
d->buffer.chunkSize()
1475 result.resize(readBytes);
1503 qint64 lastReadReturn = 0;
1504 d->baseReadLineDataCalled =
true;
1506 while (readSoFar < maxSize && (lastReadReturn =
read(&
c, 1)) == 1) {
1513#if defined QIODEVICE_DEBUG
1514 printf(
"%p QIODevice::readLineData(%p, %lld), d->pos = %lld, d->buffer.size() = %lld, "
1515 "returns %lld\n",
this,
data, maxSize,
d->pos,
d->buffer.size(), readSoFar);
1517 if (lastReadReturn != 1 && readSoFar == 0)
1542 return d->buffer.indexOf(
'\n',
d->buffer.size(),
1543 d->isSequential() ?
d->transactionPos :
Q_INT64_C(0)) >= 0;
1564 if (
d->transactionStarted) {
1565 checkWarnMessage(
this,
"startTransaction",
"Called while transaction already in progress");
1568 d->transactionPos =
d->pos;
1569 d->transactionStarted =
true;
1585 if (!
d->transactionStarted) {
1586 checkWarnMessage(
this,
"commitTransaction",
"Called while no transaction in progress");
1589 if (
d->isSequential())
1590 d->buffer.free(
d->transactionPos);
1591 d->transactionStarted =
false;
1592 d->transactionPos = 0;
1609 if (!
d->transactionStarted) {
1610 checkWarnMessage(
this,
"rollbackTransaction",
"Called while no transaction in progress");
1613 if (!
d->isSequential())
1614 d->seekBuffer(
d->transactionPos);
1615 d->transactionStarted =
false;
1616 d->transactionPos = 0;
1629 return d_func()->transactionStarted;
1645 const bool sequential =
d->isSequential();
1647 if (
d->pos !=
d->devicePos && !sequential && !
seek(
d->pos))
1651 if (
d->openMode &
Text) {
1652 const char *endOfData =
data + maxSize;
1653 const char *startOfBlock =
data;
1656 const qint64 savedPos =
d->pos;
1659 const char *endOfBlock = startOfBlock;
1660 while (endOfBlock < endOfData && *endOfBlock !=
'\n')
1667 if (writtenSoFar && !sequential)
1668 d->buffer.skip(
d->pos - savedPos);
1669 return writtenSoFar ? writtenSoFar :
ret;
1673 d->devicePos +=
ret;
1675 writtenSoFar +=
ret;
1678 if (endOfBlock == endOfData)
1683 if (writtenSoFar && !sequential)
1684 d->buffer.skip(
d->pos - savedPos);
1685 return writtenSoFar ? writtenSoFar :
ret;
1689 d->devicePos +=
ret;
1693 startOfBlock = endOfBlock + 1;
1696 if (writtenSoFar && !sequential)
1697 d->buffer.skip(
d->pos - savedPos);
1698 return writtenSoFar;
1703 if (!sequential && written > 0) {
1705 d->devicePos += written;
1706 d->buffer.skip(written);
1749 d->currentWriteChunk = &
data;
1753 d->currentWriteChunk =
nullptr;
1787 if (
d->transactionStarted) {
1788 checkWarnMessage(
this,
"ungetChar",
"Called while transaction is in progress");
1792#if defined QIODEVICE_DEBUG
1796 d->buffer.ungetChar(
c);
1797 if (!
d->isSequential())
1810 return d_func()->putCharHelper(
c);
1818 return q_func()->write(&
c, 1) == 1;
1838 if (readBytes < maxSize) {
1842 result.resize(readBytes);
1860 return (1 ==
read(
c ?
c : &
ch, 1));
1887 return d->peek(
data, maxSize);
1915 return d->peek(maxSize);
1945 const bool sequential =
d->isSequential();
1947#if defined QIODEVICE_DEBUG
1948 printf(
"%p QIODevice::skip(%lld), d->pos = %lld, d->buffer.size() = %lld\n",
1949 this, maxSize,
d->pos,
d->buffer.size());
1952 if ((sequential &&
d->transactionStarted) || (
d->openMode &
QIODevice::Text) != 0)
1953 return d->skipByReading(maxSize);
1957 if (!
d->buffer.isEmpty()) {
1958 skippedSoFar =
d->buffer.skip(maxSize);
1959#if defined QIODEVICE_DEBUG
1960 printf(
"%p \tskipping %lld bytes in buffer\n",
this, skippedSoFar);
1963 d->pos += skippedSoFar;
1964 if (
d->buffer.isEmpty())
1966 if (skippedSoFar == maxSize)
1967 return skippedSoFar;
1969 maxSize -= skippedSoFar;
1979 if (bytesToSkip > 0) {
1980 if (!
seek(
d->pos + bytesToSkip))
1981 return skippedSoFar ? skippedSoFar :
Q_INT64_C(-1);
1982 if (bytesToSkip == maxSize)
1983 return skippedSoFar + bytesToSkip;
1985 skippedSoFar += bytesToSkip;
1986 maxSize -= bytesToSkip;
1991 if (skippedSoFar == 0)
1994 if (skipResult == -1)
1995 return skippedSoFar;
1997 return skippedSoFar + skipResult;
2008 const qint64 readBytes = qMin<qint64>(maxSize,
sizeof(dummy));
2009 const qint64 readResult =
read(dummy, readBytes);
2012 if (readResult != readBytes) {
2016 if (readResult == -1)
2019 return readSoFar + readResult;
2022 readSoFar += readResult;
2023 maxSize -= readResult;
2024 }
while (maxSize > 0);
2046 return d_func()->skipByReading(maxSize);
2118 d_func()->errorString =
str;
2130 if (
d->errorString.isEmpty()) {
2134 return tr(
"Unknown error");
2137 return d->errorString;
2201#if !defined(QT_NO_DEBUG_STREAM)
2204 debug <<
"OpenMode(";
2207 modeList <<
"NotOpen"_L1;
2210 modeList <<
"ReadOnly"_L1;
2212 modeList <<
"WriteOnly"_L1;
2214 modeList <<
"Append"_L1;
2216 modeList <<
"Truncate"_L1;
2218 modeList <<
"Text"_L1;
2220 modeList <<
"Unbuffered"_L1;
2222 std::sort(modeList.begin(), modeList.end());
2223 debug << modeList.join(u
'|');
2231#ifndef QT_NO_QOBJECT
2232#include "moc_qiodevice.cpp"
IOBluetoothL2CAPChannel * channel
IOBluetoothDevice * device
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
void clear()
Clears the contents of the byte array and makes it null.
static QString toNativeSeparators(const QString &pathName)
QString fileName() const override
Returns the name set by setFileName() or to the QFile constructors.
void setCurrentWriteChannel(int channel)
QIODevice::OpenMode openMode
void write(const char *data, qint64 size)
QVarLengthArray< QRingBuffer, 1 > writeBuffers
bool allWriteBuffersEmpty() const
virtual ~QIODevicePrivate()
bool isBufferEmpty() const
bool isSequential() const
void setReadChannelCount(int count)
void seekBuffer(qint64 newPos)
QVarLengthArray< QRingBuffer, 2 > readBuffers
bool baseReadLineDataCalled
qint64 skipByReading(qint64 maxSize)
void setCurrentReadChannel(int channel)
qint64 readLine(char *data, qint64 maxSize)
const QByteArray * currentWriteChunk
qint64 read(char *data, qint64 maxSize, bool peeking=false)
bool isWriteChunkCached(const char *data, qint64 size) const
virtual bool putCharHelper(char c)
virtual qint64 peek(char *data, qint64 maxSize)
void setWriteChannelCount(int count)
\inmodule QtCore \reentrant
virtual bool open(QIODeviceBase::OpenMode mode)
Opens the device and sets its OpenMode to mode.
int writeChannelCount() const
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
virtual qint64 pos() const
For random-access devices, this function returns the position that data is written to or read from.
void setOpenMode(QIODeviceBase::OpenMode openMode)
Sets the OpenMode of the device to openMode.
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
virtual bool waitForBytesWritten(int msecs)
For buffered devices, this function waits until a payload of buffered written data has been written t...
qint64 readLine(char *data, qint64 maxlen)
This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes,...
virtual ~QIODevice()
The destructor is virtual, and QIODevice is an abstract base class.
bool putChar(char c)
Writes the character c to the device.
int currentReadChannel() const
bool isOpen() const
Returns true if the device is open; otherwise returns false.
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
int currentWriteChannel() const
virtual bool waitForReadyRead(int msecs)
Blocks until new data is available for reading and the readyRead() signal has been emitted,...
bool isReadable() const
Returns true if data can be read from the device; otherwise returns false.
void setErrorString(const QString &errorString)
Sets the human readable description of the last device error that occurred to str.
void rollbackTransaction()
QIODevice()
Constructs a QIODevice object.
void setTextModeEnabled(bool enabled)
If enabled is true, this function sets the \l Text flag on the device; otherwise the \l Text flag is ...
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
QIODeviceBase::OpenMode openMode() const
Returns the mode in which the device has been opened; i.e.
qint64 peek(char *data, qint64 maxlen)
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
qint64 skip(qint64 maxSize)
QString errorString() const
Returns a human-readable description of the last device error that occurred.
bool getChar(char *c)
Reads one character from the device and stores it in c.
virtual qint64 skipData(qint64 maxSize)
bool isTransactionStarted() const
bool isWritable() const
Returns true if data can be written to the device; otherwise returns false.
virtual qint64 readLineData(char *data, qint64 maxlen)
Reads up to maxSize characters into data and returns the number of characters read.
virtual qint64 writeData(const char *data, qint64 len)=0
Writes up to maxSize bytes from data to the device.
virtual qint64 bytesToWrite() const
For buffered devices, this function returns the number of bytes waiting to be written.
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
virtual bool seek(qint64 pos)
For random-access devices, this function sets the current position to pos, returning true on success,...
virtual bool reset()
Seeks to the start of input for random-access devices.
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i....
void setCurrentWriteChannel(int channel)
void aboutToClose()
This signal is emitted when the device is about to close.
bool isTextModeEnabled() const
Returns true if the \l Text flag is enabled; otherwise returns false.
void ungetChar(char c)
Puts the character c back into the device, and decrements the current position unless the position is...
int readChannelCount() const
void setCurrentReadChannel(int channel)
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false.
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read.
QObject * parent() const
Returns a pointer to the parent object.
\macro QT_RESTRICTED_CAST_FROM_ASCII
constexpr size_type size() const noexcept
T & emplace_back(Args &&...args)
void resize(qsizetype sz)
void reserve(qsizetype sz)
static bool readFromDevice(QIODevice *device, QJsonArray *allMetaObjects)
Combined button and popup list for selecting options.
constexpr int isAsciiPrintable(char32_t ch) noexcept
constexpr Initialization Uninitialized
QT_BEGIN_NAMESPACE constexpr qsizetype MaxByteArraySize
size_t qstrlen(const char *str)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction function
int qt_subtract_from_timeout(int timeout, int elapsed)
#define CHECK_WRITABLE(function, returnType)
static void checkWarnMessage(const QIODevice *device, const char *function, const char *what)
#define CHECK_MAXLEN(function, returnType)
static void debugBinaryString(const char *input, qint64 maxlen)
QDebug operator<<(QDebug debug, QIODevice::OpenMode modes)
#define CHECK_MAXBYTEARRAYSIZE(function)
#define CHECK_READABLE(function, returnType)
#define CHECK_LINEMAXLEN(function, returnType)
#define QIODEVICE_BUFFERSIZE
constexpr const T & qMin(const T &a, const T &b)
constexpr const T & qMax(const T &a, const T &b)
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLenum GLsizei count
GLbitfield GLuint64 timeout
[4]
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLintptr offset
GLdouble GLdouble GLdouble GLdouble q
GLenum GLenum GLenum input
#define QRINGBUFFER_CHUNKSIZE
#define qPrintable(string)
static double elapsed(qint64 after, qint64 before)
#define QT_TRANSLATE_NOOP(scope, x)
const char className[16]
[1]
obj metaObject() -> className()
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent