/[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 1093 by schoenebeck, Sun Mar 11 17:44:31 2007 UTC revision 2155 by persson, Thu Jan 6 11:33:40 2011 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-2011 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 <set>
26  #include <string.h>  #include <string.h>
27    
28  #include "RIFF.h"  #include "RIFF.h"
# Line 29  Line 31 
31    
32  namespace RIFF {  namespace RIFF {
33    
34    // *************** Internal functions **************
35    // *
36    
37        /// Returns a human readable path of the given chunk.
38        static String __resolveChunkPath(Chunk* pCk) {
39            String sPath;
40            for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) {
41                if (pChunk->GetChunkID() == CHUNK_ID_LIST) {
42                    List* pList = (List*) pChunk;
43                    sPath = "->'" + pList->GetListTypeString() + "'" + sPath;
44                } else {
45                    sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath;
46                }
47            }
48            return sPath;
49        }
50    
51    
52    
53  // *************** Chunk **************  // *************** Chunk **************
54  // *  // *
55    
# Line 39  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 53  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 63  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            if (pFile) pFile->UnlogResized(this);
99          if (pChunkData) delete[] pChunkData;          if (pChunkData) delete[] pChunkData;
100      }      }
101    
# Line 77  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 107  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 204  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 215  namespace RIFF { Line 243  namespace RIFF {
243       *  - RIFF::stream_closed :       *  - RIFF::stream_closed :
244       *    the data stream was closed somehow, no more reading possible       *    the data stream was closed somehow, no more reading possible
245       *  - RIFF::stream_end_reached :       *  - RIFF::stream_end_reached :
246       *    alreaady reached the end of the chunk data, no more reading       *    already reached the end of the chunk data, no more reading
247       *    possible without SetPos()       *    possible without SetPos()
248       */       */
249      stream_state_t Chunk::GetState() {      stream_state_t Chunk::GetState() {
# Line 253  namespace RIFF { Line 281  namespace RIFF {
281         #if DEBUG         #if DEBUG
282         std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl;         std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl;
283         #endif // DEBUG         #endif // DEBUG
284            if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet)
285          if (ulPos >= CurrentChunkSize) return 0;          if (ulPos >= CurrentChunkSize) return 0;
286          if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize;          if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize;
287          #if POSIX          #if POSIX
# Line 338  namespace RIFF { Line 367  namespace RIFF {
367          #elif defined(WIN32)          #elif defined(WIN32)
368          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) {
369              throw Exception("Could not seek to position " + ToString(ulPos) +              throw Exception("Could not seek to position " + ToString(ulPos) +
370                              " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");                                          " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");
371          }          }
372          DWORD writtenWords;          DWORD writtenWords;
373          WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL);          WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL);
# Line 705  namespace RIFF { Line 734  namespace RIFF {
734       * @see ReleaseChunkData()       * @see ReleaseChunkData()
735       */       */
736      void* Chunk::LoadChunkData() {      void* Chunk::LoadChunkData() {
737          if (!pChunkData && pFile->Filename != "") {          if (!pChunkData && pFile->Filename != "" && ulStartPos != 0) {
738              #if POSIX              #if POSIX
739              if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;              if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;
740              #elif defined(WIN32)              #elif defined(WIN32)
# Line 774  namespace RIFF { Line 803  namespace RIFF {
803       * @see File::Save()       * @see File::Save()
804       */       */
805      void Chunk::Resize(int iNewSize) {      void Chunk::Resize(int iNewSize) {
806          if (iNewSize <= 0) throw Exception("Chunk size must be at least one byte");          if (iNewSize <= 0)
807                throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this));
808          if (NewChunkSize == iNewSize) return;          if (NewChunkSize == iNewSize) return;
809          NewChunkSize = iNewSize;          NewChunkSize = iNewSize;
810          pFile->LogAsResized(this);          pFile->LogAsResized(this);
# Line 831  namespace RIFF { Line 861  namespace RIFF {
861              #else              #else
862              int iBytesMoved = 1;              int iBytesMoved = 1;
863              #endif              #endif
864              for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) {              for (unsigned long ulOffset = 0; ulToMove > 0 && iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) {
865                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;
866                  #if POSIX                  #if POSIX
867                  lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);                  lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);
# Line 921  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 929  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 1131  namespace RIFF { Line 1169  namespace RIFF {
1169          pSubChunks->push_back(pNewChunk);          pSubChunks->push_back(pNewChunk);
1170          (*pSubChunksMap)[uiChunkID] = pNewChunk;          (*pSubChunksMap)[uiChunkID] = pNewChunk;
1171          pNewChunk->Resize(uiBodySize);          pNewChunk->Resize(uiBodySize);
1172            NewChunkSize += CHUNK_HEADER_SIZE;
1173            pFile->LogAsResized(this);
1174          return pNewChunk;          return pNewChunk;
1175      }      }
1176    
1177        /** @brief Moves a sub chunk.
1178         *
1179         * Moves a sub chunk from one position in a list to another
1180         * position in the same list. The pSrc chunk is placed before the
1181         * pDst chunk.
1182         *
1183         * @param pSrc - sub chunk to be moved
1184         * @param pDst - the position to move to. pSrc will be placed
1185         *               before pDst. If pDst is 0, pSrc will be placed
1186         *               last in list.
1187         */
1188        void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) {
1189            if (!pSubChunks) LoadSubChunks();
1190            pSubChunks->remove(pSrc);
1191            ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst);
1192            pSubChunks->insert(iter, pSrc);
1193        }
1194    
1195      /** @brief Creates a new list sub chunk.      /** @brief Creates a new list sub chunk.
1196       *       *
1197       * Creates and adds a new list sub chunk to this list chunk. Note that       * Creates and adds a new list sub chunk to this list chunk. Note that
# Line 1148  namespace RIFF { Line 1206  namespace RIFF {
1206          List* pNewListChunk = new List(pFile, this, uiListType);          List* pNewListChunk = new List(pFile, this, uiListType);
1207          pSubChunks->push_back(pNewListChunk);          pSubChunks->push_back(pNewListChunk);
1208          (*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk;          (*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk;
1209            NewChunkSize += LIST_HEADER_SIZE;
1210            pFile->LogAsResized(this);
1211          return pNewListChunk;          return pNewListChunk;
1212      }      }
1213    
# Line 1184  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 1229  namespace RIFF { Line 1290  namespace RIFF {
1290          if (!pSubChunks) {          if (!pSubChunks) {
1291              pSubChunks    = new ChunkList();              pSubChunks    = new ChunkList();
1292              pSubChunksMap = new ChunkMap();              pSubChunksMap = new ChunkMap();
1293                #if defined(WIN32)
1294                if (pFile->hFileRead == INVALID_HANDLE_VALUE) return;
1295                #else
1296              if (!pFile->hFileRead) return;              if (!pFile->hFileRead) return;
1297                #endif
1298              unsigned long uiOriginalPos = GetPos();              unsigned long uiOriginalPos = GetPos();
1299              SetPos(0); // jump to beginning of list chunk body              SetPos(0); // jump to beginning of list chunk body
1300              while (RemainingBytes() >= CHUNK_HEADER_SIZE) {              while (RemainingBytes() >= CHUNK_HEADER_SIZE) {
# Line 1319  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
1394       * "from scratch". Note: there must be no empty chunks or empty list       * "from scratch". Note: there must be no empty chunks or empty list
1395       * chunks when trying to make the new RIFF file persistent with Save()!       * chunks when trying to make the new RIFF file persistent with Save()!
1396       *       *
1397         * Note: by default, the RIFF file will be saved in native endian
1398         * format; that is, as a RIFF file on little-endian machines and
1399         * as a RIFX file on big-endian. To change this behaviour, call
1400         * SetByteOrder() before calling Save().
1401         *
1402       * @param FileType - four-byte identifier of the RIFF file type       * @param FileType - four-byte identifier of the RIFF file type
1403       * @see AddSubChunk(), AddSubList()       * @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 1349  namespace RIFF { Line 1425  namespace RIFF {
1425       *                         given RIFF file       *                         given RIFF file
1426       */       */
1427      File::File(const String& path) : List(this), Filename(path) {      File::File(const String& path) : List(this), Filename(path) {
1428        #if DEBUG         #if DEBUG
1429        std::cout << "File::File("<<path<<")" << std::endl;         std::cout << "File::File("<<path<<")" << std::endl;
1430        #endif // DEBUG         #endif // DEBUG
1431          bEndianNative = true;          try {
1432          #if POSIX              bEndianNative = true;
1433          hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK);              //HACK: see _GET_RESIZED_CHUNKS() comment
1434          if (hFileRead <= 0) {              ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>));
1435              hFileRead = hFileWrite = 0;              #if POSIX
1436              throw RIFF::Exception("Can't open \"" + path + "\"");              hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK);
1437          }              if (hFileRead <= 0) {
1438          #elif defined(WIN32)                  hFileRead = hFileWrite = 0;
1439          hFileRead = hFileWrite = CreateFile(                  throw RIFF::Exception("Can't open \"" + path + "\"");
1440                                       path.c_str(), GENERIC_READ,              }
1441                                       FILE_SHARE_READ | FILE_SHARE_WRITE,              #elif defined(WIN32)
1442                                       NULL, OPEN_EXISTING,              hFileRead = hFileWrite = CreateFile(
1443                                       FILE_ATTRIBUTE_NORMAL, NULL                                           path.c_str(), GENERIC_READ,
1444                                   );                                           FILE_SHARE_READ | FILE_SHARE_WRITE,
1445          if (hFileRead == INVALID_HANDLE_VALUE) {                                           NULL, OPEN_EXISTING,
1446              hFileRead = hFileWrite = INVALID_HANDLE_VALUE;                                           FILE_ATTRIBUTE_NORMAL |
1447              throw RIFF::Exception("Can't open \"" + path + "\"");                                           FILE_FLAG_RANDOM_ACCESS, NULL
1448                                         );
1449                if (hFileRead == INVALID_HANDLE_VALUE) {
1450                    hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1451                    throw RIFF::Exception("Can't open \"" + path + "\"");
1452                }
1453                #else
1454                hFileRead = hFileWrite = fopen(path.c_str(), "rb");
1455                if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\"");
1456                #endif // POSIX
1457                Mode = stream_mode_read;
1458                ulStartPos = RIFF_HEADER_SIZE;
1459                ReadHeader(0);
1460                if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) {
1461                    throw RIFF::Exception("Not a RIFF file");
1462                }
1463          }          }
1464          #else          catch (...) {
1465          hFileRead = hFileWrite = fopen(path.c_str(), "rb");              Cleanup();
1466          if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\"");              throw;
         #endif // POSIX  
         Mode = stream_mode_read;  
         ulStartPos = RIFF_HEADER_SIZE;  
         ReadHeader(0);  
         if (ChunkID != CHUNK_ID_RIFF) {  
             throw RIFF::Exception("Not a RIFF file");  
1467          }          }
1468      }      }
1469    
# Line 1417  namespace RIFF { Line 1502  namespace RIFF {
1502                                                   Filename.c_str(), GENERIC_READ,                                                   Filename.c_str(), GENERIC_READ,
1503                                                   FILE_SHARE_READ | FILE_SHARE_WRITE,                                                   FILE_SHARE_READ | FILE_SHARE_WRITE,
1504                                                   NULL, OPEN_EXISTING,                                                   NULL, OPEN_EXISTING,
1505                                                   FILE_ATTRIBUTE_NORMAL, NULL                                                   FILE_ATTRIBUTE_NORMAL |
1506                                                     FILE_FLAG_RANDOM_ACCESS,
1507                                                     NULL
1508                                               );                                               );
1509                      if (hFileRead == INVALID_HANDLE_VALUE) {                      if (hFileRead == INVALID_HANDLE_VALUE) {
1510                          hFileRead = hFileWrite = INVALID_HANDLE_VALUE;                          hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
# Line 1445  namespace RIFF { Line 1532  namespace RIFF {
1532                                                   GENERIC_READ | GENERIC_WRITE,                                                   GENERIC_READ | GENERIC_WRITE,
1533                                                   FILE_SHARE_READ,                                                   FILE_SHARE_READ,
1534                                                   NULL, OPEN_ALWAYS,                                                   NULL, OPEN_ALWAYS,
1535                                                   FILE_ATTRIBUTE_NORMAL, NULL                                                   FILE_ATTRIBUTE_NORMAL |
1536                                                     FILE_FLAG_RANDOM_ACCESS,
1537                                                     NULL
1538                                               );                                               );
1539                      if (hFileRead == INVALID_HANDLE_VALUE) {                      if (hFileRead == INVALID_HANDLE_VALUE) {
1540                          hFileRead = hFileWrite = CreateFile(                          hFileRead = hFileWrite = CreateFile(
1541                                                       Filename.c_str(), GENERIC_READ,                                                       Filename.c_str(), GENERIC_READ,
1542                                                       FILE_SHARE_READ | FILE_SHARE_WRITE,                                                       FILE_SHARE_READ | FILE_SHARE_WRITE,
1543                                                       NULL, OPEN_EXISTING,                                                       NULL, OPEN_EXISTING,
1544                                                       FILE_ATTRIBUTE_NORMAL, NULL                                                       FILE_ATTRIBUTE_NORMAL |
1545                                                         FILE_FLAG_RANDOM_ACCESS,
1546                                                         NULL
1547                                                   );                                                   );
1548                          throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");                          throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode");
1549                      }                      }
1550                      #else                      #else
1551                      if (hFileRead) fclose(hFileRead);                      if (hFileRead) fclose(hFileRead);
# Line 1488  namespace RIFF { Line 1579  namespace RIFF {
1579          return false;          return false;
1580      }      }
1581    
1582        /** @brief Set the byte order to be used when saving.
1583         *
1584         * Set the byte order to be used in the file. A value of
1585         * endian_little will create a RIFF file, endian_big a RIFX file
1586         * and endian_native will create a RIFF file on little-endian
1587         * machines and RIFX on big-endian machines.
1588         *
1589         * @param Endian - endianess to use when file is saved.
1590         */
1591        void File::SetByteOrder(endian_t Endian) {
1592            #if WORDS_BIGENDIAN
1593            bEndianNative = Endian != endian_little;
1594            #else
1595            bEndianNative = Endian != endian_big;
1596            #endif
1597        }
1598    
1599      /** @brief Save changes to same file.      /** @brief Save changes to same file.
1600       *       *
1601       * 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 1514  namespace RIFF { Line 1622  namespace RIFF {
1622    
1623          // 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)
1624          unsigned long ulPositiveSizeDiff = 0;          unsigned long ulPositiveSizeDiff = 0;
1625          for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) {          std::set<Chunk*>* resizedChunks = _GET_RESIZED_CHUNKS();
1626              if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)");          for (std::set<Chunk*>::const_iterator iter = resizedChunks->begin(), end = resizedChunks->end(); iter != end; ++iter) {
1627              if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) {              if ((*iter)->GetNewSize() == 0) {
1628                  unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte                  throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter));
1629                  ulPositiveSizeDiff += ulDiff;              }
1630              }              unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2;
1631                unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2;
1632                if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded;
1633          }          }
1634    
1635          unsigned long ulWorkingFileSize = GetFileSize();          unsigned long ulWorkingFileSize = GetFileSize();
# Line 1535  namespace RIFF { Line 1645  namespace RIFF {
1645              #if defined(WIN32)              #if defined(WIN32)
1646              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
1647              #else              #else
1648              int iBytesMoved = 1;              int iBytesMoved = 1;
1649              #endif              #endif
1650              for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) {              for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) {
1651                  const unsigned long ulToMove = ulFileSize - ulPos;                  iBytesMoved = (ulPos < 4096) ? ulPos : 4096;
1652                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;                  ulPos -= iBytesMoved;
1653                  #if POSIX                  #if POSIX
1654                  lseek(hFileRead, ulPos, SEEK_SET);                  lseek(hFileRead, ulPos, SEEK_SET);
1655                  iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);                  iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);
# Line 1569  namespace RIFF { Line 1679  namespace RIFF {
1679          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);
1680    
1681          // forget all resized chunks          // forget all resized chunks
1682          ResizedChunks.clear();          resizedChunks->clear();
1683      }      }
1684    
1685      /** @brief Save changes to another file.      /** @brief Save changes to another file.
# Line 1602  namespace RIFF { Line 1712  namespace RIFF {
1712          #elif defined(WIN32)          #elif defined(WIN32)
1713          hFileWrite = CreateFile(          hFileWrite = CreateFile(
1714                           path.c_str(), GENERIC_WRITE, FILE_SHARE_READ,                           path.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
1715                           NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL                           NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL |
1716                             FILE_FLAG_RANDOM_ACCESS, NULL
1717                       );                       );
1718          if (hFileWrite == INVALID_HANDLE_VALUE) {          if (hFileWrite == INVALID_HANDLE_VALUE) {
1719              hFileWrite = hFileRead;              hFileWrite = hFileRead;
# Line 1625  namespace RIFF { Line 1736  namespace RIFF {
1736          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);
1737    
1738          // forget all resized chunks          // forget all resized chunks
1739          ResizedChunks.clear();          _GET_RESIZED_CHUNKS()->clear();
1740    
1741          if (Filename.length() > 0) {          #if POSIX
1742              #if POSIX          if (hFileWrite) close(hFileWrite);
1743              close(hFileWrite);          #elif defined(WIN32)
1744              #elif defined(WIN32)          if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite);
1745              CloseHandle(hFileWrite);          #else
1746              #else          if (hFileWrite) fclose(hFileWrite);
1747              fclose(hFileWrite);          #endif
1748              #endif          hFileWrite = hFileRead;
             hFileWrite = hFileRead;  
         }  
1749    
1750          // associate new file with this File object from now on          // associate new file with this File object from now on
1751          Filename = path;          Filename = path;
# Line 1663  namespace RIFF { Line 1772  namespace RIFF {
1772         #if DEBUG         #if DEBUG
1773         std::cout << "File::~File()" << std::endl;         std::cout << "File::~File()" << std::endl;
1774         #endif // DEBUG         #endif // DEBUG
1775            Cleanup();
1776        }
1777    
1778        void File::Cleanup() {
1779          #if POSIX          #if POSIX
1780          if (hFileRead) close(hFileRead);          if (hFileRead) close(hFileRead);
1781          #elif defined(WIN32)          #elif defined(WIN32)
# Line 1670  namespace RIFF { Line 1783  namespace RIFF {
1783          #else          #else
1784          if (hFileRead) fclose(hFileRead);          if (hFileRead) fclose(hFileRead);
1785          #endif // POSIX          #endif // POSIX
1786            DeleteChunkList();
1787            pFile = NULL;
1788            //HACK: see _GET_RESIZED_CHUNKS() comment
1789            delete _GET_RESIZED_CHUNKS();
1790      }      }
1791    
1792      void File::LogAsResized(Chunk* pResizedChunk) {      void File::LogAsResized(Chunk* pResizedChunk) {
1793          ResizedChunks.push_back(pResizedChunk);          _GET_RESIZED_CHUNKS()->insert(pResizedChunk);
1794        }
1795    
1796        void File::UnlogResized(Chunk* pResizedChunk) {
1797            _GET_RESIZED_CHUNKS()->erase(pResizedChunk);
1798      }      }
1799    
1800      unsigned long File::GetFileSize() {      unsigned long File::GetFileSize() {

Legend:
Removed from v.1093  
changed lines
  Added in v.2155

  ViewVC Help
Powered by ViewVC