On this page

Utils::TextFileFormat Class

class Utils::TextFileFormat

The TextFileFormat class describes the format of a text file and provides autodetection. More...

Header: #include <textfileformat.h>

Public Functions

bool decode(const QByteArray &data, QString *target) const
void detectFromData(const QByteArray &data)
Utils::TextFileFormat::ReadResult readFile(const Utils::FilePath &filePath, const Utils::TextEncoding &defaultEncoding, const Utils::TextEncoding &retryEncoding = {}, Utils::TextFileFormat::DecodingErrorDetails errorDetails = DecodingErrorDetails::Skip)
Utils::Result<> writeFile(const Utils::FilePath &filePath, QString plainText) const

Static Public Members

Utils::Result<> decodeFileUtf8(QByteArray data, const Utils::TextEncoding &fallbackEncoding, QByteArray *plainText)
QList<qsizetype> decodedPositionsForByteOffsets(const QByteArray &data, const Utils::TextEncoding &encoding, const QList<qsizetype> &byteOffsets, Utils::TextFileFormat::LineTerminationMode lineTerminationMode)
QByteArray decodingErrorSample(const QByteArray &data)

Detailed Description

The format comprises

  • Encoding represented by a the name of a codec
  • Presence of an UTF8 Byte Order Marker (BOM)
  • Line feed storage convention

The class also provides convenience functions to read text files and return them as strings or string lists and to write out files.

Member Function Documentation

bool TextFileFormat::decode(const QByteArray &data, QString *target) const

Returns data decoded to a string, target.

[static] Utils::Result<> TextFileFormat::decodeFileUtf8(QByteArray data, const Utils::TextEncoding &fallbackEncoding, QByteArray *plainText)

Converts data, the raw contents of a text file, to UTF-8 in plainText, as readFileUtf8() does for a file. This is for callers that need the raw contents as well, so that they read the file only once.

[static] QList<qsizetype> TextFileFormat::decodedPositionsForByteOffsets(const QByteArray &data, const Utils::TextEncoding &encoding, const QList<qsizetype> &byteOffsets, Utils::TextFileFormat::LineTerminationMode lineTerminationMode)

Maps ascending byte offsets to UTF-16 positions in the decoded text.

[static] QByteArray TextFileFormat::decodingErrorSample(const QByteArray &data)

Returns a piece of text specified by data suitable as display for an encoding error.

void TextFileFormat::detectFromData(const QByteArray &data)

Detects the format of text data.

Utils::TextFileFormat::ReadResult TextFileFormat::readFile(const Utils::FilePath &filePath, const Utils::TextEncoding &defaultEncoding, const Utils::TextEncoding &retryEncoding = {}, Utils::TextFileFormat::DecodingErrorDetails errorDetails = DecodingErrorDetails::Skip)

Reads a text file from filePath into a string. It detects the codec to use from the BOM in the file contents. If there is no BOM and no encoding is set, it uses defaultEncoding. If that is not set, it uses the text encoding set for the locale.

If decoding with the primary encoding fails, the method retries with retryEncoding unless a BOM is present or retryEncoding equals the primary encoding. On a successful retry, retryEncoding becomes the format's encoding and ReadResult::usedDecodingFallback is set.

With errorDetails set to Collect, retains the failed input and collects the first 1001 error offsets for editor diagnostics. Other callers skip the diagnostic scan.

Returns whether decoding was possible without errors. If an error occurs, it is returned together with a decoding error sample.

Utils::Result<> TextFileFormat::writeFile(const Utils::FilePath &filePath, QString plainText) const

Writes out a text file to filePath into a string, plainText.

Returns whether decoding was possible without errors.

Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.