Qx v0.5.8
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::TextStreamReader Class Reference

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>

Public Member Functions

 TextStreamReader ()
 
 TextStreamReader (const QString &filePath)
 
 ~TextStreamReader ()
 
bool atEnd () const
 
bool autoDetectUnicode () const
 
void closeFile ()
 
QStringConverter::Encoding encoding () const
 
const QFilefile () const
 
bool fileIsOpen () const
 
QString filePath () const
 
bool hasError () const
 
int integerBase () const
 
QLocale locale () const
 
IoOpReport openFile ()
 
template<typename T >
requires defines_right_shift_for<QTextStream, T&>
TextStreamReaderoperator>> (T &d)
 
qint64 pos () const
 
QString read (qint64 maxlen)
 
QString readAll ()
 
QString readLine (qint64 maxlen=0)
 
IoOpReport readLineInto (QString *line, qint64 maxlen=0)
 
QTextStream::RealNumberNotation realNumberNotation () const
 
void reset ()
 
void resetStatus ()
 
void setAutoDetectUnicode (bool enabled)
 
void setEncoding (QStringConverter::Encoding encoding)
 
void setFilePath (const QString &filePath)
 
void setIntegerBase (int base)
 
void setLocale (const QLocale &locale)
 
void setRealNumberNotation (QTextStream::RealNumberNotation notation)
 
void skipWhiteSpace ()
 
IoOpReport status () const
 

Detailed Description

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.

See also
TextStreamWriter and FileStreamWriter

Constructor & Destructor Documentation

◆ TextStreamReader() [1/2]

Qx::TextStreamReader::TextStreamReader ( )

Constructs text stream reader with no file set.

See also
setFilePath().

◆ TextStreamReader() [2/2]

Qx::TextStreamReader::TextStreamReader ( const QString & filePath)

Constructs a text stream reader that is linked to the file at filePath.

See also
filePath() and setFilePath().

◆ ~TextStreamReader()

Qx::TextStreamReader::~TextStreamReader ( )

Destroys the text stream reader, along with closing and deleting the underlying file, if present.

Member Function Documentation

◆ atEnd()

bool Qx::TextStreamReader::atEnd ( ) const

Returns true if the reader has reached the end of the file; otherwise returns false

◆ autoDetectUnicode()

bool Qx::TextStreamReader::autoDetectUnicode ( ) const

Returns true if automatic Unicode detection is enabled, otherwise returns false. Automatic Unicode detection is enabled by default.

See also
setAutoDetectUnicode() and setEncoding().

◆ closeFile()

void Qx::TextStreamReader::closeFile ( )

Closes the text file associated with the text stream reader, if present.

◆ encoding()

QStringConverter::Encoding Qx::TextStreamReader::encoding ( ) const

Returns the encoding that is current assigned to the stream.

See also
setEncoding(), setAutoDetectUnicode(), and locale().

◆ file()

const QFile * Qx::TextStreamReader::file ( ) const

Returns an immutable pointer to the file managed by the stream.

See also
filePath().

◆ fileIsOpen()

bool Qx::TextStreamReader::fileIsOpen ( ) const

Returns true if the file managed by the stream is open; otherwise, returns false.

◆ filePath()

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.

See also
setFilePath().

◆ hasError()

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().

◆ integerBase()

int Qx::TextStreamReader::integerBase ( ) const

Returns the current base of integers. 0 means that the base is 10 (decimal) when generating numbers.

See also
setIntegerBase(), and QString::number().

◆ locale()

QLocale Qx::TextStreamReader::locale ( ) const

Returns the locale for this stream. The default locale is C.

See also
setLocale().

◆ openFile()

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.

◆ operator>>()

template<typename T >
requires defines_right_shift_for<QTextStream, T&>
TextStreamReader & Qx::TextStreamReader::operator>> ( T & d)
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.

◆ pos()

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.

◆ read()

QString Qx::TextStreamReader::read ( qint64 maxlen)

Reads at most maxlen characters from the stream, and returns the data read as a QString.

See also
readAll(), readLine().

◆ readAll()

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.

See also
readLine().

◆ readLine()

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().

◆ readLineInto()

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.

See also
readAll().

◆ realNumberNotation()

QTextStream::RealNumberNotation Qx::TextStreamReader::realNumberNotation ( ) const

Returns the current real number notation.

See also
setRealNumberNotation(), and integerBase().

◆ reset()

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.

◆ resetStatus()

void Qx::TextStreamReader::resetStatus ( )

Resets the status of the text stream.

Note
If an error occurs while reading the stream will ignore all further read attempts and hold its current status until this function is called.
See also
status().

◆ setAutoDetectUnicode()

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.

See also
autoDetectUnicode() and setEncoding().

◆ setEncoding()

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.

See also
encoding(), setAutoDetectUnicode(), and setLocale().

◆ setFilePath()

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.

See also
filePath() and openFile().

◆ setIntegerBase()

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.

See also
integerBase(), and QString::number().

◆ setLocale()

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.

See also
locale().

◆ setRealNumberNotation()

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.

See also
realNumberNotation(), and setIntegerBase().

◆ skipWhiteSpace()

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.

See also
operator>>().

◆ status()

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.


The documentation for this class was generated from the following files: