]> granicus.if.org Git - taglib/commitdiff
Add RIFF::WAV::File::strip() function.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Mon, 3 Aug 2015 14:01:15 +0000 (23:01 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Tue, 25 Aug 2015 02:03:00 +0000 (11:03 +0900)
Avoid saving an empty tag.

taglib/riff/wav/wavfile.cpp
taglib/riff/wav/wavfile.h

index e7b8dc8a8c7709edb6ee96c702aecd7885d22a85..cab8b33b6a3339cdd72f3fb0a5f6059e6dc8eaa4 100644 (file)
@@ -106,6 +106,17 @@ RIFF::Info::Tag *RIFF::WAV::File::InfoTag() const
   return d->tag.access<RIFF::Info::Tag>(InfoIndex, false);
 }
 
+void RIFF::WAV::File::strip(TagTypes tags)
+{
+  removeTagChunks(tags);
+
+  if(tags & ID3v2)
+    d->tag.set(ID3v2Index, new ID3v2::Tag());
+
+  if(tags & Info)
+    d->tag.set(InfoIndex, new RIFF::Info::Tag());
+}
+
 PropertyMap RIFF::WAV::File::properties() const
 {
   return tag()->properties();
@@ -146,28 +157,20 @@ bool RIFF::WAV::File::save(TagTypes tags, bool stripOthers, int id3v2Version)
   if(stripOthers)
     strip(static_cast<TagTypes>(AllTags & ~tags));
 
-  const ID3v2::Tag *id3v2tag = d->tag.access<ID3v2::Tag>(ID3v2Index, false);
   if(tags & ID3v2) {
-    if(d->hasID3v2) {
-      removeChunk(d->tagChunkID);
-      d->hasID3v2 = false;
-    }
+    removeTagChunks(ID3v2);
 
-    if(!id3v2tag->isEmpty()) {
-      setChunkData(d->tagChunkID, id3v2tag->render(id3v2Version));
+    if(ID3v2Tag() && !ID3v2Tag()->isEmpty()) {
+      setChunkData(d->tagChunkID, ID3v2Tag()->render(id3v2Version));
       d->hasID3v2 = true;
     }
   }
 
-  const Info::Tag *infotag = d->tag.access<Info::Tag>(InfoIndex, false);
   if(tags & Info) {
-    if(d->hasInfo) {
-      removeChunk(findInfoTagChunk());
-      d->hasInfo = false;
-    }
+    removeTagChunks(Info);
 
-    if(!infotag->isEmpty()) {
-      setChunkData("LIST", infotag->render(), true);
+    if(InfoTag() && !InfoTag()->isEmpty()) {
+      setChunkData("LIST", InfoTag()->render(), true);
       d->hasInfo = true;
     }
   }
@@ -227,29 +230,21 @@ void RIFF::WAV::File::read(bool readProperties)
     d->properties = new Properties(this, Properties::Average);
 }
 
-void RIFF::WAV::File::strip(TagTypes tags)
+void RIFF::WAV::File::removeTagChunks(TagTypes tags)
 {
-  if(tags & ID3v2) {
-    removeChunk(d->tagChunkID);
+  if((tags & ID3v2) && d->hasID3v2) {
+    removeChunk("ID3 ");
+    removeChunk("id3 ");
+
     d->hasID3v2 = false;
   }
 
-  if(tags & Info){
-    TagLib::uint chunkId = findInfoTagChunk();
-    if(chunkId != TagLib::uint(-1)) {
-      removeChunk(chunkId);
-      d->hasInfo = false;
+  if((tags & Info) && d->hasInfo) {
+    for(int i = static_cast<int>(chunkCount()) - 1; i >= 0; --i) {
+      if(chunkName(i) == "LIST" && chunkData(i).startsWith("INFO"))
+        removeChunk(i);
     }
-  }
-}
 
-TagLib::uint RIFF::WAV::File::findInfoTagChunk()
-{
-  for(uint i = 0; i < chunkCount(); ++i) {
-    if(chunkName(i) == "LIST" && chunkData(i).startsWith("INFO")) {
-      return i;
-    }
+    d->hasInfo = false;
   }
-
-  return TagLib::uint(-1);
 }
index 129d9537ac5f2e51d9c09380deb3733efdc263d8..80f17a852aa0f750eef733389881a5c506bb515f 100644 (file)
@@ -125,6 +125,15 @@ namespace TagLib {
          */
         Info::Tag *InfoTag() const;
 
+        /*!
+         * This will strip the tags that match the OR-ed together TagTypes from the
+         * file.  By default it strips all tags.  It returns true if the tags are
+         * successfully stripped.
+         *
+         * \note This will update the file immediately.
+         */
+        void strip(TagTypes tags = AllTags);
+
         /*!
          * Implements the unified property interface -- export function.
          * This method forwards to ID3v2::Tag::properties().
@@ -171,13 +180,7 @@ namespace TagLib {
         File &operator=(const File &);
 
         void read(bool readProperties);
-
-        void strip(TagTypes tags);
-
-        /*!
-         * Returns the index of the chunk that its name is "LIST" and list type is "INFO".
-         */
-        uint findInfoTagChunk();
+        void removeTagChunks(TagTypes tags);
 
         friend class Properties;