forked from qt-creator/qt-creator
Utils: Return Result<> from TextFileFormat::readFileUTF8()
... and rename it to readFileUtf8(). More in line with the rest of Creator. Change-Id: I9e57307253dce58b638bbb5ac39c257eea9524a3 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -273,18 +273,18 @@ TextFileFormat::ReadResult
|
||||
return result;
|
||||
}
|
||||
|
||||
TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const FilePath &filePath,
|
||||
const QTextCodec *defaultCodec,
|
||||
QByteArray *plainText)
|
||||
Result<> TextFileFormat::readFileUtf8(const FilePath &filePath,
|
||||
const QTextCodec *defaultCodec,
|
||||
QByteArray *plainText)
|
||||
{
|
||||
QByteArray data;
|
||||
try {
|
||||
FileReader reader;
|
||||
if (const Result<> res = reader.fetch(filePath); !res)
|
||||
return {TextFileFormat::ReadIOError, res.error()};
|
||||
return res;
|
||||
data = reader.data();
|
||||
} catch (const std::bad_alloc &) {
|
||||
return {TextFileFormat::ReadMemoryAllocationError, Tr::tr("Out of memory.")};
|
||||
return ResultError(Tr::tr("Out of memory."));
|
||||
}
|
||||
|
||||
TextFileFormat format = TextFileFormat::detect(data);
|
||||
@@ -297,10 +297,10 @@ TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const FilePath &filePath
|
||||
if (format.lineTerminationMode == TextFileFormat::CRLFLineTerminator)
|
||||
data.replace("\r\n", "\n");
|
||||
*plainText = data;
|
||||
return TextFileFormat::ReadSuccess;
|
||||
} else {
|
||||
*plainText = target.toUtf8();
|
||||
}
|
||||
*plainText = target.toUtf8();
|
||||
return TextFileFormat::ReadSuccess;
|
||||
return ResultOk;
|
||||
}
|
||||
|
||||
tl::expected<QString, TextFileFormat::ReadResult>
|
||||
|
@@ -63,8 +63,8 @@ public:
|
||||
static ReadResult readFile(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||
QString *plainText, TextFileFormat *format,
|
||||
QByteArray *decodingErrorSample = nullptr);
|
||||
static ReadResult readFileUTF8(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||
QByteArray *plainText);
|
||||
static Utils::Result<> readFileUtf8(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||
QByteArray *plainText);
|
||||
|
||||
static tl::expected<QString, ReadResult> readFile(const FilePath &filePath,
|
||||
const QTextCodec *defaultCodec);
|
||||
|
@@ -48,10 +48,9 @@ QByteArray CppParser::getFileContent(const FilePath &filePath) const
|
||||
fileContent = *source;
|
||||
} else {
|
||||
const QTextCodec *codec = Core::EditorManager::defaultTextCodec();
|
||||
const TextFileFormat::ReadResult result =
|
||||
TextFileFormat::readFileUTF8(filePath, codec, &fileContent);
|
||||
if (result.code != TextFileFormat::ReadSuccess)
|
||||
qDebug() << "Failed to read file" << filePath << ":" << result.error;
|
||||
const Result<> result = TextFileFormat::readFileUtf8(filePath, codec, &fileContent);
|
||||
if (!result)
|
||||
qDebug() << "Failed to read file" << filePath << ":" << result.error();
|
||||
}
|
||||
fileContent.replace("\r\n", "\n");
|
||||
return fileContent;
|
||||
|
@@ -192,11 +192,11 @@ bool CppSourceProcessor::getFileContents(const FilePath &absoluteFilePath,
|
||||
|
||||
// Get from file
|
||||
*revision = 0;
|
||||
const TextFileFormat::ReadResult result =
|
||||
TextFileFormat::readFileUTF8(absoluteFilePath, m_defaultCodec, contents);
|
||||
if (result.code != TextFileFormat::ReadSuccess) {
|
||||
qWarning("Error reading file \"%s\": \"%s\".", qPrintable(absoluteFilePath.toUrlishString()),
|
||||
qPrintable(result.error));
|
||||
const Result<> result =
|
||||
TextFileFormat::readFileUtf8(absoluteFilePath, m_defaultCodec, contents);
|
||||
if (!result) {
|
||||
qWarning("Error reading file \"%s\": \"%s\".", qPrintable(absoluteFilePath.toUserOutput()),
|
||||
qPrintable(result.error()));
|
||||
return false;
|
||||
}
|
||||
contents->replace("\r\n", "\n");
|
||||
|
@@ -3865,8 +3865,7 @@ IEditor *GitClient::openShowEditor(const FilePath &workingDirectory, const QStri
|
||||
if (content.isEmpty())
|
||||
return nullptr;
|
||||
QByteArray fileContent;
|
||||
if (TextFileFormat::readFileUTF8(path, nullptr, &fileContent).code
|
||||
== TextFileFormat::ReadSuccess) {
|
||||
if (TextFileFormat::readFileUtf8(path, nullptr, &fileContent)) {
|
||||
if (fileContent == content)
|
||||
return nullptr; // open the file for read/write
|
||||
}
|
||||
|
Reference in New Issue
Block a user