]> granicus.if.org Git - taglib/commitdiff
Fix saving ID3v2/INFO tags of WAV files.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Fri, 30 Jan 2015 05:43:05 +0000 (14:43 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Fri, 30 Jan 2015 05:51:28 +0000 (14:51 +0900)
The old tag won't be removed when the new tag is empty.

taglib/riff/wav/wavfile.cpp
tests/test_wav.cpp

index c8d0578c7fdf3a25fba5ccfadcdbe51a1c14084d..6c835e702cb323a54753ffcd2a66a95e34608384 100644 (file)
@@ -57,7 +57,7 @@ public:
   }
 
   Properties *properties;
-  
+
   ByteVector tagChunkID;
 
   TagUnion tag;
@@ -146,21 +146,30 @@ bool RIFF::WAV::File::save(TagTypes tags, bool stripOthers, int id3v2Version)
   if(stripOthers)
     strip(static_cast<TagTypes>(AllTags & ~tags));
 
-  ID3v2::Tag *id3v2tag = d->tag.access<ID3v2::Tag>(ID3v2Index, false);
-  if((tags & ID3v2) && !id3v2tag->isEmpty()) {
-    setChunkData(d->tagChunkID, id3v2tag->render(id3v2Version));
-    d->hasID3v2 = true;
+  const ID3v2::Tag *id3v2tag = d->tag.access<ID3v2::Tag>(ID3v2Index, false);
+  if(tags & ID3v2) {
+    if(d->hasID3v2) {
+      removeChunk(d->tagChunkID);
+      d->hasID3v2 = false;
+    }
+
+    if(!id3v2tag->isEmpty()) {
+      setChunkData(d->tagChunkID, id3v2tag->render(id3v2Version));
+      d->hasID3v2 = true;
+    }
   }
 
-  Info::Tag *infotag = d->tag.access<Info::Tag>(InfoIndex, false);
-  if((tags & Info) && !infotag->isEmpty()) {
-    int chunkId = findInfoTagChunk();
-    if(chunkId != -1)
-      setChunkData(chunkId, infotag->render());
-    else
-      setChunkData("LIST", infotag->render(), true);
+  const Info::Tag *infotag = d->tag.access<Info::Tag>(InfoIndex, false);
+  if(tags & Info) {
+    if(d->hasInfo) {
+      removeChunk(findInfoTagChunk());
+      d->hasInfo = false;
+    }
 
-    d->hasInfo = true;
+    if(!infotag->isEmpty()) {
+      setChunkData("LIST", infotag->render(), true);
+      d->hasInfo = true;
+    }
   }
 
   return true;
@@ -239,6 +248,6 @@ TagLib::uint RIFF::WAV::File::findInfoTagChunk()
       return i;
     }
   }
-  
+
   return TagLib::uint(-1);
 }
index 7367357a55f8271ea0e82de6fc7c3bee29935d42..a962ca2324d3d0cb6ebdffd02811e1877d1568a7 100644 (file)
@@ -1,9 +1,11 @@
-#include <cppunit/extensions/HelperMacros.h>
 #include <string>
 #include <stdio.h>
 #include <tag.h>
+#include <id3v2tag.h>
+#include <infotag.h>
 #include <tbytevectorlist.h>
 #include <wavfile.h>
+#include <cppunit/extensions/HelperMacros.h>
 #include "utils.h"
 
 using namespace std;
@@ -14,6 +16,8 @@ class TestWAV : public CppUnit::TestFixture
   CPPUNIT_TEST_SUITE(TestWAV);
   CPPUNIT_TEST(testLength);
   CPPUNIT_TEST(testZeroSizeDataChunk);
+  CPPUNIT_TEST(testID3v2Tag);
+  CPPUNIT_TEST(testInfoTag);
   CPPUNIT_TEST(testStripTags);
   CPPUNIT_TEST(testFuzzedFile1);
   CPPUNIT_TEST(testFuzzedFile2);
@@ -24,14 +28,80 @@ public:
   void testLength()
   {
     RIFF::WAV::File f(TEST_FILE_PATH_C("empty.wav"));
-    CPPUNIT_ASSERT_EQUAL(true, f.isValid());
+    CPPUNIT_ASSERT(f.isValid());
     CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->length());
   }
 
   void testZeroSizeDataChunk()
   {
     RIFF::WAV::File f(TEST_FILE_PATH_C("zero-size-chunk.wav"));
-    CPPUNIT_ASSERT_EQUAL(false, f.isValid());
+    CPPUNIT_ASSERT(!f.isValid());
+  }
+
+  void testID3v2Tag()
+  {
+    ScopedFileCopy copy("empty", ".wav");
+    string filename = copy.fileName();
+
+    {
+      RIFF::WAV::File f(filename.c_str());
+      CPPUNIT_ASSERT(f.isValid());
+
+      f.ID3v2Tag()->setTitle(L"Title");
+      f.ID3v2Tag()->setArtist(L"Artist");
+      f.save();
+    }
+
+    {
+      RIFF::WAV::File f(filename.c_str());
+      CPPUNIT_ASSERT(f.isValid());
+      CPPUNIT_ASSERT_EQUAL(String(L"Title"),  f.ID3v2Tag()->title());
+      CPPUNIT_ASSERT_EQUAL(String(L"Artist"), f.ID3v2Tag()->artist());
+
+      f.ID3v2Tag()->setTitle(L"");
+      f.ID3v2Tag()->setArtist(L"");
+      f.save();
+    }
+
+    {
+      RIFF::WAV::File f(filename.c_str());
+      CPPUNIT_ASSERT(f.isValid());
+      CPPUNIT_ASSERT_EQUAL(String(L""), f.ID3v2Tag()->title());
+      CPPUNIT_ASSERT_EQUAL(String(L""), f.ID3v2Tag()->artist());
+    }
+  }
+
+  void testInfoTag()
+  {
+    ScopedFileCopy copy("empty", ".wav");
+    string filename = copy.fileName();
+
+    {
+      RIFF::WAV::File f(filename.c_str());
+      CPPUNIT_ASSERT(f.isValid());
+
+      f.InfoTag()->setTitle(L"Title");
+      f.InfoTag()->setArtist(L"Artist");
+      f.save();
+    }
+
+    {
+      RIFF::WAV::File f(filename.c_str());
+      CPPUNIT_ASSERT(f.isValid());
+      CPPUNIT_ASSERT_EQUAL(String(L"Title"),  f.InfoTag()->title());
+      CPPUNIT_ASSERT_EQUAL(String(L"Artist"), f.InfoTag()->artist());
+
+      f.InfoTag()->setTitle(L"");
+      f.InfoTag()->setArtist(L"");
+      f.save();
+    }
+
+    {
+      RIFF::WAV::File f(filename.c_str());
+      CPPUNIT_ASSERT(f.isValid());
+      CPPUNIT_ASSERT_EQUAL(String(L""), f.InfoTag()->title());
+      CPPUNIT_ASSERT_EQUAL(String(L""), f.InfoTag()->artist());
+    }
   }
 
   void testStripTags()