?? qiodevice.cpp
字號:
printf("%p QIODevice::setOpenMode(0x%x)\n", this, int(openMode));#endif d_func()->openMode = openMode; d_func()->accessMode = QIODevicePrivate::Unset;}/*! If \a enabled is true, this function sets the \l Text flag on the device; otherwise the \l Text flag is removed. This feature is useful for classes that provide custom end-of-line handling on a QIODevice. \sa open(), setOpenMode() */void QIODevice::setTextModeEnabled(bool enabled){ Q_D(QIODevice); if (enabled) d->openMode |= Text; else d->openMode &= ~Text;}/*! Returns true if the \l Text flag is enabled; otherwise returns false. \sa setTextModeEnabled()*/bool QIODevice::isTextModeEnabled() const{ return d_func()->openMode & Text;}/*! Returns true if the device is open; otherwise returns false. A device is open if it can be read from and/or written to. By default, this function returns false if openMode() returns \c NotOpen. \sa openMode() OpenMode*/bool QIODevice::isOpen() const{ return d_func()->openMode != NotOpen;}/*! Returns true if data can be read from the device; otherwise returns false. Use bytesAvailable() to determine how many bytes can be read. This is a convenience function which checks if the OpenMode of the device contains the ReadOnly flag. \sa openMode() OpenMode*/bool QIODevice::isReadable() const{ return (openMode() & ReadOnly) != 0;}/*! Returns true if data can be written to the device; otherwise returns false. This is a convenience function which checks if the OpenMode of the device contains the WriteOnly flag. \sa openMode() OpenMode*/bool QIODevice::isWritable() const{ return (openMode() & WriteOnly) != 0;}/*! Opens the device and sets its OpenMode to \a mode. Returns true if successful; otherwise returns false. \sa openMode() OpenMode*/bool QIODevice::open(OpenMode mode){ Q_D(QIODevice); d->openMode = mode; d->pos = (mode & Append) ? size() : qint64(0); d_func()->accessMode = QIODevicePrivate::Unset;#if defined QIODEVICE_DEBUG printf("%p QIODevice::open(0x%x)\n", this, quint32(mode));#endif return true;}/*! First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen. The error string is also reset. \sa setOpenMode() OpenMode*/void QIODevice::close(){ Q_D(QIODevice); if (d->openMode == NotOpen) return;#if defined QIODEVICE_DEBUG printf("%p QIODevice::close()\n", this);#endif#ifndef QT_NO_QOBJECT emit aboutToClose();#endif d->openMode = NotOpen; d->errorString.clear(); d->pos = 0; d->buffer.clear();}/*! For random-access devices, this function returns the position that data is written to or read from. For sequential devices or closed devices, where there is no concept of a "current position", 0 is returned. The current read/write position of the device is maintained internally by QIODevice, so reimplementing this function is not necessary. When subclassing QIODevice, use QIODevice::seek() to notify QIODevice about changes in the device position. \sa isSequential(), seek()*/qint64 QIODevice::pos() const{ Q_D(const QIODevice);#if defined QIODEVICE_DEBUG printf("%p QIODevice::pos() == %d\n", this, int(d->pos));#endif return d->pos;}/*! For open random-access devices, this function returns the size of the device. For open sequential devices, bytesAvailable() is returned. If the device is closed, the size returned will not reflect the actual size of the device. \sa isSequential(), pos()*/qint64 QIODevice::size() const{ return d_func()->isSequential() ? bytesAvailable() : qint64(0);}/*! For random-access devices, this function sets the current position to \a pos, returning true on success, or false if an error occurred. For sequential devices, the default behavior is to do nothing and return false. When subclassing QIODevice, you must call QIODevice::seek() at the start of your function to ensure integrity with QIODevice's built-in buffer. The base implementation always returns true. \sa pos(), isSequential()*/bool QIODevice::seek(qint64 pos){ if (d_func()->openMode == NotOpen) { qWarning("QIODevice::seek: The device is not open"); return false; } if (pos < 0) { qWarning("QIODevice::seek: Invalid pos: %d", int(pos)); return false; } Q_D(QIODevice);#if defined QIODEVICE_DEBUG printf("%p QIODevice::seek(%d), before: d->pos = %d, d->buffer.size() = %d\n", this, int(pos), int(d->pos), d->buffer.size());#endif qint64 offset = pos - d->pos; if (!d->isSequential()) { d->pos = pos; d->devicePos = pos; } if (offset > 0 && !d->buffer.isEmpty()) { // When seeking forwards, we need to pop bytes off the front of the // buffer. do { int bytesToSkip = int(qMin<qint64>(offset, INT_MAX)); d->buffer.skip(bytesToSkip); offset -= bytesToSkip; } while (offset > 0); } else if (offset < 0) { // When seeking backwards, an operation that is only allowed for // random-access devices, the buffer is cleared. The next read // operation will then refill the buffer. We can optimize this, if we // find that seeking backwards becomes a significant performance hit. d->buffer.clear(); }#if defined QIODEVICE_DEBUG printf("%p \tafter: d->pos == %d, d->buffer.size() == %d\n", this, int(d->pos), d->buffer.size());#endif return true;}/*! Returns true if the current read and write position is at the end of the device (i.e. there is no more data available for reading on the device); otherwise returns false. For some devices, atEnd() can return true even though there is more data to read. This special case only applies to devices that generate data in direct response to you calling read() (e.g., \c /dev or \c /proc files on Unix and Mac OS X, or console input / \c stdin on all platforms). \sa bytesAvailable(), read(), isSequential()*/bool QIODevice::atEnd() const{ Q_D(const QIODevice);#if defined QIODEVICE_DEBUG printf("%p QIODevice::atEnd() returns %s, d->openMode == %d, d->pos == %d\n", this, (d->openMode == NotOpen || d->pos == size()) ? "true" : "false", int(d->openMode), int(d->pos));#endif return d->openMode == NotOpen || (d->buffer.isEmpty() && bytesAvailable() == 0);}/*! Seeks to the start of input for random-access devices. Returns true on success; otherwise returns false (for example, if the device is not open). Note that when using a QTextStream on a QFile, calling reset() on the QFile will not have the expected result because QTextStream buffers the file. Use the QTextStream::seek() function instead. \sa seek()*/bool QIODevice::reset(){#if defined QIODEVICE_DEBUG printf("%p QIODevice::reset()\n", this);#endif return seek(0);}/*! Returns the number of bytes that are available for reading. This function is commonly used with sequential devices to determine the number of bytes to allocate in a buffer before reading. Subclasses that reimplement this function must call the base implementation in order to include the size of QIODevices' buffer. Example: \code qint64 CustomDevice::bytesAvailable() const { return buffer.size() + QIODevice::bytesAvailable(); } \endcode \sa bytesToWrite(), readyRead(), isSequential()*/qint64 QIODevice::bytesAvailable() const{ Q_D(const QIODevice); if (!d->isSequential()) return qMax(size() - d->pos, qint64(0)); return d->buffer.size();}/*! For buffered devices, this function returns the number of bytes waiting to be written. For devices with no buffer, this function returns 0. \sa bytesAvailable(), bytesWritten(), isSequential()*/qint64 QIODevice::bytesToWrite() const{ return qint64(0);}/*! Reads at most \a maxSize bytes from the device into \a data, and returns the number of bytes read. If an error occurs, such as when attempting to read from a device opened in WriteOnly mode, this function returns -1. 0 is returned when no more data is available for reading. \sa readData() readLine() write()*/qint64 QIODevice::read(char *data, qint64 maxSize){ Q_D(QIODevice); CHECK_OPEN(read, qint64(-1)); CHECK_READABLE(read, qint64(-1)); CHECK_MAXLEN(read, qint64(-1));#if defined QIODEVICE_DEBUG printf("%p QIODevice::read(%p, %d), d->pos = %d, d->buffer.size() = %d\n", this, data, int(maxSize), int(d->pos), int(d->buffer.size()));#endif const bool sequential = d->isSequential(); // Short circuit for getChar() if (maxSize == 1) { int chint = d->buffer.getChar(); if (chint != -1) { char c = char(uchar(chint)); if (c == '\r' && (d->openMode & Text)) { d->buffer.ungetChar(c); } else { if (data) *data = c; if (!sequential) ++d->pos;#if defined QIODEVICE_DEBUG printf("%p \tread 0x%hhx (%c) returning 1 (shortcut)\n", this, int(c), isprint(c) ? c : '?');#endif return qint64(1); } } } qint64 readSoFar = 0; bool moreToRead = true; do { int lastReadChunkSize = 0; // Try reading from the buffer. if (!d->buffer.isEmpty()) { lastReadChunkSize = d->buffer.read(data + readSoFar, maxSize - readSoFar); readSoFar += lastReadChunkSize; if (!sequential) d->pos += lastReadChunkSize;#if defined QIODEVICE_DEBUG printf("%p \treading %d bytes from buffer into position %d\n", this, lastReadChunkSize, int(readSoFar) - lastReadChunkSize);#endif } else if ((d->openMode & Unbuffered) == 0 && maxSize < QIODEVICE_BUFFERSIZE) { // In buffered mode, we try to fill up the QIODevice buffer before // we do anything else. int bytesToBuffer = qMax(maxSize - readSoFar, QIODEVICE_BUFFERSIZE); char *writePointer = d->buffer.reserve(bytesToBuffer); // Make sure the device is positioned correctly. if (d->pos != d->devicePos && !sequential && !seek(d->pos)) return qint64(-1); qint64 readFromDevice = readData(writePointer, bytesToBuffer); d->buffer.chop(bytesToBuffer - (readFromDevice < 0 ? 0 : int(readFromDevice))); if (readFromDevice > 0) { if (!sequential) d->devicePos += readFromDevice;#if defined QIODEVICE_DEBUG printf("%p \treading %d from device into buffer\n", this, int(readFromDevice));#endif if (readFromDevice < bytesToBuffer) d->buffer.truncate(readFromDevice < 0 ? 0 : int(readFromDevice)); if (!d->buffer.isEmpty()) { lastReadChunkSize = d->buffer.read(data + readSoFar, maxSize - readSoFar); readSoFar += lastReadChunkSize; if (!sequential) d->pos += lastReadChunkSize;#if defined QIODEVICE_DEBUG printf("%p \treading %d bytes from buffer at position %d\n", this, lastReadChunkSize, int(readSoFar));#endif } } } // If we need more, try reading from the device. if (readSoFar < maxSize) { // Make sure the device is positioned correctly. if (d->pos != d->devicePos && !sequential && !seek(d->pos)) return qint64(-1); qint64 readFromDevice = readData(data + readSoFar, maxSize - readSoFar);#if defined QIODEVICE_DEBUG printf("%p \treading %d bytes from device\n", this, int(readFromDevice));#endif if (readFromDevice <= 0) { moreToRead = false; } else { // see if we read as much data as we asked for if (readFromDevice < maxSize - readSoFar) moreToRead = false; lastReadChunkSize += int(readFromDevice); readSoFar += readFromDevice; if (!sequential) { d->pos += readFromDevice; d->devicePos += readFromDevice; } } } else { moreToRead = false; } if (readSoFar && d->openMode & Text) { char *readPtr = data + readSoFar - lastReadChunkSize; const char *endPtr = data + readSoFar; if (readPtr < endPtr) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -