Skip to content

Commit

Permalink
improve error message (#91)
Browse files Browse the repository at this point in the history
* clang-tidy fixes

* cosmetic

* improve error-message & cosmetic

* bump version

* changelog
  • Loading branch information
ptahmose committed Jan 8, 2024
1 parent e6baa05 commit 5bca15f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW) # enable new "MSVC runtime library selection" (https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html)

project(libCZI
VERSION 0.57.2
VERSION 0.57.3
HOMEPAGE_URL "https://github.com/ZEISS/libczi"
DESCRIPTION "libCZI is an Open Source Cross-Platform C++ library to read and write CZI")

Expand Down
3 changes: 2 additions & 1 deletion Src/libCZI/Doc/version-history.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ version history {#version_history}
0.56.0 | [82](https://github.com/ZEISS/libczi/pull/82) | add option "kCurlHttp_CaInfo" & "kCurlHttp_CaInfoBlob", allow to retrieve properties from a stream-class
0.57.0 | [84](https://github.com/ZEISS/libczi/pull/84) | add caching for accessors, update CLI11 to version 2.3.2
0.57.1 | [86](https://github.com/ZEISS/libczi/pull/86) | small improvement for CMake-build: allow to use an apt-provided CURL-package
0.57.2 | [90](https://github.com/ZEISS/libczi/pull/90) | improve thread-safety of CziReader
0.57.2 | [90](https://github.com/ZEISS/libczi/pull/90) | improve thread-safety of CziReader
0.57.3 | [91](https://github.com/ZEISS/libczi/pull/91) | improve error-message
57 changes: 36 additions & 21 deletions Src/libCZI/StreamImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,34 @@ CSimpleOutputStreamImplWindows::~CSimpleOutputStreamImplWindows()

/*virtual*/void CSimpleOutputStreamImplWindows::Write(std::uint64_t offset, const void* pv, std::uint64_t size, std::uint64_t* ptrBytesWritten)
{
OVERLAPPED ol = { 0 };
ol.Offset = (DWORD)(offset);
ol.OffsetHigh = (DWORD)(offset >> 32);
DWORD bytesWritten;
BOOL B = WriteFile(this->handle, pv, (DWORD)size, &bytesWritten, &ol);
if (size > (std::numeric_limits<DWORD>::max)())
{
throw std::runtime_error("size is too large");
}

OVERLAPPED ol = {};
ol.Offset = static_cast<DWORD>(offset);
ol.OffsetHigh = static_cast<DWORD>(offset >> 32);
DWORD bytes_written;
const BOOL B = WriteFile(this->handle, pv, static_cast<DWORD>(size), &bytes_written, &ol);
if (!B)
{
DWORD lastError = GetLastError();
const DWORD last_error = GetLastError();
std::stringstream ss;
ss << "Error writing to file (LastError=" << std::setfill('0') << std::setw(8) << std::showbase << lastError << ")";
ss << "Error writing to file (LastError=" << std::hex << std::setfill('0') << std::setw(8) << std::showbase << last_error << ")";
throw std::runtime_error(ss.str());
}

if (ptrBytesWritten != nullptr)
{
*ptrBytesWritten = bytesWritten;
*ptrBytesWritten = bytes_written;
}
}
#endif

//----------------------------------------------------------------------------
CStreamImplInMemory::CStreamImplInMemory(std::shared_ptr<const void> ptr, std::size_t dataSize)
: rawData(ptr), dataBufferSize(dataSize)
: rawData(std::move(ptr)), dataBufferSize(dataSize)
{
}

Expand Down Expand Up @@ -336,43 +341,53 @@ CSimpleInputOutputStreamImplWindows::CSimpleInputOutputStreamImplWindows(const w

/*virtual*/void CSimpleInputOutputStreamImplWindows::Read(std::uint64_t offset, void* pv, std::uint64_t size, std::uint64_t* ptrBytesRead)
{
OVERLAPPED ol = { 0 };
if (size > (std::numeric_limits<DWORD>::max)())
{
throw std::runtime_error("size is too large");
}

OVERLAPPED ol = {};
ol.Offset = static_cast<DWORD>(offset);
ol.OffsetHigh = static_cast<DWORD>(offset >> 32);
DWORD bytesRead;
BOOL B = ReadFile(this->handle, pv, static_cast<DWORD>(size), &bytesRead, &ol);
DWORD bytes_read;
const BOOL B = ReadFile(this->handle, pv, static_cast<DWORD>(size), &bytes_read, &ol);
if (!B)
{
const DWORD lastError = GetLastError();
const DWORD last_error = GetLastError();
ostringstream ss;
ss << "Error reading from file (LastError=" << std::setfill('0') << std::setw(8) << std::showbase << lastError << ")";
ss << "Error reading from file (LastError=" << std::hex << std::setfill('0') << std::setw(8) << std::showbase << last_error << ")";
throw std::runtime_error(ss.str());
}

if (ptrBytesRead != nullptr)
{
*ptrBytesRead = bytesRead;
*ptrBytesRead = bytes_read;
}
}

/*virtual*/void CSimpleInputOutputStreamImplWindows::Write(std::uint64_t offset, const void* pv, std::uint64_t size, std::uint64_t* ptrBytesWritten)
{
OVERLAPPED ol = { 0 };
if (size > (std::numeric_limits<DWORD>::max)())
{
throw std::runtime_error("size is too large");
}

OVERLAPPED ol = {};
ol.Offset = static_cast<DWORD>(offset);
ol.OffsetHigh = static_cast<DWORD>(offset >> 32);
DWORD bytesWritten;
BOOL B = WriteFile(this->handle, pv, static_cast<DWORD>(size), &bytesWritten, &ol);
DWORD bytes_written;
const BOOL B = WriteFile(this->handle, pv, static_cast<DWORD>(size), &bytes_written, &ol);
if (!B)
{
DWORD lastError = GetLastError();
const DWORD last_error = GetLastError();
ostringstream ss;
ss << "Error writing to file (LastError=" << std::setfill('0') << std::setw(8) << std::showbase << lastError << ")";
ss << "Error writing to file (LastError=" << std::hex << std::setfill('0') << std::setw(8) << std::showbase << last_error << ")";
throw std::runtime_error(ss.str());
}

if (ptrBytesWritten != nullptr)
{
*ptrBytesWritten = bytesWritten;
*ptrBytesWritten = bytes_written;
}
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions Src/libCZI/StreamsLib/windowsfileinputstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ void WindowsFileInputStream::Read(std::uint64_t offset, void* pv, std::uint64_t
const BOOL read_file_return_code = ReadFile(this->handle, pv, static_cast<DWORD>(size), &bytes_read, &ol);
if (!read_file_return_code)
{
const DWORD lastError = GetLastError();
const DWORD last_error = GetLastError();
std::stringstream ss;
ss << "Error reading from file (LastError=" << std::setfill('0') << std::setw(8) << std::showbase << lastError << ")";
ss << "Error reading from file (LastError=" << std::hex << std::setfill('0') << std::setw(8) << std::showbase << last_error << ")";
throw std::runtime_error(ss.str());
}

Expand Down

0 comments on commit 5bca15f

Please sign in to comment.