diff --git a/components/platform/file.posix.cpp b/components/platform/file.posix.cpp index b6c322de11..dd50083e7a 100644 --- a/components/platform/file.posix.cpp +++ b/components/platform/file.posix.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include namespace Platform::File { @@ -29,7 +31,6 @@ namespace Platform::File { Handle open(const char* filename) { - // Posix #ifdef O_BINARY static const int openFlags = O_RDONLY | O_BINARY; #else @@ -39,9 +40,7 @@ namespace Platform::File { auto handle = ::open(filename, openFlags, 0); if (handle == -1) { - std::ostringstream os; - os << "Failed to open '" << filename << "' for reading: " << strerror(errno); - throw std::runtime_error(os.str()); + throw std::runtime_error(std::string("Failed to open '") + filename + "' for reading: " + strerror(errno)); } return static_cast(handle); } @@ -58,7 +57,7 @@ namespace Platform::File { const auto nativeHandle = getNativeHandle(handle); const auto nativeSeekType = getNativeSeekType(type); - if (::lseek(toNativeHandle(mHandle), position, SEEK_SET) == -1) + if (::lseek(nativeHandle, position, nativeSeekType) == -1) { throw std::runtime_error("An lseek() call failed: " + std::string(strerror(errno))); } @@ -66,14 +65,14 @@ namespace Platform::File { size_t size(Handle handle) { - auto nativeHandle = getNativeHandle(handle); - - auto oldPos = tell(handle); + const auto oldPos = tell(handle); seek(handle, 0, SeekType::End); - auto size = tell(handle); + const auto fileSize = tell(handle); + seek(handle, oldPos, SeekType::Begin); - return static_cast(size); + + return static_cast(fileSize); } size_t tell(Handle handle) @@ -92,7 +91,7 @@ namespace Platform::File { { auto nativeHandle = getNativeHandle(handle); - int amount = ::read(toNativeHandle(mHandle), data, size); + int amount = ::read(nativeHandle, data, size); if (amount == -1) { throw std::runtime_error("An attempt to read " + std::to_string(size) + " bytes failed: " + strerror(errno)); diff --git a/components/platform/file.stdio.cpp b/components/platform/file.stdio.cpp index dd2660cd32..ed17bd2040 100644 --- a/components/platform/file.stdio.cpp +++ b/components/platform/file.stdio.cpp @@ -5,6 +5,8 @@ #include #include +#include +#include namespace Platform::File { @@ -26,13 +28,10 @@ namespace Platform::File { Handle open(const char* filename) { - // Stdio FILE* handle = fopen(filename, "rb"); if (handle == nullptr) { - std::ostringstream os; - os << "Failed to open '" << filename << "' for reading: " << strerror(errno); - throw std::runtime_error(os.str()); + throw std::runtime_error(std::string("Failed to open '") + filename + "' for reading: " + strerror(errno)); } return static_cast(reinterpret_cast(handle)); } @@ -49,19 +48,20 @@ namespace Platform::File { const auto nativeSeekType = getNativeSeekType(type); if (fseek(nativeHandle, position, nativeSeekType) != 0) { - throw std::runtime_error("An fseek() call failed: " + std::string(strerror(errno))); + throw std::runtime_error(std::string("An fseek() call failed: ") + strerror(errno)); } } size_t size(Handle handle) { auto nativeHandle = getNativeHandle(handle); - auto oldPos = tell(handle); - + + const auto oldPos = tell(handle); seek(handle, 0, SeekType::End); - auto size = tell(handle); + const auto fileSize = tell(handle); + seek(handle, oldPos, SeekType::Begin); - return static_cast(size); + return static_cast(fileSize); } size_t tell(Handle handle) @@ -71,7 +71,7 @@ namespace Platform::File { long position = ftell(nativeHandle); if (position == -1) { - throw std::runtime_error("An ftell() call failed: " + std::string(strerror(errno))); + throw std::runtime_error(std::string("An ftell() call failed: ") + strerror(errno)); } return static_cast(position); } @@ -83,7 +83,7 @@ namespace Platform::File { int amount = fread(data, 1, size, nativeHandle); if (amount == 0 && ferror(nativeHandle)) { - throw std::runtime_error("An attempt to read " + std::to_string(size) + " bytes failed: " + strerror(errno)); + throw std::runtime_error(std::string("An attempt to read ") + std::to_string(size) + " bytes failed: " + strerror(errno)); } return static_cast(amount); } diff --git a/components/platform/file.win32.cpp b/components/platform/file.win32.cpp index 59601caa80..31fe67ecba 100644 --- a/components/platform/file.win32.cpp +++ b/components/platform/file.win32.cpp @@ -5,6 +5,7 @@ #include #include +#include #include namespace Platform::File { @@ -31,9 +32,7 @@ namespace Platform::File { HANDLE handle = CreateFileW(wname.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); if (handle == INVALID_HANDLE_VALUE) { - std::ostringstream os; - os << "Failed to open '" << filename << "' for reading: " << GetLastError(); - throw std::runtime_error(os.str()); + throw std::runtime_error(std::string("Failed to open '") + filename + "' for reading: " + std::to_string(GetLastError())); } return static_cast(reinterpret_cast(handle)); } @@ -53,7 +52,7 @@ namespace Platform::File { { if (auto errCode = GetLastError(); errCode != ERROR_SUCCESS) { - throw std::runtime_error("An fseek() call failed: " + std::to_string(errCode)); + throw std::runtime_error(std::string("An fseek() call failed: ") + std::to_string(errCode)); } } } @@ -93,7 +92,7 @@ namespace Platform::File { DWORD bytesRead{}; if (!ReadFile(nativeHandle, data, static_cast(size), &bytesRead, nullptr)) - throw std::runtime_error("A read operation on a file failed: " + std::to_string(GetLastError())); + throw std::runtime_error(std::string("A read operation on a file failed: ") + std::to_string(GetLastError())); return bytesRead; }