📄 qtextstream.cpp
字号:
#endif Q_D(QTextStream); d->status = Ok;}/*! Constructs a QTextStream that operates on \a device.*/QTextStream::QTextStream(QIODevice *device) : d_ptr(new QTextStreamPrivate(this)){#if defined (QTEXTSTREAM_DEBUG) qDebug("QTextStream::QTextStream(QIODevice *device == *%p)", device);#endif Q_D(QTextStream); d->device = device;#ifndef QT_NO_QOBJECT d->deviceClosedNotifier.setupDevice(this, d->device);#endif d->status = Ok;}/*! Constructs a QTextStream that operates on \a string, using \a openMode to define the open mode.*/QTextStream::QTextStream(QString *string, QIODevice::OpenMode openMode) : d_ptr(new QTextStreamPrivate(this)){#if defined (QTEXTSTREAM_DEBUG) qDebug("QTextStream::QTextStream(QString *string == *%p, openMode = %d)", string, int(openMode));#endif Q_D(QTextStream); d->string = string; d->stringOpenMode = openMode; d->status = Ok;}/*! Constructs a QTextStream that operates on \a array, using \a openMode to define the open mode. Internally, the array is wrapped by a QBuffer.*/QTextStream::QTextStream(QByteArray *array, QIODevice::OpenMode openMode) : d_ptr(new QTextStreamPrivate(this)){#if defined (QTEXTSTREAM_DEBUG) qDebug("QTextStream::QTextStream(QByteArray *array == *%p, openMode = %d)", array, int(openMode));#endif Q_D(QTextStream); d->device = new QBuffer(array); d->device->open(openMode); d->deleteDevice = true;#ifndef QT_NO_QOBJECT d->deviceClosedNotifier.setupDevice(this, d->device);#endif d->status = Ok;}/*! Constructs a QTextStream that operates on \a array, using \a openMode to define the open mode. The array is accessed as read-only, regardless of the values in \a openMode. This constructor is convenient for working on constant strings. Example: \code int main(int argc, char *argv[]) { // read numeric arguments (123, 0x20, 4.5...) for (int i = 1; i < argc; ++i) { int number; QTextStream in(argv[i]); in >> number; ... } } \endcode*/QTextStream::QTextStream(const QByteArray &array, QIODevice::OpenMode openMode) : d_ptr(new QTextStreamPrivate(this)){#if defined (QTEXTSTREAM_DEBUG) qDebug("QTextStream::QTextStream(const QByteArray &array == *(%p), openMode = %d)", &array, int(openMode));#endif QBuffer *buffer = new QBuffer; buffer->setData(array); buffer->open(openMode); Q_D(QTextStream); d->device = buffer; d->deleteDevice = true;#ifndef QT_NO_QOBJECT d->deviceClosedNotifier.setupDevice(this, d->device);#endif d->status = Ok;}/*! Constructs a QTextStream that operates on \a fileHandle, using \a openMode to define the open mode. Internally, a QFile is created to handle the FILE pointer. This constructor is useful for working directly with the common FILE based input and output streams: stdin, stdout and stderr. Example: \code QString str; QTextStream in(stdin); in >> str; \endcode*/QTextStream::QTextStream(FILE *fileHandle, QIODevice::OpenMode openMode) : d_ptr(new QTextStreamPrivate(this)){#if defined (QTEXTSTREAM_DEBUG) qDebug("QTextStream::QTextStream(FILE *fileHandle = %p, openMode = %d)", fileHandle, int(openMode));#endif QFile *file = new QFile; file->open(fileHandle, openMode); Q_D(QTextStream); d->device = file; d->deleteDevice = true;#ifndef QT_NO_QOBJECT d->deviceClosedNotifier.setupDevice(this, d->device);#endif d->status = Ok;}/*! Destroys the QTextStream. If the stream operates on a device, flush() will be called implicitly. Otherwise, the device is unaffected.*/QTextStream::~QTextStream(){ Q_D(QTextStream);#if defined (QTEXTSTREAM_DEBUG) qDebug("QTextStream::~QTextStream()");#endif if (!d->writeBuffer.isEmpty()) d->flushWriteBuffer(); delete d; d_ptr = 0;}/*! Resets QTextStream's formatting options, bringing it back to its original constructed state. The device, string and any buffered data is left untouched.*/void QTextStream::reset(){ Q_D(QTextStream); d->realNumberPrecision = 6; d->integerBase = 0; d->fieldWidth = 0; d->padChar = QLatin1Char(' '); d->fieldAlignment = QTextStream::AlignRight; d->realNumberNotation = QTextStream::SmartNotation; d->numberFlags = 0;}/*! Flushes any buffered data waiting to be written to the device. If QTextStream operates on a string, this function does nothing.*/void QTextStream::flush(){ Q_D(QTextStream); d->flushWriteBuffer();}/*! Seeks to the position \a pos in the device. Returns true on success; otherwise returns false.*/bool QTextStream::seek(qint64 pos){ Q_D(QTextStream); d->lastTokenSize = 0; if (d->device) { // Empty the write buffer d->flushWriteBuffer(); if (!d->device->seek(pos)) return false; d->readBuffer.clear(); d->readBufferOffset = 0; d->readBufferStartDevicePos = d->device->pos();#ifndef QT_NO_TEXTCODEC // Reset the codec converter states. ::resetCodecConverterState(&d->readConverterState); ::resetCodecConverterState(&d->readBufferStartReadConverterState); ::resetCodecConverterState(&d->writeConverterState);#endif return true; } // string if (d->string && pos <= d->string->size()) { d->stringOffset = int(pos); return true; } return false;}/*! \since 4.2 Returns the device position corresponding to the current position of the stream, or -1 if an error occurs (e.g., if there is no device or string, or if there's a device error). Because QTextStream is buffered, this function may have to seek the device to reconstruct a valid device position. This operation can be expensive, so you may want to avoid calling this function in a tight loop. \sa seek()*/qint64 QTextStream::pos() const{ Q_D(const QTextStream); if (d->device) { // Cutoff if (d->readBuffer.isEmpty()) return d->device->pos(); if (d->device->isSequential()) return 0; // Seek the device if (!d->device->seek(d->readBufferStartDevicePos)) return qint64(-1); // Reset the read buffer QTextStreamPrivate *thatd = const_cast<QTextStreamPrivate *>(d); thatd->readBuffer.clear(); // Restore the codec converter state and end state to the read buffer // start state.#ifndef QT_NO_TEXTCODEC ::copyConverterState(&thatd->readConverterState, &d->readBufferStartReadConverterState);#endif#ifndef QT_NO_TEXTCODEC if (d->readBufferStartDevicePos == 0) thatd->autoDetectUnicode = true;#endif // Rewind the device to get to the current position Ensure that // readBufferOffset is unaffected by fillReadBuffer() int oldReadBufferOffset = d->readBufferOffset; while (d->readBuffer.size() < oldReadBufferOffset) { if (!thatd->fillReadBuffer(1)) return qint64(-1); } thatd->readBufferOffset = oldReadBufferOffset; // Return the device position. return d->device->pos(); } if (d->string) return d->stringOffset; qWarning("QTextStream::pos: no device"); return qint64(-1);}/*! Reads and discards whitespace from the stream until either a non-space character is detected, or until atEnd() returns true. This function is useful when reading a stream character by character. Whitespace characters are all characters for which QChar::isSpace() returns true. \sa operator>>()*/void QTextStream::skipWhiteSpace(){ Q_D(QTextStream); CHECK_VALID_STREAM(Q_VOID); d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); d->consumeLastToken();}/*! Sets the current device to \a device. If a device has already been assigned, QTextStream will call flush() before the old device is replaced. \note This function resets the codec to the default codec, QTextCodec::codecForLocale(). \sa device(), setString()*/void QTextStream::setDevice(QIODevice *device){ Q_D(QTextStream); flush(); if (d->deleteDevice) {#ifndef QT_NO_QOBJECT d->deviceClosedNotifier.disconnect();#endif delete d->device; d->deleteDevice = false; } d->reset(); d->status = Ok; d->device = device;#ifndef QT_NO_QOBJECT d->deviceClosedNotifier.setupDevice(this, d->device);#endif}/*! Returns the current device associated with the QTextStream, or 0 if no device has been assigned. \sa setDevice(), string()*/QIODevice *QTextStream::device() const{ Q_D(const QTextStream); return d->device;}/*! Sets the current string to \a string, using the given \a openMode. If a device has already been assigned, QTextStream will call flush() before replacing it. \sa string(), setDevice()*/void QTextStream::setString(QString *string, QIODevice::OpenMode openMode){ Q_D(QTextStream); flush(); if (d->deleteDevice) {#ifndef QT_NO_QOBJECT d->deviceClosedNotifier.disconnect();#endif delete d->device; d->deleteDevice = false; } d->reset(); d->status = Ok; d->string = string; d->stringOpenMode = openMode;}/*! Returns the current string assigned to the QTextStream, or 0 if no string has been assigned. \sa setString(), device()*/QString *QTextStream::string() const{ Q_D(const QTextStream); return d->string;}/*! Sets the field alignment to \a mode. When used together with setFieldWidth(), this function allows you to generate formatted output with text aligned to the left, to the right or center aligned. \sa fieldAlignment(), setFieldWidth()*/void QTextStream::setFieldAlignment(FieldAlignment mode){ Q_D(QTextStream); d->fieldAlignment = mode;}/*! Returns the current field alignment. \sa setFieldAlignment(), fieldWidth()*/QTextStream::FieldAlignment QTextStream::fieldAlignment() const{ Q_D(const QTextStream); return d->fieldAlignment;}/*! Sets the pad character to \a ch. The default value is the ASCII space character (' '), or QChar(0x20). This character is used to fill in the space in fields when generating text. Example: \code QString s; QTextStream out(&s); out.setFieldWidth(10); out.setPadChar('-'); out << "Qt" << endl << "rocks!" << endl; \endcode Output: \code ----Qt---- --rocks!-- \endcode \sa padChar(), setFieldWidth()*/void QTextStream::setPadChar(QChar ch){ Q_D(QTextStream); d->padChar = ch;}/*! Returns the current pad character. \sa setPadChar(), setFieldWidth()*/QChar QTextStream::padChar() const{ Q_D(const QTextStream); return d->padChar;}/*! Sets the current field width to \a width. If \a width is 0 (the default), the field width is equal to the length of the generated text. \sa fieldWidth(), setPadChar()*/void QTextStream::setFieldWidth(int width){ Q_D(QTextStream); d->fieldWidth = width;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -