Bug fix of String Convert 19/233219/2
authorhj kim <backto.kim@samsung.com>
Wed, 13 May 2020 06:01:22 +0000 (15:01 +0900)
committerhj kim <backto.kim@samsung.com>
Wed, 13 May 2020 06:02:29 +0000 (15:02 +0900)
String::toCString(true) convert code to UTF8. and String::toCString(false) convert to Latin.
We need UTF8 so "true" should be set to toCString().

Change-Id: Iae6071f4288e90de2068e026d3774e08ecda4518

include/metadata_editor_private.h
src/metadata_editor.cpp

index 47e2840..3889ab9 100755 (executable)
@@ -55,9 +55,6 @@ extern "C" {
 #define LOG_TAG "CAPI_MEDIA_METADATA_EDITOR"
 #define META_SAFE_FREE(src)      { if (src) {free(src); src = NULL; } }
 
-#define META_MAX_BUF_LEN 20
-
-
 #define metadata_editor_debug(fmt, arg...) do { \
                LOGD("" fmt "", ##arg);     \
        } while (0)
index 3cd51fe..80d3be3 100755 (executable)
@@ -192,30 +192,18 @@ static int __ID3_setTwixFrameByName(metadata_editor_s* _metadata, TagLib::ID3v1:
        return METADATA_EDITOR_ERROR_NONE;
 }
 
-// *** This function reads frames that exist only in ID3v2 tag *** //
-static int __ID3_getFrameByName(metadata_editor_s* _metadata, TagLib::ID3v2::Tag* tag2, const char* frameID, char** value) {
+static int __ID3_getFrameByName(metadata_editor_s *_metadata, TagLib::ID3v2::Tag *tag2, const char *frameID, char **value)
+{
        int ret = METADATA_EDITOR_ERROR_NONE;
 
        ret = __check_metadata_get_parameter(_metadata, value);
        metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, ret, "fail to __check_metadata_get_parameter() [%d]", ret);
        metadata_editor_retvm_if(!frameID, METADATA_EDITOR_ERROR_INVALID_PARAMETER, "Invalid frameID");
-
-       *value = NULL;
-
-       // Check if the frame is empty (nothing to read) or ID3v2 tag does not exist
        metadata_editor_retvm_if(!tag2 || tag2->frameListMap()[frameID].isEmpty(), METADATA_EDITOR_ERROR_NONE, "The frame %s does not exist", frameID);
 
        metadata_editor_info("The frame %s exists", frameID);
-       // This string is used to copy the value in the frame
-       TagLib::String str = tag2->frameListMap()[frameID][0]->toString();
-       bool isUTF = false;
-       if (!str.isLatin1()) isUTF = true;
-       metadata_editor_info("String is %sUTF", (isUTF ? "" : "not "));
-
-       uint length = strlen(str.toCString(isUTF));
-       metadata_editor_retvm_if(length == 0, METADATA_EDITOR_ERROR_NONE, "Empty string");
 
-       *value = strndup(str.toCString(isUTF), length);
+       *value = g_strdup(tag2->frameListMap()[frameID][0]->toString().toCString(true));
 
        return METADATA_EDITOR_ERROR_NONE;
 }
@@ -266,29 +254,23 @@ static int __ID3_getNumberOfPictures(metadata_editor_s* _metadata, TagLib::ID3v2
        return METADATA_EDITOR_ERROR_NONE;
 }
 
-// *** This function is used to receive unsynchronized lyrics from ID3v2 tag in file *** //
-// *** This frame differs from other string-type frames and uses UnsynchronizedLyricsFrame instead of TextIdentificationFrame *** //
-static int __ID3_getLyricsFrame(metadata_editor_s* _metadata, TagLib::ID3v2::Tag* tag2, char** value) {
+static int __ID3_getLyricsFrame(metadata_editor_s *_metadata, TagLib::ID3v2::Tag *tag2, char **value)
+{
        int ret = METADATA_EDITOR_ERROR_NONE;
 
        ret = __check_metadata_get_parameter(_metadata, value);
        metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, ret, "fail to __check_metadata_get_parameter() [%d]", ret);
        metadata_editor_retvm_if(!tag2, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Error. ID3v2 tag does not exist. Can not process further");
 
-       TagLib::ID3v2::FrameList lst = tag2->frameListMap()["USLT"];            // link to unsynchronized lyric frames in tag
-       // Check if frames exist in file
+       TagLib::ID3v2::FrameList lst = tag2->frameListMap()["USLT"];
        metadata_editor_retvm_if(lst.isEmpty(), METADATA_EDITOR_ERROR_NONE, "The frame USLT does not exist");
 
        metadata_editor_info("The frame USLT exists");
        TagLib::ID3v2::FrameList::Iterator it = lst.begin();
        auto frame = static_cast<TagLib::ID3v2::UnsynchronizedLyricsFrame*>(*it);
-       TagLib::String str = frame->text();
-       bool isUTF = false;
-       if (!str.isLatin1()) isUTF = true;
-       metadata_editor_info("String is %sUTF", (isUTF ? "" : "not "));
-       uint length = strlen(str.toCString(isUTF));
-       metadata_editor_retvm_if(length == 0, METADATA_EDITOR_ERROR_NONE, "Empty string");
-       *value = strndup(str.toCString(isUTF), length);
+
+       *value = g_strdup(frame->text().toCString(true));
+
        return METADATA_EDITOR_ERROR_NONE;
 }
 
@@ -366,7 +348,7 @@ static int __ID3_setLyricsFrame(metadata_editor_s* _metadata, TagLib::ID3v2::Tag
        return METADATA_EDITOR_ERROR_NONE;
 }
 
-static int __MP4_getStringItem(metadata_editor_s* _metadata, const char* itemname, char **value)
+static int __MP4_getStringItem(metadata_editor_s *_metadata, const char *itemname, char **value)
 {
        int ret = METADATA_EDITOR_ERROR_NONE;
 
@@ -377,27 +359,18 @@ static int __MP4_getStringItem(metadata_editor_s* _metadata, const char* itemnam
        auto tag = dynamic_cast<TagLib::MP4::Tag*>(_metadata->file->tag());
        metadata_editor_retvm_if(!tag, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Tag does not exist");
 
-       // Get map of items directly from tag and launch a search of specific item
        TagLib::MP4::ItemListMap& itemMap = tag->itemListMap();
        TagLib::MP4::ItemListMap::ConstIterator it = itemMap.find(itemname);
-       if (it != itemMap.end()) {                                                              // Item was found
-               TagLib::String str = it->second.toStringList()[0];                      // Get the first string in item
-               // Check the encoding of the string (1252 or not)
-               bool isUTF = false;
-               if (!str.isLatin1()) isUTF = true;
-               metadata_editor_info("String is %sUTF", (isUTF ? "" : "not "));
-               // Get the length of the string and check if it is empty or not
-               uint length = strlen(str.toCString(isUTF));
-               metadata_editor_retvm_if(length == 0, METADATA_EDITOR_ERROR_NONE, "Empty string");
-               *value = strndup(str.toCString(isUTF), length);
-               return METADATA_EDITOR_ERROR_NONE;
-       } else {                                                                                // Item was not found
+
+       if (it != itemMap.end())
+               *value = g_strdup(it->second.toStringList()[0].toCString(true));
+       else
                metadata_editor_info("No item <%s> in file", itemname);
-               return METADATA_EDITOR_ERROR_NONE;
-       }
+
+       return METADATA_EDITOR_ERROR_NONE;
 }
 
-static int __MP4_getIntegerItem(metadata_editor_s* _metadata, const char* itemname, char** value)
+static int __MP4_getIntegerItem(metadata_editor_s *_metadata, const char *itemname, char **value)
 {
        int ret = METADATA_EDITOR_ERROR_NONE;
 
@@ -408,22 +381,15 @@ static int __MP4_getIntegerItem(metadata_editor_s* _metadata, const char* itemna
        auto tag = dynamic_cast<TagLib::MP4::Tag*>(_metadata->file->tag());
        metadata_editor_retvm_if(!tag, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Tag does not exist");
 
-       // Get map of items directly from tag and launch a search of specific item
        TagLib::MP4::ItemListMap& itemMap = tag->itemListMap();
        TagLib::MP4::ItemListMap::ConstIterator it = itemMap.find(itemname);
-       if (it != itemMap.end()) {                                                              // Item was found
-               char buf[META_MAX_BUF_LEN] = {0, };
-               int num = it->second.toInt();                                           // Get integer value in item
-               snprintf(buf, META_MAX_BUF_LEN, "%u", num);                                             // Convert int into char[]
-               // Determine the length of created c-string and copy it into the output variable
-               int length = strlen(buf);
-               metadata_editor_retvm_if(length == 0, METADATA_EDITOR_ERROR_NONE, "Empty string");
-               *value = strndup(buf, length);
-               return METADATA_EDITOR_ERROR_NONE;
-       } else {                                                                                // Item was not found
+
+       if (it != itemMap.end())
+               *value = g_strdup_printf("%u", it->second.toInt());
+       else
                metadata_editor_info("No item <%s> in file", itemname);
-               return METADATA_EDITOR_ERROR_NONE;
-       }
+
+       return METADATA_EDITOR_ERROR_NONE;
 }
 
 static int __MP4_updateStringItem(metadata_editor_s* _metadata, const char* itemname, const char* value)
@@ -500,9 +466,10 @@ static int __MP4_getNumberOfPictures(metadata_editor_s* _metadata, char** value)
 
        return METADATA_EDITOR_ERROR_NONE;
 }
+
 #if 0
-// *** This function is used to extract string from Xiph Comment field *** //
-static int __xiph_getFieldValue(metadata_editor_s* _metadata, TagLib::Ogg::XiphComment* xtag, const char* fieldname, char** value) {
+static int __xiph_getFieldValue(metadata_editor_s *_metadata, TagLib::Ogg::XiphComment *xtag, const char *fieldname, char **value)
+{
        int ret = METADATA_EDITOR_ERROR_NONE;
 
        ret = __check_metadata_get_parameter(_metadata, value);
@@ -513,22 +480,12 @@ static int __xiph_getFieldValue(metadata_editor_s* _metadata, TagLib::Ogg::XiphC
        const TagLib::Ogg::FieldListMap& fieldMap = xtag->fieldListMap();
        TagLib::Ogg::FieldListMap::ConstIterator it = fieldMap.find(fieldname);
 
-       if ((xtag->contains(fieldname)) && (it != fieldMap.end())) {                    // Field was found
-               metadata_editor_info("Field %s was found. Extracting", fieldname);
-               TagLib::String str = it->second[0];                                     // Get the first string in xiph field
-               // Check the encoding of the string (1252 or not)
-               bool isUTF = false;
-               if (!str.isLatin1()) isUTF = true;
-               metadata_editor_info("String is %sUTF", (isUTF ? "" : "not "));
-               // Get the length of the string and check if it is empty or not
-               uint length = strlen(str.toCString(isUTF));
-               metadata_editor_retvm_if(length == 0, METADATA_EDITOR_ERROR_NONE, "Empty string");
-               *value = strndup(str.toCString(isUTF), length);
-               return METADATA_EDITOR_ERROR_NONE;
-       } else {                                                                                // Field was not found
+       if ((xtag->contains(fieldname)) && (it != fieldMap.end()))
+               *value = g_strdup(it->second[0].toCString(true));
+       else
                metadata_editor_info("No field %s in Xiph Comment", fieldname);
-               return METADATA_EDITOR_ERROR_NONE;
-       }
+
+       return METADATA_EDITOR_ERROR_NONE;
 }
 
 // *** This function is used to write string into Xiph Comment fields *** //