Skip to content

Commit

Permalink
export "list of propbag parameters" for streams-property-bag (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptahmose authored Jan 8, 2024
1 parent 5bca15f commit 5e5a1fc
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 29 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.3
VERSION 0.58.0
HOMEPAGE_URL "https://github.com/ZEISS/libczi"
DESCRIPTION "libCZI is an Open Source Cross-Platform C++ library to read and write CZI")

Expand Down
37 changes: 10 additions & 27 deletions Src/CZICmd/cmdlineoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ bool CCmdLineOptions::IsLogLevelEnabled(int level) const
level = 31;
}

return (this->enabledOutputLevels & (1 << level)) ? true : false;;
return (this->enabledOutputLevels & (1 << level)) ? true : false;
}

void CCmdLineOptions::SetOutputFilename(const std::wstring& s)
Expand Down Expand Up @@ -2265,25 +2265,8 @@ void CCmdLineOptions::PrintHelpStreamsObjects()
// Here we parse the JSON-formatted string that contains the property bag for the input stream and
// construct a map<int, libCZI::StreamsFactory::Property> from it.

static constexpr struct
{
const char* name;
int stream_property_id;
libCZI::StreamsFactory::Property::Type property_type;
}
kKeyStringToId[] =
{
{"CurlHttp_Proxy", libCZI::StreamsFactory::StreamProperties::kCurlHttp_Proxy, libCZI::StreamsFactory::Property::Type::String},
{"CurlHttp_UserAgent", libCZI::StreamsFactory::StreamProperties::kCurlHttp_UserAgent, libCZI::StreamsFactory::Property::Type::String},
{"CurlHttp_Timeout", libCZI::StreamsFactory::StreamProperties::kCurlHttp_Timeout, libCZI::StreamsFactory::Property::Type::Int32},
{"CurlHttp_ConnectTimeout", libCZI::StreamsFactory::StreamProperties::kCurlHttp_ConnectTimeout, libCZI::StreamsFactory::Property::Type::Int32},
{"CurlHttp_Xoauth2Bearer", libCZI::StreamsFactory::StreamProperties::kCurlHttp_Xoauth2Bearer, libCZI::StreamsFactory::Property::Type::String},
{"CurlHttp_Cookie", libCZI::StreamsFactory::StreamProperties::kCurlHttp_Cookie, libCZI::StreamsFactory::Property::Type::String},
{"CurlHttp_SslVerifyPeer", libCZI::StreamsFactory::StreamProperties::kCurlHttp_SslVerifyPeer, libCZI::StreamsFactory::Property::Type::Boolean},
{"CurlHttp_SslVerifyHost", libCZI::StreamsFactory::StreamProperties::kCurlHttp_SslVerifyHost, libCZI::StreamsFactory::Property::Type::Boolean},
{"CurlHttp_FollowLocation", libCZI::StreamsFactory::StreamProperties::kCurlHttp_FollowLocation, libCZI::StreamsFactory::Property::Type::Boolean},
{"CurlHttp_MaxRedirs", libCZI::StreamsFactory::StreamProperties::kCurlHttp_MaxRedirs, libCZI::StreamsFactory::Property::Type::Int32},
};
int property_info_count;
const libCZI::StreamsFactory::StreamPropertyBagPropertyInfo* property_infos = libCZI::StreamsFactory::GetStreamPropertyBagPropertyInfo(&property_info_count);

rapidjson::Document document;
document.Parse(s.c_str());
Expand All @@ -2301,9 +2284,9 @@ void CCmdLineOptions::PrintHelpStreamsObjects()

string name = itr->name.GetString();
size_t index_of_key = numeric_limits<size_t>::max();
for (size_t i = 0; i < sizeof(kKeyStringToId) / sizeof(kKeyStringToId[0]); ++i)
for (size_t i = 0; i < static_cast<size_t>(property_info_count); ++i)
{
if (name == kKeyStringToId[i].name)
if (name == property_infos[i].property_name)
{
index_of_key = i;
break;
Expand All @@ -2315,7 +2298,7 @@ void CCmdLineOptions::PrintHelpStreamsObjects()
return false;
}

switch (kKeyStringToId[index_of_key].property_type)
switch (property_infos[index_of_key].property_type)
{
case libCZI::StreamsFactory::Property::Type::String:
if (!itr->value.IsString())
Expand All @@ -2325,7 +2308,7 @@ void CCmdLineOptions::PrintHelpStreamsObjects()

if (property_bag != nullptr)
{
property_bag->insert(std::make_pair(kKeyStringToId[index_of_key].stream_property_id, libCZI::StreamsFactory::Property(itr->value.GetString())));
property_bag->insert(std::make_pair(property_infos[index_of_key].property_id, libCZI::StreamsFactory::Property(itr->value.GetString())));
}

break;
Expand All @@ -2337,7 +2320,7 @@ void CCmdLineOptions::PrintHelpStreamsObjects()

if (property_bag != nullptr)
{
property_bag->insert(std::make_pair(kKeyStringToId[index_of_key].stream_property_id, libCZI::StreamsFactory::Property(itr->value.GetBool())));
property_bag->insert(std::make_pair(property_infos[index_of_key].property_id, libCZI::StreamsFactory::Property(itr->value.GetBool())));
}

break;
Expand All @@ -2349,12 +2332,12 @@ void CCmdLineOptions::PrintHelpStreamsObjects()

if (property_bag != nullptr)
{
property_bag->insert(std::make_pair(kKeyStringToId[index_of_key].stream_property_id, libCZI::StreamsFactory::Property(itr->value.GetInt())));
property_bag->insert(std::make_pair(property_infos[index_of_key].property_id, libCZI::StreamsFactory::Property(itr->value.GetInt())));
}

break;
default:
// this actually indicates an internal error - the table kKeyStringToId contains a not yet implemented property type
// this actually indicates an internal error - the table property_infos contains a not yet implemented property type
return false;
}
}
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 @@ -15,4 +15,5 @@ version history {#version_history}
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.3 | [91](https://github.com/ZEISS/libczi/pull/91) | improve error-message
0.57.3 | [91](https://github.com/ZEISS/libczi/pull/91) | improve error-message
0.58.0 | [92](https://github.com/ZEISS/libczi/pull/92) | export a list with properties for streams-property-bag
31 changes: 31 additions & 0 deletions Src/libCZI/StreamsLib/streamsFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <libCZI_Config.h>
#include <memory>
#include <mutex>
#include <assert.h>
#include "curlhttpinputstream.h"
#include "windowsfileinputstream.h"
#include "simplefileinputstream.h"
Expand Down Expand Up @@ -92,6 +93,36 @@ void libCZI::StreamsFactory::Initialize()
});
}

/*static*/const libCZI::StreamsFactory::StreamPropertyBagPropertyInfo* libCZI::StreamsFactory::GetStreamPropertyBagPropertyInfo(int* count)
{
static const StreamsFactory::StreamPropertyBagPropertyInfo kStreamPropertyBagPropertyInfo[] =
{
#if LIBCZI_CURL_BASED_STREAM_AVAILABLE
{"CurlHttp_Proxy", StreamsFactory::StreamProperties::kCurlHttp_Proxy, StreamsFactory::Property::Type::String},
{"CurlHttp_UserAgent", StreamsFactory::StreamProperties::kCurlHttp_UserAgent, StreamsFactory::Property::Type::String},
{"CurlHttp_Timeout", StreamsFactory::StreamProperties::kCurlHttp_Timeout, StreamsFactory::Property::Type::Int32},
{"CurlHttp_ConnectTimeout", StreamsFactory::StreamProperties::kCurlHttp_ConnectTimeout, StreamsFactory::Property::Type::Int32},
{"CurlHttp_Xoauth2Bearer", StreamsFactory::StreamProperties::kCurlHttp_Xoauth2Bearer, StreamsFactory::Property::Type::String},
{"CurlHttp_Cookie", StreamsFactory::StreamProperties::kCurlHttp_Cookie, StreamsFactory::Property::Type::String},
{"CurlHttp_SslVerifyPeer", StreamsFactory::StreamProperties::kCurlHttp_SslVerifyPeer, StreamsFactory::Property::Type::Boolean},
{"CurlHttp_SslVerifyHost", StreamsFactory::StreamProperties::kCurlHttp_SslVerifyHost, StreamsFactory::Property::Type::Boolean},
{"CurlHttp_FollowLocation", StreamsFactory::StreamProperties::kCurlHttp_FollowLocation, StreamsFactory::Property::Type::Boolean},
{"CurlHttp_MaxRedirs", StreamsFactory::StreamProperties::kCurlHttp_MaxRedirs, StreamsFactory::Property::Type::Int32},
{"CurlHttp_CaInfo", StreamsFactory::StreamProperties::kCurlHttp_CaInfo, StreamsFactory::Property::Type::String},
{"CurlHttp_CaInfoBlob", StreamsFactory::StreamProperties::kCurlHttp_CaInfoBlob, StreamsFactory::Property::Type::String},
#endif
{nullptr, 0, StreamsFactory::Property::Type::Invalid},
};

if (count != nullptr)
{
static_assert(sizeof(kStreamPropertyBagPropertyInfo) / sizeof(kStreamPropertyBagPropertyInfo[0]) > 0, "kStreamPropertyBagPropertyInfo must contain at least one element.");
*count = sizeof(kStreamPropertyBagPropertyInfo) / sizeof(kStreamPropertyBagPropertyInfo[0]) - 1;
}

return kStreamPropertyBagPropertyInfo;
}

bool libCZI::StreamsFactory::GetStreamInfoForClass(int index, StreamClassInfo& stream_info)
{
if (index < 0 || index >= GetStreamClassesCount())
Expand Down
18 changes: 18 additions & 0 deletions Src/libCZI/libCZI_StreamsLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,24 @@ namespace libCZI
std::map<int, Property> property_bag; ///< A property-bag with options for creating the stream-object.
};

/// Information about a property for the property bag when creating a stream object.
struct StreamPropertyBagPropertyInfo
{
const char* property_name; ///< (Proposed) name of the property. This is a null-terminated static string. It is enum name with the initial "k" removed.
int property_id; ///< The numerical identifier for the property.
Property::Type property_type; ///< Type of the property.
};

/// Gets a (static) list of all properties which can be used for the property bag when creating a stream object.
/// This list is terminated by an entry with a null property_name. The list is static and will not change during
/// the lifetime of the application, the returned pointer is valid until the application terminates. It is
/// pointing to static memory and must not be freed.
///
/// \param [out] count If non-null, the number of valid elements (not including the terminal element) is put here.
///
/// \returns A pointer to an array containing property-bag information.
static const StreamPropertyBagPropertyInfo* GetStreamPropertyBagPropertyInfo(int* count);

/// Perform one-time initialization of the streams objects.
/// Some stream objects may require some one-time initialization for being operational (this is e.g. the case with
/// the libcurl-based ones). It is considered good practice to call this function before using any of the other methods.
Expand Down
23 changes: 23 additions & 0 deletions Src/libCZI_UnitTests/test_streamslib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,26 @@ TEST(StreamsLib, TestGetProperty)
}
}
}

TEST(StreamsLib, TestGetStreamPropertyBagPropertyInfo)
{
int property_infos_count = -1;
const auto property_infos = StreamsFactory::GetStreamPropertyBagPropertyInfo(&property_infos_count);

ASSERT_TRUE(property_infos != nullptr);
ASSERT_GE(property_infos_count, 0);

// now, check that the fields 'property_name' and 'property_id' are unique
for (int i = 0; i < property_infos_count; ++i)
{
const auto& info = property_infos[i];
for (int j = i + 1; j < property_infos_count; ++j)
{
const auto& info2 = property_infos[j];
EXPECT_FALSE(info.property_name == info2.property_name || info.property_id == info2.property_id);
}
}

// check that the list of properties is terminated with an empty entry
ASSERT_TRUE(property_infos[property_infos_count].property_name == nullptr);
}

0 comments on commit 5e5a1fc

Please sign in to comment.