]> granicus.if.org Git - taglib/commitdiff
Enable FLAC::File to remove non-standard tags.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Mon, 21 Dec 2015 06:42:41 +0000 (15:42 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Mon, 21 Dec 2015 06:42:41 +0000 (15:42 +0900)
taglib/flac/flacfile.cpp
taglib/flac/flacfile.h
tests/test_flac.cpp

index fb7418d1ce68b77139ea8ae83585eca247dd351b..b31cc65e07385b1dfcb662db0dd68a26567f991f 100644 (file)
@@ -372,6 +372,20 @@ void FLAC::File::removePictures()
   }
 }
 
+void FLAC::File::strip(int tags)
+{
+  if(tags & ID3v1)
+    d->tag.set(FlacID3v1Index, 0);
+
+  if(tags & ID3v2)
+    d->tag.set(FlacID3v2Index, 0);
+
+  if(tags & XiphComment) {
+    xiphComment()->removeAllFields();
+    xiphComment()->removeAllPictures();
+  }
+}
+
 bool FLAC::File::hasXiphComment() const
 {
   return !d->xiphCommentData.isEmpty();
index cf8c15c0f09220cb9d9570efad44eba68daba104..65d856792b608ce099bc157b168e72046359e120 100644 (file)
@@ -66,6 +66,23 @@ namespace TagLib {
     class TAGLIB_EXPORT File : public TagLib::File
     {
     public:
+      /*!
+       * This set of flags is used for various operations and is suitable for
+       * being OR-ed together.
+       */
+      enum TagTypes {
+        //! Empty set.  Matches no tag types.
+        NoTags      = 0x0000,
+        //! Matches Vorbis comments.
+        XiphComment = 0x0001,
+        //! Matches ID3v1 tags.
+        ID3v1       = 0x0002,
+        //! Matches ID3v2 tags.
+        ID3v2       = 0x0004,
+        //! Matches all tag types.
+        AllTags     = 0xffff
+      };
+
       /*!
        * Constructs a FLAC file from \a file.  If \a readProperties is true the
        * file's audio properties will also be read.
@@ -265,6 +282,21 @@ namespace TagLib {
        */
       void addPicture(Picture *picture);
 
+      /*!
+       * This will remove the tags that match the OR-ed together TagTypes from
+       * the file.  By default it removes all tags.
+       *
+       * \warning This will also invalidate pointers to the tags as their memory
+       * will be freed.
+       *
+       * \note In order to make the removal permanent save() still needs to be
+       * called.
+       *
+       * \note This won't remove the Vorbis comment block completely.  The
+       * vendor ID will be preserved.
+       */
+      void strip(int tags = AllTags);
+
       /*!
        * Returns whether or not the file on disk actually has a XiphComment.
        *
index 5bc98e7a47a71d913243693812b88d6432608c15..cf59ef3714fc49ce0ad481c949cecdc5780baceb 100644 (file)
@@ -36,6 +36,7 @@ class TestFLAC : public CppUnit::TestFixture
   CPPUNIT_TEST(testSaveID3v1);
   CPPUNIT_TEST(testUpdateID3v2);
   CPPUNIT_TEST(testEmptyID3v2);
+  CPPUNIT_TEST(testStripTags);
   CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -416,6 +417,57 @@ public:
     }
   }
 
+  void testStripTags()
+  {
+    ScopedFileCopy copy("silence-44-s", ".flac");
+
+    {
+      FLAC::File f(copy.fileName().c_str());
+      f.xiphComment(true)->setTitle("XiphComment Title");
+      f.ID3v1Tag(true)->setTitle("ID3v1 Title");
+      f.ID3v2Tag(true)->setTitle("ID3v2 Title");
+      f.save();
+    }
+    {
+      FLAC::File f(copy.fileName().c_str());
+      CPPUNIT_ASSERT(f.hasXiphComment());
+      CPPUNIT_ASSERT(f.hasID3v1Tag());
+      CPPUNIT_ASSERT(f.hasID3v2Tag());
+      CPPUNIT_ASSERT_EQUAL(String("XiphComment Title"), f.xiphComment()->title());
+      CPPUNIT_ASSERT_EQUAL(String("ID3v1 Title"), f.ID3v1Tag()->title());
+      CPPUNIT_ASSERT_EQUAL(String("ID3v2 Title"), f.ID3v2Tag()->title());
+      f.strip(FLAC::File::ID3v2);
+      f.save();
+    }
+    {
+      FLAC::File f(copy.fileName().c_str());
+      CPPUNIT_ASSERT(f.hasXiphComment());
+      CPPUNIT_ASSERT(f.hasID3v1Tag());
+      CPPUNIT_ASSERT(!f.hasID3v2Tag());
+      CPPUNIT_ASSERT_EQUAL(String("XiphComment Title"), f.xiphComment()->title());
+      CPPUNIT_ASSERT_EQUAL(String("ID3v1 Title"), f.ID3v1Tag()->title());
+      f.strip(FLAC::File::ID3v1);
+      f.save();
+    }
+    {
+      FLAC::File f(copy.fileName().c_str());
+      CPPUNIT_ASSERT(f.hasXiphComment());
+      CPPUNIT_ASSERT(!f.hasID3v1Tag());
+      CPPUNIT_ASSERT(!f.hasID3v2Tag());
+      CPPUNIT_ASSERT_EQUAL(String("XiphComment Title"), f.xiphComment()->title());
+      f.strip(FLAC::File::XiphComment);
+      f.save();
+    }
+    {
+      FLAC::File f(copy.fileName().c_str());
+      CPPUNIT_ASSERT(f.hasXiphComment());
+      CPPUNIT_ASSERT(!f.hasID3v1Tag());
+      CPPUNIT_ASSERT(!f.hasID3v2Tag());
+      CPPUNIT_ASSERT(f.xiphComment()->isEmpty());
+      CPPUNIT_ASSERT_EQUAL(String("reference libFLAC 1.1.0 20030126"), f.xiphComment()->vendorID());
+    }
+  }
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(TestFLAC);