|
|
|
@ -89,16 +89,37 @@ nonstd::expected<QByteArray, Utils::IO::ReadError> Utils::IO::readFile(const Pat
@@ -89,16 +89,37 @@ nonstd::expected<QByteArray, Utils::IO::ReadError> Utils::IO::readFile(const Pat
|
|
|
|
|
return nonstd::make_unexpected(ReadError {ReadError::ExceedSize, message}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Do not use `QIODevice::readAll()` it won't stop when reading `/dev/zero`
|
|
|
|
|
const QByteArray data = file.read(fileSize); |
|
|
|
|
if (const qint64 dataSize = data.size(); dataSize != fileSize) |
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) |
|
|
|
|
QByteArray ret {fileSize, Qt::Uninitialized}; |
|
|
|
|
#else |
|
|
|
|
QByteArray ret {static_cast<int>(fileSize), Qt::Uninitialized}; |
|
|
|
|
#endif |
|
|
|
|
const qint64 actualSize = file.read(ret.data(), fileSize); |
|
|
|
|
|
|
|
|
|
if (actualSize < 0) |
|
|
|
|
{ |
|
|
|
|
const QString message = QCoreApplication::translate("Utils::IO", "File read error. File: \"%1\". Error: \"%2\"") |
|
|
|
|
.arg(file.fileName(), file.errorString()); |
|
|
|
|
return nonstd::make_unexpected(ReadError {ReadError::Failed, message}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (actualSize < fileSize) |
|
|
|
|
{ |
|
|
|
|
// `QIODevice::Text` will convert CRLF to LF on-the-fly and affects return value
|
|
|
|
|
// of `qint64 QIODevice::read(char *data, qint64 maxSize)`
|
|
|
|
|
if (additionalMode.testFlag(QIODevice::Text)) |
|
|
|
|
{ |
|
|
|
|
ret.truncate(actualSize); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
const QString message = QCoreApplication::translate("Utils::IO", "Read size mismatch. File: \"%1\". Expected: %2. Actual: %3") |
|
|
|
|
.arg(file.fileName(), QString::number(fileSize), QString::number(dataSize)); |
|
|
|
|
.arg(file.fileName(), QString::number(fileSize), QString::number(actualSize)); |
|
|
|
|
return nonstd::make_unexpected(ReadError {ReadError::SizeMismatch, message}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return data; |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
nonstd::expected<void, QString> Utils::IO::saveToFile(const Path &path, const QByteArray &data) |
|
|
|
|