]> granicus.if.org Git - taglib/commitdiff
Untested chunk writing code.
authorScott Wheeler <wheeler@kde.org>
Fri, 16 May 2008 06:01:52 +0000 (06:01 +0000)
committerScott Wheeler <wheeler@kde.org>
Fri, 16 May 2008 06:01:52 +0000 (06:01 +0000)
git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@808237 283d02a7-25f6-0310-bc7c-ecb5cbfe19da

taglib/riff/rifffile.cpp
taglib/riff/rifffile.h

index 1b3b9d84eef8f026fda1e1e57936477ec556f503..ce8d118e2e2de3d37f316cbfdbafe669a2cd1ae5 100644 (file)
  *   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);
+}
index c02e6ec7a06011118602f52af4e08fccd1966923..3b375e29e8f6e4adfedce5c153b1f96e9f1f7e19 100644 (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;