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,7 +273,7 @@ TextFileFormat::ReadResult
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const FilePath &filePath,
|
Result<> TextFileFormat::readFileUtf8(const FilePath &filePath,
|
||||||
const QTextCodec *defaultCodec,
|
const QTextCodec *defaultCodec,
|
||||||
QByteArray *plainText)
|
QByteArray *plainText)
|
||||||
{
|
{
|
||||||
@@ -281,10 +281,10 @@ TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const FilePath &filePath
|
|||||||
try {
|
try {
|
||||||
FileReader reader;
|
FileReader reader;
|
||||||
if (const Result<> res = reader.fetch(filePath); !res)
|
if (const Result<> res = reader.fetch(filePath); !res)
|
||||||
return {TextFileFormat::ReadIOError, res.error()};
|
return res;
|
||||||
data = reader.data();
|
data = reader.data();
|
||||||
} catch (const std::bad_alloc &) {
|
} 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);
|
TextFileFormat format = TextFileFormat::detect(data);
|
||||||
@@ -297,10 +297,10 @@ TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const FilePath &filePath
|
|||||||
if (format.lineTerminationMode == TextFileFormat::CRLFLineTerminator)
|
if (format.lineTerminationMode == TextFileFormat::CRLFLineTerminator)
|
||||||
data.replace("\r\n", "\n");
|
data.replace("\r\n", "\n");
|
||||||
*plainText = data;
|
*plainText = data;
|
||||||
return TextFileFormat::ReadSuccess;
|
} else {
|
||||||
}
|
|
||||||
*plainText = target.toUtf8();
|
*plainText = target.toUtf8();
|
||||||
return TextFileFormat::ReadSuccess;
|
}
|
||||||
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
tl::expected<QString, TextFileFormat::ReadResult>
|
tl::expected<QString, TextFileFormat::ReadResult>
|
||||||
|
@@ -63,7 +63,7 @@ public:
|
|||||||
static ReadResult readFile(const FilePath &filePath, const QTextCodec *defaultCodec,
|
static ReadResult readFile(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||||
QString *plainText, TextFileFormat *format,
|
QString *plainText, TextFileFormat *format,
|
||||||
QByteArray *decodingErrorSample = nullptr);
|
QByteArray *decodingErrorSample = nullptr);
|
||||||
static ReadResult readFileUTF8(const FilePath &filePath, const QTextCodec *defaultCodec,
|
static Utils::Result<> readFileUtf8(const FilePath &filePath, const QTextCodec *defaultCodec,
|
||||||
QByteArray *plainText);
|
QByteArray *plainText);
|
||||||
|
|
||||||
static tl::expected<QString, ReadResult> readFile(const FilePath &filePath,
|
static tl::expected<QString, ReadResult> readFile(const FilePath &filePath,
|
||||||
|
@@ -48,10 +48,9 @@ QByteArray CppParser::getFileContent(const FilePath &filePath) const
|
|||||||
fileContent = *source;
|
fileContent = *source;
|
||||||
} else {
|
} else {
|
||||||
const QTextCodec *codec = Core::EditorManager::defaultTextCodec();
|
const QTextCodec *codec = Core::EditorManager::defaultTextCodec();
|
||||||
const TextFileFormat::ReadResult result =
|
const Result<> result = TextFileFormat::readFileUtf8(filePath, codec, &fileContent);
|
||||||
TextFileFormat::readFileUTF8(filePath, codec, &fileContent);
|
if (!result)
|
||||||
if (result.code != TextFileFormat::ReadSuccess)
|
qDebug() << "Failed to read file" << filePath << ":" << result.error();
|
||||||
qDebug() << "Failed to read file" << filePath << ":" << result.error;
|
|
||||||
}
|
}
|
||||||
fileContent.replace("\r\n", "\n");
|
fileContent.replace("\r\n", "\n");
|
||||||
return fileContent;
|
return fileContent;
|
||||||
|
@@ -192,11 +192,11 @@ bool CppSourceProcessor::getFileContents(const FilePath &absoluteFilePath,
|
|||||||
|
|
||||||
// Get from file
|
// Get from file
|
||||||
*revision = 0;
|
*revision = 0;
|
||||||
const TextFileFormat::ReadResult result =
|
const Result<> result =
|
||||||
TextFileFormat::readFileUTF8(absoluteFilePath, m_defaultCodec, contents);
|
TextFileFormat::readFileUtf8(absoluteFilePath, m_defaultCodec, contents);
|
||||||
if (result.code != TextFileFormat::ReadSuccess) {
|
if (!result) {
|
||||||
qWarning("Error reading file \"%s\": \"%s\".", qPrintable(absoluteFilePath.toUrlishString()),
|
qWarning("Error reading file \"%s\": \"%s\".", qPrintable(absoluteFilePath.toUserOutput()),
|
||||||
qPrintable(result.error));
|
qPrintable(result.error()));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
contents->replace("\r\n", "\n");
|
contents->replace("\r\n", "\n");
|
||||||
|
@@ -3865,8 +3865,7 @@ IEditor *GitClient::openShowEditor(const FilePath &workingDirectory, const QStri
|
|||||||
if (content.isEmpty())
|
if (content.isEmpty())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
QByteArray fileContent;
|
QByteArray fileContent;
|
||||||
if (TextFileFormat::readFileUTF8(path, nullptr, &fileContent).code
|
if (TextFileFormat::readFileUtf8(path, nullptr, &fileContent)) {
|
||||||
== TextFileFormat::ReadSuccess) {
|
|
||||||
if (fileContent == content)
|
if (fileContent == content)
|
||||||
return nullptr; // open the file for read/write
|
return nullptr; // open the file for read/write
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user