From: Scott Wheeler Date: Fri, 16 May 2008 06:01:52 +0000 (+0000) Subject: Untested chunk writing code. X-Git-Tag: v1.6rc1~83 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b2778ed71580c605f858e842f7bb8c3b2ec10d8a;p=taglib Untested chunk writing code. git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@808237 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- diff --git a/taglib/riff/rifffile.cpp b/taglib/riff/rifffile.cpp index 1b3b9d84..ce8d118e 100644 --- a/taglib/riff/rifffile.cpp +++ b/taglib/riff/rifffile.cpp @@ -23,8 +23,12 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ +#include +#include +#include + #include "rifffile.h" -#include +#include using namespace TagLib; @@ -42,9 +46,9 @@ public: uint size; ByteVector format; - ByteVectorList chunkNames; - List chunkOffsets; - List chunkSizes; + std::vector chunkNames; + std::vector chunkOffsets; + std::vector chunkSizes; }; //////////////////////////////////////////////////////////////////////////////// @@ -104,6 +108,39 @@ ByteVector RIFF::File::chunkData(uint i) return readBlock(d->chunkSizes[i]); } +void RIFF::File::setChunkData(const ByteVector &name, const ByteVector &data) +{ + if(d->chunkNames.size() == 0) + { + debug("RIFF::File::setChunkData - No valid chunks found."); + return; + } + + for(uint i = 0; i < d->chunkNames.size(); i++) { + if(d->chunkNames[i] == name) { + + int sizeDifference = data.size() - d->chunkSizes[i]; + + // First we update the global size + + insert(ByteVector::fromUInt(d->size + sizeDifference, d->endianness == BigEndian), 4, 4); + + // Now update the specific chunk + + writeChunk(name, data, d->chunkOffsets[i] - 8, d->chunkOffsets[i] + 8); + + // Now update the internal offsets + + for(i++; i < d->chunkNames.size(); i++) + d->chunkOffsets[i] += sizeDifference; + + return; + } + } + + debug("Could not find chunk."); +} + //////////////////////////////////////////////////////////////////////////////// // private members //////////////////////////////////////////////////////////////////////////////// @@ -120,11 +157,20 @@ void RIFF::File::read() ByteVector chunkName = readBlock(4); uint chunkSize = readBlock(4).toUInt(bigEndian); - d->chunkNames.append(chunkName); - d->chunkSizes.append(chunkSize); + d->chunkNames.push_back(chunkName); + d->chunkSizes.push_back(chunkSize); - d->chunkOffsets.append(tell()); + d->chunkOffsets.push_back(tell()); seek(chunkSize, Current); } } + +void RIFF::File::writeChunk(const ByteVector &name, const ByteVector &data, + ulong offset, ulong replace) +{ + ByteVector value = name; + value.append(ByteVector::fromUInt(data.size(), d->endianness == BigEndian)); + value.append(data); + insert(data, offset, replace); +} diff --git a/taglib/riff/rifffile.h b/taglib/riff/rifffile.h index c02e6ec7..3b375e29 100644 --- a/taglib/riff/rifffile.h +++ b/taglib/riff/rifffile.h @@ -93,6 +93,8 @@ namespace TagLib { File &operator=(const File &); void read(); + void writeChunk(const ByteVector &name, const ByteVector &data, + ulong offset, ulong replace = 0); class FilePrivate; FilePrivate *d;