Untested chunk writing code.

git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@808237 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
This commit is contained in:
Scott Wheeler
2008-05-16 06:01:52 +00:00
parent d302b299d1
commit b2778ed715
2 changed files with 55 additions and 7 deletions

View File

@ -23,8 +23,12 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#include <tbytevector.h>
#include <tdebug.h>
#include <tstring.h>
#include "rifffile.h"
#include <tbytevectorlist.h>
#include <vector>
using namespace TagLib;
@ -42,9 +46,9 @@ public:
uint size;
ByteVector format;
ByteVectorList chunkNames;
List<uint> chunkOffsets;
List<uint> chunkSizes;
std::vector<ByteVector> chunkNames;
std::vector<uint> chunkOffsets;
std::vector<uint> 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);
}

View File

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