From 204dad55119df079ca54309bc4b740280fa54c31 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Tue, 24 Sep 2024 07:33:05 -0500 Subject: Fine tune read interface of CBinaryFile. --- source/fud_c_file.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'source') diff --git a/source/fud_c_file.cpp b/source/fud_c_file.cpp index 3d15431..a85fd00 100644 --- a/source/fud_c_file.cpp +++ b/source/fud_c_file.cpp @@ -97,48 +97,56 @@ Result CBinaryFile::size() const return RetType::okay(fileSize); } -FileStatus CBinaryFile::read(void* destination, size_t destinationSize, size_t length) +ReadResult CBinaryFile::read(void* destination, size_t destinationSize, size_t length) { return read(destination, destinationSize, length, 0); } -FileStatus CBinaryFile::read(void* destination, size_t destinationSize, size_t length, size_t offset) +ReadResult CBinaryFile::read(void* destination, size_t destinationSize, size_t length, size_t offset) { + ReadResult result{}; if (length == 0) { - return FileStatus::Success; + return result; } if (destination == nullptr) { - return FileStatus::NullPointer; + result.status = FileStatus::NullPointer; + return result; } if (offset > LONG_MAX || SIZE_MAX - offset < length || destinationSize < length) { - return FileStatus::InvalidArgument; + result.status = FileStatus::InvalidArgument; + return result; } auto fileSizeResult = size(); if (fileSizeResult.isError()) { - return fileSizeResult.getError(); + result.status = fileSizeResult.getError(); + return result; } auto fileSize = fileSizeResult.getOkay(); if (offset + length > fileSize) { - return FileStatus::InvalidArgument; + result.status = FileStatus::InvalidArgument; + return result; } auto seekResult = fseek(m_file, static_cast(offset), SEEK_SET); if (seekResult != 0) { - return FileStatus::Error; + result.status = FileStatus::Error; + return result; } auto* destPtr = static_cast(destination); - auto readResult = fread(destPtr, length, 1, m_file); - if (readResult != 1) { - static_cast(reset()); - return FileStatus::Error; + result.bytesRead = fread(destPtr, 1, length, m_file); + static_cast(reset()); + if (result.bytesRead != length) { + result.status = FileStatus::PartialSuccess; + } else { + result.status = FileStatus::Success; } - return FileStatus::Success; + return result; } FileStatus CBinaryFile::reset() const { -- cgit v1.2.3