From 8110b4a406bbec83f619084e4e038157d48b384f Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 28 Mar 2024 22:46:44 +0800 Subject: [PATCH] tag_c set properties use utf-8; add 2 func taglib_property_keys_strings, taglib_property_get_strings which return value with len facilitate decoding to multiple strings by Rust --- bindings/c/tag_c.cpp | 35 ++++++++++++++++++++++++++--------- bindings/c/tag_c.h | 21 +++++++++++++++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/bindings/c/tag_c.cpp b/bindings/c/tag_c.cpp index 764079f2..b1803981 100644 --- a/bindings/c/tag_c.cpp +++ b/bindings/c/tag_c.cpp @@ -410,14 +410,14 @@ void _taglib_property_set(TagLib_File *file, const char* prop, const char* value if(value) { auto property = map.find(prop); if(property == map.end()) { - map.insert(prop, StringList(value)); + map.insert(prop, StringList(String(value, String::UTF8))); } else { if(append) { - property->second.append(value); + property->second.append(String(value, String::UTF8)); } else { - property->second = StringList(value); + property->second = StringList(String(value, String::UTF8)); } } } @@ -440,15 +440,23 @@ void taglib_property_set_append(TagLib_File *file, const char *prop, const char _taglib_property_set(file, prop, value, true); } +const TagLib_Strings EMPTY_STRINGS = { 0, NULL }; + char** taglib_property_keys(const TagLib_File *file) { + TagLib_Strings s = taglib_property_keys_strings(file); + return s.value; +} + +TagLib_Strings taglib_property_keys_strings(const TagLib_File *file) { if(file == NULL) - return NULL; + return EMPTY_STRINGS; const PropertyMap map = reinterpret_cast(file)->properties(); if(map.isEmpty()) - return NULL; + return EMPTY_STRINGS; + unsigned int count = map.size(); auto props = static_cast(malloc(sizeof(char *) * (map.size() + 1))); char **pp = props; @@ -457,20 +465,28 @@ char** taglib_property_keys(const TagLib_File *file) } *pp = NULL; - return props; + TagLib_Strings result = { count, props }; + return result; } char **taglib_property_get(const TagLib_File *file, const char *prop) +{ + TagLib_Strings s = taglib_property_get_strings(file, prop); + return s.value; +} + +TagLib_Strings taglib_property_get_strings(const TagLib_File *file, const char *prop) { if(file == NULL || prop == NULL) - return NULL; + return EMPTY_STRINGS; const PropertyMap map = reinterpret_cast(file)->properties(); auto property = map.find(prop); if(property == map.end()) - return NULL; + return EMPTY_STRINGS; + unsigned int count = property->second.size(); auto props = static_cast(malloc(sizeof(char *) * (property->second.size() + 1))); char **pp = props; @@ -479,7 +495,8 @@ char **taglib_property_get(const TagLib_File *file, const char *prop) } *pp = NULL; - return props; + TagLib_Strings result = { count, props }; + return result; } void taglib_property_free(char **props) diff --git a/bindings/c/tag_c.h b/bindings/c/tag_c.h index f3f4e315..7ce79e61 100644 --- a/bindings/c/tag_c.h +++ b/bindings/c/tag_c.h @@ -356,6 +356,19 @@ TAGLIB_C_EXPORT void taglib_property_set_append(TagLib_File *file, const char *p */ TAGLIB_C_EXPORT char** taglib_property_keys(const TagLib_File *file); +typedef struct { + unsigned int len; + char **value; +} TagLib_Strings; + +/*! + * Get the keys of the property map. + * + * It same as taglib_property_keys, but the return value with lenth, for Rust convert it to UTF8 String. + * It must be freed by the client using taglib_property_free(). + */ +TAGLIB_C_EXPORT TagLib_Strings taglib_property_keys_strings(const TagLib_File *file); + /*! * Get value(s) of property \a prop. * @@ -364,6 +377,14 @@ TAGLIB_C_EXPORT char** taglib_property_keys(const TagLib_File *file); */ TAGLIB_C_EXPORT char** taglib_property_get(const TagLib_File *file, const char *prop); +/*! + * Get value(s) of property \a prop. + * + * It same as taglib_property_get, but the return value with lenth, for Rust convert it to UTF8 String. + * It must be freed by the client using taglib_property_free(). + */ +TAGLIB_C_EXPORT TagLib_Strings taglib_property_get_strings(const TagLib_File *file, const char *prop); + /*! * Frees the NULL terminated array \a props and the C-strings it contains. */