Fix function to get extension 81/276981/6
authorminje.ahn <minje.ahn@samsung.com>
Wed, 29 Jun 2022 01:07:14 +0000 (10:07 +0900)
committerMinje ahn <minje.ahn@samsung.com>
Wed, 29 Jun 2022 07:49:14 +0000 (07:49 +0000)
Use Taglib::String instead of char.

Change-Id: I386d9612298465ad93b148dabf4bcd324c6f5052
Signed-off-by: minje.ahn <minje.ahn@samsung.com>
src/metadata_editor.cpp

index c2d1612..48a2310 100755 (executable)
@@ -65,45 +65,33 @@ typedef struct {
        metadata_editor_format_e format;
 } metadata_editor_s;
 
-static bool __metadata_editor_get_file_ext(const char *file_path, char *file_ext, int max_len)
+static String __get_file_ext(const char *file_path)
 {
-       unsigned int i = 0;
-       unsigned int path_len = strlen(file_path);
-
-       for (i = path_len; i > 0; i--) {
-               if ((file_path[i] == '.') && (i < path_len)) {
-                       strncpy(file_ext, &file_path[i + 1], max_len);
-                       return true;
-               }
-
-               /* meet the dir. no ext */
-               if (file_path[i] == '/')
-                       return false;
-       }
-
-       return false;
+       String s = String(file_path, String::UTF8);
+       const int pos = s.rfind(".");
+       if (pos != -1)
+               return s.substr(pos + 1).upper();
+       return "";
 }
 
 static metadata_editor_format_e __metadata_editor_get_file_type(const char *path)
 {
        char mimetype[EXT_LEN] = {0, };
-       char ext[EXT_LEN] = { 0 };
 
        /* get content type and mime type from file. */
        if (aul_get_mime_from_file(path, mimetype, sizeof(mimetype)) < 0) {
                metadata_editor_debug("aul_get_mime_from_file fail.. Now trying to get type by extension");
 
-               metadata_editor_retvm_if(!__metadata_editor_get_file_ext(path, ext, EXT_LEN), METADATA_EDITOR_FORMAT_NOTYPE, "__metadata_editor_get_file_ext failed");
-
-               if (strcasecmp(ext, "MP3") == 0)
+               String ext = __get_file_ext(path);
+               if (ext == "MP3")
                        return METADATA_EDITOR_FORMAT_MP3;
-               else if (strcasecmp(ext, "MP4") == 0)
+               else if (ext == "MP4")
                        return METADATA_EDITOR_FORMAT_MP4;
-               else if (strcasecmp(ext, "FLAC") == 0)
+               else if (ext == "FLAC")
                        return METADATA_EDITOR_FORMAT_FLAC;
-               else if (strcasecmp(ext, "WAV") == 0)
+               else if (ext == "WAV")
                        return METADATA_EDITOR_FORMAT_WAV;
-               else if (strcasecmp(ext, "OGG") == 0)
+               else if (ext == "OGG")
                        return METADATA_EDITOR_FORMAT_OGG_VORBIS;
                else
                        return METADATA_EDITOR_FORMAT_NOTYPE;
@@ -130,22 +118,18 @@ static metadata_editor_format_e __metadata_editor_get_file_type(const char *path
 
 static int __metadata_editor_get_picture_type(const char *path, char **type)
 {
-       int ret = 0;
        char mimetype[EXT_LEN] = {0, };
-       char ext[EXT_LEN] = {0, };
 
        /* get content type and mime type from file. */
        if (aul_get_mime_from_file(path, mimetype, sizeof(mimetype)) < 0) {
                metadata_editor_debug("aul_get_mime_from_file fail.. Now trying to get type by extension");
 
-               ret = __metadata_editor_get_file_ext(path, ext, EXT_LEN);
-               metadata_editor_retvm_if(ret < 0, METADATA_EDITOR_ERROR_OPERATION_FAILED, "__metadata_editor_get_file_ext failed");
-
-               if (strcasecmp(ext, "JPG") == 0 || strcasecmp(ext, "JPEG") == 0) {
+               String ext = __get_file_ext(path);
+               if (ext == "JPG" || ext == "JPEG") {
                        *type = g_strdup(MIME_TYPE_JPEG);
                        return METADATA_EDITOR_ERROR_NONE;
-               } else if (strcasecmp(ext, "PNG") == 0) {
-                       *type = g_strdup(MIME_TYPE_JPEG);
+               } else if (ext == "PNG") {
+                       *type = g_strdup(MIME_TYPE_PNG);
                        return METADATA_EDITOR_ERROR_NONE;
                } else {
                        return METADATA_EDITOR_ERROR_NOT_SUPPORTED;
@@ -173,8 +157,7 @@ static int __metadata_editor_get_picture_info(const char *path, char **picture,
        char *mime_type = NULL;
 
        ret = __metadata_editor_get_picture_type(path, &mime_type);
-       if (ret != METADATA_EDITOR_ERROR_NONE)
-               return ret;
+       metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, ret, "__metadata_editor_get_picture_type failed");
 
        if (!g_file_get_contents(path, picture, size, &error)) {
                metadata_editor_error("Unable to read [%s], error [%s]", path, error->message);
@@ -183,7 +166,7 @@ static int __metadata_editor_get_picture_info(const char *path, char **picture,
                else
                        ret = METADATA_EDITOR_ERROR_PERMISSION_DENIED;
 
-               g_error_free (error);
+               g_error_free(error);
                g_free(mime_type);
 
                return ret;
@@ -203,10 +186,10 @@ static int __check_file_validity(const char *path)
                if (errno == EACCES || errno == EPERM) {
                        metadata_editor_error("Permission denied");
                        return METADATA_EDITOR_ERROR_PERMISSION_DENIED;
-               } else {
-                       metadata_editor_error("Fail to open path");
-                       return METADATA_EDITOR_ERROR_FILE_EXISTS;
                }
+
+               metadata_editor_error("Fail to open path");
+               return METADATA_EDITOR_ERROR_FILE_EXISTS;
        }
 
        return METADATA_EDITOR_ERROR_NONE;
@@ -233,7 +216,6 @@ static char * __get_mime_type_from_cover_art(const MP4::CoverArt& cover_art)
 static int __is_valid_picture_index(bool is_empty, unsigned int list_size, int index)
 {
        metadata_editor_debug("list size [%d] picture index[%d]", list_size, index);
-
        metadata_editor_retvm_if(is_empty, METADATA_EDITOR_ERROR_INVALID_PARAMETER, "No picture");
        metadata_editor_retvm_if((index < 0) || (list_size <= (uint)index), METADATA_EDITOR_ERROR_INVALID_PARAMETER, "Out of range");
 
@@ -625,15 +607,13 @@ static int __get_from_property_map(PropertyMap tags, String key, char **value)
 
 class OggFileTypeResolver : public TagLib::FileRef::FileTypeResolver {
        TagLib::File *createFile(TagLib::FileName fileName, bool, AudioProperties::ReadStyle) const {
-               char ext[EXT_LEN] = {0, };
-               if (__metadata_editor_get_file_ext(fileName, ext, EXT_LEN)) {
-                       if (strcasecmp(ext, "OGG") == 0) {
-                               File *file = new Ogg::FLAC::File(fileName);
-                               if(file->isValid())
-                                       return file;
-                               delete file;
-                               return new Ogg::Vorbis::File(fileName);
-                       }
+               String ext = __get_file_ext(fileName);
+               if (ext == "OGG") {
+                       File *file = new Ogg::FLAC::File(fileName);
+                       if (file->isValid())
+                               return file;
+                       delete file;
+                       return new Ogg::Vorbis::File(fileName);
                }
                return 0;
        }