forked from qt-creator/qt-creator
Utils: Add a convenience function to TextFileFormat
All these output parameters make for awful code on the calling site. Change-Id: Ie13294959cc011bc8220bab084e1685f85d8e06b Reviewed-by: David Schulz <david.schulz@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -288,6 +288,19 @@ TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const FilePath &filePath
|
|||||||
return TextFileFormat::ReadSuccess;
|
return TextFileFormat::ReadSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tl::expected<QString, std::pair<TextFileFormat::ReadResult, QString>>
|
||||||
|
TextFileFormat::readFile(const FilePath &filePath, const QTextCodec *defaultCodec)
|
||||||
|
{
|
||||||
|
QString plainText;
|
||||||
|
TextFileFormat format;
|
||||||
|
QString errorString;
|
||||||
|
const TextFileFormat::ReadResult result =
|
||||||
|
readTextFile(filePath, defaultCodec, &plainText, &format, &errorString, nullptr);
|
||||||
|
if (result != TextFileFormat::ReadSuccess)
|
||||||
|
return tl::unexpected(std::make_pair(result, errorString));
|
||||||
|
return plainText;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Writes out a text file to \a filePath into a string, \a plainText.
|
Writes out a text file to \a filePath into a string, \a plainText.
|
||||||
|
|
||||||
|
@@ -3,9 +3,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "expected.h"
|
||||||
#include "utils_global.h"
|
#include "utils_global.h"
|
||||||
|
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QTextCodec;
|
class QTextCodec;
|
||||||
@@ -53,6 +55,8 @@ public:
|
|||||||
QByteArray *decodingErrorSample = nullptr);
|
QByteArray *decodingErrorSample = nullptr);
|
||||||
static ReadResult readFileUTF8(const FilePath &filePath, const QTextCodec *defaultCodec,
|
static ReadResult readFileUTF8(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||||
QByteArray *plainText, QString *errorString);
|
QByteArray *plainText, QString *errorString);
|
||||||
|
static tl::expected<QString, std::pair<ReadResult, QString>>
|
||||||
|
readFile(const FilePath &filePath, const QTextCodec *defaultCodec);
|
||||||
|
|
||||||
bool writeFile(const FilePath &filePath, QString plainText, QString *errorString) const;
|
bool writeFile(const FilePath &filePath, QString plainText, QString *errorString) const;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user