/[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 1859 by persson, Sun Mar 8 12:24:56 2009 UTC revision 1953 by schoenebeck, Thu Jul 30 08:16:02 2009 UTC
# Line 22  Line 22 
22   ***************************************************************************/   ***************************************************************************/
23    
24  #include <algorithm>  #include <algorithm>
25    #include <set>
26  #include <string.h>  #include <string.h>
27    
28  #include "RIFF.h"  #include "RIFF.h"
# Line 59  namespace RIFF { Line 60  namespace RIFF {
60          ulPos      = 0;          ulPos      = 0;
61          pParent    = NULL;          pParent    = NULL;
62          pChunkData = NULL;          pChunkData = NULL;
63            CurrentChunkSize = 0;
64            NewChunkSize = 0;
65          ulChunkDataSize = 0;          ulChunkDataSize = 0;
66          ChunkID    = CHUNK_ID_RIFF;          ChunkID    = CHUNK_ID_RIFF;
67          this->pFile = pFile;          this->pFile = pFile;
# Line 73  namespace RIFF { Line 76  namespace RIFF {
76          pParent       = Parent;          pParent       = Parent;
77          ulPos         = 0;          ulPos         = 0;
78          pChunkData    = NULL;          pChunkData    = NULL;
79            CurrentChunkSize = 0;
80            NewChunkSize = 0;
81          ulChunkDataSize = 0;          ulChunkDataSize = 0;
82          ReadHeader(StartPos);          ReadHeader(StartPos);
83      }      }
# Line 83  namespace RIFF { Line 88  namespace RIFF {
88          this->pParent    = pParent;          this->pParent    = pParent;
89          ulPos            = 0;          ulPos            = 0;
90          pChunkData       = NULL;          pChunkData       = NULL;
         ulChunkDataSize  = 0;  
91          ChunkID          = uiChunkID;          ChunkID          = uiChunkID;
92            ulChunkDataSize  = 0;
93          CurrentChunkSize = 0;          CurrentChunkSize = 0;
94          NewChunkSize     = uiBodySize;          NewChunkSize     = uiBodySize;
95      }      }
96    
97      Chunk::~Chunk() {      Chunk::~Chunk() {
98          pFile->UnlogResized(this);          if (pFile) pFile->UnlogResized(this);
99          if (pChunkData) delete[] pChunkData;          if (pChunkData) delete[] pChunkData;
100      }      }
101    
# Line 98  namespace RIFF { Line 103  namespace RIFF {
103          #if DEBUG          #if DEBUG
104          std::cout << "Chunk::Readheader(" << fPos << ") ";          std::cout << "Chunk::Readheader(" << fPos << ") ";
105          #endif // DEBUG          #endif // DEBUG
106            ChunkID = 0;
107            NewChunkSize = CurrentChunkSize = 0;
108          #if POSIX          #if POSIX
109          if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) {          if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) {
110              read(pFile->hFileRead, &ChunkID, 4);              read(pFile->hFileRead, &ChunkID, 4);
# Line 128  namespace RIFF { Line 135  namespace RIFF {
135              }              }
136              #if DEBUG              #if DEBUG
137              std::cout << "ckID=" << convertToString(ChunkID) << " ";              std::cout << "ckID=" << convertToString(ChunkID) << " ";
138              std::cout << "ckSize=" << ChunkSize << " ";              std::cout << "ckSize=" << CurrentChunkSize << " ";
139              std::cout << "bEndianNative=" << bEndianNative << std::endl;              std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl;
140              #endif // DEBUG              #endif // DEBUG
141              NewChunkSize = CurrentChunkSize;              NewChunkSize = CurrentChunkSize;
142          }          }
# Line 225  namespace RIFF { Line 232  namespace RIFF {
232         #if DEBUG         #if DEBUG
233         std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl;         std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl;
234         #endif // DEBUG         #endif // DEBUG
235          return CurrentChunkSize - ulPos;          return (CurrentChunkSize > ulPos) ? CurrentChunkSize - ulPos : 0;
236      }      }
237    
238      /**      /**
# Line 944  namespace RIFF { Line 951  namespace RIFF {
951        #if DEBUG        #if DEBUG
952        std::cout << "List::~List()" << std::endl;        std::cout << "List::~List()" << std::endl;
953        #endif // DEBUG        #endif // DEBUG
954            DeleteChunkList();
955        }
956    
957        void List::DeleteChunkList() {
958          if (pSubChunks) {          if (pSubChunks) {
959              ChunkList::iterator iter = pSubChunks->begin();              ChunkList::iterator iter = pSubChunks->begin();
960              ChunkList::iterator end  = pSubChunks->end();              ChunkList::iterator end  = pSubChunks->end();
# Line 952  namespace RIFF { Line 963  namespace RIFF {
963                  iter++;                  iter++;
964              }              }
965              delete pSubChunks;              delete pSubChunks;
966                pSubChunks = NULL;
967            }
968            if (pSubChunksMap) {
969                delete pSubChunksMap;
970                pSubChunksMap = NULL;
971          }          }
         if (pSubChunksMap) delete pSubChunksMap;  
972      }      }
973    
974      /**      /**
# Line 1229  namespace RIFF { Line 1244  namespace RIFF {
1244        std::cout << "List::Readheader(ulong) ";        std::cout << "List::Readheader(ulong) ";
1245        #endif // DEBUG        #endif // DEBUG
1246          Chunk::ReadHeader(fPos);          Chunk::ReadHeader(fPos);
1247            if (CurrentChunkSize < 4) return;
1248          NewChunkSize = CurrentChunkSize -= 4;          NewChunkSize = CurrentChunkSize -= 4;
1249          #if POSIX          #if POSIX
1250          lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);          lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
# Line 1368  namespace RIFF { Line 1384  namespace RIFF {
1384  // *************** File ***************  // *************** File ***************
1385  // *  // *
1386    
1387    //HACK: to avoid breaking DLL compatibility to older versions of libgig we roll the new std::set<Chunk*> into the old std::list<Chunk*> container, should be replaced on member variable level soon though
1388    #define _GET_RESIZED_CHUNKS() \
1389            (reinterpret_cast<std::set<Chunk*>*>(ResizedChunks.front()))
1390    
1391      /** @brief Create new RIFF file.      /** @brief Create new RIFF file.
1392       *       *
1393       * Use this constructor if you want to create a new RIFF file completely       * Use this constructor if you want to create a new RIFF file completely
# Line 1383  namespace RIFF { Line 1403  namespace RIFF {
1403       * @see AddSubChunk(), AddSubList(), SetByteOrder()       * @see AddSubChunk(), AddSubList(), SetByteOrder()
1404       */       */
1405      File::File(uint32_t FileType) : List(this) {      File::File(uint32_t FileType) : List(this) {
1406            //HACK: see _GET_RESIZED_CHUNKS() comment
1407            ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>));
1408          #if defined(WIN32)          #if defined(WIN32)
1409          hFileRead = hFileWrite = INVALID_HANDLE_VALUE;          hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1410          #else          #else
# Line 1407  namespace RIFF { Line 1429  namespace RIFF {
1429        std::cout << "File::File("<<path<<")" << std::endl;        std::cout << "File::File("<<path<<")" << std::endl;
1430        #endif // DEBUG        #endif // DEBUG
1431          bEndianNative = true;          bEndianNative = true;
1432            //HACK: see _GET_RESIZED_CHUNKS() comment
1433            ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>));
1434          #if POSIX          #if POSIX
1435          hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK);          hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK);
1436          if (hFileRead <= 0) {          if (hFileRead <= 0) {
# Line 1432  namespace RIFF { Line 1456  namespace RIFF {
1456          Mode = stream_mode_read;          Mode = stream_mode_read;
1457          ulStartPos = RIFF_HEADER_SIZE;          ulStartPos = RIFF_HEADER_SIZE;
1458          ReadHeader(0);          ReadHeader(0);
1459          if (ChunkID != CHUNK_ID_RIFF) {          if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) {
1460              throw RIFF::Exception("Not a RIFF file");              throw RIFF::Exception("Not a RIFF file");
1461          }          }
1462      }      }
# Line 1592  namespace RIFF { Line 1616  namespace RIFF {
1616    
1617          // 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)
1618          unsigned long ulPositiveSizeDiff = 0;          unsigned long ulPositiveSizeDiff = 0;
1619          for (std::set<Chunk*>::const_iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) {          std::set<Chunk*>* resizedChunks = _GET_RESIZED_CHUNKS();
1620            for (std::set<Chunk*>::const_iterator iter = resizedChunks->begin(), end = resizedChunks->end(); iter != end; ++iter) {
1621              if ((*iter)->GetNewSize() == 0) {              if ((*iter)->GetNewSize() == 0) {
1622                  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));
1623              }              }
# Line 1648  namespace RIFF { Line 1673  namespace RIFF {
1673          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);
1674    
1675          // forget all resized chunks          // forget all resized chunks
1676          ResizedChunks.clear();          resizedChunks->clear();
1677      }      }
1678    
1679      /** @brief Save changes to another file.      /** @brief Save changes to another file.
# Line 1705  namespace RIFF { Line 1730  namespace RIFF {
1730          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);
1731    
1732          // forget all resized chunks          // forget all resized chunks
1733          ResizedChunks.clear();          _GET_RESIZED_CHUNKS()->clear();
1734    
1735          #if POSIX          #if POSIX
1736          if (hFileWrite) close(hFileWrite);          if (hFileWrite) close(hFileWrite);
# Line 1748  namespace RIFF { Line 1773  namespace RIFF {
1773          #else          #else
1774          if (hFileRead) fclose(hFileRead);          if (hFileRead) fclose(hFileRead);
1775          #endif // POSIX          #endif // POSIX
1776            DeleteChunkList();
1777            pFile = NULL;
1778            //HACK: see _GET_RESIZED_CHUNKS() comment
1779            delete _GET_RESIZED_CHUNKS();
1780      }      }
1781    
1782      void File::LogAsResized(Chunk* pResizedChunk) {      void File::LogAsResized(Chunk* pResizedChunk) {
1783          ResizedChunks.insert(pResizedChunk);          _GET_RESIZED_CHUNKS()->insert(pResizedChunk);
1784      }      }
1785    
1786      void File::UnlogResized(Chunk* pResizedChunk) {      void File::UnlogResized(Chunk* pResizedChunk) {
1787          ResizedChunks.erase(pResizedChunk);          _GET_RESIZED_CHUNKS()->erase(pResizedChunk);
1788      }      }
1789    
1790      unsigned long File::GetFileSize() {      unsigned long File::GetFileSize() {

Legend:
Removed from v.1859  
changed lines
  Added in v.1953

  ViewVC Help
Powered by ViewVC