[easytag/wip/taglib-mp4-cxx: 9/10] Use C++ TagLib API instead of C wrapper



commit 7fb89646e4d4a37f96f8d9cde4de4fc7e6b24854
Author: David King <amigadave amigadave com>
Date:   Thu Mar 6 23:38:39 2014 +0000

    Use C++ TagLib API instead of C wrapper
    
    Eventually, this will enable handling of cover art and extended tags for
    MP4 files.

 Makefile.am                   |    6 ++-
 configure.ac                  |    2 +-
 src/{mp4_tag.c => mp4_tag.cc} |  124 +++++++++++++++--------------------------
 src/mp4_tag.h                 |   15 ++---
 4 files changed, 57 insertions(+), 90 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 0260c1c..0bdcf00 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -27,6 +27,10 @@ easytag_CFLAGS = \
        $(WARN_CFLAGS) \
        $(EASYTAG_CFLAGS)
 
+easytag_CXXFLAGS = \
+       $(WARN_CFLAGS) \
+       $(EASYTAG_CFLAGS)
+
 easytag_SOURCES = \
        src/id3lib/c_wrapper.cpp \
        src/libapetag/apetaglib.c \
@@ -55,7 +59,7 @@ easytag_SOURCES = \
        src/monkeyaudio_header.c \
        src/mpeg_header.c \
        src/mp4_header.c \
-       src/mp4_tag.c \
+       src/mp4_tag.cc \
        src/musepack_header.c \
        src/ogg_header.c \
        src/ogg_tag.c \
diff --git a/configure.ac b/configure.ac
index fbe1111..7fe6699 100644
--- a/configure.ac
+++ b/configure.ac
@@ -257,7 +257,7 @@ dnl ################################################
 dnl # taglib library
 dnl ################################################
 
-TAGLIB_DEPS="taglib_c >= 1.6.0"
+TAGLIB_DEPS="taglib >= 1.6.0"
 AS_IF([test "x$enable_mp4" != "xno"],
       [PKG_CHECK_EXISTS([$TAGLIB_DEPS], [have_taglib=yes], [have_taglib=no])],
       [have_taglib=no])
diff --git a/src/mp4_tag.c b/src/mp4_tag.cc
similarity index 62%
rename from src/mp4_tag.c
rename to src/mp4_tag.cc
index fa047de..37d2a02 100644
--- a/src/mp4_tag.c
+++ b/src/mp4_tag.cc
@@ -1,6 +1,6 @@
-/* mp4_tag.c - 2005/08/06 */
 /*
  *  EasyTAG - Tag editor for MP3 and Ogg Vorbis files
+ *  Copyright (C) 2012-1014  David King <amigadave amigadave com>
  *  Copyright (C) 2001-2005  Jerome Couderc <easytag gmail com>
  *  Copyright (C) 2005  Michael Ihde <mike ihde randomwalking com>
  *  Copyright (C) 2005  Stewart Whitman <swhitman cox net>
@@ -37,9 +37,11 @@
 #include "et_core.h"
 #include "charset.h"
 
-#include <tag_c.h>
+#include <mp4file.h>
 
 
+extern "C" {
+
 /*
  * Mp4_Tag_Read_File_Tag:
  *
@@ -51,58 +53,46 @@
  */
 gboolean Mp4tag_Read_File_Tag (gchar *filename, File_Tag *FileTag)
 {
-    TagLib_File *mp4file;
-    TagLib_Tag *tag;
+    TagLib::Tag *tag;
     guint track;
 
     g_return_val_if_fail (filename != NULL && FileTag != NULL, FALSE);
 
-    /* Get data from tag */
-    mp4file = taglib_file_new_type(filename,TagLib_File_MP4);
-    if (mp4file == NULL)
-    {
-        gchar *filename_utf8 = filename_to_display(filename);
-        Log_Print(LOG_ERROR,_("Error while opening file: '%s' (%s)."),filename_utf8,_("MP4 format invalid"));
-        g_free(filename_utf8);
-        return FALSE;
-    }
+    /* Get data from tag. */
+    TagLib::MP4::File mp4file (filename);
 
-    /* Check for audio track */
-    if (!taglib_file_is_valid (mp4file))
+    if (!mp4file.isOpen ())
     {
         gchar *filename_utf8 = filename_to_display (filename);
-        Log_Print (LOG_ERROR, _("File contains no audio track: '%s'"),
-                   filename_utf8);
+        Log_Print (LOG_ERROR, _("Error while opening file: '%s' (%s)."),
+                   filename_utf8, _("MP4 format invalid"));
         g_free (filename_utf8);
-        taglib_file_free (mp4file);
         return FALSE;
     }
 
-    tag = taglib_file_tag (mp4file);
-    if (tag == NULL)
+    if (!(tag = mp4file.tag ()))
     {
         gchar *filename_utf8 = filename_to_display (filename);
         Log_Print (LOG_ERROR, _("Error reading tags from file: '%s'"),
                    filename_utf8);
         g_free (filename_utf8);
-        taglib_file_free (mp4file);
         return FALSE;
     }
 
     /*********
      * Title *
      *********/
-    FileTag->title = g_strdup(taglib_tag_title(tag));
+    FileTag->title = g_strdup (tag->title ().toCString (true));
 
     /**********
      * Artist *
      **********/
-    FileTag->artist = g_strdup(taglib_tag_artist(tag));
+    FileTag->artist = g_strdup (tag->title ().toCString (true));
 
     /*********
      * Album *
      *********/
-    FileTag->album = g_strdup(taglib_tag_album(tag));
+    FileTag->album = g_strdup (tag->title ().toCString (true));
 
     /****************
      * Album Artist *
@@ -112,12 +102,12 @@ gboolean Mp4tag_Read_File_Tag (gchar *filename, File_Tag *FileTag)
     /********
      * Year *
      ********/
-    FileTag->year = g_strdup_printf("%u", taglib_tag_year(tag));
+    FileTag->year = g_strdup_printf ("%u", tag->year ());
 
     /*************************
      * Track and Total Track *
      *************************/
-    track = taglib_tag_track(tag);
+    track = tag->track ();
 
     if (track != 0)
         FileTag->track = et_track_number_to_string (track);
@@ -126,12 +116,12 @@ gboolean Mp4tag_Read_File_Tag (gchar *filename, File_Tag *FileTag)
     /*********
      * Genre *
      *********/
-    FileTag->genre = g_strdup(taglib_tag_genre(tag));
+    FileTag->genre = g_strdup (tag->genre ().toCString (true));
 
     /***********
      * Comment *
      ***********/
-    FileTag->comment = g_strdup(taglib_tag_comment(tag));
+    FileTag->comment = g_strdup (tag->comment ().toCString (true));
 
     /**********************
      * Composer or Writer *
@@ -148,10 +138,6 @@ gboolean Mp4tag_Read_File_Tag (gchar *filename, File_Tag *FileTag)
      ***********/
     /* TODO: No encode_by support in the TagLib C API! */
 
-    /* Free allocated data */
-    taglib_tag_free_strings();
-    taglib_file_free(mp4file);
-
     return TRUE;
 }
 
@@ -169,8 +155,7 @@ gboolean Mp4tag_Write_File_Tag (ET_File *ETFile)
     File_Tag *FileTag;
     gchar    *filename;
     gchar    *filename_utf8;
-    TagLib_File *mp4file = NULL;
-    TagLib_Tag *tag;
+    TagLib::Tag *tag;
     gboolean success;
 
     g_return_val_if_fail (ETFile != NULL && ETFile->FileTag != NULL, FALSE);
@@ -180,100 +165,83 @@ gboolean Mp4tag_Write_File_Tag (ET_File *ETFile)
     filename_utf8 = ((File_Name *)ETFile->FileNameCur->data)->value_utf8;
 
     /* Open file for writing */
-    mp4file = taglib_file_new_type(filename, TagLib_File_MP4);
-    if (mp4file == NULL)
+    TagLib::MP4::File mp4file (filename);
+
+    if (!mp4file.isOpen ())
     {
-        Log_Print(LOG_ERROR,_("Error while opening file: '%s' (%s)."),filename_utf8,_("MP4 format invalid"));
+        Log_Print (LOG_ERROR, _("Error while opening file: '%s' (%s)."),
+                   filename_utf8, _("MP4 format invalid"));
         return FALSE;
     }
 
-    tag = taglib_file_tag (mp4file);
-    if (tag == NULL)
+    if (!(tag = mp4file.tag ()))
     {
         gchar *filename_utf8 = filename_to_display (filename);
         Log_Print (LOG_ERROR, _("Error reading tags from file: '%s'"),
                    filename_utf8);
         g_free (filename_utf8);
-        taglib_file_free (mp4file);
         return FALSE;
     }
 
     /*********
      * Title *
      *********/
-    if (FileTag->title && g_utf8_strlen(FileTag->title, -1) > 0)
-    {
-        taglib_tag_set_title(tag, FileTag->title);
-    }else
+    if (FileTag->title && *(FileTag->title))
     {
-        taglib_tag_set_title(tag,"");
+        TagLib::String string (FileTag->title, TagLib::String::UTF8);
+        tag->setTitle (string);
     }
 
     /**********
      * Artist *
      **********/
-    if (FileTag->artist && g_utf8_strlen(FileTag->artist, -1) > 0)
+    if (FileTag->artist && *(FileTag->artist))
     {
-        taglib_tag_set_artist(tag,FileTag->artist);
-    }else
-    {
-        taglib_tag_set_artist(tag,"");
+        TagLib::String string (FileTag->artist, TagLib::String::UTF8);
+        tag->setArtist (string);
     }
 
     /*********
      * Album *
      *********/
-    if (FileTag->album && g_utf8_strlen(FileTag->album, -1) > 0)
-    {
-        taglib_tag_set_album(tag,FileTag->album);
-    }else
+    if (FileTag->album && *(FileTag->album))
     {
-        taglib_tag_set_album(tag,"");
+        TagLib::String string (FileTag->album, TagLib::String::UTF8);
+        tag->setAlbum (string);
     }
 
-
     /********
      * Year *
      ********/
-    if (FileTag->year && g_utf8_strlen(FileTag->year, -1) > 0)
-    {
-        taglib_tag_set_year(tag,atoi(FileTag->year));
-    }else
+    if (FileTag->year)
     {
-        taglib_tag_set_year(tag,0);
+        tag->setYear (atoi (FileTag->year));
     }
 
     /*************************
      * Track and Total Track *
      *************************/
-    if ( FileTag->track && g_utf8_strlen(FileTag->track, -1) > 0 )
+    if (FileTag->track)
     {
-        taglib_tag_set_track(tag,atoi(FileTag->track));
-    }else
-    {
-        taglib_tag_set_track(tag,0);
+        tag->setTrack (atoi (FileTag->track));
     }
 
     /*********
      * Genre *
      *********/
-    if (FileTag->genre && g_utf8_strlen(FileTag->genre, -1) > 0 )
-    {
-        taglib_tag_set_genre(tag,FileTag->genre);
-    }else
+    if (FileTag->genre && *(FileTag->genre))
     {
-        taglib_tag_set_genre(tag,"");
+        TagLib::String string (FileTag->genre, TagLib::String::UTF8);
+        tag->setGenre (string);
     }
 
     /***********
      * Comment *
      ***********/
-    if (FileTag->comment && g_utf8_strlen(FileTag->comment, -1) > 0)
+    if (FileTag->comment && *(FileTag->comment))
     {
-        taglib_tag_set_comment(tag,FileTag->comment);
-    }else
-    {
-        taglib_tag_set_comment(tag,"");
+        TagLib::String string (FileTag->comment, TagLib::String::UTF8);
+        tag->setComment (string);
     }
 
     /**********************
@@ -288,11 +256,11 @@ gboolean Mp4tag_Write_File_Tag (ET_File *ETFile)
      * Picture *
      ***********/
 
-    success = taglib_file_save (mp4file) ? TRUE : FALSE;
-    taglib_file_free(mp4file);
+    success = mp4file.save () ? TRUE : FALSE;
 
     return success;
 }
 
+}
 
 #endif /* ENABLE_MP4 */
diff --git a/src/mp4_tag.h b/src/mp4_tag.h
index 9c6c948..64d7f54 100644
--- a/src/mp4_tag.h
+++ b/src/mp4_tag.h
@@ -1,6 +1,6 @@
-/* mp4_tag.h - 2005/08/06 */
 /*
  *  EasyTAG - Tag editor for MP3 and Ogg Vorbis files
+ *  Copyright (C) 2012-1014  David King <amigadave amigadave com>
  *  Copyright (C) 2001-2005  Jerome Couderc <easytag gmail com>
  *  Copyright (C) 2005  Michael Ihde <mike ihde randomwalking com>
  *
@@ -19,21 +19,16 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-
-#ifndef __MP4_TAG_H__
-#define __MP4_TAG_H__
-
+#ifndef ET_MP4_TAG_H_
+#define ET_MP4_TAG_H_
 
 #include "et_core.h"
 
 G_BEGIN_DECLS
 
-/**************
- * Prototypes *
- **************/
-gboolean Mp4tag_Read_File_Tag  (gchar *filename, File_Tag *FileTag);
+gboolean Mp4tag_Read_File_Tag (gchar *filename, File_Tag *FileTag);
 gboolean Mp4tag_Write_File_Tag (ET_File *ETFile);
 
 G_END_DECLS
 
-#endif /* __MP4_TAG_H__ */
+#endif /* ET_MP4_TAG_H_ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]