Skip to content

Commit

Permalink
cosmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
ptahmose committed Oct 30, 2023
1 parent ad1d1bb commit d760ab2
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 306 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ check_cxx_symbol_exists(pwrite unistd.h HAVE_UNISTD_H_PWRITE)
BoolToFoundNotFound(HAVE_UNISTD_H_PWRITE HAVE_UNISTD_H_PWRITE_TEXT)
message("check for open -> ${HAVE_FCNTL_H_OPEN_TEXT} ; check for pread -> ${HAVE_UNISTD_H_PREAD_TEXT} ; check for pwrite -> ${HAVE_UNISTD_H_PWRITE_TEXT}")

option(LIBCZI_BUILD_CURL_BASED_STREAM "include curl-based http-/https-stream object" ON)
option(LIBCZI_BUILD_CURL_BASED_STREAM "include curl-based http-/https-stream object" OFF)

# This option allows to exclude the unit-tests from the build. The unit-tests are using the
# Google-Test-framework which is downloaded from GitHub during the CMake-run.
Expand Down
50 changes: 1 addition & 49 deletions Src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (LIBCZI_BUILD_CURL_BASED_STREAM)
# Set the prefix to control where it's installed
PREFIX ${CMAKE_BINARY_DIR}/curl
# Pass CMake arguments to build curl as a static library
# for possible options -> c.f https://github.com/curl/curl/blob/master/CMakeLists.txt
CMAKE_ARGS -DBUILD_SHARED_LIBS=OFF
-DBUILD_STATIC_LIBS=ON
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
Expand All @@ -36,55 +37,6 @@ if (LIBCZI_BUILD_CURL_BASED_STREAM)
)

FetchContent_MakeAvailable(libcurl)
# SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/curl
# PATCH_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/cURL/buildconf
# CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/cURL/configure --prefix=<INSTALL_DIR>
# BUILD_COMMAND ${MAKE}
# UPDATE_COMMAND ""
# INSTALL_COMMAND ""
# LOG_DOWNLOAD ON
# LOG_UPDATE ON
# LOG_CONFIGURE ON
# LOG_BUILD ON
# LOG_TEST ON
# LOG_INSTALL ON
#[[
# Specify the include directories
#ExternalProject_Get_Property(libcurl INSTALL_DIR)
message(STATUS "********** CURL-INSTALL_DIR **********: ${INSTALL_DIR}")
#ExternalProject_Get_Property(libcurl source_dir)
#ExternalProject_Get_Property(libcurl binary_dir)
# Define an imported target
#add_library(CURL::libcurl STATIC IMPORTED GLOBAL)
#message(STATUS "********** CURL-source **********: ${source_dir}")
#add_dependencies(CURL::libcurl libcurl)
#add_custom_target(set_curl_properties
#COMMAND ${CMAKE_COMMAND} -P set_curl_properties.cmake)
#add_dependencies(set_curl_properties libcurl)
# Set properties for the imported target
# set_target_properties(CURL::libcurl PROPERTIES
# IMPORTED_LOCATION ${binary_dir}
# INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_BINARY_DIR}/curl/include"
# )
set(CURL_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/curl/include)
set(CURL_LIBRARIES ${CMAKE_BINARY_DIR}/curl/lib/libcurl.a)
#find_package(CURL REQUIRED)
message(STATUS "********** CURL_INCLUDE_DIRS **********: ${CURL_INCLUDE_DIRS}")
#set(CURL_SOURCE_DIR ${source_dir})
#set(CURL_BINARY_DIR ${binary_dir})
#set(CURL_LIBRARIES ${CURL_BINARY_DIR}/lib/.libs/libcurl.dylib)
#include_directories(${CURL_SOURCE_DIR})
#set(DEPENDENCIES ${DEPENDENCIES} libcurl)
set(CURL_DOWNLOADED_AND_BUILT TRUE)
#]]
endif(${CURL_FOUND})

endif(LIBCZI_BUILD_CURL_BASED_STREAM)
Expand Down
4 changes: 2 additions & 2 deletions Src/CZICmd/cmdlineoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1933,12 +1933,12 @@ void CCmdLineOptions::PrintHelpBitmapGenerator()

void CCmdLineOptions::PrintHelpStreamsObjects()
{
this->GetLog()->WriteLineStdOut("Available Input-Stream objects: [default class is denoted with '(*)']");
this->GetLog()->WriteLineStdOut("Available Input-Stream objects:");
this->GetLog()->WriteLineStdOut("");

int stream_object_count = libCZI::StreamsFactory::GetStreamInfoCount();
ostringstream string_stream;
for (int i=0;i<stream_object_count;++i)
for (int i = 0; i < stream_object_count; ++i)
{
libCZI::StreamsFactory::StreamClassInfo stream_class_info;
libCZI::StreamsFactory::GetStreamInfoForClass(i, stream_class_info);
Expand Down
141 changes: 0 additions & 141 deletions Src/libCZI/StreamImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,6 @@

using namespace std;

//CSimpleStreamImpl::CSimpleStreamImpl(const wchar_t* filename)
//{
//#if defined(_WIN32)
// errno_t err = _wfopen_s(&this->fp, filename, L"rb");
//#else
// int /*error_t*/ err = 0;
//
// // convert the wchar_t to an UTF8-string
// auto filename_utf8 = Utilities::convertWchar_tToUtf8(filename);
//
// this->fp = fopen(filename_utf8.c_str(), "rb");
// if (!this->fp)
// {
// err = errno;
// }
//#endif
// if (err != 0)
// {
// std::stringstream ss;
//#if (_WIN32)
// char errMsg[100];
// strerror_s(errMsg, sizeof(errMsg), err);
// ss << "Error opening the file \"" << Utilities::convertWchar_tToUtf8(filename) << "\" -> errno=" << err << " (" << errMsg << ")";
//#else
// ss << "Error opening the file \"" << filename_utf8 << "\" -> errno=" << err << " (" << strerror(err) << ")";
//#endif
// throw std::runtime_error(ss.str());
// }
//}
//
//CSimpleStreamImpl::~CSimpleStreamImpl()
//{
// fclose(this->fp);
//}
//
///*virtual*/void CSimpleStreamImpl::Read(std::uint64_t offset, void* pv, std::uint64_t size, std::uint64_t* ptrBytesRead)
//{
//#if defined(_WIN32)
// int r = _fseeki64(this->fp, offset, SEEK_SET);
//#else
// int r = fseeko(this->fp, offset, SEEK_SET);
//#endif
// if (r != 0)
// {
// const auto err = errno;
// stringstream ss;
// ss << "Seek to file-position " << offset << " failed, errno=<<" << err << ".";
// throw std::runtime_error(ss.str());
// }
//
// const std::uint64_t bytesRead = fread(pv, 1, (size_t)size, this->fp);
// if (ptrBytesRead != nullptr)
// {
// *ptrBytesRead = bytesRead;
// }
//}

//----------------------------------------------------------------------------


Expand Down Expand Up @@ -107,46 +50,6 @@ void CSimpleStreamImplCppStreams::Read(std::uint64_t offset, void* pv, std::uint
//----------------------------------------------------------------------------

#if LIBCZI_USE_PREADPWRITEBASED_STREAMIMPL
//CStreamImplPread::CStreamImplPread(const wchar_t* filename)
// : fileDescriptor(0)
//{
// auto filename_utf8 = Utilities::convertWchar_tToUtf8(filename);
// this->fileDescriptor = open(filename_utf8.c_str(), O_RDONLY);
// if (this->fileDescriptor < 0)
// {
// auto err = errno;
// std::stringstream ss;
// ss << "Error opening the file \"" << filename_utf8 << "\" -> errno=" << err << " (" << strerror(err) << ")";
// throw std::runtime_error(ss.str());
// }
//}
//
//CStreamImplPread::~CStreamImplPread()
//{
// if (this->fileDescriptor != 0)
// {
// close(this->fileDescriptor);
// }
//}
//
///*virtual*/void CStreamImplPread::Read(std::uint64_t offset, void* pv, std::uint64_t size, std::uint64_t* ptrBytesRead)
//{
// ssize_t bytesRead = pread(this->fileDescriptor, pv, size, offset);
// if (bytesRead < 0)
// {
// auto err = errno;
// std::stringstream ss;
// ss << "Error reading from file (errno=" << err << " -> " << strerror(err) << ")";
// throw std::runtime_error(ss.str());
// }
//
// if (ptrBytesRead != nullptr)
// {
// *ptrBytesRead = bytesRead;
// }
//}

//----------------------------------------------------------------------------

COutputStreamImplPwrite::COutputStreamImplPwrite(const wchar_t* filename, bool overwriteExisting)
: fileDescriptor(0)
Expand Down Expand Up @@ -200,50 +103,6 @@ COutputStreamImplPwrite::~COutputStreamImplPwrite()
//----------------------------------------------------------------------------

#if defined(_WIN32)
//CSimpleStreamImplWindows::CSimpleStreamImplWindows(const wchar_t* filename)
// : handle(INVALID_HANDLE_VALUE)
//{
// HANDLE h = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL);
// if (h == INVALID_HANDLE_VALUE)
// {
// std::stringstream ss;
// wstring_convert<codecvt_utf8<wchar_t>> utf8_conv;
// ss << "Error opening the file \"" << utf8_conv.to_bytes(filename) << "\"";
// throw std::runtime_error(ss.str());
// }
//
// this->handle = h;
//}
//
//CSimpleStreamImplWindows::~CSimpleStreamImplWindows()
//{
// if (this->handle != INVALID_HANDLE_VALUE)
// {
// CloseHandle(this->handle);
// }
//}
//
//void CSimpleStreamImplWindows::Read(std::uint64_t offset, void* pv, std::uint64_t size, std::uint64_t* ptrBytesRead)
//{
// OVERLAPPED ol = { 0 };
// 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);
// if (!B)
// {
// DWORD lastError = GetLastError();
// std::stringstream ss;
// ss << "Error reading from file (LastError=" << std::setfill('0') << std::setw(8) << std::showbase << lastError << ")";
// throw std::runtime_error(ss.str());
// }
//
// if (ptrBytesRead != nullptr)
// {
// *ptrBytesRead = bytesRead;
// }
//}
//----------------------------------------------------------------------------

CSimpleOutputStreamImplWindows::CSimpleOutputStreamImplWindows(const wchar_t* filename, bool overwriteExisting)
: handle(INVALID_HANDLE_VALUE)
Expand Down
40 changes: 0 additions & 40 deletions Src/libCZI/StreamImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@
#include <Windows.h>
#endif

#if 0
/// <summary> A simplistic stream implementation (based on C-runtime fopen). Note that this implementation is NOT thread-safe.</summary>
class CSimpleStreamImpl : public libCZI::IStream
{
private:
FILE* fp;
public:
CSimpleStreamImpl() = delete;
explicit CSimpleStreamImpl(const wchar_t* filename);
~CSimpleStreamImpl() override;
public: // interface libCZI::IStream
void Read(std::uint64_t offset, void* pv, std::uint64_t size, std::uint64_t* ptrBytesRead) override;
};
#endif

/// <summary> A simplistic output-stream implementation (based on C-runtime fopen). Note that this implementation is NOT thread-safe.</summary>
class CSimpleOutputStreamStreams : public libCZI::IOutputStream
{
Expand Down Expand Up @@ -54,20 +39,6 @@ class CSimpleStreamImplCppStreams : public libCZI::IStream
};

#if LIBCZI_USE_PREADPWRITEBASED_STREAMIMPL
/*
/// <summary> An input-stream implementation (based on open and pread). This implementation is thread-safe.</summary>
class CStreamImplPread : public libCZI::IStream
{
private:
int fileDescriptor;
public:
CStreamImplPread() = delete;
explicit CStreamImplPread(const wchar_t* filename);
~CStreamImplPread() override;
public: // interface libCZI::IStream
void Read(std::uint64_t offset, void* pv, std::uint64_t size, std::uint64_t* ptrBytesRead) override;
};
*/

/// <summary> An output-stream implementation (based on open and pwrite). This implementation is thread-safe.</summary>
class COutputStreamImplPwrite : public libCZI::IOutputStream
Expand All @@ -84,17 +55,6 @@ class COutputStreamImplPwrite : public libCZI::IOutputStream
#endif

#if defined(_WIN32)
//class CSimpleStreamImplWindows : public libCZI::IStream
//{
//private:
// HANDLE handle;
//public:
// CSimpleStreamImplWindows() = delete;
// explicit CSimpleStreamImplWindows(const wchar_t* filename);
// ~CSimpleStreamImplWindows() override;
//public: // interface libCZI::IStream
// void Read(std::uint64_t offset, void* pv, std::uint64_t size, std::uint64_t* ptrBytesRead) override;
//};

class CSimpleOutputStreamImplWindows : public libCZI::IOutputStream
{
Expand Down
8 changes: 7 additions & 1 deletion Src/libCZI/StreamsLib/curlhttpinputstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ CurlHttpInputStream::CurlHttpInputStream(const std::string& url, const std::map<
ThrowIfCurlSetOptError(return_code, "CURLOPT_NOPROGRESS");

return_code = curl_easy_setopt(up_curl_handle.get(), CURLOPT_TCP_KEEPALIVE, 1L);
//ThrowIfCurlSetOptError(return_code, "CURLOPT_TCP_KEEPALIVE");
ThrowIfCurlSetOptError(return_code, "CURLOPT_TCP_KEEPALIVE");

// set the "write function" - to this function curl will deliver the payload data
return_code = curl_easy_setopt(up_curl_handle.get(), CURLOPT_WRITEFUNCTION, CurlHttpInputStream::WriteData);
ThrowIfCurlSetOptError(return_code, "CURLOPT_WRITEFUNCTION");

Expand Down Expand Up @@ -148,6 +149,11 @@ CurlHttpInputStream::CurlHttpInputStream(const std::string& url, const std::map<
{
std::lock_guard<std::mutex> lck(this->request_mutex_);

// TODO(JBL): We may be able to use a "header-function" (https://curl.se/libcurl/c/CURLOPT_HEADERFUNCTION.html) in order to find out
// whether the server accepted out "Range-Request". According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests,
// we can expect to have a line "something like 'Accept-Ranges: bytes'" in the response header with a server that supports range
// requests (and a line 'Accept-Ranges: none') would tell us explicitely that range requests are *not* supported.

// https://curl.se/libcurl/c/CURLOPT_RANGE.html states that the range may be ignored by the server, and it would then
// deliver the entire document. And, it says, that there is no way to detect that the range was ignored. We take precautions
// that we only accept as many bytes as we have requested, and otherwise the "curl_easy_perform" should report an error.
Expand Down
4 changes: 3 additions & 1 deletion Src/libCZI/StreamsLib/preadfileinputstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#if LIBCZI_USE_PREADPWRITEBASED_STREAMIMPL
#include "../libCZI.h"

/// <summary> An input-stream implementation (based on open and pread). This implementation is thread-safe.</summary>
/// Implementation of the IStream-interface for files based on the Unix-specific pread-API.
/// It leverages the pread function passing in an offset, thus allowing for concurrent
/// access without locking.
class PreadFileInputStream : public libCZI::IStream
{
private:
Expand Down
Loading

0 comments on commit d760ab2

Please sign in to comment.