/[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 1093 by schoenebeck, Sun Mar 11 17:44:31 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 81  namespace RIFF { Line 81  namespace RIFF {
81          if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) {          if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) {
82              read(pFile->hFileRead, &ChunkID, 4);              read(pFile->hFileRead, &ChunkID, 4);
83              read(pFile->hFileRead, &CurrentChunkSize, 4);              read(pFile->hFileRead, &CurrentChunkSize, 4);
84            #elif defined(WIN32)
85            if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) {
86                DWORD dwBytesRead;
87                ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL);
88                ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL);
89          #else          #else
90          if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) {          if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) {
91              fread(&ChunkID, 4, 1, pFile->hFileRead);              fread(&ChunkID, 4, 1, pFile->hFileRead);
# Line 129  namespace RIFF { Line 134  namespace RIFF {
134              write(pFile->hFileWrite, &uiNewChunkID, 4);              write(pFile->hFileWrite, &uiNewChunkID, 4);
135              write(pFile->hFileWrite, &uiNewChunkSize, 4);              write(pFile->hFileWrite, &uiNewChunkSize, 4);
136          }          }
137            #elif defined(WIN32)
138            if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) {
139                DWORD dwBytesWritten;
140                WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL);
141                WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL);
142            }
143          #else          #else
144          if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) {          if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) {
145              fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite);              fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite);
# Line 212  namespace RIFF { Line 223  namespace RIFF {
223        std::cout << "Chunk::GetState()" << std::endl;        std::cout << "Chunk::GetState()" << std::endl;
224        #endif // DEBUG        #endif // DEBUG
225          #if POSIX          #if POSIX
226          if (pFile->hFileRead == 0)    return stream_closed;          if (pFile->hFileRead == 0) return stream_closed;
227            #elif defined (WIN32)
228            if (pFile->hFileRead == INVALID_HANDLE_VALUE)
229                return stream_closed;
230          #else          #else
231          if (pFile->hFileRead == NULL) return stream_closed;          if (pFile->hFileRead == NULL) return stream_closed;
232          #endif // POSIX          #endif // POSIX
# Line 246  namespace RIFF { Line 260  namespace RIFF {
260          unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize);          unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize);
261          if (readWords < 1) return 0;          if (readWords < 1) return 0;
262          readWords /= WordSize;          readWords /= WordSize;
263            #elif defined(WIN32)
264            if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0;
265            DWORD readWords;
266            ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL);
267            if (readWords < 1) return 0;
268            readWords /= WordSize;
269          #else // standard C functions          #else // standard C functions
270          if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0;          if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0;
271          unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead);          unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead);
# Line 315  namespace RIFF { Line 335  namespace RIFF {
335          unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize);          unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize);
336          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");
337          writtenWords /= WordSize;          writtenWords /= WordSize;
338            #elif defined(WIN32)
339            if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
340                throw Exception("Could not seek to position " + ToString(ulPos) +
341                                " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");            
342            }
343            DWORD writtenWords;
344            WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL);
345            if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data");
346            writtenWords /= WordSize;
347          #else // standard C functions          #else // standard C functions
348          if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) {          if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) {
349              throw Exception("Could not seek to position " + ToString(ulPos) +              throw Exception("Could not seek to position " + ToString(ulPos) +
# Line 679  namespace RIFF { Line 708  namespace RIFF {
708          if (!pChunkData && pFile->Filename != "") {          if (!pChunkData && pFile->Filename != "") {
709              #if POSIX              #if POSIX
710              if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;              if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;
711                #elif defined(WIN32)
712                if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL;
713              #else              #else
714              if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL;              if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL;
715              #endif // POSIX              #endif // POSIX
# Line 688  namespace RIFF { Line 719  namespace RIFF {
719              memset(pChunkData, 0, ulBufferSize);              memset(pChunkData, 0, ulBufferSize);
720              #if POSIX              #if POSIX
721              unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize());              unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize());
722                #elif defined(WIN32)
723                DWORD readWords;
724                ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL);
725              #else              #else
726              unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead);              unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead);
727              #endif // POSIX              #endif // POSIX
# Line 775  namespace RIFF { Line 809  namespace RIFF {
809              if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) {              if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) {
810                  throw Exception("Writing Chunk data (from RAM) failed");                  throw Exception("Writing Chunk data (from RAM) failed");
811              }              }
812                #elif defined(WIN32)
813                SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN);
814                DWORD dwBytesWritten;
815                WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL);
816                if (dwBytesWritten != NewChunkSize) {
817                    throw Exception("Writing Chunk data (from RAM) failed");
818                }
819              #else              #else
820              fseek(pFile->hFileWrite, ulWritePos, SEEK_SET);              fseek(pFile->hFileWrite, ulWritePos, SEEK_SET);
821              if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) {              if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) {
# Line 785  namespace RIFF { Line 826  namespace RIFF {
826              // 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
827              int8_t* pCopyBuffer = new int8_t[4096];              int8_t* pCopyBuffer = new int8_t[4096];
828              unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize;              unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize;
829                #if defined(WIN32)
830                DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
831                #else
832              int iBytesMoved = 1;              int iBytesMoved = 1;
833                #endif
834              for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) {              for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) {
835                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;
836                  #if POSIX                  #if POSIX
# Line 793  namespace RIFF { Line 838  namespace RIFF {
838                  iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved);                  iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved);
839                  lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET);                  lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET);
840                  iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved);                  iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved);
841                    #elif defined(WIN32)
842                    SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN);
843                    ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
844                    SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN);
845                    WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
846                  #else                  #else
847                  fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);                  fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);
848                  iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead);                  iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead);
# Line 818  namespace RIFF { Line 868  namespace RIFF {
868              #if POSIX              #if POSIX
869              lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);              lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);
870              write(pFile->hFileWrite, &cPadByte, 1);              write(pFile->hFileWrite, &cPadByte, 1);
871                #elif defined(WIN32)
872                SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN);
873                DWORD dwBytesWritten;
874                WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL);
875              #else              #else
876              fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);              fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);
877              fwrite(&cPadByte, 1, 1, pFile->hFileWrite);              fwrite(&cPadByte, 1, 1, pFile->hFileWrite);
# Line 1101  namespace RIFF { Line 1155  namespace RIFF {
1155       *       *
1156       * 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
1157       * 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
1158       * 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,
1159       * change persistent at any time.       * all its subchunks (if any) will be removed recursively as well. You
1160         * should call File::Save() to make this change persistent at any time.
1161       *       *
1162       * @param pSubChunk - sub chunk or sub list chunk to be removed       * @param pSubChunk - sub chunk or sub list chunk to be removed
1163       */       */
# Line 1133  namespace RIFF { Line 1188  namespace RIFF {
1188          #if POSIX          #if POSIX
1189          lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);          lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1190          read(pFile->hFileRead, &ListType, 4);          read(pFile->hFileRead, &ListType, 4);
1191            #elif defined(WIN32)
1192            SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN);
1193            DWORD dwBytesRead;
1194            ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL);
1195          #else          #else
1196          fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);          fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1197          fread(&ListType, 4, 1, pFile->hFileRead);          fread(&ListType, 4, 1, pFile->hFileRead);
# Line 1153  namespace RIFF { Line 1212  namespace RIFF {
1212          #if POSIX          #if POSIX
1213          lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);          lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1214          write(pFile->hFileWrite, &ListType, 4);          write(pFile->hFileWrite, &ListType, 4);
1215            #elif defined(WIN32)
1216            SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN);
1217            DWORD dwBytesWritten;
1218            WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL);
1219          #else          #else
1220          fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);          fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1221          fwrite(&ListType, 4, 1, pFile->hFileWrite);          fwrite(&ListType, 4, 1, pFile->hFileWrite);
# Line 1192  namespace RIFF { Line 1255  namespace RIFF {
1255          }          }
1256      }      }
1257    
1258        void List::LoadSubChunksRecursively() {
1259            for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList())
1260                pList->LoadSubChunksRecursively();
1261        }
1262    
1263      /** @brief Write list chunk persistently e.g. to disk.      /** @brief Write list chunk persistently e.g. to disk.
1264       *       *
1265       * 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 1329  namespace RIFF {
1329       * @see AddSubChunk(), AddSubList()       * @see AddSubChunk(), AddSubList()
1330       */       */
1331      File::File(uint32_t FileType) : List(this) {      File::File(uint32_t FileType) : List(this) {
1332            #if defined(WIN32)
1333            hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1334            #else
1335          hFileRead = hFileWrite = 0;          hFileRead = hFileWrite = 0;
1336            #endif
1337          Mode = stream_mode_closed;          Mode = stream_mode_closed;
1338          bEndianNative = true;          bEndianNative = true;
1339          ulStartPos = RIFF_HEADER_SIZE;          ulStartPos = RIFF_HEADER_SIZE;
# Line 1287  namespace RIFF { Line 1359  namespace RIFF {
1359              hFileRead = hFileWrite = 0;              hFileRead = hFileWrite = 0;
1360              throw RIFF::Exception("Can't open \"" + path + "\"");              throw RIFF::Exception("Can't open \"" + path + "\"");
1361          }          }
1362            #elif defined(WIN32)
1363            hFileRead = hFileWrite = CreateFile(
1364                                         path.c_str(), GENERIC_READ,
1365                                         FILE_SHARE_READ | FILE_SHARE_WRITE,
1366                                         NULL, OPEN_EXISTING,
1367                                         FILE_ATTRIBUTE_NORMAL, NULL
1368                                     );
1369            if (hFileRead == INVALID_HANDLE_VALUE) {
1370                hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1371                throw RIFF::Exception("Can't open \"" + path + "\"");
1372            }
1373          #else          #else
1374          hFileRead = hFileWrite = fopen(path.c_str(), "rb");          hFileRead = hFileWrite = fopen(path.c_str(), "rb");
1375          if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\"");          if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\"");
1376          #endif // POSIX          #endif // POSIX
1377          Mode = stream_mode_read;          Mode = stream_mode_read;
1378          ulStartPos = RIFF_HEADER_SIZE;          ulStartPos = RIFF_HEADER_SIZE;
# Line 1328  namespace RIFF { Line 1411  namespace RIFF {
1411                          hFileRead = hFileWrite = 0;                          hFileRead = hFileWrite = 0;
1412                          throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");                          throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
1413                      }                      }
1414                        #elif defined(WIN32)
1415                        if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1416                        hFileRead = hFileWrite = CreateFile(
1417                                                     Filename.c_str(), GENERIC_READ,
1418                                                     FILE_SHARE_READ | FILE_SHARE_WRITE,
1419                                                     NULL, OPEN_EXISTING,
1420                                                     FILE_ATTRIBUTE_NORMAL, NULL
1421                                                 );
1422                        if (hFileRead == INVALID_HANDLE_VALUE) {
1423                            hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1424                            throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
1425                        }
1426                      #else                      #else
1427                      if (hFileRead) fclose(hFileRead);                      if (hFileRead) fclose(hFileRead);
1428                      hFileRead = hFileWrite = fopen(path.c_str(), "rb");                      hFileRead = hFileWrite = fopen(Filename.c_str(), "rb");
1429                      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");
1430                      #endif                      #endif
1431                      __resetPos(); // reset read/write position of ALL 'Chunk' objects                      __resetPos(); // reset read/write position of ALL 'Chunk' objects
# Line 1343  namespace RIFF { Line 1438  namespace RIFF {
1438                          hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);                          hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);
1439                          throw Exception("Could not open file \"" + Filename + "\" in read+write mode");                          throw Exception("Could not open file \"" + Filename + "\" in read+write mode");
1440                      }                      }
1441                        #elif defined(WIN32)
1442                        if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1443                        hFileRead = hFileWrite = CreateFile(
1444                                                     Filename.c_str(),
1445                                                     GENERIC_READ | GENERIC_WRITE,
1446                                                     FILE_SHARE_READ,
1447                                                     NULL, OPEN_ALWAYS,
1448                                                     FILE_ATTRIBUTE_NORMAL, NULL
1449                                                 );
1450                        if (hFileRead == INVALID_HANDLE_VALUE) {
1451                            hFileRead = hFileWrite = CreateFile(
1452                                                         Filename.c_str(), GENERIC_READ,
1453                                                         FILE_SHARE_READ | FILE_SHARE_WRITE,
1454                                                         NULL, OPEN_EXISTING,
1455                                                         FILE_ATTRIBUTE_NORMAL, NULL
1456                                                     );
1457                            throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
1458                        }
1459                      #else                      #else
1460                      if (hFileRead) fclose(hFileRead);                      if (hFileRead) fclose(hFileRead);
1461                      hFileRead = hFileWrite = fopen(path.c_str(), "r+b");                      hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b");
1462                      if (!hFileRead) {                      if (!hFileRead) {
1463                          hFileRead = hFileWrite = fopen(path.c_str(), "rb");                          hFileRead = hFileWrite = fopen(Filename.c_str(), "rb");
1464                          throw Exception("Could not open file \"" + Filename + "\" in read+write mode");                          throw Exception("Could not open file \"" + Filename + "\" in read+write mode");
1465                      }                      }
1466                      #endif                      #endif
# Line 1357  namespace RIFF { Line 1470  namespace RIFF {
1470                      #if POSIX                      #if POSIX
1471                      if (hFileRead)  close(hFileRead);                      if (hFileRead)  close(hFileRead);
1472                      if (hFileWrite) close(hFileWrite);                      if (hFileWrite) close(hFileWrite);
1473                        #elif defined(WIN32)
1474                        if (hFileRead  != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1475                        if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite);
1476                      #else                      #else
1477                      if (hFileRead)  fclose(hFileRead);                      if (hFileRead)  fclose(hFileRead);
1478                      if (hFileWrite) fclose(hFileWrite);                      if (hFileWrite) fclose(hFileWrite);
# Line 1383  namespace RIFF { Line 1499  namespace RIFF {
1499       *                         chunk or any kind of IO error occured       *                         chunk or any kind of IO error occured
1500       */       */
1501      void File::Save() {      void File::Save() {
1502            // make sure the RIFF tree is built (from the original file)
1503            LoadSubChunksRecursively();
1504    
1505          // reopen file in write mode          // reopen file in write mode
1506          SetMode(stream_mode_read_write);          SetMode(stream_mode_read_write);
1507    
# Line 1413  namespace RIFF { Line 1532  namespace RIFF {
1532              // ... and move current data by the same amount towards end of file.              // ... and move current data by the same amount towards end of file.
1533              int8_t* pCopyBuffer = new int8_t[4096];              int8_t* pCopyBuffer = new int8_t[4096];
1534              const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE;              const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE;
1535              int iBytesMoved = 1;              #if defined(WIN32)
1536                DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
1537                #else
1538                int iBytesMoved = 1;
1539                #endif
1540              for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) {              for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) {
1541                  const unsigned long ulToMove = ulFileSize - ulPos;                  const unsigned long ulToMove = ulFileSize - ulPos;
1542                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;
# Line 1422  namespace RIFF { Line 1545  namespace RIFF {
1545                  iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);                  iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);
1546                  lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET);                  lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET);
1547                  iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved);                  iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved);
1548                    #elif defined(WIN32)
1549                    SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN);
1550                    ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
1551                    SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN);
1552                    WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
1553                  #else                  #else
1554                  fseek(hFileRead, ulPos, SEEK_SET);                  fseek(hFileRead, ulPos, SEEK_SET);
1555                  iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead);                  iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead);
# Line 1460  namespace RIFF { Line 1588  namespace RIFF {
1588      void File::Save(const String& path) {      void File::Save(const String& path) {
1589          //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
1590    
1591            // make sure the RIFF tree is built (from the original file)
1592            LoadSubChunksRecursively();
1593    
1594          if (Filename.length() > 0) SetMode(stream_mode_read);          if (Filename.length() > 0) SetMode(stream_mode_read);
1595          // 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
1596          #if POSIX          #if POSIX
# Line 1468  namespace RIFF { Line 1599  namespace RIFF {
1599              hFileWrite = hFileRead;              hFileWrite = hFileRead;
1600              throw Exception("Could not open file \"" + path + "\" for writing");              throw Exception("Could not open file \"" + path + "\" for writing");
1601          }          }
1602            #elif defined(WIN32)
1603            hFileWrite = CreateFile(
1604                             path.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
1605                             NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
1606                         );
1607            if (hFileWrite == INVALID_HANDLE_VALUE) {
1608                hFileWrite = hFileRead;
1609                throw Exception("Could not open file \"" + path + "\" for writing");
1610            }
1611          #else          #else
1612          hFileWrite = fopen(path.c_str(), "w+b");          hFileWrite = fopen(path.c_str(), "w+b");
1613          if (!hFileWrite) {          if (!hFileWrite) {
# Line 1490  namespace RIFF { Line 1630  namespace RIFF {
1630          if (Filename.length() > 0) {          if (Filename.length() > 0) {
1631              #if POSIX              #if POSIX
1632              close(hFileWrite);              close(hFileWrite);
1633                #elif defined(WIN32)
1634                CloseHandle(hFileWrite);
1635              #else              #else
1636              fclose(hFileWrite);              fclose(hFileWrite);
1637              #endif              #endif
# Line 1506  namespace RIFF { Line 1648  namespace RIFF {
1648          #if POSIX          #if POSIX
1649          if (ftruncate(hFileWrite, ulNewSize) < 0)          if (ftruncate(hFileWrite, ulNewSize) < 0)
1650              throw Exception("Could not resize file \"" + Filename + "\"");              throw Exception("Could not resize file \"" + Filename + "\"");
1651            #elif defined(WIN32)
1652            if (
1653                SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER ||
1654                !SetEndOfFile(hFileWrite)
1655            ) throw Exception("Could not resize file \"" + Filename + "\"");
1656          #else          #else
1657          # error Sorry, this version of libgig only supports POSIX systems yet.          # error Sorry, this version of libgig only supports POSIX and Windows systems yet.
1658          # error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)!          # error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)!
1659          #endif          #endif
1660      }      }
# Line 1518  namespace RIFF { Line 1665  namespace RIFF {
1665         #endif // DEBUG         #endif // DEBUG
1666          #if POSIX          #if POSIX
1667          if (hFileRead) close(hFileRead);          if (hFileRead) close(hFileRead);
1668            #elif defined(WIN32)
1669            if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1670          #else          #else
1671          if (hFileRead) fclose(hFileRead);          if (hFileRead) fclose(hFileRead);
1672          #endif // POSIX          #endif // POSIX
# Line 1538  namespace RIFF { Line 1687  namespace RIFF {
1687          long size = filestat.st_size;          long size = filestat.st_size;
1688          return size;          return size;
1689      }      }
1690        #elif defined(WIN32)
1691        unsigned long File::__GetFileSize(HANDLE hFile) {
1692            DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/);
1693            if (dwSize == INVALID_FILE_SIZE)
1694                throw Exception("Windows FS error: could not determine file size");
1695            return dwSize;
1696        }
1697      #else // standard C functions      #else // standard C functions
1698      unsigned long File::__GetFileSize(FILE* hFile) {      unsigned long File::__GetFileSize(FILE* hFile) {
1699          long curpos = ftell(hFile);          long curpos = ftell(hFile);

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

  ViewVC Help
Powered by ViewVC