/[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 1678 by persson, Sun Feb 10 16:07:22 2008 UTC revision 1869 by persson, Sun Mar 22 11:13:25 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 58  namespace RIFF { Line 59  namespace RIFF {
59          ulPos      = 0;          ulPos      = 0;
60          pParent    = NULL;          pParent    = NULL;
61          pChunkData = NULL;          pChunkData = NULL;
62            CurrentChunkSize = 0;
63            NewChunkSize = 0;
64          ulChunkDataSize = 0;          ulChunkDataSize = 0;
65          ChunkID    = CHUNK_ID_RIFF;          ChunkID    = CHUNK_ID_RIFF;
66          this->pFile = pFile;          this->pFile = pFile;
# Line 72  namespace RIFF { Line 75  namespace RIFF {
75          pParent       = Parent;          pParent       = Parent;
76          ulPos         = 0;          ulPos         = 0;
77          pChunkData    = NULL;          pChunkData    = NULL;
78            CurrentChunkSize = 0;
79            NewChunkSize = 0;
80          ulChunkDataSize = 0;          ulChunkDataSize = 0;
81          ReadHeader(StartPos);          ReadHeader(StartPos);
82      }      }
# Line 82  namespace RIFF { Line 87  namespace RIFF {
87          this->pParent    = pParent;          this->pParent    = pParent;
88          ulPos            = 0;          ulPos            = 0;
89          pChunkData       = NULL;          pChunkData       = NULL;
         ulChunkDataSize  = 0;  
90          ChunkID          = uiChunkID;          ChunkID          = uiChunkID;
91            ulChunkDataSize  = 0;
92          CurrentChunkSize = 0;          CurrentChunkSize = 0;
93          NewChunkSize     = uiBodySize;          NewChunkSize     = uiBodySize;
94      }      }
95    
96      Chunk::~Chunk() {      Chunk::~Chunk() {
97          pFile->UnlogResized(this);          if (pFile) pFile->UnlogResized(this);
98          if (pChunkData) delete[] pChunkData;          if (pChunkData) delete[] pChunkData;
99      }      }
100    
# Line 97  namespace RIFF { Line 102  namespace RIFF {
102          #if DEBUG          #if DEBUG
103          std::cout << "Chunk::Readheader(" << fPos << ") ";          std::cout << "Chunk::Readheader(" << fPos << ") ";
104          #endif // DEBUG          #endif // DEBUG
105            ChunkID = 0;
106            NewChunkSize = CurrentChunkSize = 0;
107          #if POSIX          #if POSIX
108          if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) {          if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) {
109              read(pFile->hFileRead, &ChunkID, 4);              read(pFile->hFileRead, &ChunkID, 4);
# Line 127  namespace RIFF { Line 134  namespace RIFF {
134              }              }
135              #if DEBUG              #if DEBUG
136              std::cout << "ckID=" << convertToString(ChunkID) << " ";              std::cout << "ckID=" << convertToString(ChunkID) << " ";
137              std::cout << "ckSize=" << ChunkSize << " ";              std::cout << "ckSize=" << CurrentChunkSize << " ";
138              std::cout << "bEndianNative=" << bEndianNative << std::endl;              std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl;
139              #endif // DEBUG              #endif // DEBUG
140              NewChunkSize = CurrentChunkSize;              NewChunkSize = CurrentChunkSize;
141          }          }
# Line 224  namespace RIFF { Line 231  namespace RIFF {
231         #if DEBUG         #if DEBUG
232         std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl;         std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl;
233         #endif // DEBUG         #endif // DEBUG
234          return CurrentChunkSize - ulPos;          return (CurrentChunkSize > ulPos) ? CurrentChunkSize - ulPos : 0;
235      }      }
236    
237      /**      /**
# Line 273  namespace RIFF { Line 280  namespace RIFF {
280         #if DEBUG         #if DEBUG
281         std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl;         std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl;
282         #endif // DEBUG         #endif // DEBUG
283            if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet)
284          if (ulPos >= CurrentChunkSize) return 0;          if (ulPos >= CurrentChunkSize) return 0;
285          if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize;          if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize;
286          #if POSIX          #if POSIX
# Line 725  namespace RIFF { Line 733  namespace RIFF {
733       * @see ReleaseChunkData()       * @see ReleaseChunkData()
734       */       */
735      void* Chunk::LoadChunkData() {      void* Chunk::LoadChunkData() {
736          if (!pChunkData && pFile->Filename != "") {          if (!pChunkData && pFile->Filename != "" && ulStartPos != 0) {
737              #if POSIX              #if POSIX
738              if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;              if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;
739              #elif defined(WIN32)              #elif defined(WIN32)
# Line 942  namespace RIFF { Line 950  namespace RIFF {
950        #if DEBUG        #if DEBUG
951        std::cout << "List::~List()" << std::endl;        std::cout << "List::~List()" << std::endl;
952        #endif // DEBUG        #endif // DEBUG
953            DeleteChunkList();
954        }
955    
956        void List::DeleteChunkList() {
957          if (pSubChunks) {          if (pSubChunks) {
958              ChunkList::iterator iter = pSubChunks->begin();              ChunkList::iterator iter = pSubChunks->begin();
959              ChunkList::iterator end  = pSubChunks->end();              ChunkList::iterator end  = pSubChunks->end();
# Line 950  namespace RIFF { Line 962  namespace RIFF {
962                  iter++;                  iter++;
963              }              }
964              delete pSubChunks;              delete pSubChunks;
965                pSubChunks = NULL;
966            }
967            if (pSubChunksMap) {
968                delete pSubChunksMap;
969                pSubChunksMap = NULL;
970          }          }
         if (pSubChunksMap) delete pSubChunksMap;  
971      }      }
972    
973      /**      /**
# Line 1227  namespace RIFF { Line 1243  namespace RIFF {
1243        std::cout << "List::Readheader(ulong) ";        std::cout << "List::Readheader(ulong) ";
1244        #endif // DEBUG        #endif // DEBUG
1245          Chunk::ReadHeader(fPos);          Chunk::ReadHeader(fPos);
1246            if (CurrentChunkSize < 8) return;
1247          NewChunkSize = CurrentChunkSize -= 4;          NewChunkSize = CurrentChunkSize -= 4;
1248          #if POSIX          #if POSIX
1249          lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);          lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
# Line 1430  namespace RIFF { Line 1447  namespace RIFF {
1447          Mode = stream_mode_read;          Mode = stream_mode_read;
1448          ulStartPos = RIFF_HEADER_SIZE;          ulStartPos = RIFF_HEADER_SIZE;
1449          ReadHeader(0);          ReadHeader(0);
1450          if (ChunkID != CHUNK_ID_RIFF) {          if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) {
1451              throw RIFF::Exception("Not a RIFF file");              throw RIFF::Exception("Not a RIFF file");
1452          }          }
1453      }      }
# Line 1594  namespace RIFF { Line 1611  namespace RIFF {
1611              if ((*iter)->GetNewSize() == 0) {              if ((*iter)->GetNewSize() == 0) {
1612                  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));
1613              }              }
1614              unsigned long ulDiff = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2 - (*iter)->GetSize() - (*iter)->GetSize() % 2;              unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2;
1615              if (ulDiff > 0) ulPositiveSizeDiff += ulDiff;              unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2;
1616                if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded;
1617          }          }
1618    
1619          unsigned long ulWorkingFileSize = GetFileSize();          unsigned long ulWorkingFileSize = GetFileSize();
# Line 1745  namespace RIFF { Line 1763  namespace RIFF {
1763          #else          #else
1764          if (hFileRead) fclose(hFileRead);          if (hFileRead) fclose(hFileRead);
1765          #endif // POSIX          #endif // POSIX
1766            DeleteChunkList();
1767            pFile = NULL;
1768      }      }
1769    
1770      void File::LogAsResized(Chunk* pResizedChunk) {      void File::LogAsResized(Chunk* pResizedChunk) {

Legend:
Removed from v.1678  
changed lines
  Added in v.1869

  ViewVC Help
Powered by ViewVC