/[svn]/libgig/trunk/src/RIFF.cpp
ViewVC logotype

Diff of /libgig/trunk/src/RIFF.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1459 by schoenebeck, Fri Oct 26 10:58:41 2007 UTC revision 1851 by schoenebeck, Sun Mar 1 22:08:32 2009 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file access library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2007 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2009 by Christian Schoenebeck                      *
6   *                              <cuse@users.sourceforge.net>               *   *                              <cuse@users.sourceforge.net>               *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
# Line 21  Line 21 
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
23    
24    #include <algorithm>
25  #include <string.h>  #include <string.h>
26    
27  #include "RIFF.h"  #include "RIFF.h"
# Line 89  namespace RIFF { Line 90  namespace RIFF {
90      }      }
91    
92      Chunk::~Chunk() {      Chunk::~Chunk() {
93          if (CurrentChunkSize != NewChunkSize) pFile->UnlogResized(this);          pFile->UnlogResized(this);
94          if (pChunkData) delete[] pChunkData;          if (pChunkData) delete[] pChunkData;
95      }      }
96    
# Line 273  namespace RIFF { Line 274  namespace RIFF {
274         #if DEBUG         #if DEBUG
275         std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl;         std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl;
276         #endif // DEBUG         #endif // DEBUG
277            if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet)
278          if (ulPos >= CurrentChunkSize) return 0;          if (ulPos >= CurrentChunkSize) return 0;
279          if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize;          if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize;
280          #if POSIX          #if POSIX
# Line 725  namespace RIFF { Line 727  namespace RIFF {
727       * @see ReleaseChunkData()       * @see ReleaseChunkData()
728       */       */
729      void* Chunk::LoadChunkData() {      void* Chunk::LoadChunkData() {
730          if (!pChunkData && pFile->Filename != "") {          if (!pChunkData && pFile->Filename != "" && ulStartPos != 0) {
731              #if POSIX              #if POSIX
732              if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;              if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;
733              #elif defined(WIN32)              #elif defined(WIN32)
# Line 1152  namespace RIFF { Line 1154  namespace RIFF {
1154          pSubChunks->push_back(pNewChunk);          pSubChunks->push_back(pNewChunk);
1155          (*pSubChunksMap)[uiChunkID] = pNewChunk;          (*pSubChunksMap)[uiChunkID] = pNewChunk;
1156          pNewChunk->Resize(uiBodySize);          pNewChunk->Resize(uiBodySize);
1157            NewChunkSize += CHUNK_HEADER_SIZE;
1158            pFile->LogAsResized(this);
1159          return pNewChunk;          return pNewChunk;
1160      }      }
1161    
# Line 1187  namespace RIFF { Line 1191  namespace RIFF {
1191          List* pNewListChunk = new List(pFile, this, uiListType);          List* pNewListChunk = new List(pFile, this, uiListType);
1192          pSubChunks->push_back(pNewListChunk);          pSubChunks->push_back(pNewListChunk);
1193          (*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk;          (*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk;
1194            NewChunkSize += LIST_HEADER_SIZE;
1195            pFile->LogAsResized(this);
1196          return pNewListChunk;          return pNewListChunk;
1197      }      }
1198    
# Line 1586  namespace RIFF { Line 1592  namespace RIFF {
1592    
1593          // first we sum up all positive chunk size changes (and skip all negative ones)          // first we sum up all positive chunk size changes (and skip all negative ones)
1594          unsigned long ulPositiveSizeDiff = 0;          unsigned long ulPositiveSizeDiff = 0;
1595          for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) {          for (std::set<Chunk*>::const_iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) {
1596              if ((*iter)->GetNewSize() == 0) {              if ((*iter)->GetNewSize() == 0) {
1597                  throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter));                  throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter));
1598              }              }
1599              if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) {              unsigned long ulDiff = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2 - (*iter)->GetSize() - (*iter)->GetSize() % 2;
1600                  unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte              if (ulDiff > 0) ulPositiveSizeDiff += ulDiff;
                 ulPositiveSizeDiff += ulDiff;  
             }  
1601          }          }
1602    
1603          unsigned long ulWorkingFileSize = GetFileSize();          unsigned long ulWorkingFileSize = GetFileSize();
# Line 1611  namespace RIFF { Line 1615  namespace RIFF {
1615              #else              #else
1616              int iBytesMoved = 1;              int iBytesMoved = 1;
1617              #endif              #endif
1618              for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) {              for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) {
1619                  const unsigned long ulToMove = ulFileSize - ulPos;                  iBytesMoved = (ulPos < 4096) ? ulPos : 4096;
1620                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;                  ulPos -= iBytesMoved;
1621                  #if POSIX                  #if POSIX
1622                  lseek(hFileRead, ulPos, SEEK_SET);                  lseek(hFileRead, ulPos, SEEK_SET);
1623                  iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);                  iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);
# Line 1746  namespace RIFF { Line 1750  namespace RIFF {
1750      }      }
1751    
1752      void File::LogAsResized(Chunk* pResizedChunk) {      void File::LogAsResized(Chunk* pResizedChunk) {
1753          ResizedChunks.push_back(pResizedChunk);          ResizedChunks.insert(pResizedChunk);
1754      }      }
1755    
1756      void File::UnlogResized(Chunk* pResizedChunk) {      void File::UnlogResized(Chunk* pResizedChunk) {
1757          ResizedChunks.remove(pResizedChunk);          ResizedChunks.erase(pResizedChunk);
1758      }      }
1759    
1760      unsigned long File::GetFileSize() {      unsigned long File::GetFileSize() {

Legend:
Removed from v.1459  
changed lines
  Added in v.1851

  ViewVC Help
Powered by ViewVC