/[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 1183 by persson, Sun May 13 10:34:29 2007 UTC revision 1713 by persson, Thu Mar 6 20:42:22 2008 UTC
# 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 33  namespace RIFF { Line 34  namespace RIFF {
34  // *  // *
35    
36      /// Returns a human readable path of the given chunk.      /// Returns a human readable path of the given chunk.
37      String __resolveChunkPath(Chunk* pCk) {      static String __resolveChunkPath(Chunk* pCk) {
38          String sPath;          String sPath;
39          for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) {          for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) {
40              if (pChunk->GetChunkID() == CHUNK_ID_LIST) {              if (pChunk->GetChunkID() == CHUNK_ID_LIST) {
# 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 235  namespace RIFF { Line 236  namespace RIFF {
236       *  - RIFF::stream_closed :       *  - RIFF::stream_closed :
237       *    the data stream was closed somehow, no more reading possible       *    the data stream was closed somehow, no more reading possible
238       *  - RIFF::stream_end_reached :       *  - RIFF::stream_end_reached :
239       *    alreaady reached the end of the chunk data, no more reading       *    already reached the end of the chunk data, no more reading
240       *    possible without SetPos()       *    possible without SetPos()
241       */       */
242      stream_state_t Chunk::GetState() {      stream_state_t Chunk::GetState() {
# Line 358  namespace RIFF { Line 359  namespace RIFF {
359          #elif defined(WIN32)          #elif defined(WIN32)
360          if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {          if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
361              throw Exception("Could not seek to position " + ToString(ulPos) +              throw Exception("Could not seek to position " + ToString(ulPos) +
362                              " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");                                          " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");
363          }          }
364          DWORD writtenWords;          DWORD writtenWords;
365          WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL);          WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL);
# Line 1152  namespace RIFF { Line 1153  namespace RIFF {
1153          pSubChunks->push_back(pNewChunk);          pSubChunks->push_back(pNewChunk);
1154          (*pSubChunksMap)[uiChunkID] = pNewChunk;          (*pSubChunksMap)[uiChunkID] = pNewChunk;
1155          pNewChunk->Resize(uiBodySize);          pNewChunk->Resize(uiBodySize);
1156            NewChunkSize += CHUNK_HEADER_SIZE;
1157            pFile->LogAsResized(this);
1158          return pNewChunk;          return pNewChunk;
1159      }      }
1160    
# Line 1187  namespace RIFF { Line 1190  namespace RIFF {
1190          List* pNewListChunk = new List(pFile, this, uiListType);          List* pNewListChunk = new List(pFile, this, uiListType);
1191          pSubChunks->push_back(pNewListChunk);          pSubChunks->push_back(pNewListChunk);
1192          (*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk;          (*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk;
1193            NewChunkSize += LIST_HEADER_SIZE;
1194            pFile->LogAsResized(this);
1195          return pNewListChunk;          return pNewListChunk;
1196      }      }
1197    
# Line 1268  namespace RIFF { Line 1273  namespace RIFF {
1273          if (!pSubChunks) {          if (!pSubChunks) {
1274              pSubChunks    = new ChunkList();              pSubChunks    = new ChunkList();
1275              pSubChunksMap = new ChunkMap();              pSubChunksMap = new ChunkMap();
1276                #if defined(WIN32)
1277                if (pFile->hFileRead == INVALID_HANDLE_VALUE) return;
1278                #else
1279              if (!pFile->hFileRead) return;              if (!pFile->hFileRead) return;
1280                #endif
1281              unsigned long uiOriginalPos = GetPos();              unsigned long uiOriginalPos = GetPos();
1282              SetPos(0); // jump to beginning of list chunk body              SetPos(0); // jump to beginning of list chunk body
1283              while (RemainingBytes() >= CHUNK_HEADER_SIZE) {              while (RemainingBytes() >= CHUNK_HEADER_SIZE) {
# Line 1363  namespace RIFF { Line 1372  namespace RIFF {
1372       * 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
1373       * "from scratch". Note: there must be no empty chunks or empty list       * "from scratch". Note: there must be no empty chunks or empty list
1374       * chunks when trying to make the new RIFF file persistent with Save()!       * chunks when trying to make the new RIFF file persistent with Save()!
1375       * Note that this constructor will create a RIFX file on       *
1376       * big-endian machines.       * Note: by default, the RIFF file will be saved in native endian
1377         * format; that is, as a RIFF file on little-endian machines and
1378         * as a RIFX file on big-endian. To change this behaviour, call
1379         * SetByteOrder() before calling Save().
1380       *       *
1381       * @param FileType - four-byte identifier of the RIFF file type       * @param FileType - four-byte identifier of the RIFF file type
1382       * @see AddSubChunk(), AddSubList()       * @see AddSubChunk(), AddSubList(), SetByteOrder()
1383       */       */
1384      File::File(uint32_t FileType) : List(this) {      File::File(uint32_t FileType) : List(this) {
1385          #if defined(WIN32)          #if defined(WIN32)
# Line 1381  namespace RIFF { Line 1393  namespace RIFF {
1393          ListType = FileType;          ListType = FileType;
1394      }      }
1395    
   
     /** @brief Create new RIFF file.  
      *  
      * Use this constructor if you want to create a new RIFF file  
      * completely "from scratch" and also want to specify  
      * endianess. Note: there must be no empty chunks or empty list  
      * chunks when trying to make the new RIFF file persistent with  
      * Save()!  
      *  
      * @param FileType - four-byte identifier of the RIFF file type  
      * @param Endian - endianess used in the new file. A value of  
      *                 endian_little will create a RIFF file,  
      *                 endian_big a RIFX file, endian_native will  
      *                 create a RIFF file on little-endian machines  
      *                 and RIFX on big-endian.  
      * @see AddSubChunk(), AddSubList()  
      */  
     File::File(uint32_t FileType, endian_t Endian) : List(this) {  
         #if defined(WIN32)  
         hFileRead = hFileWrite = INVALID_HANDLE_VALUE;  
         #else  
         hFileRead = hFileWrite = 0;  
         #endif  
         Mode = stream_mode_closed;  
         #if WORDS_BIGENDIAN  
         bEndianNative = Endian != endian_little;  
         #else  
         bEndianNative = Endian != endian_big;  
         #endif  
         ulStartPos = RIFF_HEADER_SIZE;  
         ListType = FileType;  
     }  
   
1396      /** @brief Load existing RIFF file.      /** @brief Load existing RIFF file.
1397       *       *
1398       * Loads an existing RIFF file with all its chunks.       * Loads an existing RIFF file with all its chunks.
# Line 1438  namespace RIFF { Line 1417  namespace RIFF {
1417                                       path.c_str(), GENERIC_READ,                                       path.c_str(), GENERIC_READ,
1418                                       FILE_SHARE_READ | FILE_SHARE_WRITE,                                       FILE_SHARE_READ | FILE_SHARE_WRITE,
1419                                       NULL, OPEN_EXISTING,                                       NULL, OPEN_EXISTING,
1420                                       FILE_ATTRIBUTE_NORMAL, NULL                                       FILE_ATTRIBUTE_NORMAL |
1421                                         FILE_FLAG_RANDOM_ACCESS, NULL
1422                                   );                                   );
1423          if (hFileRead == INVALID_HANDLE_VALUE) {          if (hFileRead == INVALID_HANDLE_VALUE) {
1424              hFileRead = hFileWrite = INVALID_HANDLE_VALUE;              hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
# Line 1491  namespace RIFF { Line 1471  namespace RIFF {
1471                                                   Filename.c_str(), GENERIC_READ,                                                   Filename.c_str(), GENERIC_READ,
1472                                                   FILE_SHARE_READ | FILE_SHARE_WRITE,                                                   FILE_SHARE_READ | FILE_SHARE_WRITE,
1473                                                   NULL, OPEN_EXISTING,                                                   NULL, OPEN_EXISTING,
1474                                                   FILE_ATTRIBUTE_NORMAL, NULL                                                   FILE_ATTRIBUTE_NORMAL |
1475                                                     FILE_FLAG_RANDOM_ACCESS,
1476                                                     NULL
1477                                               );                                               );
1478                      if (hFileRead == INVALID_HANDLE_VALUE) {                      if (hFileRead == INVALID_HANDLE_VALUE) {
1479                          hFileRead = hFileWrite = INVALID_HANDLE_VALUE;                          hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
# Line 1519  namespace RIFF { Line 1501  namespace RIFF {
1501                                                   GENERIC_READ | GENERIC_WRITE,                                                   GENERIC_READ | GENERIC_WRITE,
1502                                                   FILE_SHARE_READ,                                                   FILE_SHARE_READ,
1503                                                   NULL, OPEN_ALWAYS,                                                   NULL, OPEN_ALWAYS,
1504                                                   FILE_ATTRIBUTE_NORMAL, NULL                                                   FILE_ATTRIBUTE_NORMAL |
1505                                                     FILE_FLAG_RANDOM_ACCESS,
1506                                                     NULL
1507                                               );                                               );
1508                      if (hFileRead == INVALID_HANDLE_VALUE) {                      if (hFileRead == INVALID_HANDLE_VALUE) {
1509                          hFileRead = hFileWrite = CreateFile(                          hFileRead = hFileWrite = CreateFile(
1510                                                       Filename.c_str(), GENERIC_READ,                                                       Filename.c_str(), GENERIC_READ,
1511                                                       FILE_SHARE_READ | FILE_SHARE_WRITE,                                                       FILE_SHARE_READ | FILE_SHARE_WRITE,
1512                                                       NULL, OPEN_EXISTING,                                                       NULL, OPEN_EXISTING,
1513                                                       FILE_ATTRIBUTE_NORMAL, NULL                                                       FILE_ATTRIBUTE_NORMAL |
1514                                                         FILE_FLAG_RANDOM_ACCESS,
1515                                                         NULL
1516                                                   );                                                   );
1517                          throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");                          throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode");
1518                      }                      }
1519                      #else                      #else
1520                      if (hFileRead) fclose(hFileRead);                      if (hFileRead) fclose(hFileRead);
# Line 1562  namespace RIFF { Line 1548  namespace RIFF {
1548          return false;          return false;
1549      }      }
1550    
1551        /** @brief Set the byte order to be used when saving.
1552         *
1553         * Set the byte order to be used in the file. A value of
1554         * endian_little will create a RIFF file, endian_big a RIFX file
1555         * and endian_native will create a RIFF file on little-endian
1556         * machines and RIFX on big-endian machines.
1557         *
1558         * @param Endian - endianess to use when file is saved.
1559         */
1560        void File::SetByteOrder(endian_t Endian) {
1561            #if WORDS_BIGENDIAN
1562            bEndianNative = Endian != endian_little;
1563            #else
1564            bEndianNative = Endian != endian_big;
1565            #endif
1566        }
1567    
1568      /** @brief Save changes to same file.      /** @brief Save changes to same file.
1569       *       *
1570       * Make all changes of all chunks persistent by writing them to the       * Make all changes of all chunks persistent by writing them to the
# Line 1588  namespace RIFF { Line 1591  namespace RIFF {
1591    
1592          // 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)
1593          unsigned long ulPositiveSizeDiff = 0;          unsigned long ulPositiveSizeDiff = 0;
1594          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) {
1595              if ((*iter)->GetNewSize() == 0) {              if ((*iter)->GetNewSize() == 0) {
1596                  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));
1597              }              }
1598              if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) {              unsigned long ulDiff = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2 - (*iter)->GetSize() - (*iter)->GetSize() % 2;
1599                  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;  
             }  
1600          }          }
1601    
1602          unsigned long ulWorkingFileSize = GetFileSize();          unsigned long ulWorkingFileSize = GetFileSize();
# Line 1611  namespace RIFF { Line 1612  namespace RIFF {
1612              #if defined(WIN32)              #if defined(WIN32)
1613              DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured              DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
1614              #else              #else
1615              int iBytesMoved = 1;              int iBytesMoved = 1;
1616              #endif              #endif
1617              for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) {              for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) {
1618                  const unsigned long ulToMove = ulFileSize - ulPos;                  iBytesMoved = (ulPos < 4096) ? ulPos : 4096;
1619                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;                  ulPos -= iBytesMoved;
1620                  #if POSIX                  #if POSIX
1621                  lseek(hFileRead, ulPos, SEEK_SET);                  lseek(hFileRead, ulPos, SEEK_SET);
1622                  iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);                  iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);
# Line 1678  namespace RIFF { Line 1679  namespace RIFF {
1679          #elif defined(WIN32)          #elif defined(WIN32)
1680          hFileWrite = CreateFile(          hFileWrite = CreateFile(
1681                           path.c_str(), GENERIC_WRITE, FILE_SHARE_READ,                           path.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
1682                           NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL                           NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL |
1683                             FILE_FLAG_RANDOM_ACCESS, NULL
1684                       );                       );
1685          if (hFileWrite == INVALID_HANDLE_VALUE) {          if (hFileWrite == INVALID_HANDLE_VALUE) {
1686              hFileWrite = hFileRead;              hFileWrite = hFileRead;
# Line 1703  namespace RIFF { Line 1705  namespace RIFF {
1705          // forget all resized chunks          // forget all resized chunks
1706          ResizedChunks.clear();          ResizedChunks.clear();
1707    
1708          if (Filename.length() > 0) {          #if POSIX
1709              #if POSIX          if (hFileWrite) close(hFileWrite);
1710              close(hFileWrite);          #elif defined(WIN32)
1711              #elif defined(WIN32)          if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite);
1712              CloseHandle(hFileWrite);          #else
1713              #else          if (hFileWrite) fclose(hFileWrite);
1714              fclose(hFileWrite);          #endif
1715              #endif          hFileWrite = hFileRead;
             hFileWrite = hFileRead;  
         }  
1716    
1717          // associate new file with this File object from now on          // associate new file with this File object from now on
1718          Filename = path;          Filename = path;
# Line 1749  namespace RIFF { Line 1749  namespace RIFF {
1749      }      }
1750    
1751      void File::LogAsResized(Chunk* pResizedChunk) {      void File::LogAsResized(Chunk* pResizedChunk) {
1752          ResizedChunks.push_back(pResizedChunk);          ResizedChunks.insert(pResizedChunk);
1753      }      }
1754    
1755      void File::UnlogResized(Chunk* pResizedChunk) {      void File::UnlogResized(Chunk* pResizedChunk) {
1756          ResizedChunks.remove(pResizedChunk);          ResizedChunks.erase(pResizedChunk);
1757      }      }
1758    
1759      unsigned long File::GetFileSize() {      unsigned long File::GetFileSize() {

Legend:
Removed from v.1183  
changed lines
  Added in v.1713

  ViewVC Help
Powered by ViewVC