From ecc21a2cfc07540900bc9a19bf01e7bb5c80ab2e Mon Sep 17 00:00:00 2001 From: akallabeth Date: Fri, 13 Oct 2023 12:24:05 +0200 Subject: [PATCH] [settings] add freerdp_settings_copy_item convenience function to copy a value of opaque type from one settings struct to another, cleaning up possible existing data first --- include/freerdp/settings.h | 14 +++++++++ libfreerdp/common/settings_str.c | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/include/freerdp/settings.h b/include/freerdp/settings.h index a197fdeeb..b5731740b 100644 --- a/include/freerdp/settings.h +++ b/include/freerdp/settings.h @@ -1755,6 +1755,20 @@ extern "C" */ FREERDP_API BOOL freerdp_settings_copy(rdpSettings* dst, const rdpSettings* src); + /** \brief copies one setting identified by \b id from \b src to \b dst + * + * The function frees up all allocated data in \b dst before copying the data from \b src + * + * \param dst A pointer for the settings to copy data to. May be NULL (fails copy) + * \param src A pointer to the settings to copy. May be NULL (fails copy) + * \param id The settings identifier to copy + * + * \return \b TRUE for success, \b FALSE for failure. + */ + + FREERDP_API BOOL freerdp_settings_copy_item(rdpSettings* dst, const rdpSettings* src, + size_t id); + /** \brief Free a settings struct with all data in it * * \param settings A pointer to the settings to free, May be NULL diff --git a/libfreerdp/common/settings_str.c b/libfreerdp/common/settings_str.c index a67ae5ef8..fe30357bc 100644 --- a/libfreerdp/common/settings_str.c +++ b/libfreerdp/common/settings_str.c @@ -997,3 +997,57 @@ const char* freerdp_settings_get_name_for_key(size_t key) } return NULL; } + +BOOL freerdp_settings_copy_item(rdpSettings* dst, const rdpSettings* src, size_t id) +{ + WINPR_ASSERT(dst); + WINPR_ASSERT(src); + + const SSIZE_T key = freerdp_settings_get_type_for_key(id); + switch (key) + { + case FREERDP_SETTINGS_TYPE_BOOL: + { + const BOOL val = freerdp_settings_get_bool(src, id); + return freerdp_settings_set_bool(dst, id, val); + } + case FREERDP_SETTINGS_TYPE_INT16: + { + const INT16 val = freerdp_settings_get_int16(src, id); + return freerdp_settings_set_int16(dst, id, val); + } + case FREERDP_SETTINGS_TYPE_INT32: + { + const INT32 val = freerdp_settings_get_int32(src, id); + return freerdp_settings_set_int32(dst, id, val); + } + case FREERDP_SETTINGS_TYPE_INT64: + { + const INT64 val = freerdp_settings_get_int64(src, id); + return freerdp_settings_set_int64(dst, id, val); + } + case FREERDP_SETTINGS_TYPE_UINT16: + { + const UINT16 val = freerdp_settings_get_uint16(src, id); + return freerdp_settings_set_uint16(dst, id, val); + } + case FREERDP_SETTINGS_TYPE_UINT32: + { + const UINT32 val = freerdp_settings_get_uint32(src, id); + return freerdp_settings_set_uint32(dst, id, val); + } + case FREERDP_SETTINGS_TYPE_UINT64: + { + const UINT64 val = freerdp_settings_get_uint64(src, id); + return freerdp_settings_set_uint64(dst, id, val); + } + case FREERDP_SETTINGS_TYPE_STRING: + { + const char* val = freerdp_settings_get_string(src, id); + return freerdp_settings_set_string(dst, id, val); + } + case FREERDP_SETTINGS_TYPE_POINTER: + default: + return FALSE; + } +}