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 * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
#include <string.h> |
#include <string.h> |
|
#include <sstream> |
|
25 |
|
|
26 |
#include "RIFF.h" |
#include "RIFF.h" |
27 |
|
|
28 |
namespace RIFF { |
#include "helper.h" |
|
|
|
|
// *************** Helper Functions ************** |
|
|
// * |
|
|
|
|
|
template<class T> inline String ToString(T o) { |
|
|
std::stringstream ss; |
|
|
ss << o; |
|
|
return ss.str(); |
|
|
} |
|
|
|
|
29 |
|
|
30 |
|
namespace RIFF { |
31 |
|
|
32 |
// *************** Chunk ************** |
// *************** Chunk ************** |
33 |
// * |
// * |
39 |
ulPos = 0; |
ulPos = 0; |
40 |
pParent = NULL; |
pParent = NULL; |
41 |
pChunkData = NULL; |
pChunkData = NULL; |
42 |
|
ulChunkDataSize = 0; |
43 |
|
ChunkID = CHUNK_ID_RIFF; |
44 |
this->pFile = pFile; |
this->pFile = pFile; |
45 |
} |
} |
46 |
|
|
53 |
pParent = Parent; |
pParent = Parent; |
54 |
ulPos = 0; |
ulPos = 0; |
55 |
pChunkData = NULL; |
pChunkData = NULL; |
56 |
|
ulChunkDataSize = 0; |
57 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
58 |
} |
} |
59 |
|
|
63 |
this->pParent = pParent; |
this->pParent = pParent; |
64 |
ulPos = 0; |
ulPos = 0; |
65 |
pChunkData = NULL; |
pChunkData = NULL; |
66 |
|
ulChunkDataSize = 0; |
67 |
ChunkID = uiChunkID; |
ChunkID = uiChunkID; |
68 |
CurrentChunkSize = 0; |
CurrentChunkSize = 0; |
69 |
NewChunkSize = uiBodySize; |
NewChunkSize = uiBodySize; |
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); |
93 |
#endif // POSIX |
#endif // POSIX |
94 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
95 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
96 |
bEndianNative = false; |
pFile->bEndianNative = false; |
97 |
} |
} |
98 |
#else // little endian |
#else // little endian |
99 |
if (ChunkID == CHUNK_ID_RIFX) { |
if (ChunkID == CHUNK_ID_RIFX) { |
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); |
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 |
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); |
307 |
* @see Resize() |
* @see Resize() |
308 |
*/ |
*/ |
309 |
unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) { |
unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) { |
310 |
if (pFile->Mode == stream_mode_read) |
if (pFile->Mode != stream_mode_read_write) |
311 |
throw Exception("Writing chunk data in read-only file access mode was attempted"); |
throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first"); |
312 |
if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize >= CurrentChunkSize) |
if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize > CurrentChunkSize) |
313 |
throw Exception("End of chunk reached while trying to write data"); |
throw Exception("End of chunk reached while trying to write data"); |
314 |
if (!pFile->bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
315 |
switch (WordSize) { |
switch (WordSize) { |
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) + |
690 |
* |
* |
691 |
* <b>Caution:</b> the buffer pointer will be invalidated once |
* <b>Caution:</b> the buffer pointer will be invalidated once |
692 |
* File::Save() was called. You have to call LoadChunkData() again to |
* File::Save() was called. You have to call LoadChunkData() again to |
693 |
* get a new pointer whenever File::Save() was called. |
* get a new, valid pointer whenever File::Save() was called. |
694 |
|
* |
695 |
|
* You can call LoadChunkData() again if you previously scheduled to |
696 |
|
* enlarge this chunk with a Resize() call. In that case the buffer will |
697 |
|
* be enlarged to the new, scheduled chunk size and you can already |
698 |
|
* place the new chunk data to the buffer and finally call File::Save() |
699 |
|
* to enlarge the chunk physically and write the new data in one rush. |
700 |
|
* This approach is definitely recommended if you have to enlarge and |
701 |
|
* write new data to a lot of chunks. |
702 |
* |
* |
703 |
* @returns a pointer to the data in RAM on success, NULL otherwise |
* @returns a pointer to the data in RAM on success, NULL otherwise |
704 |
|
* @throws Exception if data buffer could not be enlarged |
705 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
706 |
*/ |
*/ |
707 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
708 |
if (!pChunkData) { |
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 |
pChunkData = new uint8_t[GetSize()]; |
#elif defined(WIN32) |
712 |
if (!pChunkData) return NULL; |
if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL; |
|
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
|
713 |
#else |
#else |
714 |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
715 |
pChunkData = new uint8_t[GetSize()]; |
#endif // POSIX |
716 |
|
unsigned long ulBufferSize = (CurrentChunkSize > NewChunkSize) ? CurrentChunkSize : NewChunkSize; |
717 |
|
pChunkData = new uint8_t[ulBufferSize]; |
718 |
if (!pChunkData) return NULL; |
if (!pChunkData) return NULL; |
719 |
|
memset(pChunkData, 0, ulBufferSize); |
720 |
|
#if POSIX |
721 |
|
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 |
726 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
727 |
#endif // POSIX |
#endif // POSIX |
728 |
if (readWords != GetSize()) { |
if (readWords != GetSize()) { |
729 |
delete[] pChunkData; |
delete[] pChunkData; |
730 |
return (pChunkData = NULL); |
return (pChunkData = NULL); |
731 |
} |
} |
732 |
|
ulChunkDataSize = ulBufferSize; |
733 |
|
} else if (NewChunkSize > ulChunkDataSize) { |
734 |
|
uint8_t* pNewBuffer = new uint8_t[NewChunkSize]; |
735 |
|
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(NewChunkSize) + " bytes"); |
736 |
|
memset(pNewBuffer, 0 , NewChunkSize); |
737 |
|
memcpy(pNewBuffer, pChunkData, ulChunkDataSize); |
738 |
|
delete[] pChunkData; |
739 |
|
pChunkData = pNewBuffer; |
740 |
|
ulChunkDataSize = NewChunkSize; |
741 |
} |
} |
742 |
return pChunkData; |
return pChunkData; |
743 |
} |
} |
767 |
* |
* |
768 |
* <b>Caution:</b> You cannot directly write to enlarged chunks before |
* <b>Caution:</b> You cannot directly write to enlarged chunks before |
769 |
* calling File::Save() as this might exceed the current chunk's body |
* calling File::Save() as this might exceed the current chunk's body |
770 |
* boundary. |
* boundary! |
771 |
* |
* |
772 |
* @param iNewSize - new chunk body size in bytes (must be greater than zero) |
* @param iNewSize - new chunk body size in bytes (must be greater than zero) |
773 |
* @throws RIFF::Exception if \a iNewSize is less than 1 |
* @throws RIFF::Exception if \a iNewSize is less than 1 |
775 |
*/ |
*/ |
776 |
void Chunk::Resize(int iNewSize) { |
void Chunk::Resize(int iNewSize) { |
777 |
if (iNewSize <= 0) throw Exception("Chunk size must be at least one byte"); |
if (iNewSize <= 0) throw Exception("Chunk size must be at least one byte"); |
778 |
|
if (NewChunkSize == iNewSize) return; |
779 |
NewChunkSize = iNewSize; |
NewChunkSize = iNewSize; |
780 |
pFile->LogAsResized(this); |
pFile->LogAsResized(this); |
781 |
} |
} |
793 |
* (including its header size of course) |
* (including its header size of course) |
794 |
*/ |
*/ |
795 |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
796 |
unsigned long ulOriginalPos = ulWritePos; |
const unsigned long ulOriginalPos = ulWritePos; |
797 |
ulWritePos += CHUNK_HEADER_SIZE; |
ulWritePos += CHUNK_HEADER_SIZE; |
798 |
|
|
799 |
|
if (pFile->Mode != stream_mode_read_write) |
800 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
801 |
|
|
802 |
// if the whole chunk body was loaded into RAM |
// if the whole chunk body was loaded into RAM |
803 |
if (pChunkData) { |
if (pChunkData) { |
804 |
// in case the chunk size was changed, reallocate the data in RAM with the chunk's new size |
// make sure chunk data buffer in RAM is at least as large as the new chunk size |
805 |
if (NewChunkSize != CurrentChunkSize) { |
LoadChunkData(); |
|
uint8_t* pNewBuffer = new uint8_t[NewChunkSize]; |
|
|
if (NewChunkSize > CurrentChunkSize) { |
|
|
memcpy(pNewBuffer, pChunkData, CurrentChunkSize); |
|
|
memset(pNewBuffer + CurrentChunkSize, 0, NewChunkSize - CurrentChunkSize); |
|
|
} else { |
|
|
memcpy(pNewBuffer, pChunkData, NewChunkSize); |
|
|
} |
|
|
delete[] pChunkData; |
|
|
pChunkData = pNewBuffer; |
|
|
} |
|
|
|
|
806 |
// write chunk data from RAM persistently to the file |
// write chunk data from RAM persistently to the file |
807 |
#if POSIX |
#if POSIX |
808 |
lseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
lseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
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) { |
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 |
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); |
864 |
|
|
865 |
// add pad byte if needed |
// add pad byte if needed |
866 |
if ((ulStartPos + NewChunkSize) % 2 != 0) { |
if ((ulStartPos + NewChunkSize) % 2 != 0) { |
867 |
char cPadByte = 0; |
const char cPadByte = 0; |
868 |
#if POSIX |
#if POSIX |
869 |
|
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); |
877 |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
878 |
#endif |
#endif |
879 |
return ulStartPos + NewChunkSize + 1; |
return ulStartPos + NewChunkSize + 1; |
1127 |
Chunk* List::AddSubChunk(uint32_t uiChunkID, uint uiBodySize) { |
Chunk* List::AddSubChunk(uint32_t uiChunkID, uint uiBodySize) { |
1128 |
if (uiBodySize == 0) throw Exception("Chunk body size must be at least 1 byte"); |
if (uiBodySize == 0) throw Exception("Chunk body size must be at least 1 byte"); |
1129 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1130 |
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, uiBodySize); |
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0); |
1131 |
pSubChunks->push_back(pNewChunk); |
pSubChunks->push_back(pNewChunk); |
1132 |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1133 |
pFile->ResizedChunks.push_back(pNewChunk); |
pNewChunk->Resize(uiBodySize); |
1134 |
return pNewChunk; |
return pNewChunk; |
1135 |
} |
} |
1136 |
|
|
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 |
*/ |
*/ |
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); |
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); |
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 |
1275 |
* (including its header size of course) |
* (including its header size of course) |
1276 |
*/ |
*/ |
1277 |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
1278 |
unsigned long ulOriginalPos = ulWritePos; |
const unsigned long ulOriginalPos = ulWritePos; |
1279 |
ulWritePos += LIST_HEADER_SIZE; |
ulWritePos += LIST_HEADER_SIZE; |
1280 |
|
|
1281 |
|
if (pFile->Mode != stream_mode_read_write) |
1282 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
1283 |
|
|
1284 |
// write all subchunks (including sub list chunks) recursively |
// write all subchunks (including sub list chunks) recursively |
1285 |
if (pSubChunks) { |
if (pSubChunks) { |
1286 |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
1292 |
CurrentChunkSize = NewChunkSize = ulWritePos - ulOriginalPos - LIST_HEADER_SIZE; |
CurrentChunkSize = NewChunkSize = ulWritePos - ulOriginalPos - LIST_HEADER_SIZE; |
1293 |
WriteHeader(ulOriginalPos); |
WriteHeader(ulOriginalPos); |
1294 |
|
|
1295 |
|
// offset of this list chunk in new written file may have changed |
1296 |
|
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
1297 |
|
|
1298 |
return ulWritePos; |
return ulWritePos; |
1299 |
} |
} |
1300 |
|
|
1325 |
* "from scratch". Note: there must be no empty chunks or empty list |
* "from scratch". Note: there must be no empty chunks or empty list |
1326 |
* chunks when trying to make the new RIFF file persistent with Save()! |
* chunks when trying to make the new RIFF file persistent with Save()! |
1327 |
* |
* |
1328 |
|
* @param FileType - four-byte identifier of the RIFF file type |
1329 |
* @see AddSubChunk(), AddSubList() |
* @see AddSubChunk(), AddSubList() |
1330 |
*/ |
*/ |
1331 |
File::File() : 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; |
1338 |
bEndianNative = true; |
bEndianNative = true; |
1339 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1340 |
|
ListType = FileType; |
1341 |
} |
} |
1342 |
|
|
1343 |
/** @brief Load existing RIFF file. |
/** @brief Load existing RIFF file. |
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; |
1378 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1379 |
ReadHeader(0); |
ReadHeader(0); |
1380 |
if (ChunkID != CHUNK_ID_RIFF) { |
if (ChunkID != CHUNK_ID_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 |
1432 |
return true; |
break; |
1433 |
case stream_mode_read_write: |
case stream_mode_read_write: |
1434 |
#if POSIX |
#if POSIX |
1435 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
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 |
1467 |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1468 |
return true; |
break; |
1469 |
|
case stream_mode_closed: |
1470 |
|
#if POSIX |
1471 |
|
if (hFileRead) close(hFileRead); |
1472 |
|
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 |
1477 |
|
if (hFileRead) fclose(hFileRead); |
1478 |
|
if (hFileWrite) fclose(hFileWrite); |
1479 |
|
#endif |
1480 |
|
hFileRead = hFileWrite = 0; |
1481 |
|
break; |
1482 |
default: |
default: |
1483 |
throw Exception("Unknown file access mode"); |
throw Exception("Unknown file access mode"); |
1484 |
} |
} |
1485 |
|
Mode = NewMode; |
1486 |
|
return true; |
1487 |
} |
} |
1488 |
return false; |
return false; |
1489 |
} |
} |
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 |
bool bModeWasChanged = SetMode(stream_mode_read_write); |
SetMode(stream_mode_read_write); |
1507 |
|
|
1508 |
// to be able to save the whole file without loading everything into |
// to be able to save the whole file without loading everything into |
1509 |
// RAM and without having to store the data in a temporary file, we |
// RAM and without having to store the data in a temporary file, we |
1516 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1517 |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
1518 |
if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)"); |
if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)"); |
1519 |
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
1520 |
if (ulDiff > 0) ulPositiveSizeDiff += ulDiff; |
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
1521 |
|
ulPositiveSizeDiff += ulDiff; |
1522 |
|
} |
1523 |
} |
} |
1524 |
|
|
1525 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |
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; |
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); |
1562 |
} |
} |
1563 |
|
|
1564 |
// rebuild / rewrite complete RIFF tree |
// rebuild / rewrite complete RIFF tree |
1565 |
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
1566 |
|
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1567 |
|
|
1568 |
// resize file to the final size |
// resize file to the final size |
1569 |
if (ulTotalSize < ulWorkingFileSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1570 |
|
|
1571 |
// forget all resized chunks |
// forget all resized chunks |
1572 |
ResizedChunks.clear(); |
ResizedChunks.clear(); |
|
|
|
|
// reopen file in read mode |
|
|
if (bModeWasChanged) SetMode(stream_mode_read); |
|
1573 |
} |
} |
1574 |
|
|
1575 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
1577 |
* Make all changes of all chunks persistent by writing them to another |
* Make all changes of all chunks persistent by writing them to another |
1578 |
* file. <b>Caution:</b> this method is optimized for writing to |
* file. <b>Caution:</b> this method is optimized for writing to |
1579 |
* <b>another</b> file, do not use it to save the changes to the same |
* <b>another</b> file, do not use it to save the changes to the same |
1580 |
* file! Use File::Save() in that case instead! |
* file! Use File::Save() in that case instead! Ignoring this might |
1581 |
|
* result in a corrupted file, especially in case chunks were resized! |
1582 |
* |
* |
1583 |
* After calling this method, this File object will be associated with |
* After calling this method, this File object will be associated with |
1584 |
* the new file (given by \a path) afterwards. |
* the new file (given by \a path) afterwards. |
1586 |
* @param path - path and file name where everything should be written to |
* @param path - path and file name where everything should be written to |
1587 |
*/ |
*/ |
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 |
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); |
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 |
1597 |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT | O_TRUNC); |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
1598 |
if (hFileWrite < 0) { |
if (hFileWrite < 0) { |
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) { |
1615 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
throw Exception("Could not open file \"" + path + "\" for writing"); |
1616 |
} |
} |
1617 |
#endif // POSIX |
#endif // POSIX |
1618 |
|
Mode = stream_mode_read_write; |
1619 |
|
|
1620 |
// write complete RIFF tree to the other (new) file |
// write complete RIFF tree to the other (new) file |
1621 |
WriteChunk(0, 0); |
unsigned long ulTotalSize = WriteChunk(0, 0); |
1622 |
|
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1623 |
|
|
1624 |
|
// resize file to the final size (if the file was originally larger) |
1625 |
|
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1626 |
|
|
1627 |
// forget all resized chunks |
// forget all resized chunks |
1628 |
ResizedChunks.clear(); |
ResizedChunks.clear(); |
1629 |
|
|
1630 |
|
if (Filename.length() > 0) { |
1631 |
|
#if POSIX |
1632 |
|
close(hFileWrite); |
1633 |
|
#elif defined(WIN32) |
1634 |
|
CloseHandle(hFileWrite); |
1635 |
|
#else |
1636 |
|
fclose(hFileWrite); |
1637 |
|
#endif |
1638 |
|
hFileWrite = hFileRead; |
1639 |
|
} |
1640 |
|
|
1641 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1642 |
Filename = path; |
Filename = path; |
1643 |
stream_mode_t oldMode = Mode; |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1644 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
|
SetMode(oldMode); // ... so SetMode() has to reopen the file handles. |
|
1645 |
} |
} |
1646 |
|
|
1647 |
void File::ResizeFile(unsigned long ulNewSize) { |
void File::ResizeFile(unsigned long ulNewSize) { |
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! |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
1659 |
#endif |
#endif |
1660 |
} |
} |
1661 |
|
|
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 |
1677 |
} |
} |
1678 |
|
|
1679 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |
1680 |
#if POSIX |
return __GetFileSize(hFileRead); |
1681 |
|
} |
1682 |
|
|
1683 |
|
#if POSIX |
1684 |
|
unsigned long File::__GetFileSize(int hFile) { |
1685 |
struct stat filestat; |
struct stat filestat; |
1686 |
fstat(hFileRead, &filestat); |
fstat(hFile, &filestat); |
1687 |
long size = filestat.st_size; |
long size = filestat.st_size; |
|
#else // standard C functions |
|
|
long curpos = ftell(hFileRead); |
|
|
fseek(hFileRead, 0, SEEK_END); |
|
|
long size = ftell(hFileRead); |
|
|
fseek(hFileRead, curpos, SEEK_SET); |
|
|
#endif // POSIX |
|
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 |
1698 |
|
unsigned long File::__GetFileSize(FILE* hFile) { |
1699 |
|
long curpos = ftell(hFile); |
1700 |
|
fseek(hFile, 0, SEEK_END); |
1701 |
|
long size = ftell(hFile); |
1702 |
|
fseek(hFile, curpos, SEEK_SET); |
1703 |
|
return size; |
1704 |
|
} |
1705 |
|
#endif |
1706 |
|
|
1707 |
|
|
1708 |
// *************** Exception *************** |
// *************** Exception *************** |