Add RIFF::WAV::File::strip() function.

Avoid saving an empty tag.
This commit is contained in:
Tsuda Kageyu 2015-08-03 23:01:15 +09:00
parent 35aa6c4e84
commit 1bb06b1f7a
2 changed files with 38 additions and 40 deletions

View 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);
}
d->hasInfo = false;
}
}
TagLib::uint RIFF::WAV::File::findInfoTagChunk()
{
for(uint i = 0; i < chunkCount(); ++i) {
if(chunkName(i) == "LIST" && chunkData(i).startsWith("INFO")) {
return i;
}
}
return TagLib::uint(-1);
}

View 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;