Qx v0.5.8
Qt Extensions Library
|
The TextStreamReader class is a specialized wrapper for QTextStream that narrows and simplifies its usage for reading text files. More...
#include <qx/io/qx-textstreamreader.h>
Most member functions are the same or slightly modified versions of those from QDataStream.
The file on which to operate is specified as a path and the underlying handle is managed by the stream.
Qx::TextStreamReader::TextStreamReader | ( | ) |
Constructs text stream reader with no file set.
Qx::TextStreamReader::TextStreamReader | ( | const QString & | filePath | ) |
Constructs a text stream reader that is linked to the file at filePath.
Qx::TextStreamReader::~TextStreamReader | ( | ) |
Destroys the text stream reader, along with closing and deleting the underlying file, if present.
bool Qx::TextStreamReader::atEnd | ( | ) | const |
Returns true
if the reader has reached the end of the file; otherwise returns false
bool Qx::TextStreamReader::autoDetectUnicode | ( | ) | const |
Returns true
if automatic Unicode detection is enabled, otherwise returns false
. Automatic Unicode detection is enabled by default.
void Qx::TextStreamReader::closeFile | ( | ) |
Closes the text file associated with the text stream reader, if present.
QStringConverter::Encoding Qx::TextStreamReader::encoding | ( | ) | const |
Returns the encoding that is current assigned to the stream.
const QFile * Qx::TextStreamReader::file | ( | ) | const |
Returns an immutable pointer to the file managed by the stream.
bool Qx::TextStreamReader::fileIsOpen | ( | ) | const |
Returns true
if the file managed by the stream is open; otherwise, returns false
.
QString Qx::TextStreamReader::filePath | ( | ) | const |
Returns the path of the file associated with the stream, if present.
If no file is assigned the path will be null.
bool Qx::TextStreamReader::hasError | ( | ) | const |
Returns true
if the stream's current status indicates that an error has occurred; otherwise, returns false
.
Equivalent to status().isFailure()
.
int Qx::TextStreamReader::integerBase | ( | ) | const |
Returns the current base of integers. 0
means that the base is 10
(decimal) when generating numbers.
QLocale Qx::TextStreamReader::locale | ( | ) | const |
Returns the locale for this stream. The default locale is C
.
IoOpReport Qx::TextStreamReader::openFile | ( | ) |
Opens the file associated with the text stream reader and returns an operation report.
This function must be called before any data is read after a file is assigned to the stream.
|
inline |
Reads the data type T
into d, and returns a reference to the stream.
This template is constrained such that effectively, the extraction operator for this class is available for all data types that QTextStream defines an extraction operator for.
qint64 Qx::TextStreamReader::pos | ( | ) | const |
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.
QString Qx::TextStreamReader::read | ( | qint64 | maxlen | ) |
Reads at most maxlen characters from the stream, and returns the data read as a QString.
QString Qx::TextStreamReader::readAll | ( | ) |
Reads the entire content of the stream, and returns it as a QString. Avoid this function when working on large files, as it will consume a significant amount of memory.
Calling readLine() is better if you do not know how much data is available.
QString Qx::TextStreamReader::readLine | ( | qint64 | maxlen = 0 | ) |
Reads one line of text from the stream, and returns it as a QString. The maximum allowed line length is set to maxlen. If the stream contains lines longer than this, then the lines will be split after maxlen characters and returned in parts.
If maxlen is 0, the lines can be of any length.
The returned line has no trailing end-of-line characters ('
' or "\r\n"), so calling QString::trimmed() can be unnecessary.
If the stream has read to the end of the file, readLine() will return a null QString. For strings, or for devices that support it, you can explicitly test for the end of the stream using atEnd().
See also readAll() and QIODevice::readLine().
IoOpReport Qx::TextStreamReader::readLineInto | ( | QString * | line, |
qint64 | maxlen = 0 ) |
Reads one line of text from the stream into line and returns an operation report. If line is nullptr, the read line is not stored.
The maximum allowed line length is set to maxlen. If the stream contains lines longer than this, then the lines will be split after maxlen characters and returned in parts.
If maxlen is 0, the lines can be of any length.
The resulting line has no trailing end-of-line characters ('
' or "\r\n"), so calling QString::trimmed() can be unnecessary.
If line has sufficient capacity for the data that is about to be read, this function may not need to allocate new memory. Because of this, it can be faster than readLine().
Returns false
if the stream has read to the end of the file or an error has occurred; otherwise returns true
. The contents in line before the call are discarded in any case.
QTextStream::RealNumberNotation Qx::TextStreamReader::realNumberNotation | ( | ) | const |
Returns the current real number notation.
void Qx::TextStreamReader::reset | ( | ) |
Resets TextStreamReader's formatting options, bringing it back to its original constructed state. The device, string and any buffered data is left untouched.
void Qx::TextStreamReader::resetStatus | ( | ) |
Resets the status of the text stream.
void Qx::TextStreamReader::setAutoDetectUnicode | ( | bool | enabled | ) |
If enabled is true
, QTextStream will attempt to detect Unicode encoding by peeking into the stream data to see if it can find the UTF-8
, UTF-16
, or UTF-32
1Byte Order Mark (BOM)1. If this mark is found, QTextStream will replace the current encoding with the UTF
encoding.
This function can be used together with setEncoding(). It is common to set the encoding to UTF-8
, and then enable UTF-16
detection.
void Qx::TextStreamReader::setEncoding | ( | QStringConverter::Encoding | encoding | ) |
Sets the encoding for this stream to encoding. The encoding is used for any data that is read from the assigned device. By default, QStringConverter::Utf8 is used.
void Qx::TextStreamReader::setFilePath | ( | const QString & | filePath | ) |
Links the stream to the file at filePath, which can be a null QString to unset the current file. If a file was already set to the stream, it will be closed as it is replaced.
The file must be opened through the stream before it can be used.
void Qx::TextStreamReader::setIntegerBase | ( | int | base | ) |
Sets the base of integers to base. base can be either 2
(binary), 8
(octal), 10
(decimal) or 16
(hexadecimal). QTextStream assumes base is 10
unless the base has been set explicitly.
void Qx::TextStreamReader::setLocale | ( | const QLocale & | locale | ) |
Sets the locale for this stream to locale. The specified locale is used for conversions between numbers and their string representations.
The default locale is C
and it is a special case - the thousands group separator is not used for backward compatibility reasons.
void Qx::TextStreamReader::setRealNumberNotation | ( | QTextStream::RealNumberNotation | notation | ) |
Sets the real number notation to notation. When reading numbers, TextStreamReader uses this value to detect the formatting of real numbers.
void Qx::TextStreamReader::skipWhiteSpace | ( | ) |
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
.
IoOpReport Qx::TextStreamReader::status | ( | ) | const |
Returns the status of the text stream reader.
The status is a report of the last read operation performed by TextStreamReader. If no read operation has been performed since the stream was constructed or resetStatus() was last called the report will be null.