Skip to content

Commit

Permalink
[core,client] replace cJSON with WinPR wrapper
Browse files Browse the repository at this point in the history
use the new WinPR JSON wrapper API
  • Loading branch information
akallabeth committed May 14, 2024
1 parent e84e7da commit 4f0da96
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 207 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,6 @@ endif()

find_feature(PCSC ${PCSC_FEATURE_TYPE} ${PCSC_FEATURE_PURPOSE} ${PCSC_FEATURE_DESCRIPTION})

find_package(cJSON)
option(WITH_AAD "Compile with support for Azure AD authentication" ${cJSON_FOUND})

if (WITH_DSP_FFMPEG OR WITH_VIDEO_FFMPEG OR WITH_FFMPEG)
set(FFMPEG_FEATURE_TYPE "REQUIRED" )
endif()
Expand Down Expand Up @@ -730,6 +727,8 @@ else()
include_directories(${WinPR_INCLUDE_DIR})
endif()

option(WITH_AAD "Compile with support for Azure AD authentication" ${WITH_WINPR_JSON})

# Include directories
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
Expand Down
6 changes: 0 additions & 6 deletions client/SDL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,8 @@ endif()
find_package(SDL2 REQUIRED COMPONENTS)
include_directories(${SDL2_INCLUDE_DIR})
include_directories(${SDL2_INCLUDE_DIRS})
find_package(cJSON)

set(LIBS "")
if (cJSON_FOUND)
include_directories(${CJSON_INCLUDE_DIRS})
list(APPEND LIBS ${CJSON_LIBRARIES})
add_compile_definitions(CJSON_FOUND)
endif()

find_package(Threads REQUIRED)

Expand Down
3 changes: 2 additions & 1 deletion client/SDL/sdl_freerdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <freerdp/channels/channels.h>

#include <winpr/crt.h>
#include <winpr/config.h>
#include <winpr/assert.h>
#include <winpr/synch.h>
#include <freerdp/log.h>
Expand Down Expand Up @@ -1553,7 +1554,7 @@ static void SDLCALL winpr_LogOutputFunction(void* userdata, int category, SDL_Lo

static void print_config_file_help()
{
#if defined(CJSON_FOUND)
#if defined(WITH_WINPR_JSON)
std::cout << "CONFIGURATION FILE" << std::endl;
std::cout << std::endl;
std::cout << " The SDL client supports some user defined configuration options." << std::endl;
Expand Down
47 changes: 23 additions & 24 deletions client/SDL/sdl_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ namespace fs = std::experimental::filesystem;
#include <SDL.h>

#include <winpr/path.h>
#include <winpr/config.h>
#include <freerdp/version.h>
#if defined(CJSON_FOUND)
#include <cjson/cJSON.h>
#endif
#include <winpr/json.h>

const char* sdl_event_type_str(Uint32 type)
{
Expand Down Expand Up @@ -355,33 +354,33 @@ std::string sdl_window_event_str(Uint8 ev)
}
}

#if defined(CJSON_FOUND)
using cJSONPtr = std::unique_ptr<cJSON, decltype(&cJSON_Delete)>;
#if defined(WINPR_JSON_FOUND)
using WINPR_JSONPtr = std::unique_ptr<WINPR_JSON, decltype(&WINPR_JSON_Delete)>;

static cJSONPtr get()
static WINPR_JSONPtr get()
{
auto config = sdl_get_pref_file();

std::ifstream ifs(config);
std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
return { cJSON_ParseWithLength(content.c_str(), content.size()), cJSON_Delete };
return { WINPR_JSON_ParseWithLength(content.c_str(), content.size()), WINPR_JSON_Delete };
}

static cJSON* get_item(const std::string& key)
static WINPR_JSON* get_item(const std::string& key)
{
static cJSONPtr config{ nullptr, cJSON_Delete };
static WINPR_JSONPtr config{ nullptr, WINPR_JSON_Delete };
if (!config)
config = get();
if (!config)
return nullptr;
return cJSON_GetObjectItem(config.get(), key.c_str());
return WINPR_JSON_GetObjectItem(config.get(), key.c_str());
}

static std::string item_to_str(cJSON* item, const std::string& fallback = "")
static std::string item_to_str(WINPR_JSON* item, const std::string& fallback = "")
{
if (!item || !cJSON_IsString(item))
if (!item || !WINPR_JSON_IsString(item))
return fallback;
auto str = cJSON_GetStringValue(item);
auto str = WINPR_JSON_GetStringValue(item);
if (!str)
return {};
return str;
Expand All @@ -390,7 +389,7 @@ static std::string item_to_str(cJSON* item, const std::string& fallback = "")

std::string sdl_get_pref_string(const std::string& key, const std::string& fallback)
{
#if defined(CJSON_FOUND)
#if defined(WINPR_JSON_FOUND)
auto item = get_item(key);
return item_to_str(item, fallback);
#else
Expand All @@ -400,23 +399,23 @@ std::string sdl_get_pref_string(const std::string& key, const std::string& fallb

bool sdl_get_pref_bool(const std::string& key, bool fallback)
{
#if defined(CJSON_FOUND)
#if defined(WINPR_JSON_FOUND)
auto item = get_item(key);
if (!item || !cJSON_IsBool(item))
if (!item || !WINPR_JSON_IsBool(item))
return fallback;
return cJSON_IsTrue(item);
return WINPR_JSON_IsTrue(item);
#else
return fallback;
#endif
}

int64_t sdl_get_pref_int(const std::string& key, int64_t fallback)
{
#if defined(CJSON_FOUND)
#if defined(WINPR_JSON_FOUND)
auto item = get_item(key);
if (!item || !cJSON_IsNumber(item))
if (!item || !WINPR_JSON_IsNumber(item))
return fallback;
auto val = cJSON_GetNumberValue(item);
auto val = WINPR_JSON_GetNumberValue(item);
return static_cast<int64_t>(val);
#else
return fallback;
Expand All @@ -426,15 +425,15 @@ int64_t sdl_get_pref_int(const std::string& key, int64_t fallback)
std::vector<std::string> sdl_get_pref_array(const std::string& key,
const std::vector<std::string>& fallback)
{
#if defined(CJSON_FOUND)
#if defined(WINPR_JSON_FOUND)
auto item = get_item(key);
if (!item || !cJSON_IsArray(item))
if (!item || !WINPR_JSON_IsArray(item))
return fallback;

std::vector<std::string> values;
for (int x = 0; x < cJSON_GetArraySize(item); x++)
for (int x = 0; x < WINPR_JSON_GetArraySize(item); x++)
{
auto cur = cJSON_GetArrayItem(item, x);
auto cur = WINPR_JSON_GetArrayItem(item, x);
values.push_back(item_to_str(cur));
}

Expand Down
25 changes: 4 additions & 21 deletions libfreerdp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,9 @@ if(FAAC_FOUND)
endif()

if (WITH_AAD)
if (NOT cJSON_FOUND)
find_package(PkgConfig REQUIRED)
pkg_check_modules(CJSON libcjson)
endif()
if (NOT CJSON_LIBRARIES OR NOT CJSON_INCLUDE_DIRS)
find_path(CJSON_INCLUDE_DIRS
NAMES cjson/cJSON.h
REQUIRED
)
find_library(CJSON_LIBRARIES
NAMES cjson
REQUIRED
)
endif()

freerdp_library_add(${CJSON_LIBRARIES})
include_directories(${CJSON_INCLUDE_DIRS})
if (NOT WITH_WINPR_JSON)
message(FATAL_ERROR "Trying to build -DWITH_AAD=ON but WITH_WINPR_JSON is not defined")
endif()
endif()

if(WITH_NEON)
Expand Down Expand Up @@ -424,10 +410,7 @@ set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "FreeRDP/libfreerdp")

include(pkg-config-install-prefix)
set(FREERDP_REQUIRES_PRIVATE "")
if(cJSON_FOUND)
string(APPEND FREERDP_REQUIRES_PRIVATE " libcjson")
list(APPEND FREERDP_PC_PRIVATE_LIBS "-lcjson")
endif()

if(WITH_SMARTCARD_EMULATE)
string(APPEND FREERDP_REQUIRES_PRIVATE " zlib")
list(APPEND FREERDP_PC_PRIVATE_LIBS "-lz")
Expand Down
4 changes: 1 addition & 3 deletions libfreerdp/FreeRDPConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
include(CMakeFindDependencyMacro)
find_dependency(WinPR @FREERDP_VERSION@)
if("@cJSON_FOUND@" AND NOT "@BUILD_SHARED_LIBS@")
find_dependency(cJSON)
endif()

if("@WITH_SMARTCARD_EMULATE@" AND NOT "@BUILD_SHARED_LIBS@")
find_dependency(ZLIB)
endif()
Expand Down
Loading

0 comments on commit 4f0da96

Please sign in to comment.