📄 qiodevice.cpp
字号:
OpenMode to NotOpen. The error string is also reset. \sa setOpenMode() OpenMode*/void QIODevice::close(){ Q_D(QIODevice); if (d->openMode == NotOpen) return;#ifndef QT_NO_QOBJECT emit aboutToClose();#endif d->openMode = NotOpen;#ifdef QT_NO_QOBJECT d->errorString = QT_TRANSLATE_NOOP(QIODevice, "Unknown error");#else d->errorString = tr("Unknown error");#endif}/*! 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. \sa isSequential(), seek()*/qint64 QIODevice::pos() const{ return qint64(0);}/*! 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.*/qint64 QIODevice::size() const{ if (isSequential()) return bytesAvailable(); return 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 ungetbuffer. The base implementation always returns true. \sa pos()*/bool QIODevice::seek(qint64 pos){ Q_D(QIODevice); qint64 offset = pos - this->pos(); if (offset >= 0) d->ungetBuffer.chop(offset); else d->ungetBuffer.clear(); 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{ return isOpen() && (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). \sa seek()*/bool QIODevice::reset(){ 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 call the base implementation in order to include the size of the unget buffer.*/qint64 QIODevice::bytesAvailable() const{ Q_D(const QIODevice); if (!isSequential()) return qMax(size() - pos(), qint64(0)); return d->ungetBuffer.size();}/*! For buffered devices, this function returns the number of bytes waiting to be written. For devices with no buffer, this function returns 0.*/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)); qint64 readSoFar = qint64(0); if (int ungetSize = d->ungetBuffer.size()) { do { if (readSoFar + 1 > maxSize) { seek(pos() + readSoFar); return readSoFar; } data[readSoFar++] = d->ungetBuffer[ungetSize-- - 1]; } while (ungetSize > 0); seek(pos() + readSoFar); } qint64 ret = readData(data + readSoFar, maxSize - readSoFar); if (ret <= 0) return readSoFar ? readSoFar : ret; if (d->openMode & Text) { forever { char *readPtr = data + readSoFar; char *endPtr = readPtr + ret; // optimization to avoid initial self-assignment while (*readPtr != '\r') { if (++readPtr == endPtr) return readSoFar + ret; } char *writePtr = readPtr; while (readPtr < endPtr) { char ch = *readPtr++; if (ch != '\r') { *writePtr++ = ch; } else { --ret; } } if (readPtr == writePtr) break; qint64 newRet = readData(writePtr, readPtr - writePtr); if (newRet <= 0) break; readSoFar += ret; ret = newRet; } } return readSoFar + ret;}/*! \overload Reads at most \a maxSize bytes from the device, and returns the data read as a QByteArray. 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.*/QByteArray QIODevice::read(qint64 maxSize){ CHECK_MAXLEN(read, QByteArray()); QByteArray tmp; qint64 readSoFar = 0; char buffer[4096]; do { qint64 bytesToRead = qMin(int(maxSize - readSoFar), int(sizeof(buffer))); qint64 readBytes = read(buffer, bytesToRead); if (readBytes <= 0) break; tmp += QByteArray(buffer, (int) readBytes); readSoFar += readBytes; } while (readSoFar < maxSize && bytesAvailable() > 0); return tmp;}/*! \overload Reads all available data from the device, and returns it as a QByteArray. 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.*/QByteArray QIODevice::readAll(){ QByteArray tmp; if (isSequential()) { // Read it in chunks, bytesAvailable() is unreliable for sequential // devices. const int chunkSize = 4096; qint64 totalRead = 0; forever { tmp.resize(tmp.size() + chunkSize); qint64 readBytes = read(tmp.data() + totalRead, chunkSize); tmp.chop(chunkSize - (readBytes < 0 ? 0 : readBytes)); if (readBytes <= 0) return tmp; totalRead += readBytes; } } else { // Read it all in one go. tmp.resize(int(bytesAvailable())); qint64 readBytes = read(tmp.data(), tmp.size()); tmp.resize(readBytes < 0 ? 0 : int(readBytes)); } return tmp;}/*! This function reads a line of ASCII characters from the device, up to a maximum of \a maxSize - 1 bytes, stores the characters in \a data, and returns the number of bytes read. If an error occurred, -1 is returned. A terminating '\0' byte is always appended to \a data, so \a maxSize must be larger than 1. Data is read until either of the following conditions are met: \list \o The first '\n' character is read. \o \a maxSize - 1 bytes are read. \o The end of the device data is detected. \endlist For example, the following code reads a line of characters from a file: \code QFile file("box.txt"); if (file.open(QFile::ReadOnly)) { char buf[1024]; qint64 lineLength = file.readLine(buf, sizeof(buf)); if (lineLength != -1) { // the line is available in buf } } \endcode If the '\n' character is the 1023th character read then it will be inserted into the buffer; if it occurs after the 1023 character then it is not read. This function calls readLineData(), which is implemented using repeated calls to getChar(). You can provide a more efficient implementation by reimplementing readLineData() in your own subclass. \sa getChar(), read(), write()*/qint64 QIODevice::readLine(char *data, qint64 maxSize){ Q_D(QIODevice); if (maxSize < 2) { qWarning("QIODevice::readLine() called with maxSize < 2"); return qint64(-1); } // Leave room for a '\0' --maxSize; qint64 readSoFar = 0; if (int ungetSize = d->ungetBuffer.size()) { do { char c = d->ungetBuffer[ungetSize-- - 1]; data[readSoFar++] = c; if (c == '\n' || readSoFar + 1 > maxSize) { data[readSoFar] = '\0'; seek(pos() + readSoFar); return readSoFar; } } while (ungetSize > 0); seek(pos() + readSoFar); } qint64 readBytes = readLineData(data + readSoFar, maxSize - readSoFar); if (readBytes <= 0) { data[readSoFar] = '\0'; return readSoFar ? readSoFar : -1; } readSoFar += readBytes; data[readSoFar] = '\0'; if (d->openMode & Text) { if (readSoFar > 1 && data[readSoFar - 1] == '\n' && data[readSoFar - 2] == '\r') { data[readSoFar - 2] = '\n'; data[readSoFar - 1] = '\0'; --readSoFar; } } return readSoFar;}/*! \overload Reads a line from the device, but no more than \a maxSize characters, and returns the result as a QByteArray. 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.*/QByteArray QIODevice::readLine(qint64 maxSize){ CHECK_MAXLEN(readLine, QByteArray()); QByteArray tmp; const int BufferGrowth = 4096; qint64 readSoFar = 0; qint64 readBytes = 0; do { if (maxSize != 0) tmp.resize(readSoFar + qMin(int(maxSize), BufferGrowth)); else tmp.resize(readSoFar + BufferGrowth); readBytes = readLine(tmp.data() + readSoFar, tmp.size() - readSoFar); if (readBytes <= 0) break; readSoFar += readBytes; } while (readSoFar < maxSize && readBytes > 0 && readBytes == tmp.size() && tmp.at(readBytes - 1) != '\n'); tmp.resize(readSoFar); return tmp;}/*! Reads up to \a maxSize characters into \a data and returns the number of characters read. This function is called by readLine(), and provides its base implementation, using getChar(). Buffered devices can improve the performance of readLine() by reimplementing this function. readLine() appends a '\0' byte to \a data; readLineData() does not need to do this.*/qint64 QIODevice::readLineData(char *data, qint64 maxSize){ qint64 readSoFar = 0; char c; bool lastGetSucceeded = false; while (readSoFar < maxSize && (lastGetSucceeded = getChar(&c))) { *data++ = c; ++readSoFar; if (c == '\n') break; } if (!lastGetSucceeded && readSoFar == 0) return qint64(-1); return readSoFar;}/*! Returns true if a complete line of data can be read from the device; otherwise returns false. Note that unbuffered devices, which have no way of determining what can be read, always return false. This function is often called in conjunction with the readyRead() signal. \sa readyRead(), readLine()*/bool QIODevice::canReadLine() const{ return false;}/*! \fn bool QIODevice::getChar(char *c) Reads one character from the device and stores it in \a c. If \a c is 0, the character is discarded. Returns true on success; otherwise returns false. \sa read() putChar() ungetChar()*//*! Writes at most \a maxSize bytes of data from \a data to the device. Returns the number of bytes that were actually written, or -1 if an error occurred. \sa read() writeData()*/qint64 QIODevice::write(const char *data, qint64 maxSize){ Q_D(QIODevice); CHECK_OPEN(write, qint64(-1)); CHECK_WRITABLE(write, qint64(-1)); CHECK_MAXLEN(write, qint64(-1));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -