Skip to content

Commit

Permalink
Add raw data text parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Silarn committed Jul 11, 2024
1 parent d60eb11 commit b5dd565
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,30 +849,35 @@ bool shellDeleteQuiet(const QString& fileName, QWidget* dialog)

QString readFileText(const QString& fileName, QString* encoding)
{
QStringConverter::Encoding codec = QStringConverter::Encoding::Utf8;
QStringEncoder encoder(codec);
QStringDecoder decoder(codec);

QFile textFile(fileName);
if (!textFile.open(QIODevice::ReadOnly)) {
return QString();
}

QByteArray buffer = textFile.readAll();
QString text = decoder.decode(buffer);
return decodeTextData(buffer);
}

QString decodeTextData(const QByteArray& fileData, QString* encoding)
{
QStringConverter::Encoding codec = QStringConverter::Encoding::Utf8;
QStringEncoder encoder(codec);
QStringDecoder decoder(codec);
QString text = decoder.decode(fileData);

// check reverse conversion. If this was unicode text there can't be data loss
// this assumes QString doesn't normalize the data in any way so this is a bit unsafe
if (encoder.encode(text) != buffer) {
if (encoder.encode(text) != fileData) {
log::debug("conversion failed assuming local encoding");
auto codecSearch = QStringConverter::encodingForData(buffer);
auto codecSearch = QStringConverter::encodingForData(fileData);
if (codecSearch.has_value()) {
codec = codecSearch.value();
decoder = QStringDecoder(codec);
} else {
decoder = QStringDecoder(QStringConverter::Encoding::System);
}
text = decoder.decode(buffer);
text = decoder.decode(fileData);
}

if (encoding != nullptr) {
Expand Down
11 changes: 11 additions & 0 deletions src/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,17 @@ QDLLEXPORT QString getStartMenuDirectory();
**/
QDLLEXPORT QString readFileText(const QString& fileName, QString* encoding = nullptr);


/**
* @brief decode raw text data. This tries to guess the encoding used in the file
* @param fileData raw data to decode
* @param encoding (optional) if this is set, the target variable received the name of
*the encoding used
* @return the textual content of the file or an empty string if the file doesn't exist
**/
QDLLEXPORT QString decodeTextData(const QByteArray& fileData,
QString* encoding = nullptr);

/**
* @brief delete files matching a pattern
* @param directory in which to delete files
Expand Down

0 comments on commit b5dd565

Please sign in to comment.