/[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 808 by schoenebeck, Tue Nov 22 09:11:17 2005 UTC revision 1102 by persson, Sun Mar 18 07:13:06 2007 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file loader library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2005 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2007 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 70  namespace RIFF { Line 70  namespace RIFF {
70      }      }
71    
72      Chunk::~Chunk() {      Chunk::~Chunk() {
73            if (CurrentChunkSize != NewChunkSize) pFile->UnlogResized(this);
74          if (pChunkData) delete[] pChunkData;          if (pChunkData) delete[] pChunkData;
75      }      }
76    
# Line 81  namespace RIFF { Line 82  namespace RIFF {
82          if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) {          if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) {
83              read(pFile->hFileRead, &ChunkID, 4);              read(pFile->hFileRead, &ChunkID, 4);
84              read(pFile->hFileRead, &CurrentChunkSize, 4);              read(pFile->hFileRead, &CurrentChunkSize, 4);
85            #elif defined(WIN32)
86            if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) {
87                DWORD dwBytesRead;
88                ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL);
89                ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL);
90          #else          #else
91          if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) {          if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) {
92              fread(&ChunkID, 4, 1, pFile->hFileRead);              fread(&ChunkID, 4, 1, pFile->hFileRead);
# Line 129  namespace RIFF { Line 135  namespace RIFF {
135              write(pFile->hFileWrite, &uiNewChunkID, 4);              write(pFile->hFileWrite, &uiNewChunkID, 4);
136              write(pFile->hFileWrite, &uiNewChunkSize, 4);              write(pFile->hFileWrite, &uiNewChunkSize, 4);
137          }          }
138            #elif defined(WIN32)
139            if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) {
140                DWORD dwBytesWritten;
141                WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL);
142                WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL);
143            }
144          #else          #else
145          if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) {          if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) {
146              fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite);              fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite);
# Line 212  namespace RIFF { Line 224  namespace RIFF {
224        std::cout << "Chunk::GetState()" << std::endl;        std::cout << "Chunk::GetState()" << std::endl;
225        #endif // DEBUG        #endif // DEBUG
226          #if POSIX          #if POSIX
227          if (pFile->hFileRead == 0)    return stream_closed;          if (pFile->hFileRead == 0) return stream_closed;
228            #elif defined (WIN32)
229            if (pFile->hFileRead == INVALID_HANDLE_VALUE)
230                return stream_closed;
231          #else          #else
232          if (pFile->hFileRead == NULL) return stream_closed;          if (pFile->hFileRead == NULL) return stream_closed;
233          #endif // POSIX          #endif // POSIX
# Line 246  namespace RIFF { Line 261  namespace RIFF {
261          unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize);          unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize);
262          if (readWords < 1) return 0;          if (readWords < 1) return 0;
263          readWords /= WordSize;          readWords /= WordSize;
264            #elif defined(WIN32)
265            if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0;
266            DWORD readWords;
267            ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL);
268            if (readWords < 1) return 0;
269            readWords /= WordSize;
270          #else // standard C functions          #else // standard C functions
271          if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0;          if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0;
272          unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead);          unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead);
# Line 315  namespace RIFF { Line 336  namespace RIFF {
336          unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize);          unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize);
337          if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data");          if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data");
338          writtenWords /= WordSize;          writtenWords /= WordSize;
339            #elif defined(WIN32)
340            if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
341                throw Exception("Could not seek to position " + ToString(ulPos) +
342                                " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");            
343            }
344            DWORD writtenWords;
345            WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL);
346            if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data");
347            writtenWords /= WordSize;
348          #else // standard C functions          #else // standard C functions
349          if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) {          if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) {
350              throw Exception("Could not seek to position " + ToString(ulPos) +              throw Exception("Could not seek to position " + ToString(ulPos) +
# Line 679  namespace RIFF { Line 709  namespace RIFF {
709          if (!pChunkData && pFile->Filename != "") {          if (!pChunkData && pFile->Filename != "") {
710              #if POSIX              #if POSIX
711              if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;              if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;
712                #elif defined(WIN32)
713                if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL;
714              #else              #else
715              if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL;              if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL;
716              #endif // POSIX              #endif // POSIX
# Line 688  namespace RIFF { Line 720  namespace RIFF {
720              memset(pChunkData, 0, ulBufferSize);              memset(pChunkData, 0, ulBufferSize);
721              #if POSIX              #if POSIX
722              unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize());              unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize());
723                #elif defined(WIN32)
724                DWORD readWords;
725                ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL);
726              #else              #else
727              unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead);              unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead);
728              #endif // POSIX              #endif // POSIX
# Line 775  namespace RIFF { Line 810  namespace RIFF {
810              if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) {              if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) {
811                  throw Exception("Writing Chunk data (from RAM) failed");                  throw Exception("Writing Chunk data (from RAM) failed");
812              }              }
813                #elif defined(WIN32)
814                SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN);
815                DWORD dwBytesWritten;
816                WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL);
817                if (dwBytesWritten != NewChunkSize) {
818                    throw Exception("Writing Chunk data (from RAM) failed");
819                }
820              #else              #else
821              fseek(pFile->hFileWrite, ulWritePos, SEEK_SET);              fseek(pFile->hFileWrite, ulWritePos, SEEK_SET);
822              if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) {              if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) {
# Line 785  namespace RIFF { Line 827  namespace RIFF {
827              // move chunk data from the end of the file to the appropriate position              // move chunk data from the end of the file to the appropriate position
828              int8_t* pCopyBuffer = new int8_t[4096];              int8_t* pCopyBuffer = new int8_t[4096];
829              unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize;              unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize;
830                #if defined(WIN32)
831                DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
832                #else
833              int iBytesMoved = 1;              int iBytesMoved = 1;
834                #endif
835              for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) {              for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) {
836                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;
837                  #if POSIX                  #if POSIX
# Line 793  namespace RIFF { Line 839  namespace RIFF {
839                  iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved);                  iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved);
840                  lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET);                  lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET);
841                  iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved);                  iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved);
842                    #elif defined(WIN32)
843                    SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN);
844                    ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
845                    SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN);
846                    WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
847                  #else                  #else
848                  fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);                  fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);
849                  iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead);                  iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead);
# Line 818  namespace RIFF { Line 869  namespace RIFF {
869              #if POSIX              #if POSIX
870              lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);              lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);
871              write(pFile->hFileWrite, &cPadByte, 1);              write(pFile->hFileWrite, &cPadByte, 1);
872                #elif defined(WIN32)
873                SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN);
874                DWORD dwBytesWritten;
875                WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL);
876              #else              #else
877              fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);              fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);
878              fwrite(&cPadByte, 1, 1, pFile->hFileWrite);              fwrite(&cPadByte, 1, 1, pFile->hFileWrite);
# Line 1080  namespace RIFF { Line 1135  namespace RIFF {
1135          return pNewChunk;          return pNewChunk;
1136      }      }
1137    
1138        /** @brief Moves a sub chunk.
1139         *
1140         * Moves a sub chunk from one position in a list to another
1141         * position in the same list. The pSrc chunk is placed before the
1142         * pDst chunk.
1143         *
1144         * @param pSrc - sub chunk to be moved
1145         * @param pDst - the position to move to. pSrc will be placed
1146         *               before pDst. If pDst is 0, pSrc will be placed
1147         *               last in list.
1148         */
1149        void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) {
1150            if (!pSubChunks) LoadSubChunks();
1151            pSubChunks->remove(pSrc);
1152            ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst);
1153            pSubChunks->insert(iter, pSrc);
1154        }
1155    
1156      /** @brief Creates a new list sub chunk.      /** @brief Creates a new list sub chunk.
1157       *       *
1158       * 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 1101  namespace RIFF { Line 1174  namespace RIFF {
1174       *       *
1175       * Removes the sub chunk given by \a pSubChunk from this list and frees       * Removes the sub chunk given by \a pSubChunk from this list and frees
1176       * it completely from RAM. The given chunk can either be a normal sub       * it completely from RAM. The given chunk can either be a normal sub
1177       * chunk or a list sub chunk. You should call File::Save() to make this       * chunk or a list sub chunk. In case the given chunk is a list chunk,
1178       * change persistent at any time.       * all its subchunks (if any) will be removed recursively as well. You
1179         * should call File::Save() to make this change persistent at any time.
1180       *       *
1181       * @param pSubChunk - sub chunk or sub list chunk to be removed       * @param pSubChunk - sub chunk or sub list chunk to be removed
1182       */       */
# Line 1133  namespace RIFF { Line 1207  namespace RIFF {
1207          #if POSIX          #if POSIX
1208          lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);          lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1209          read(pFile->hFileRead, &ListType, 4);          read(pFile->hFileRead, &ListType, 4);
1210            #elif defined(WIN32)
1211            SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN);
1212            DWORD dwBytesRead;
1213            ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL);
1214          #else          #else
1215          fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);          fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1216          fread(&ListType, 4, 1, pFile->hFileRead);          fread(&ListType, 4, 1, pFile->hFileRead);
# Line 1153  namespace RIFF { Line 1231  namespace RIFF {
1231          #if POSIX          #if POSIX
1232          lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);          lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1233          write(pFile->hFileWrite, &ListType, 4);          write(pFile->hFileWrite, &ListType, 4);
1234            #elif defined(WIN32)
1235            SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN);
1236            DWORD dwBytesWritten;
1237            WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL);
1238          #else          #else
1239          fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);          fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1240          fwrite(&ListType, 4, 1, pFile->hFileWrite);          fwrite(&ListType, 4, 1, pFile->hFileWrite);
# Line 1192  namespace RIFF { Line 1274  namespace RIFF {
1274          }          }
1275      }      }
1276    
1277        void List::LoadSubChunksRecursively() {
1278            for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList())
1279                pList->LoadSubChunksRecursively();
1280        }
1281    
1282      /** @brief Write list chunk persistently e.g. to disk.      /** @brief Write list chunk persistently e.g. to disk.
1283       *       *
1284       * Stores the list chunk persistently to its actual "physical" file. All       * Stores the list chunk persistently to its actual "physical" file. All
# Line 1261  namespace RIFF { Line 1348  namespace RIFF {
1348       * @see AddSubChunk(), AddSubList()       * @see AddSubChunk(), AddSubList()
1349       */       */
1350      File::File(uint32_t FileType) : List(this) {      File::File(uint32_t FileType) : List(this) {
1351            #if defined(WIN32)
1352            hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1353            #else
1354          hFileRead = hFileWrite = 0;          hFileRead = hFileWrite = 0;
1355            #endif
1356          Mode = stream_mode_closed;          Mode = stream_mode_closed;
1357          bEndianNative = true;          bEndianNative = true;
1358          ulStartPos = RIFF_HEADER_SIZE;          ulStartPos = RIFF_HEADER_SIZE;
# Line 1287  namespace RIFF { Line 1378  namespace RIFF {
1378              hFileRead = hFileWrite = 0;              hFileRead = hFileWrite = 0;
1379              throw RIFF::Exception("Can't open \"" + path + "\"");              throw RIFF::Exception("Can't open \"" + path + "\"");
1380          }          }
1381            #elif defined(WIN32)
1382            hFileRead = hFileWrite = CreateFile(
1383                                         path.c_str(), GENERIC_READ,
1384                                         FILE_SHARE_READ | FILE_SHARE_WRITE,
1385                                         NULL, OPEN_EXISTING,
1386                                         FILE_ATTRIBUTE_NORMAL, NULL
1387                                     );
1388            if (hFileRead == INVALID_HANDLE_VALUE) {
1389                hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1390                throw RIFF::Exception("Can't open \"" + path + "\"");
1391            }
1392          #else          #else
1393          hFileRead = hFileWrite = fopen(path.c_str(), "rb");          hFileRead = hFileWrite = fopen(path.c_str(), "rb");
1394          if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\"");          if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\"");
1395          #endif // POSIX          #endif // POSIX
1396          Mode = stream_mode_read;          Mode = stream_mode_read;
1397          ulStartPos = RIFF_HEADER_SIZE;          ulStartPos = RIFF_HEADER_SIZE;
# Line 1328  namespace RIFF { Line 1430  namespace RIFF {
1430                          hFileRead = hFileWrite = 0;                          hFileRead = hFileWrite = 0;
1431                          throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");                          throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
1432                      }                      }
1433                        #elif defined(WIN32)
1434                        if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1435                        hFileRead = hFileWrite = CreateFile(
1436                                                     Filename.c_str(), GENERIC_READ,
1437                                                     FILE_SHARE_READ | FILE_SHARE_WRITE,
1438                                                     NULL, OPEN_EXISTING,
1439                                                     FILE_ATTRIBUTE_NORMAL, NULL
1440                                                 );
1441                        if (hFileRead == INVALID_HANDLE_VALUE) {
1442                            hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1443                            throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
1444                        }
1445                      #else                      #else
1446                      if (hFileRead) fclose(hFileRead);                      if (hFileRead) fclose(hFileRead);
1447                      hFileRead = hFileWrite = fopen(path.c_str(), "rb");                      hFileRead = hFileWrite = fopen(Filename.c_str(), "rb");
1448                      if (!hFileRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");                      if (!hFileRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
1449                      #endif                      #endif
1450                      __resetPos(); // reset read/write position of ALL 'Chunk' objects                      __resetPos(); // reset read/write position of ALL 'Chunk' objects
# Line 1343  namespace RIFF { Line 1457  namespace RIFF {
1457                          hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);                          hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);
1458                          throw Exception("Could not open file \"" + Filename + "\" in read+write mode");                          throw Exception("Could not open file \"" + Filename + "\" in read+write mode");
1459                      }                      }
1460                        #elif defined(WIN32)
1461                        if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1462                        hFileRead = hFileWrite = CreateFile(
1463                                                     Filename.c_str(),
1464                                                     GENERIC_READ | GENERIC_WRITE,
1465                                                     FILE_SHARE_READ,
1466                                                     NULL, OPEN_ALWAYS,
1467                                                     FILE_ATTRIBUTE_NORMAL, NULL
1468                                                 );
1469                        if (hFileRead == INVALID_HANDLE_VALUE) {
1470                            hFileRead = hFileWrite = CreateFile(
1471                                                         Filename.c_str(), GENERIC_READ,
1472                                                         FILE_SHARE_READ | FILE_SHARE_WRITE,
1473                                                         NULL, OPEN_EXISTING,
1474                                                         FILE_ATTRIBUTE_NORMAL, NULL
1475                                                     );
1476                            throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
1477                        }
1478                      #else                      #else
1479                      if (hFileRead) fclose(hFileRead);                      if (hFileRead) fclose(hFileRead);
1480                      hFileRead = hFileWrite = fopen(path.c_str(), "r+b");                      hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b");
1481                      if (!hFileRead) {                      if (!hFileRead) {
1482                          hFileRead = hFileWrite = fopen(path.c_str(), "rb");                          hFileRead = hFileWrite = fopen(Filename.c_str(), "rb");
1483                          throw Exception("Could not open file \"" + Filename + "\" in read+write mode");                          throw Exception("Could not open file \"" + Filename + "\" in read+write mode");
1484                      }                      }
1485                      #endif                      #endif
# Line 1357  namespace RIFF { Line 1489  namespace RIFF {
1489                      #if POSIX                      #if POSIX
1490                      if (hFileRead)  close(hFileRead);                      if (hFileRead)  close(hFileRead);
1491                      if (hFileWrite) close(hFileWrite);                      if (hFileWrite) close(hFileWrite);
1492                        #elif defined(WIN32)
1493                        if (hFileRead  != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1494                        if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite);
1495                      #else                      #else
1496                      if (hFileRead)  fclose(hFileRead);                      if (hFileRead)  fclose(hFileRead);
1497                      if (hFileWrite) fclose(hFileWrite);                      if (hFileWrite) fclose(hFileWrite);
# Line 1383  namespace RIFF { Line 1518  namespace RIFF {
1518       *                         chunk or any kind of IO error occured       *                         chunk or any kind of IO error occured
1519       */       */
1520      void File::Save() {      void File::Save() {
1521            // make sure the RIFF tree is built (from the original file)
1522            LoadSubChunksRecursively();
1523    
1524          // reopen file in write mode          // reopen file in write mode
1525          SetMode(stream_mode_read_write);          SetMode(stream_mode_read_write);
1526    
# Line 1396  namespace RIFF { Line 1534  namespace RIFF {
1534          // 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)
1535          unsigned long ulPositiveSizeDiff = 0;          unsigned long ulPositiveSizeDiff = 0;
1536          for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) {          for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) {
1537              if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)");              if ((*iter)->GetNewSize() == 0) {
1538                    // just to make the exception message a bit more verbose: resolve the chunk's path
1539                    String sChunkPath;
1540                    for (Chunk* pChunk = *iter; pChunk; pChunk = pChunk->GetParent()) {
1541                        if (pChunk->GetChunkID() == CHUNK_ID_LIST) {
1542                            List* pList = (List*) pChunk;
1543                            sChunkPath = "->'" + pList->GetListTypeString() + "'" + sChunkPath;
1544                        } else {
1545                            sChunkPath = "->'" + pChunk->GetChunkIDString() + "'" + sChunkPath;
1546                        }
1547                    }
1548                    throw Exception("There is at least one empty chunk (zero size): " + sChunkPath);
1549                }
1550              if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) {              if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) {
1551                  unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte                  unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte
1552                  ulPositiveSizeDiff += ulDiff;                  ulPositiveSizeDiff += ulDiff;
# Line 1413  namespace RIFF { Line 1563  namespace RIFF {
1563              // ... and move current data by the same amount towards end of file.              // ... and move current data by the same amount towards end of file.
1564              int8_t* pCopyBuffer = new int8_t[4096];              int8_t* pCopyBuffer = new int8_t[4096];
1565              const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE;              const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE;
1566              int iBytesMoved = 1;              #if defined(WIN32)
1567                DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
1568                #else
1569                int iBytesMoved = 1;
1570                #endif
1571              for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) {              for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) {
1572                  const unsigned long ulToMove = ulFileSize - ulPos;                  const unsigned long ulToMove = ulFileSize - ulPos;
1573                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;
# Line 1422  namespace RIFF { Line 1576  namespace RIFF {
1576                  iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);                  iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);
1577                  lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET);                  lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET);
1578                  iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved);                  iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved);
1579                    #elif defined(WIN32)
1580                    SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN);
1581                    ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
1582                    SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN);
1583                    WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
1584                  #else                  #else
1585                  fseek(hFileRead, ulPos, SEEK_SET);                  fseek(hFileRead, ulPos, SEEK_SET);
1586                  iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead);                  iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead);
# Line 1460  namespace RIFF { Line 1619  namespace RIFF {
1619      void File::Save(const String& path) {      void File::Save(const String& path) {
1620          //TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case          //TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case
1621    
1622            // make sure the RIFF tree is built (from the original file)
1623            LoadSubChunksRecursively();
1624    
1625          if (Filename.length() > 0) SetMode(stream_mode_read);          if (Filename.length() > 0) SetMode(stream_mode_read);
1626          // open the other (new) file for writing and truncate it to zero size          // open the other (new) file for writing and truncate it to zero size
1627          #if POSIX          #if POSIX
# Line 1468  namespace RIFF { Line 1630  namespace RIFF {
1630              hFileWrite = hFileRead;              hFileWrite = hFileRead;
1631              throw Exception("Could not open file \"" + path + "\" for writing");              throw Exception("Could not open file \"" + path + "\" for writing");
1632          }          }
1633            #elif defined(WIN32)
1634            hFileWrite = CreateFile(
1635                             path.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
1636                             NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
1637                         );
1638            if (hFileWrite == INVALID_HANDLE_VALUE) {
1639                hFileWrite = hFileRead;
1640                throw Exception("Could not open file \"" + path + "\" for writing");
1641            }
1642          #else          #else
1643          hFileWrite = fopen(path.c_str(), "w+b");          hFileWrite = fopen(path.c_str(), "w+b");
1644          if (!hFileWrite) {          if (!hFileWrite) {
# Line 1490  namespace RIFF { Line 1661  namespace RIFF {
1661          if (Filename.length() > 0) {          if (Filename.length() > 0) {
1662              #if POSIX              #if POSIX
1663              close(hFileWrite);              close(hFileWrite);
1664                #elif defined(WIN32)
1665                CloseHandle(hFileWrite);
1666              #else              #else
1667              fclose(hFileWrite);              fclose(hFileWrite);
1668              #endif              #endif
# Line 1506  namespace RIFF { Line 1679  namespace RIFF {
1679          #if POSIX          #if POSIX
1680          if (ftruncate(hFileWrite, ulNewSize) < 0)          if (ftruncate(hFileWrite, ulNewSize) < 0)
1681              throw Exception("Could not resize file \"" + Filename + "\"");              throw Exception("Could not resize file \"" + Filename + "\"");
1682            #elif defined(WIN32)
1683            if (
1684                SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER ||
1685                !SetEndOfFile(hFileWrite)
1686            ) throw Exception("Could not resize file \"" + Filename + "\"");
1687          #else          #else
1688          # error Sorry, this version of libgig only supports POSIX systems yet.          # error Sorry, this version of libgig only supports POSIX and Windows systems yet.
1689          # error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)!          # error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)!
1690          #endif          #endif
1691      }      }
# Line 1518  namespace RIFF { Line 1696  namespace RIFF {
1696         #endif // DEBUG         #endif // DEBUG
1697          #if POSIX          #if POSIX
1698          if (hFileRead) close(hFileRead);          if (hFileRead) close(hFileRead);
1699            #elif defined(WIN32)
1700            if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1701          #else          #else
1702          if (hFileRead) fclose(hFileRead);          if (hFileRead) fclose(hFileRead);
1703          #endif // POSIX          #endif // POSIX
# Line 1527  namespace RIFF { Line 1707  namespace RIFF {
1707          ResizedChunks.push_back(pResizedChunk);          ResizedChunks.push_back(pResizedChunk);
1708      }      }
1709    
1710        void File::UnlogResized(Chunk* pResizedChunk) {
1711            ResizedChunks.remove(pResizedChunk);
1712        }
1713    
1714      unsigned long File::GetFileSize() {      unsigned long File::GetFileSize() {
1715          return __GetFileSize(hFileRead);          return __GetFileSize(hFileRead);
1716      }      }
# Line 1538  namespace RIFF { Line 1722  namespace RIFF {
1722          long size = filestat.st_size;          long size = filestat.st_size;
1723          return size;          return size;
1724      }      }
1725        #elif defined(WIN32)
1726        unsigned long File::__GetFileSize(HANDLE hFile) {
1727            DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/);
1728            if (dwSize == INVALID_FILE_SIZE)
1729                throw Exception("Windows FS error: could not determine file size");
1730            return dwSize;
1731        }
1732      #else // standard C functions      #else // standard C functions
1733      unsigned long File::__GetFileSize(FILE* hFile) {      unsigned long File::__GetFileSize(FILE* hFile) {
1734          long curpos = ftell(hFile);          long curpos = ftell(hFile);

Legend:
Removed from v.808  
changed lines
  Added in v.1102

  ViewVC Help
Powered by ViewVC