Commits

shuerhaaken committed 8f91f75

Allow RAII access to API in C++ code via InfoRef class

Comments (0)

Files changed (6)

+commit 2df64b36e85845172c0bb8eff9a4a6a51d66554c
+Author: Jörn Magens <shuerhaaken@googlemail.com>
+Date:   Mon Nov 4 16:22:11 2013 +0100
+
+    extend tests
+
 commit 0e4ab45792f09b64b33c974d8ec0e15bc7e67da7
 Author: Jörn Magens <shuerhaaken@googlemail.com>
 Date:   Mon Nov 4 15:50:09 2013 +0100

libtaginfo/Makefile.am

     image.cc \
     id3info.cc \
     info.cc \
+    inforef.cc \
     itinfo.cc \
     modinfo.cc \
     modtaginfo.cc \

libtaginfo/info.cc

 
 using namespace TagInfo;
 
+
+
+/*! \mainpage TagInfo Documentation
+ * 
+ * <b>libtaginfo is a wrapper library for taglib</b> and alows access to media tags in an abstract way. 
+ * 
+ * libtaginfo is a C++ library.
+ * 
+ * C and vala bindings are available with this library.
+ * 
+ * Known tag formats: 
+ *  - Ape tags, 
+ *  - Asf tags and 
+ *  - Id3 tags (v1, v2.2, v2.3, v2.4),
+ *  - Mod tags.
+ *  - Mp4 tags, 
+ *  - Xiphcomment/Vorbis tag, 
+ * 
+ * These tags are used with the following media files (extensions / mime):
+ *  - aac  : audio/aac, audio/aacp, audio/mp4, audio/x-aac
+ *  - aif  : audio/x-aiff
+ *  - aiff : audio/x-aiff
+ *  - ape  : application/x-ape
+ *  - asf  : audio/x-ms-asf, video/x-ms-asf
+ *  - flac : audio/flac, audio/x-flac+ogg, audio/x-flac
+ *  - it   : audio/x-it
+ *  - m4a  : audio/mp4a-latm, audio/x-m4a
+ *  - m4b  : audio/m4b
+ *  - m4p  : audio/x-m4p
+ *  - mod  : audio/x-mod
+ *  - mp3  : audio/mpeg, audio/x-mpegurl
+ *  - mp4  : audio/mp4, video/mp4
+ *  - mpc  : audio/mpc, audio/x-musepack
+ *  - oga  : audio/x-vorbis+ogg, audio/ogg
+ *  - ogg  : audio/x-vorbis+ogg, audio/ogg
+ *  - s3m  : audio/x-s3m
+ *  - spx  : audio/x-spx, audio/x-speex, audio/x-speex+ogg
+ *  - tta  : audio/tta, audio/x-tta
+ *  - wav  : audio/x-wav
+ *  - wma  : audio/x-ms-wma
+ *  - wmv  : video/x-ms-wmv, video/x-msvideo
+ *  - wv   : application/x-wavpack, audio/wavpack, audio/x-wavpack
+ *  - xm   : audio/x-xm
+ *
+ * There is an API for accessing the following media tags:
+ *  - Album
+ *  - Album artist
+ *  - Artist
+ *  - Comments;
+ *  - Compilation/V.A. flag
+ *  - Composer;
+ *  - Copyright;
+ *  - Encoder;
+ *  - Genre
+ *  - Homepage;
+ *  - Images (embedded image files)
+ *  - Original artist
+ *  - Play count
+ *  - Rating (1 - 5 stars, 0 not set) 
+ *  - Title
+ *  - Track count;
+ *  - Track number;
+ *  - User defined album labels;
+ *  - User defined artist labels;
+ *  - User defined track labels;
+ *  - Volume count;
+ *  - Volume number;
+ *  - Year
+ *  - BPM
+ * 
+ * However, some media tags are not supported by some tag formats. (E.g. mod tags are very limited)
+ * 
+ * There is an API for accessing the following audio properties:
+ *  - Bitrate
+ *  - Channel count
+ *  - Lenght in seconds
+ *  - Sample rate
+ *  
+ * 
+ * <b>Getting Started</b>
+ * 
+ * Libtaginfo provides a simple API which makes it possible to ignore the differences between tagging formats and format specific 
+ * APIs which allow programmers to work with the features of specific tagging schemes. 
+ * 
+ * Here's a very simple example with TagLib:
+ * 
+ *     TagInfo::Info * info = TagInfo::Info::create("song.mp3");
+ *     info->load();
+ *     TagLib::String artist = info->get_artist(); // artist == "Don van Vliet"
+ *     delete info;
+ *     
+ *     TagInfo::Info * info = TagInfo::Info::create("song.ogg");
+ *     info->load();
+ *     TagLib::String artist = info->get_artist(); // artist == "Mambo Boyz"
+ *     info->set_album("Mambo No. 1");
+ *     info->save();
+ *     delete info;
+ * 
+ * More examples can be found in the examples directory in the source code package.
+ * 
+ */
+
+
+
+
+
+
+
+
+
+
 /*!
  * Create a Info from \a file. If \a format is given, libtaginfo will try to create 
  * an Info object for the desired \a format.

libtaginfo/inforef.cc

+/*
+ * Copyright (C) 2008-2013 J.Rios <anonbeat@gmail.com>
+ * Copyright (C) 2013 Jörn Magens
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This Program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file LICENSE.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth 
+ * Floor, Boston, MA  02110-1301  USA
+ * https://www.gnu.org/licenses/lgpl-2.1.txt
+ *
+ * Author:
+ * 	Jörn Magens <shuerhaaken@googlemail.com>
+ */
+
+
+#include <algorithm>
+#include "taginfo.h"
+//#include "taginfo_internal.h"
+
+//#include "taginfo_apetags.h"
+//#include "taginfo_asftags.h"
+//#include "taginfo_modtags.h"
+//#include "taginfo_mp4tags.h"
+//#include "taginfo_id3tags.h"
+//#include "taginfo_xiphtags.h"
+
+
+
+using namespace TagInfo;
+
+
+
+
+// InfoRef
+
+InfoRef::InfoRef(const String &filename) {
+    i = NULL;
+    i = Info::create(filename);
+}
+
+InfoRef::InfoRef(const String &filename, MediaFileType type) {
+    i = NULL;
+    i = Info::create_with_format(filename, type);
+}
+
+InfoRef::InfoRef(const String &filename, const String &mime) {
+    i = NULL;
+    i = Info::create_from_mime(filename, mime);
+}
+
+bool InfoRef::is_null() const {
+    return i == NULL;
+}
+
+bool InfoRef::load() const {
+    if(!i)
+        return false;
+    return i->load();
+}
+
+bool InfoRef::save() {
+    if(!i)
+        return false;
+    return i->save();
+}
+
+TagInfo::Info * InfoRef::info() const {
+    return i;
+}
+
+InfoRef::~InfoRef() {
+    delete i;
+}
+
+

libtaginfo/taginfo.h

 
 /** @file */
 
-/*! \mainpage TagInfo Documentation
- * 
- * <b>libtaginfo is a wrapper library for taglib</b> and alows access to media tags in an abstract way. 
- * 
- * libtaginfo is a C++ library.
- * 
- * C and vala bindings are available with this library.
- * 
- * Known tag formats: 
- *  - Ape tags, 
- *  - Asf tags and 
- *  - Id3 tags (v1, v2.2, v2.3, v2.4),
- *  - Mod tags.
- *  - Mp4 tags, 
- *  - Xiphcomment/Vorbis tag, 
- * 
- * These tags are used with the following media files (extensions / mime):
- *  - aac  : audio/aac, audio/aacp, audio/mp4, audio/x-aac
- *  - aif  : audio/x-aiff
- *  - aiff : audio/x-aiff
- *  - ape  : application/x-ape
- *  - asf  : audio/x-ms-asf, video/x-ms-asf
- *  - flac : audio/flac, audio/x-flac+ogg, audio/x-flac
- *  - it   : audio/x-it
- *  - m4a  : audio/mp4a-latm, audio/x-m4a
- *  - m4b  : audio/m4b
- *  - m4p  : audio/x-m4p
- *  - mod  : audio/x-mod
- *  - mp3  : audio/mpeg, audio/x-mpegurl
- *  - mp4  : audio/mp4, video/mp4
- *  - mpc  : audio/mpc, audio/x-musepack
- *  - oga  : audio/x-vorbis+ogg, audio/ogg
- *  - ogg  : audio/x-vorbis+ogg, audio/ogg
- *  - s3m  : audio/x-s3m
- *  - spx  : audio/x-spx, audio/x-speex, audio/x-speex+ogg
- *  - tta  : audio/tta, audio/x-tta
- *  - wav  : audio/x-wav
- *  - wma  : audio/x-ms-wma
- *  - wmv  : video/x-ms-wmv, video/x-msvideo
- *  - wv   : application/x-wavpack, audio/wavpack, audio/x-wavpack
- *  - xm   : audio/x-xm
- *
- * There is an API for accessing the following media tags:
- *  - Album
- *  - Album artist
- *  - Artist
- *  - Comments;
- *  - Compilation/V.A. flag
- *  - Composer;
- *  - Copyright;
- *  - Encoder;
- *  - Genre
- *  - Homepage;
- *  - Images (embedded image files)
- *  - Original artist
- *  - Play count
- *  - Rating (1 - 5 stars, 0 not set) 
- *  - Title
- *  - Track count;
- *  - Track number;
- *  - User defined album labels;
- *  - User defined artist labels;
- *  - User defined track labels;
- *  - Volume count;
- *  - Volume number;
- *  - Year
- * 
- * However, some media tags are not supported by some tag formats. (E.g. mod tags are very limited)
- * 
- * There is an API for accessing the following audio properties:
- *  - Bitrate
- *  - Channel count
- *  - Lenght in seconds
- *  - Sample rate
- *          
- */
 
  /*!
   * 
     };
     
     
-    //! A simple, generic interface to common audio meta data fields
     
+    //! A simple, generic interface to common audio meta data fields
     /*!
     * Info is an abstract base class for media tag reading/writing  
     */
             
         public:
             
+            // CREATION METHODS
+            static Info * create(const String &filename);
+            static Info * create_with_format(const String &file, MediaFileType format);
+            static Info * create_from_mime(const String &filename, const String &mime_type);
+            
             //General stuff
-
+            
             //! \return name of media file
             String get_file_name(void);
-
+            
             //! Information if the media file could be recognized and appears to be valid
             //! \return true if valid 
             bool is_valid();
 
             virtual String    get_lyrics(void) const;
             virtual void      set_lyrics(const String &lyrics);
-            
-            // CREATION METHODS
-            static Info * create(const String &filename);
-            static Info * create_with_format(const String &file, MediaFileType format);
-            static Info * create_from_mime(const String &filename, const String &mime_type);
     };
+    
+    /* InfoRef exists to provide a minimal, generic and value-based wrapper around
+     * a Info.  It is lightweight and suitable for pass-by-value use.  
+     * This hides the TagInfo::Info memory management.
+     */
+    class InfoRef {
+        public:
+            InfoRef(const String &filename);
+            InfoRef(const String &filename, MediaFileType format);
+            InfoRef(const String &filename, const String &mime);
+            ~InfoRef();
+            
+            Info * info() const;
+            bool is_null() const;
+            bool load() const;
+            bool save();
+            
+        private:
+            Info * i;
+    };
+    
 }
 #endif

tests/write_mp3/main.cc

 }
 
 int main( void ) {
-    Info * info;
     std::string val = TESTDIR "samples/sample_v2_only.mp3";
     //std::cout << std::endl << "val: " << val << std::endl;
     
-    std::string target("/tmp/out_09.mp3");
+    String target("/tmp/out_09.mp3");
     
     std::ifstream  src(val.c_str());
-    std::ofstream  dst(target.c_str());
+    std::ofstream  dst(target.toCString(false));
     dst << src.rdbuf();
     
-    info = Info::create(target);
+    InfoRef iref(target);
     
     StringList trlabels = StringList();
     trlabels.append("user label1 äöüé");
     arlabels.append("Ärtißztá");
     arlabels.append("user label1 äiiüé");
     
-    if( info ) {
-        info->set_artist("artißt");
-        info->set_genre("gen re");
-        info->set_composer("cömpozér");
-        info->set_year(1982);
-        info->set_rating(2);
-        info->set_beats_per_minute(180);
-        info->set_title("titlöe");
-        info->set_original_artist ("ürgar t");
-        info->set_homepage("ööö");
-        info->set_encoder("encödr");
-        info->set_track_number(2);
-        info->set_track_count(5);
-        info->set_volume_number(1);
-        info->set_volume_count(3);
-        info->set_copyright("Cöpppyright");
-        info->set_album_artist("äöé");
-        info->set_is_compilation(true);
-        info->set_comments("Cömmméé\nèntß");
-        info->set_album_labels_list(allabels);
-        info->set_artist_labels_list(arlabels);
-        info->set_track_labels_list(trlabels);
-        info->save();
-        //std::cout <<  "done writing mp3" << std::endl;
+    if( !iref.is_null() ) {
+        iref.info()->set_artist("artißt");
+        iref.info()->set_genre("gen re");
+        iref.info()->set_composer("cömpozér");
+        iref.info()->set_year(1982);
+        iref.info()->set_rating(2);
+        iref.info()->set_beats_per_minute(180);
+        iref.info()->set_title("titlöe");
+        iref.info()->set_original_artist ("ürgar t");
+        iref.info()->set_homepage("ööö");
+        iref.info()->set_encoder("encödr");
+        iref.info()->set_track_number(2);
+        iref.info()->set_track_count(5);
+        iref.info()->set_volume_number(1);
+        iref.info()->set_volume_count(3);
+        iref.info()->set_copyright("Cöpppyright");
+        iref.info()->set_album_artist("äöé");
+        iref.info()->set_is_compilation(true);
+        iref.info()->set_comments("Cömmméé\nèntß");
+        iref.info()->set_album_labels_list(allabels);
+        iref.info()->set_artist_labels_list(arlabels);
+        iref.info()->set_track_labels_list(trlabels);
+        iref.save();
     }
-    delete info;
-    info = NULL;
-    
-    info = Info::create(target);
-    if( info ) {
-        if( info->load() ) {
+    iref;
+    InfoRef iref2(target);
+    if( !iref2.is_null() ) {
+        if( iref2.load() ) {
             //std::cout << "info->get_track_number(): " << info->get_track_number() << std::endl;
             //std::cout << "info->get_track_count(): " << info->get_track_count() << std::endl;
-            if(info->get_artist() == "artißt" &&
-               info->get_genre() == "gen re" &&
-               info->get_composer() == "cömpozér" &&
-               info->get_year() == 1982 &&
-               info->get_rating() == 2 &&
-               info->get_beats_per_minute() == 180 &&
-               info->get_title() == "titlöe" &&
-               info->get_original_artist () == "ürgar t" &&
-               info->get_homepage() == "ööö" &&
-               info->get_encoder() == "encödr" &&
-               info->get_track_number() == 2 &&
-               info->get_track_count() == 5 &&
-               info->get_volume_number() == 1 &&
-               info->get_volume_count() == 3 &&
-               info->get_copyright() == "Cöpppyright" &&
-               info->get_album_artist() == "äöé" &&
-               info->get_is_compilation() &&
-               info->get_comments() == "Cömmméé\nèntß" &&
-               check_list_equal(info->get_artist_labels_list(), arlabels) &&
-               check_list_equal(info->get_album_labels_list(), allabels) &&
-               check_list_equal(info->get_track_labels_list(), trlabels)    ) {
-                delete info;
-                if(remove(target.c_str()) != 0 )
+            if(iref2.info()->get_artist() == "artißt" &&
+               iref2.info()->get_genre() == "gen re" &&
+               iref2.info()->get_composer() == "cömpozér" &&
+               iref2.info()->get_year() == 1982 &&
+               iref2.info()->get_rating() == 2 &&
+               iref2.info()->get_beats_per_minute() == 180 &&
+               iref2.info()->get_title() == "titlöe" &&
+               iref2.info()->get_original_artist () == "ürgar t" &&
+               iref2.info()->get_homepage() == "ööö" &&
+               iref2.info()->get_encoder() == "encödr" &&
+               iref2.info()->get_track_number() == 2 &&
+               iref2.info()->get_track_count() == 5 &&
+               iref2.info()->get_volume_number() == 1 &&
+               iref2.info()->get_volume_count() == 3 &&
+               iref2.info()->get_copyright() == "Cöpppyright" &&
+               iref2.info()->get_album_artist() == "äöé" &&
+               iref2.info()->get_is_compilation() &&
+               iref2.info()->get_comments() == "Cömmméé\nèntß" &&
+               check_list_equal(iref2.info()->get_artist_labels_list(), arlabels) &&
+               check_list_equal(iref2.info()->get_album_labels_list(), allabels) &&
+               check_list_equal(iref2.info()->get_track_labels_list(), trlabels)    ) {
+                if(remove(target.toCString(false)) != 0 )
                     return EXIT_FAILURE;
                 return EXIT_SUCCESS;
             }
         }
-        delete info;
-        if(remove(target.c_str()) != 0 )
+        if(remove(target.toCString(false)) != 0 )
             return EXIT_FAILURE;
         return EXIT_FAILURE;
     }
     else {
-       delete info;
-        if(remove(target.c_str()) != 0 )
+        if(remove(target.toCString(false)) != 0 )
             return EXIT_FAILURE;
         return EXIT_FAILURE;
     }