51 #ifdef QIODEVICE_DEBUG 57 #ifdef QIODEVICE_DEBUG 58 void debugBinaryString(
const QByteArray &input)
62 for (
int i = 0; i < input.
size(); ++i) {
65 if ((i % 16) == 15 || i == (input.
size() - 1)) {
66 printf(
"\n%15d:", startOffset);
67 startOffset += tmp.
size();
69 for (
int j = 0; j < tmp.
size(); ++j)
70 printf(
" %02x",
int(
uchar(tmp[j])));
71 for (
int j = tmp.
size(); j < 16 + 1; ++j)
73 for (
int j = 0; j < tmp.
size(); ++j)
74 printf(
"%c", isprint(
int(
uchar(tmp[j]))) ? tmp[j] :
'.');
81 void debugBinaryString(
const char *
data,
qint64 maxlen)
89 #define CHECK_MAXLEN(function, returnType) \ 92 qWarning("QIODevice::"#function": Called with maxSize < 0"); \ 97 #define CHECK_WRITABLE(function, returnType) \ 99 if ((d->openMode & WriteOnly) == 0) { \ 100 if (d->openMode == NotOpen) \ 102 qWarning("QIODevice::"#function": ReadOnly device"); \ 107 #define CHECK_READABLE(function, returnType) \ 109 if ((d->openMode & ReadOnly) == 0) { \ 110 if (d->openMode == NotOpen) \ 112 qWarning("QIODevice::"#function": WriteOnly device"); \ 121 pos(0), devicePos(0), seqDumpPos(0)
122 , pPos(&pos), pDevicePos(&devicePos)
123 , baseReadLineDataCalled(false)
393 #if defined QIODEVICE_DEBUG 407 #if defined QIODEVICE_DEBUG 429 #if defined QIODEVICE_DEBUG 430 printf(
"%p QIODevice::~QIODevice()\n",
this);
467 return d_func()->openMode;
480 #if defined QIODEVICE_DEBUG 481 printf(
"%p QIODevice::setOpenMode(0x%x)\n",
this,
int(openMode));
503 qWarning(
"QIODevice::setTextModeEnabled: The device is not open");
509 d->openMode &= ~
Text;
519 return d_func()->openMode &
Text;
532 return d_func()->openMode !=
NotOpen;
578 #if defined QIODEVICE_DEBUG 579 printf(
"%p QIODevice::open(0x%x)\n",
this,
quint32(mode));
596 #if defined QIODEVICE_DEBUG 597 printf(
"%p QIODevice::close()\n",
this);
600 #ifndef QT_NO_QOBJECT 604 d->errorString.clear();
627 #if defined QIODEVICE_DEBUG 628 printf(
"%p QIODevice::pos() == %d\n",
this,
int(
d->pos));
663 qWarning(
"QIODevice::seek: The device is not open");
667 qWarning(
"QIODevice::seek: Invalid pos: %d",
int(pos));
671 #if defined QIODEVICE_DEBUG 672 printf(
"%p QIODevice::seek(%d), before: d->pos = %d, d->buffer.size() = %d\n",
673 this,
int(pos),
int(
d->pos),
d->buffer.size());
677 if (!
d->isSequential()) {
683 || offset >=
qint64(
d->buffer.size()))
689 else if (!
d->buffer.isEmpty())
690 d->buffer.skip(
int(offset));
692 #if defined QIODEVICE_DEBUG 693 printf(
"%p \tafter: d->pos == %d, d->buffer.size() == %d\n",
this,
int(
d->pos),
714 #if defined QIODEVICE_DEBUG 715 printf(
"%p QIODevice::atEnd() returns %s, d->openMode == %d, d->pos == %d\n",
this, (
d->openMode ==
NotOpen ||
d->pos ==
size()) ?
"true" :
"false",
716 int(
d->openMode),
int(
d->pos));
734 #if defined QIODEVICE_DEBUG 735 printf(
"%p QIODevice::reset()\n",
this);
755 if (!
d->isSequential())
757 return d->buffer.size();
795 #if defined QIODEVICE_DEBUG 796 printf(
"%p QIODevice::read(%p, %d), d->pos = %d, d->buffer.size() = %d\n",
797 this, data,
int(maxSize),
int(
d->pos),
int(
d->buffer.size()));
803 while ((chint =
d->buffer.getChar()) != -1) {
806 char c = char(
uchar(chint));
807 if (c ==
'\r' && (
d->openMode &
Text))
810 #if defined QIODEVICE_DEBUG 811 printf(
"%p \tread 0x%hhx (%c) returning 1 (shortcut)\n",
this,
812 int(c), isprint(c) ? c :
'?');
820 bool moreToRead =
true;
823 int lastReadChunkSize =
d->buffer.read(data, maxSize);
824 if (lastReadChunkSize > 0) {
825 *
d->pPos += lastReadChunkSize;
826 readSoFar += lastReadChunkSize;
828 if (lastReadChunkSize == maxSize && !(
d->openMode &
Text))
831 data += lastReadChunkSize;
832 maxSize -= lastReadChunkSize;
833 #if defined QIODEVICE_DEBUG 834 printf(
"%p \treading %d bytes from buffer into position %d\n",
this, lastReadChunkSize,
835 int(readSoFar) - lastReadChunkSize);
842 d->firstRead =
false;
843 if (
d->isSequential()) {
844 d->pPos = &
d->seqDumpPos;
845 d->pDevicePos = &
d->seqDumpPos;
857 char *writePointer =
d->buffer.reserve(bytesToBuffer);
860 if (
d->pos !=
d->devicePos && !
d->isSequential() && !
seek(
d->pos))
861 return readSoFar ? readSoFar :
qint64(-1);
863 d->buffer.chop(bytesToBuffer - (readFromDevice < 0 ? 0 :
int(readFromDevice)));
865 if (readFromDevice > 0) {
866 *
d->pDevicePos += readFromDevice;
867 #if defined QIODEVICE_DEBUG 868 printf(
"%p \treading %d from device into buffer\n",
this,
int(readFromDevice));
871 if (!
d->buffer.isEmpty()) {
872 lastReadChunkSize =
d->buffer.read(data, maxSize);
873 readSoFar += lastReadChunkSize;
874 data += lastReadChunkSize;
875 maxSize -= lastReadChunkSize;
876 *
d->pPos += lastReadChunkSize;
877 #if defined QIODEVICE_DEBUG 878 printf(
"%p \treading %d bytes from buffer at position %d\n",
this,
879 lastReadChunkSize,
int(readSoFar));
889 if (
d->pos !=
d->devicePos && !
d->isSequential() && !
seek(
d->pos))
890 return readSoFar ? readSoFar :
qint64(-1);
892 #if defined QIODEVICE_DEBUG 893 printf(
"%p \treading %d bytes from device (total %d)\n",
this,
int(readFromDevice),
int(readSoFar));
895 if (readFromDevice == -1 && readSoFar == 0) {
899 if (readFromDevice > 0) {
900 lastReadChunkSize += int(readFromDevice);
901 readSoFar += readFromDevice;
902 data += readFromDevice;
903 maxSize -= readFromDevice;
904 *
d->pPos += readFromDevice;
905 *
d->pDevicePos += readFromDevice;
911 if (readSoFar &&
d->openMode &
Text) {
912 char *readPtr = data - lastReadChunkSize;
913 const char *endPtr =
data;
915 if (readPtr < endPtr) {
917 while (*readPtr !=
'\r') {
918 if (++readPtr == endPtr)
922 char *writePtr = readPtr;
924 while (readPtr < endPtr) {
925 char ch = *readPtr++;
938 moreToRead = (readPtr != writePtr);
941 }
while (moreToRead);
943 #if defined QIODEVICE_DEBUG 944 printf(
"%p \treturning %d, d->pos == %d, d->buffer.size() == %d\n",
this,
945 int(readSoFar),
int(
d->pos),
d->buffer.size());
946 debugBinaryString(data - readSoFar, readSoFar);
975 #if defined QIODEVICE_DEBUG 976 printf(
"%p QIODevice::read(%d), d->pos = %d, d->buffer.size() = %d\n",
977 this,
int(maxSize),
int(
d->pos),
int(
d->buffer.size()));
982 if (maxSize !=
qint64(
int(maxSize))) {
983 qWarning(
"QIODevice::read: maxSize argument exceeds QByteArray size limit");
989 result.
resize(
int(maxSize));
990 if (!result.
size()) {
995 readResult =
read(result.
data() + readBytes, result.
size() - readBytes);
996 if (readResult > 0 || readBytes == 0)
997 readBytes += readResult;
1007 result.
resize(
int(readBytes));
1028 #if defined QIODEVICE_DEBUG 1029 printf(
"%p QIODevice::readAll(), d->pos = %d, d->buffer.size() = %d\n",
1030 this,
int(
d->pos),
int(
d->buffer.size()));
1037 if (!(
d->openMode &
Text) && !
d->buffer.isEmpty()) {
1038 result =
d->buffer.readAll();
1039 readBytes = result.
size();
1040 d->pos += readBytes;
1044 if (
d->isSequential() || (theSize =
size()) == 0) {
1049 readResult =
read(result.
data() + readBytes, result.
size() - readBytes);
1050 if (readResult > 0 || readBytes == 0)
1051 readBytes += readResult;
1052 }
while (readResult > 0);
1056 result.
resize(
int(readBytes + theSize -
d->pos));
1057 readBytes +=
read(result.
data() + readBytes, result.
size() - readBytes);
1063 result.
resize(
int(readBytes));
1114 qWarning(
"QIODevice::readLine: Called with maxSize < 2");
1118 #if defined QIODEVICE_DEBUG 1119 printf(
"%p QIODevice::readLine(%p, %d), d->pos = %d, d->buffer.size() = %d\n",
1120 this, data,
int(maxSize),
int(
d->pos),
int(
d->buffer.size()));
1126 const bool sequential =
d->isSequential();
1129 if (!
d->buffer.isEmpty()) {
1130 readSoFar =
d->buffer.readLine(data, maxSize);
1132 d->pos += readSoFar;
1133 #if defined QIODEVICE_DEBUG 1134 printf(
"%p \tread from buffer: %d bytes, last character read: %hhx\n",
this,
1135 int(readSoFar), data[
int(readSoFar) - 1]);
1137 debugBinaryString(data,
int(readSoFar));
1139 #if defined(Q_OS_SYMBIAN) 1141 if ((
d->openMode &
Text) &&
1143 data[readSoFar - 1] ==
'\0' &&
1144 data[readSoFar - 2] ==
'\n') {
1148 if (readSoFar && data[readSoFar - 1] ==
'\n') {
1149 if (
d->openMode &
Text) {
1151 if (readSoFar > 1 && data[readSoFar - 2] ==
'\r') {
1153 data[readSoFar - 1] =
'\n';
1156 data[readSoFar] =
'\0';
1161 if (
d->pos !=
d->devicePos && !sequential && !
seek(
d->pos))
1163 d->baseReadLineDataCalled =
false;
1165 #if defined QIODEVICE_DEBUG 1166 printf(
"%p \tread from readLineData: %d bytes, readSoFar = %d bytes\n",
this,
1167 int(readBytes),
int(readSoFar));
1168 if (readBytes > 0) {
1169 debugBinaryString(data,
int(readSoFar + readBytes));
1172 if (readBytes < 0) {
1173 data[readSoFar] =
'\0';
1174 return readSoFar ? readSoFar : -1;
1176 readSoFar += readBytes;
1177 if (!
d->baseReadLineDataCalled && !sequential) {
1178 d->pos += readBytes;
1183 data[readSoFar] =
'\0';
1185 if (
d->openMode &
Text) {
1186 #if defined(Q_OS_SYMBIAN) 1188 if (readSoFar > 1 && data[readSoFar - 1] ==
'\0' && data[readSoFar - 2] ==
'\n') {
1192 if (readSoFar > 1 && data[readSoFar - 1] ==
'\n' && data[readSoFar - 2] ==
'\r') {
1193 data[readSoFar - 2] =
'\n';
1194 data[readSoFar - 1] =
'\0';
1199 #if defined QIODEVICE_DEBUG 1200 printf(
"%p \treturning %d, d->pos = %d, d->buffer.size() = %d, size() = %d\n",
1201 this,
int(readSoFar),
int(
d->pos),
d->buffer.size(), int(
size()));
1202 debugBinaryString(data,
int(readSoFar));
1227 #if defined QIODEVICE_DEBUG 1228 printf(
"%p QIODevice::readLine(%d), d->pos = %d, d->buffer.size() = %d\n",
1229 this,
int(maxSize),
int(
d->pos),
int(
d->buffer.size()));
1235 qWarning(
"QIODevice::read: maxSize argument exceeds QByteArray size limit");
1239 result.
resize(
int(maxSize));
1241 if (!result.
size()) {
1252 readResult =
readLine(result.
data() + readBytes, result.
size() - readBytes);
1253 if (readResult > 0 || readBytes == 0)
1254 readBytes += readResult;
1256 && result[
int(readBytes - 1)] !=
'\n');
1263 result.
resize(readBytes);
1290 int lastReadReturn = 0;
1291 d->baseReadLineDataCalled =
true;
1293 while (readSoFar < maxSize && (lastReadReturn =
read(&c, 1)) == 1) {
1300 #if defined QIODEVICE_DEBUG 1301 printf(
"%p QIODevice::readLineData(%p, %d), d->pos = %d, d->buffer.size() = %d, returns %d\n",
1302 this, data,
int(maxSize),
int(
d->pos),
int(
d->buffer.size()),
int(readSoFar));
1304 if (lastReadReturn != 1 && readSoFar == 0)
1332 return d_func()->buffer.canReadLine();
1348 const bool sequential =
d->isSequential();
1350 if (
d->pos !=
d->devicePos && !sequential && !
seek(
d->pos))
1354 if (
d->openMode &
Text) {
1355 const char *endOfData = data + maxSize;
1356 const char *startOfBlock =
data;
1361 const char *endOfBlock = startOfBlock;
1362 while (endOfBlock < endOfData && *endOfBlock !=
'\n')
1366 if (blockSize > 0) {
1369 if (writtenSoFar && !sequential)
1370 d->buffer.skip(writtenSoFar);
1371 return writtenSoFar ? writtenSoFar : ret;
1375 d->devicePos += ret;
1377 writtenSoFar += ret;
1380 if (endOfBlock == endOfData)
1385 if (writtenSoFar && !sequential)
1386 d->buffer.skip(writtenSoFar);
1387 return writtenSoFar ? writtenSoFar : ret;
1391 d->devicePos += ret;
1395 startOfBlock = endOfBlock + 1;
1398 if (writtenSoFar && !sequential)
1399 d->buffer.skip(writtenSoFar);
1400 return writtenSoFar;
1408 d->devicePos += written;
1410 if (!
d->buffer.isEmpty() && !sequential)
1411 d->buffer.skip(written);
1467 #if defined QIODEVICE_DEBUG 1468 printf(
"%p QIODevice::ungetChar(0x%hhx '%c')\n",
this, c, isprint(c) ? c :
'?');
1471 d->buffer.ungetChar(c);
1472 if (!
d->isSequential())
1488 return d_func()->putCharHelper(c);
1496 return q_func()->write(&c, 1) == 1;
1504 qint64 readBytes = q_func()->read(data, maxSize);
1508 buffer.ungetBlock(data, readBytes);
1524 *pPos -= result.
size();
1540 return (1 ==
read(c ? c : &ch, 1));
1565 return d_func()->peek(data, maxSize);
1590 return d_func()->peek(maxSize);
1662 d_func()->errorString = str;
1674 if (
d->errorString.isEmpty()) {
1675 #ifdef QT_NO_QOBJECT 1678 return tr(
"Unknown error");
1681 return d->errorString;
1907 #if defined QT3_SUPPORT 1910 #if !defined(QT_NO_QOBJECT) 1912 if (f)
return (
int) f->
error();
1914 return isOpen() ? 0 : 8 ;
1923 void QIODevice::resetStatus()
1925 #if !defined(QT_NO_QOBJECT) 1932 #if !defined(QT_NO_DEBUG_STREAM) 1935 debug <<
"OpenMode(";
The QDebug class provides an output stream for debugging information.
QString fileName() const
Returns the name set by setFileName() or to the QFile constructors.
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
#define QT_END_NAMESPACE
This macro expands to.
void setOpenMode(OpenMode openMode)
Sets the OpenMode of the device to openMode.
#define CHECK_WRITABLE(function, returnType)
virtual bool waitForReadyRead(int msecs)
Blocks until new data is available for reading and the readyRead() signal has been emitted...
char * data()
Returns a pointer to the data stored in the byte array.
bool isReadable() const
Returns true if data can be read from the device; otherwise returns false.
bool isWritable() const
Returns true if data can be written to the device; otherwise returns false.
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
#define CHECK_MAXLEN(function, returnType)
virtual qint64 peek(char *data, qint64 maxSize)
void aboutToClose()
This signal is emitted when the device is about to close.
virtual qint64 writeData(const char *data, qint64 len)=0
Writes up to maxSize bytes from data to the device.
The QByteArray class provides an array of bytes.
QString errorString() const
Returns a human-readable description of the last device error that occurred.
virtual qint64 pos() const
For random-access devices, this function returns the position that data is written to or read from...
bool getChar(char *c)
Reads one character from the device and stores it in c.
static LibLoadStatus status
#define QT_TRANSLATE_NOOP(scope, x)
Marks the string literal sourceText for dynamic translation in the given context; i...
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool putChar(char c)
Writes the character c to the device.
#define QIODEVICE_BUFFERSIZE
void ungetChar(char c)
Puts the character c back into the device, and decrements the current position unless the position is...
The QString class provides a Unicode character string.
T * qobject_cast(QObject *object)
The QObject class is the base class of all Qt objects.
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i.e.
void setTextModeEnabled(bool enabled)
If enabled is true, this function sets the Text flag on the device; otherwise the Text flag is remove...
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
virtual qint64 readLineData(char *data, qint64 maxlen)
Reads up to maxSize characters into data and returns the number of characters read.
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
virtual qint64 bytesToWrite() const
For buffered devices, this function returns the number of bytes waiting to be written.
#define QT_BEGIN_NAMESPACE
This macro expands to.
virtual ~QIODevice()
The destructor is virtual, and QIODevice is an abstract base class.
bool isOpen() const
Returns true if the device is open; otherwise returns false.
virtual bool putCharHelper(char c)
The QStringList class provides a list of strings.
qint64 peek(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, without side effects (i.
Q_CORE_EXPORT void qWarning(const char *,...)
QDebug operator<<(QDebug debug, QIODevice::OpenMode modes)
static const char * data(const QByteArray &arr)
void qSort(RandomAccessIterator start, RandomAccessIterator end)
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
virtual qint64 readData(char *data, qint64 maxlen)=0
Reads up to maxSize bytes from the device into data, and returns the number of bytes read or -1 if an...
void unsetError()
Sets the file's error to QFile::NoError.
const char * constData() const
Returns a pointer to the data stored in the byte array.
QString join(const QString &sep) const
Joins all the string list's strings into a single string with each element separated by the given sep...
uint qstrlen(const char *str)
virtual ~QIODevicePrivate()
The QFile class provides an interface for reading from and writing to files.
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...
OpenMode openMode() const
Returns the mode in which the device has been opened; i.e.
QObject * parent() const
Returns a pointer to the parent object.
void setErrorString(const QString &errorString)
Sets the human readable description of the last device error that occurred to str.
void resize(int size)
Sets the size of the byte array to size bytes.
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
QIODevice()
Constructs a QIODevice object.
virtual bool reset()
Seeks to the start of input for random-access devices.
virtual bool waitForBytesWritten(int msecs)
For buffered devices, this function waits until a payload of buffered written data has been written t...
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
#define CHECK_READABLE(function, returnType)
bool isTextModeEnabled() const
Returns true if the Text flag is enabled; otherwise returns false.
int size() const
Returns the number of bytes in this byte array.
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false...
FileError error() const
Returns the file error status.
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
The QIODevice class is the base interface class of all I/O devices in Qt.
#define qPrintable(string)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
virtual bool seek(qint64 pos)
For random-access devices, this function sets the current position to pos, returning true on success...
void clear()
Clears the contents of the byte array and makes it empty.
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
The Text item allows you to add formatted text to a scene.
#define forever
This macro is provided for convenience for writing infinite loops.