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-2014 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 * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
|
#include <algorithm> |
25 |
|
#include <set> |
26 |
#include <string.h> |
#include <string.h> |
|
#include <sstream> |
|
27 |
|
|
28 |
#include "RIFF.h" |
#include "RIFF.h" |
29 |
|
|
30 |
|
#include "helper.h" |
31 |
|
|
32 |
namespace RIFF { |
namespace RIFF { |
33 |
|
|
34 |
// *************** Helper Functions ************** |
// *************** Internal functions ************** |
35 |
// * |
// * |
36 |
|
|
37 |
template<class T> inline String ToString(T o) { |
/// Returns a human readable path of the given chunk. |
38 |
std::stringstream ss; |
static String __resolveChunkPath(Chunk* pCk) { |
39 |
ss << o; |
String sPath; |
40 |
return ss.str(); |
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { |
41 |
|
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
42 |
|
List* pList = (List*) pChunk; |
43 |
|
sPath = "->'" + pList->GetListTypeString() + "'" + sPath; |
44 |
|
} else { |
45 |
|
sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath; |
46 |
|
} |
47 |
|
} |
48 |
|
return sPath; |
49 |
} |
} |
50 |
|
|
51 |
|
|
60 |
ulPos = 0; |
ulPos = 0; |
61 |
pParent = NULL; |
pParent = NULL; |
62 |
pChunkData = NULL; |
pChunkData = NULL; |
63 |
|
CurrentChunkSize = 0; |
64 |
|
NewChunkSize = 0; |
65 |
|
ulChunkDataSize = 0; |
66 |
|
ChunkID = CHUNK_ID_RIFF; |
67 |
this->pFile = pFile; |
this->pFile = pFile; |
68 |
} |
} |
69 |
|
|
76 |
pParent = Parent; |
pParent = Parent; |
77 |
ulPos = 0; |
ulPos = 0; |
78 |
pChunkData = NULL; |
pChunkData = NULL; |
79 |
|
CurrentChunkSize = 0; |
80 |
|
NewChunkSize = 0; |
81 |
|
ulChunkDataSize = 0; |
82 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
83 |
} |
} |
84 |
|
|
89 |
ulPos = 0; |
ulPos = 0; |
90 |
pChunkData = NULL; |
pChunkData = NULL; |
91 |
ChunkID = uiChunkID; |
ChunkID = uiChunkID; |
92 |
|
ulChunkDataSize = 0; |
93 |
CurrentChunkSize = 0; |
CurrentChunkSize = 0; |
94 |
NewChunkSize = uiBodySize; |
NewChunkSize = uiBodySize; |
95 |
} |
} |
96 |
|
|
97 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
98 |
|
if (pFile) pFile->UnlogResized(this); |
99 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
100 |
} |
} |
101 |
|
|
103 |
#if DEBUG |
#if DEBUG |
104 |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
105 |
#endif // DEBUG |
#endif // DEBUG |
106 |
|
ChunkID = 0; |
107 |
|
NewChunkSize = CurrentChunkSize = 0; |
108 |
#if POSIX |
#if POSIX |
109 |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
110 |
read(pFile->hFileRead, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
111 |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
112 |
|
#elif defined(WIN32) |
113 |
|
if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
114 |
|
DWORD dwBytesRead; |
115 |
|
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
116 |
|
ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL); |
117 |
#else |
#else |
118 |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
119 |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
121 |
#endif // POSIX |
#endif // POSIX |
122 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
123 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
124 |
bEndianNative = false; |
pFile->bEndianNative = false; |
125 |
} |
} |
126 |
#else // little endian |
#else // little endian |
127 |
if (ChunkID == CHUNK_ID_RIFX) { |
if (ChunkID == CHUNK_ID_RIFX) { |
135 |
} |
} |
136 |
#if DEBUG |
#if DEBUG |
137 |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
138 |
std::cout << "ckSize=" << ChunkSize << " "; |
std::cout << "ckSize=" << CurrentChunkSize << " "; |
139 |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; |
140 |
#endif // DEBUG |
#endif // DEBUG |
141 |
NewChunkSize = CurrentChunkSize; |
NewChunkSize = CurrentChunkSize; |
142 |
} |
} |
162 |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
163 |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
164 |
} |
} |
165 |
|
#elif defined(WIN32) |
166 |
|
if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
167 |
|
DWORD dwBytesWritten; |
168 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
169 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL); |
170 |
|
} |
171 |
#else |
#else |
172 |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
173 |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
232 |
#if DEBUG |
#if DEBUG |
233 |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
234 |
#endif // DEBUG |
#endif // DEBUG |
235 |
return CurrentChunkSize - ulPos; |
return (CurrentChunkSize > ulPos) ? CurrentChunkSize - ulPos : 0; |
236 |
} |
} |
237 |
|
|
238 |
/** |
/** |
243 |
* - RIFF::stream_closed : |
* - RIFF::stream_closed : |
244 |
* the data stream was closed somehow, no more reading possible |
* the data stream was closed somehow, no more reading possible |
245 |
* - RIFF::stream_end_reached : |
* - RIFF::stream_end_reached : |
246 |
* alreaady reached the end of the chunk data, no more reading |
* already reached the end of the chunk data, no more reading |
247 |
* possible without SetPos() |
* possible without SetPos() |
248 |
*/ |
*/ |
249 |
stream_state_t Chunk::GetState() { |
stream_state_t Chunk::GetState() { |
251 |
std::cout << "Chunk::GetState()" << std::endl; |
std::cout << "Chunk::GetState()" << std::endl; |
252 |
#endif // DEBUG |
#endif // DEBUG |
253 |
#if POSIX |
#if POSIX |
254 |
if (pFile->hFileRead == 0) return stream_closed; |
if (pFile->hFileRead == 0) return stream_closed; |
255 |
|
#elif defined (WIN32) |
256 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) |
257 |
|
return stream_closed; |
258 |
#else |
#else |
259 |
if (pFile->hFileRead == NULL) return stream_closed; |
if (pFile->hFileRead == NULL) return stream_closed; |
260 |
#endif // POSIX |
#endif // POSIX |
281 |
#if DEBUG |
#if DEBUG |
282 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
283 |
#endif // DEBUG |
#endif // DEBUG |
284 |
|
//if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
285 |
if (ulPos >= CurrentChunkSize) return 0; |
if (ulPos >= CurrentChunkSize) return 0; |
286 |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
287 |
#if POSIX |
#if POSIX |
289 |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
290 |
if (readWords < 1) return 0; |
if (readWords < 1) return 0; |
291 |
readWords /= WordSize; |
readWords /= WordSize; |
292 |
|
#elif defined(WIN32) |
293 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
294 |
|
DWORD readWords; |
295 |
|
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); |
296 |
|
if (readWords < 1) return 0; |
297 |
|
readWords /= WordSize; |
298 |
#else // standard C functions |
#else // standard C functions |
299 |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
300 |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
336 |
* @see Resize() |
* @see Resize() |
337 |
*/ |
*/ |
338 |
unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) { |
unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) { |
339 |
if (pFile->Mode == stream_mode_read) |
if (pFile->Mode != stream_mode_read_write) |
340 |
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"); |
341 |
if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize >= CurrentChunkSize) |
if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize > CurrentChunkSize) |
342 |
throw Exception("End of chunk reached while trying to write data"); |
throw Exception("End of chunk reached while trying to write data"); |
343 |
if (!pFile->bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
344 |
switch (WordSize) { |
switch (WordSize) { |
364 |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
365 |
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"); |
366 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
367 |
|
#elif defined(WIN32) |
368 |
|
if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
369 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
370 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
371 |
|
} |
372 |
|
DWORD writtenWords; |
373 |
|
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
374 |
|
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
375 |
|
writtenWords /= WordSize; |
376 |
#else // standard C functions |
#else // standard C functions |
377 |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
378 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
throw Exception("Could not seek to position " + ToString(ulPos) + |
595 |
} |
} |
596 |
|
|
597 |
/** |
/** |
598 |
|
* Reads a null-padded string of size characters and copies it |
599 |
|
* into the string \a s. The position within the chunk will |
600 |
|
* automatically be incremented. |
601 |
|
* |
602 |
|
* @param s destination string |
603 |
|
* @param size number of characters to read |
604 |
|
* @throws RIFF::Exception if an error occured or less than |
605 |
|
* \a size characters could be read! |
606 |
|
*/ |
607 |
|
void Chunk::ReadString(String& s, int size) { |
608 |
|
char* buf = new char[size]; |
609 |
|
ReadSceptical(buf, 1, size); |
610 |
|
s.assign(buf, std::find(buf, buf + size, '\0')); |
611 |
|
delete[] buf; |
612 |
|
} |
613 |
|
|
614 |
|
/** |
615 |
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
616 |
* buffer pointed by \a pData to the chunk's body, directly to the |
* buffer pointed by \a pData to the chunk's body, directly to the |
617 |
* actual "physical" file. The position within the chunk will |
* actual "physical" file. The position within the chunk will |
736 |
* |
* |
737 |
* <b>Caution:</b> the buffer pointer will be invalidated once |
* <b>Caution:</b> the buffer pointer will be invalidated once |
738 |
* File::Save() was called. You have to call LoadChunkData() again to |
* File::Save() was called. You have to call LoadChunkData() again to |
739 |
* get a new pointer whenever File::Save() was called. |
* get a new, valid pointer whenever File::Save() was called. |
740 |
|
* |
741 |
|
* You can call LoadChunkData() again if you previously scheduled to |
742 |
|
* enlarge this chunk with a Resize() call. In that case the buffer will |
743 |
|
* be enlarged to the new, scheduled chunk size and you can already |
744 |
|
* place the new chunk data to the buffer and finally call File::Save() |
745 |
|
* to enlarge the chunk physically and write the new data in one rush. |
746 |
|
* This approach is definitely recommended if you have to enlarge and |
747 |
|
* write new data to a lot of chunks. |
748 |
* |
* |
749 |
* @returns a pointer to the data in RAM on success, NULL otherwise |
* @returns a pointer to the data in RAM on success, NULL otherwise |
750 |
|
* @throws Exception if data buffer could not be enlarged |
751 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
752 |
*/ |
*/ |
753 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
754 |
if (!pChunkData) { |
if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) { |
755 |
#if POSIX |
#if POSIX |
756 |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
757 |
pChunkData = new uint8_t[GetSize()]; |
#elif defined(WIN32) |
758 |
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()); |
|
759 |
#else |
#else |
760 |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
761 |
pChunkData = new uint8_t[GetSize()]; |
#endif // POSIX |
762 |
|
unsigned long ulBufferSize = (CurrentChunkSize > NewChunkSize) ? CurrentChunkSize : NewChunkSize; |
763 |
|
pChunkData = new uint8_t[ulBufferSize]; |
764 |
if (!pChunkData) return NULL; |
if (!pChunkData) return NULL; |
765 |
|
memset(pChunkData, 0, ulBufferSize); |
766 |
|
#if POSIX |
767 |
|
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
768 |
|
#elif defined(WIN32) |
769 |
|
DWORD readWords; |
770 |
|
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); |
771 |
|
#else |
772 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
773 |
#endif // POSIX |
#endif // POSIX |
774 |
if (readWords != GetSize()) { |
if (readWords != GetSize()) { |
775 |
delete[] pChunkData; |
delete[] pChunkData; |
776 |
return (pChunkData = NULL); |
return (pChunkData = NULL); |
777 |
} |
} |
778 |
|
ulChunkDataSize = ulBufferSize; |
779 |
|
} else if (NewChunkSize > ulChunkDataSize) { |
780 |
|
uint8_t* pNewBuffer = new uint8_t[NewChunkSize]; |
781 |
|
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(NewChunkSize) + " bytes"); |
782 |
|
memset(pNewBuffer, 0 , NewChunkSize); |
783 |
|
memcpy(pNewBuffer, pChunkData, ulChunkDataSize); |
784 |
|
delete[] pChunkData; |
785 |
|
pChunkData = pNewBuffer; |
786 |
|
ulChunkDataSize = NewChunkSize; |
787 |
} |
} |
788 |
return pChunkData; |
return pChunkData; |
789 |
} |
} |
813 |
* |
* |
814 |
* <b>Caution:</b> You cannot directly write to enlarged chunks before |
* <b>Caution:</b> You cannot directly write to enlarged chunks before |
815 |
* calling File::Save() as this might exceed the current chunk's body |
* calling File::Save() as this might exceed the current chunk's body |
816 |
* boundary. |
* boundary! |
817 |
* |
* |
818 |
* @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) |
819 |
* @throws RIFF::Exception if \a iNewSize is less than 1 |
* @throws RIFF::Exception if \a iNewSize is less than 1 |
820 |
* @see File::Save() |
* @see File::Save() |
821 |
*/ |
*/ |
822 |
void Chunk::Resize(int iNewSize) { |
void Chunk::Resize(int iNewSize) { |
823 |
if (iNewSize <= 0) throw Exception("Chunk size must be at least one byte"); |
if (iNewSize <= 0) |
824 |
|
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this)); |
825 |
|
if (NewChunkSize == iNewSize) return; |
826 |
NewChunkSize = iNewSize; |
NewChunkSize = iNewSize; |
827 |
pFile->LogAsResized(this); |
pFile->LogAsResized(this); |
828 |
} |
} |
840 |
* (including its header size of course) |
* (including its header size of course) |
841 |
*/ |
*/ |
842 |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
843 |
unsigned long ulOriginalPos = ulWritePos; |
const unsigned long ulOriginalPos = ulWritePos; |
844 |
ulWritePos += CHUNK_HEADER_SIZE; |
ulWritePos += CHUNK_HEADER_SIZE; |
845 |
|
|
846 |
|
if (pFile->Mode != stream_mode_read_write) |
847 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
848 |
|
|
849 |
// if the whole chunk body was loaded into RAM |
// if the whole chunk body was loaded into RAM |
850 |
if (pChunkData) { |
if (pChunkData) { |
851 |
// 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 |
852 |
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; |
|
|
} |
|
|
|
|
853 |
// write chunk data from RAM persistently to the file |
// write chunk data from RAM persistently to the file |
854 |
#if POSIX |
#if POSIX |
855 |
lseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
lseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
856 |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
857 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
858 |
} |
} |
859 |
|
#elif defined(WIN32) |
860 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN); |
861 |
|
DWORD dwBytesWritten; |
862 |
|
WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL); |
863 |
|
if (dwBytesWritten != NewChunkSize) { |
864 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
865 |
|
} |
866 |
#else |
#else |
867 |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
868 |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
873 |
// 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 |
874 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
875 |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
876 |
|
#if defined(WIN32) |
877 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
878 |
|
#else |
879 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
880 |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
#endif |
881 |
|
for (unsigned long ulOffset = 0; ulToMove > 0 && iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
882 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
883 |
#if POSIX |
#if POSIX |
884 |
lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
885 |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
886 |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
887 |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
888 |
|
#elif defined(WIN32) |
889 |
|
SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
890 |
|
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
891 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
892 |
|
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
893 |
#else |
#else |
894 |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
895 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
911 |
|
|
912 |
// add pad byte if needed |
// add pad byte if needed |
913 |
if ((ulStartPos + NewChunkSize) % 2 != 0) { |
if ((ulStartPos + NewChunkSize) % 2 != 0) { |
914 |
char cPadByte = 0; |
const char cPadByte = 0; |
915 |
#if POSIX |
#if POSIX |
916 |
|
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
917 |
write(pFile->hFileWrite, &cPadByte, 1); |
write(pFile->hFileWrite, &cPadByte, 1); |
918 |
|
#elif defined(WIN32) |
919 |
|
SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN); |
920 |
|
DWORD dwBytesWritten; |
921 |
|
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
922 |
#else |
#else |
923 |
|
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
924 |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
925 |
#endif |
#endif |
926 |
return ulStartPos + NewChunkSize + 1; |
return ulStartPos + NewChunkSize + 1; |
968 |
#if DEBUG |
#if DEBUG |
969 |
std::cout << "List::~List()" << std::endl; |
std::cout << "List::~List()" << std::endl; |
970 |
#endif // DEBUG |
#endif // DEBUG |
971 |
|
DeleteChunkList(); |
972 |
|
} |
973 |
|
|
974 |
|
void List::DeleteChunkList() { |
975 |
if (pSubChunks) { |
if (pSubChunks) { |
976 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
977 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
980 |
iter++; |
iter++; |
981 |
} |
} |
982 |
delete pSubChunks; |
delete pSubChunks; |
983 |
|
pSubChunks = NULL; |
984 |
|
} |
985 |
|
if (pSubChunksMap) { |
986 |
|
delete pSubChunksMap; |
987 |
|
pSubChunksMap = NULL; |
988 |
} |
} |
|
if (pSubChunksMap) delete pSubChunksMap; |
|
989 |
} |
} |
990 |
|
|
991 |
/** |
/** |
1182 |
Chunk* List::AddSubChunk(uint32_t uiChunkID, uint uiBodySize) { |
Chunk* List::AddSubChunk(uint32_t uiChunkID, uint uiBodySize) { |
1183 |
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"); |
1184 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1185 |
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, uiBodySize); |
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0); |
1186 |
pSubChunks->push_back(pNewChunk); |
pSubChunks->push_back(pNewChunk); |
1187 |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1188 |
pFile->ResizedChunks.push_back(pNewChunk); |
pNewChunk->Resize(uiBodySize); |
1189 |
|
NewChunkSize += CHUNK_HEADER_SIZE; |
1190 |
|
pFile->LogAsResized(this); |
1191 |
return pNewChunk; |
return pNewChunk; |
1192 |
} |
} |
1193 |
|
|
1194 |
|
/** @brief Moves a sub chunk witin this list. |
1195 |
|
* |
1196 |
|
* Moves a sub chunk from one position in this list to another |
1197 |
|
* position in the same list. The pSrc chunk is placed before the |
1198 |
|
* pDst chunk. |
1199 |
|
* |
1200 |
|
* @param pSrc - sub chunk to be moved |
1201 |
|
* @param pDst - the position to move to. pSrc will be placed |
1202 |
|
* before pDst. If pDst is 0, pSrc will be placed |
1203 |
|
* last in list. |
1204 |
|
*/ |
1205 |
|
void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) { |
1206 |
|
if (!pSubChunks) LoadSubChunks(); |
1207 |
|
pSubChunks->remove(pSrc); |
1208 |
|
ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst); |
1209 |
|
pSubChunks->insert(iter, pSrc); |
1210 |
|
} |
1211 |
|
|
1212 |
|
/** @brief Moves a sub chunk from this list to another list. |
1213 |
|
* |
1214 |
|
* Moves a sub chunk from this list list to the end of another |
1215 |
|
* list. |
1216 |
|
* |
1217 |
|
* @param pSrc - sub chunk to be moved |
1218 |
|
* @param pDst - destination list where the chunk shall be moved to |
1219 |
|
*/ |
1220 |
|
void List::MoveSubChunk(Chunk* pSrc, List* pNewParent) { |
1221 |
|
if (pNewParent == this || !pNewParent) return; |
1222 |
|
if (!pSubChunks) LoadSubChunks(); |
1223 |
|
if (!pNewParent->pSubChunks) pNewParent->LoadSubChunks(); |
1224 |
|
pSubChunks->remove(pSrc); |
1225 |
|
pNewParent->pSubChunks->push_back(pSrc); |
1226 |
|
// update chunk id map of this List |
1227 |
|
if ((*pSubChunksMap)[pSrc->GetChunkID()] == pSrc) { |
1228 |
|
pSubChunksMap->erase(pSrc->GetChunkID()); |
1229 |
|
// try to find another chunk of the same chunk ID |
1230 |
|
ChunkList::iterator iter = pSubChunks->begin(); |
1231 |
|
ChunkList::iterator end = pSubChunks->end(); |
1232 |
|
for (; iter != end; ++iter) { |
1233 |
|
if ((*iter)->GetChunkID() == pSrc->GetChunkID()) { |
1234 |
|
(*pSubChunksMap)[pSrc->GetChunkID()] = *iter; |
1235 |
|
break; // we're done, stop search |
1236 |
|
} |
1237 |
|
} |
1238 |
|
} |
1239 |
|
// update chunk id map of other list |
1240 |
|
if (!(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()]) |
1241 |
|
(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()] = pSrc; |
1242 |
|
} |
1243 |
|
|
1244 |
/** @brief Creates a new list sub chunk. |
/** @brief Creates a new list sub chunk. |
1245 |
* |
* |
1246 |
* 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 |
1255 |
List* pNewListChunk = new List(pFile, this, uiListType); |
List* pNewListChunk = new List(pFile, this, uiListType); |
1256 |
pSubChunks->push_back(pNewListChunk); |
pSubChunks->push_back(pNewListChunk); |
1257 |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1258 |
|
NewChunkSize += LIST_HEADER_SIZE; |
1259 |
|
pFile->LogAsResized(this); |
1260 |
return pNewListChunk; |
return pNewListChunk; |
1261 |
} |
} |
1262 |
|
|
1264 |
* |
* |
1265 |
* 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 |
1266 |
* 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 |
1267 |
* 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, |
1268 |
* change persistent at any time. |
* all its subchunks (if any) will be removed recursively as well. You |
1269 |
|
* should call File::Save() to make this change persistent at any time. |
1270 |
* |
* |
1271 |
* @param pSubChunk - sub chunk or sub list chunk to be removed |
* @param pSubChunk - sub chunk or sub list chunk to be removed |
1272 |
*/ |
*/ |
1293 |
std::cout << "List::Readheader(ulong) "; |
std::cout << "List::Readheader(ulong) "; |
1294 |
#endif // DEBUG |
#endif // DEBUG |
1295 |
Chunk::ReadHeader(fPos); |
Chunk::ReadHeader(fPos); |
1296 |
|
if (CurrentChunkSize < 4) return; |
1297 |
NewChunkSize = CurrentChunkSize -= 4; |
NewChunkSize = CurrentChunkSize -= 4; |
1298 |
#if POSIX |
#if POSIX |
1299 |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1300 |
read(pFile->hFileRead, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1301 |
|
#elif defined(WIN32) |
1302 |
|
SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1303 |
|
DWORD dwBytesRead; |
1304 |
|
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
1305 |
#else |
#else |
1306 |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1307 |
fread(&ListType, 4, 1, pFile->hFileRead); |
fread(&ListType, 4, 1, pFile->hFileRead); |
1322 |
#if POSIX |
#if POSIX |
1323 |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1324 |
write(pFile->hFileWrite, &ListType, 4); |
write(pFile->hFileWrite, &ListType, 4); |
1325 |
|
#elif defined(WIN32) |
1326 |
|
SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1327 |
|
DWORD dwBytesWritten; |
1328 |
|
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
1329 |
#else |
#else |
1330 |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1331 |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
1339 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1340 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1341 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1342 |
|
#if defined(WIN32) |
1343 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) return; |
1344 |
|
#else |
1345 |
if (!pFile->hFileRead) return; |
if (!pFile->hFileRead) return; |
1346 |
|
#endif |
1347 |
unsigned long uiOriginalPos = GetPos(); |
unsigned long uiOriginalPos = GetPos(); |
1348 |
SetPos(0); // jump to beginning of list chunk body |
SetPos(0); // jump to beginning of list chunk body |
1349 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
1369 |
} |
} |
1370 |
} |
} |
1371 |
|
|
1372 |
|
void List::LoadSubChunksRecursively() { |
1373 |
|
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList()) |
1374 |
|
pList->LoadSubChunksRecursively(); |
1375 |
|
} |
1376 |
|
|
1377 |
/** @brief Write list chunk persistently e.g. to disk. |
/** @brief Write list chunk persistently e.g. to disk. |
1378 |
* |
* |
1379 |
* Stores the list chunk persistently to its actual "physical" file. All |
* Stores the list chunk persistently to its actual "physical" file. All |
1389 |
* (including its header size of course) |
* (including its header size of course) |
1390 |
*/ |
*/ |
1391 |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
1392 |
unsigned long ulOriginalPos = ulWritePos; |
const unsigned long ulOriginalPos = ulWritePos; |
1393 |
ulWritePos += LIST_HEADER_SIZE; |
ulWritePos += LIST_HEADER_SIZE; |
1394 |
|
|
1395 |
|
if (pFile->Mode != stream_mode_read_write) |
1396 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
1397 |
|
|
1398 |
// write all subchunks (including sub list chunks) recursively |
// write all subchunks (including sub list chunks) recursively |
1399 |
if (pSubChunks) { |
if (pSubChunks) { |
1400 |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
1406 |
CurrentChunkSize = NewChunkSize = ulWritePos - ulOriginalPos - LIST_HEADER_SIZE; |
CurrentChunkSize = NewChunkSize = ulWritePos - ulOriginalPos - LIST_HEADER_SIZE; |
1407 |
WriteHeader(ulOriginalPos); |
WriteHeader(ulOriginalPos); |
1408 |
|
|
1409 |
|
// offset of this list chunk in new written file may have changed |
1410 |
|
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
1411 |
|
|
1412 |
return ulWritePos; |
return ulWritePos; |
1413 |
} |
} |
1414 |
|
|
1433 |
// *************** File *************** |
// *************** File *************** |
1434 |
// * |
// * |
1435 |
|
|
1436 |
|
//HACK: to avoid breaking DLL compatibility to older versions of libgig we roll the new std::set<Chunk*> into the old std::list<Chunk*> container, should be replaced on member variable level soon though |
1437 |
|
#define _GET_RESIZED_CHUNKS() \ |
1438 |
|
(reinterpret_cast<std::set<Chunk*>*>(ResizedChunks.front())) |
1439 |
|
|
1440 |
/** @brief Create new RIFF file. |
/** @brief Create new RIFF file. |
1441 |
* |
* |
1442 |
* Use this constructor if you want to create a new RIFF file completely |
* Use this constructor if you want to create a new RIFF file completely |
1443 |
* "from scratch". Note: there must be no empty chunks or empty list |
* "from scratch". Note: there must be no empty chunks or empty list |
1444 |
* chunks when trying to make the new RIFF file persistent with Save()! |
* chunks when trying to make the new RIFF file persistent with Save()! |
1445 |
* |
* |
1446 |
* @see AddSubChunk(), AddSubList() |
* Note: by default, the RIFF file will be saved in native endian |
1447 |
*/ |
* format; that is, as a RIFF file on little-endian machines and |
1448 |
File::File() : List(this) { |
* as a RIFX file on big-endian. To change this behaviour, call |
1449 |
|
* SetByteOrder() before calling Save(). |
1450 |
|
* |
1451 |
|
* @param FileType - four-byte identifier of the RIFF file type |
1452 |
|
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1453 |
|
*/ |
1454 |
|
File::File(uint32_t FileType) |
1455 |
|
: List(this), bIsNewFile(true), Layout(layout_standard) |
1456 |
|
{ |
1457 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1458 |
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1459 |
|
#if defined(WIN32) |
1460 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1461 |
|
#else |
1462 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1463 |
|
#endif |
1464 |
|
Mode = stream_mode_closed; |
1465 |
bEndianNative = true; |
bEndianNative = true; |
1466 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1467 |
|
ListType = FileType; |
1468 |
} |
} |
1469 |
|
|
1470 |
/** @brief Load existing RIFF file. |
/** @brief Load existing RIFF file. |
1475 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occured while trying to load the |
1476 |
* given RIFF file |
* given RIFF file |
1477 |
*/ |
*/ |
1478 |
File::File(const String& path) : List(this), Filename(path) { |
File::File(const String& path) |
1479 |
#if DEBUG |
: List(this), Filename(path), bIsNewFile(false), Layout(layout_standard) |
1480 |
std::cout << "File::File("<<path<<")" << std::endl; |
{ |
1481 |
#endif // DEBUG |
#if DEBUG |
1482 |
|
std::cout << "File::File("<<path<<")" << std::endl; |
1483 |
|
#endif // DEBUG |
1484 |
bEndianNative = true; |
bEndianNative = true; |
1485 |
|
try { |
1486 |
|
__openExistingFile(path); |
1487 |
|
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1488 |
|
throw RIFF::Exception("Not a RIFF file"); |
1489 |
|
} |
1490 |
|
} |
1491 |
|
catch (...) { |
1492 |
|
Cleanup(); |
1493 |
|
throw; |
1494 |
|
} |
1495 |
|
} |
1496 |
|
|
1497 |
|
/** @brief Load existing RIFF-like file. |
1498 |
|
* |
1499 |
|
* Loads an existing file, which is not a "real" RIFF file, but similar to |
1500 |
|
* an ordinary RIFF file. |
1501 |
|
* |
1502 |
|
* A "real" RIFF file contains at top level a List chunk either with chunk |
1503 |
|
* ID "RIFF" or "RIFX". The simple constructor above expects this to be |
1504 |
|
* case, and if it finds the toplevel List chunk to have another chunk ID |
1505 |
|
* than one of those two expected ones, it would throw an Exception and |
1506 |
|
* would refuse to load the file accordingly. |
1507 |
|
* |
1508 |
|
* Since there are however a lot of file formats which use the same simple |
1509 |
|
* principles of the RIFF format, with another toplevel List chunk ID |
1510 |
|
* though, you can use this alternative constructor here to be able to load |
1511 |
|
* and handle those files in the same way as you would do with "real" RIFF |
1512 |
|
* files. |
1513 |
|
* |
1514 |
|
* @param path - path and file name of the RIFF-alike file to be opened |
1515 |
|
* @param FileType - expected toplevel List chunk ID (this is the very |
1516 |
|
* first chunk found in the file) |
1517 |
|
* @param Endian - whether the file uses little endian or big endian layout |
1518 |
|
* @param layout - general file structure type |
1519 |
|
* @throws RIFF::Exception if error occured while trying to load the |
1520 |
|
* given RIFF-alike file |
1521 |
|
*/ |
1522 |
|
File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout) |
1523 |
|
: List(this), Filename(path), bIsNewFile(false), Layout(layout) |
1524 |
|
{ |
1525 |
|
SetByteOrder(Endian); |
1526 |
|
try { |
1527 |
|
__openExistingFile(path, &FileType); |
1528 |
|
} |
1529 |
|
catch (...) { |
1530 |
|
Cleanup(); |
1531 |
|
throw; |
1532 |
|
} |
1533 |
|
} |
1534 |
|
|
1535 |
|
/** |
1536 |
|
* Opens an already existing RIFF file or RIFF-alike file. This method |
1537 |
|
* shall only be called once (in a File class constructor). |
1538 |
|
* |
1539 |
|
* @param path - path and file name of the RIFF file or RIFF-alike file to |
1540 |
|
* be opened |
1541 |
|
* @param FileType - (optional) expected chunk ID of first chunk in file |
1542 |
|
* @throws RIFF::Exception if error occured while trying to load the |
1543 |
|
* given RIFF file or RIFF-alike file |
1544 |
|
*/ |
1545 |
|
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
1546 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1547 |
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1548 |
#if POSIX |
#if POSIX |
1549 |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1550 |
if (hFileRead <= 0) { |
if (hFileRead <= 0) { |
1551 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1552 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1553 |
} |
} |
1554 |
|
#elif defined(WIN32) |
1555 |
|
hFileRead = hFileWrite = CreateFile( |
1556 |
|
path.c_str(), GENERIC_READ, |
1557 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1558 |
|
NULL, OPEN_EXISTING, |
1559 |
|
FILE_ATTRIBUTE_NORMAL | |
1560 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1561 |
|
); |
1562 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1563 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1564 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1565 |
|
} |
1566 |
#else |
#else |
1567 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1568 |
if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1569 |
#endif // POSIX |
#endif // POSIX |
1570 |
ulStartPos = RIFF_HEADER_SIZE; |
Mode = stream_mode_read; |
1571 |
ReadHeader(0); |
switch (Layout) { |
1572 |
if (ChunkID != CHUNK_ID_RIFF) { |
case layout_standard: // this is a normal RIFF file |
1573 |
throw RIFF::Exception("Not a RIFF file"); |
ulStartPos = RIFF_HEADER_SIZE; |
1574 |
|
ReadHeader(0); |
1575 |
|
if (FileType && ChunkID != *FileType) |
1576 |
|
throw RIFF::Exception("Invalid file container ID"); |
1577 |
|
break; |
1578 |
|
case layout_flat: // non-standard RIFF-alike file |
1579 |
|
ulStartPos = 0; |
1580 |
|
NewChunkSize = CurrentChunkSize = GetFileSize(); |
1581 |
|
if (FileType) { |
1582 |
|
uint32_t ckid; |
1583 |
|
if (Read(&ckid, 4, 1) != 4) { |
1584 |
|
throw RIFF::Exception("Invalid file header ID (premature end of header)"); |
1585 |
|
} else if (ckid != *FileType) { |
1586 |
|
String s = " (expected '" + convertToString(*FileType) + "' but got '" + convertToString(ckid) + "')"; |
1587 |
|
throw RIFF::Exception("Invalid file header ID" + s); |
1588 |
|
} |
1589 |
|
SetPos(0); // reset to first byte of file |
1590 |
|
} |
1591 |
|
LoadSubChunks(); |
1592 |
|
break; |
1593 |
} |
} |
1594 |
} |
} |
1595 |
|
|
1596 |
String File::GetFileName() { |
String File::GetFileName() { |
1597 |
return Filename; |
return Filename; |
1598 |
} |
} |
1599 |
|
|
1600 |
|
void File::SetFileName(const String& path) { |
1601 |
|
Filename = path; |
1602 |
|
} |
1603 |
|
|
1604 |
stream_mode_t File::GetMode() { |
stream_mode_t File::GetMode() { |
1605 |
return Mode; |
return Mode; |
1606 |
} |
} |
1607 |
|
|
1608 |
|
layout_t File::GetLayout() const { |
1609 |
|
return Layout; |
1610 |
|
} |
1611 |
|
|
1612 |
/** @brief Change file access mode. |
/** @brief Change file access mode. |
1613 |
* |
* |
1614 |
* Changes files access mode either to read-only mode or to read/write |
* Changes files access mode either to read-only mode or to read/write |
1630 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1631 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1632 |
} |
} |
1633 |
|
#elif defined(WIN32) |
1634 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1635 |
|
hFileRead = hFileWrite = CreateFile( |
1636 |
|
Filename.c_str(), GENERIC_READ, |
1637 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1638 |
|
NULL, OPEN_EXISTING, |
1639 |
|
FILE_ATTRIBUTE_NORMAL | |
1640 |
|
FILE_FLAG_RANDOM_ACCESS, |
1641 |
|
NULL |
1642 |
|
); |
1643 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1644 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1645 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1646 |
|
} |
1647 |
#else |
#else |
1648 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1649 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1650 |
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"); |
1651 |
#endif |
#endif |
1652 |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1653 |
return true; |
break; |
1654 |
case stream_mode_read_write: |
case stream_mode_read_write: |
1655 |
#if POSIX |
#if POSIX |
1656 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1659 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1660 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1661 |
} |
} |
1662 |
|
#elif defined(WIN32) |
1663 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1664 |
|
hFileRead = hFileWrite = CreateFile( |
1665 |
|
Filename.c_str(), |
1666 |
|
GENERIC_READ | GENERIC_WRITE, |
1667 |
|
FILE_SHARE_READ, |
1668 |
|
NULL, OPEN_ALWAYS, |
1669 |
|
FILE_ATTRIBUTE_NORMAL | |
1670 |
|
FILE_FLAG_RANDOM_ACCESS, |
1671 |
|
NULL |
1672 |
|
); |
1673 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1674 |
|
hFileRead = hFileWrite = CreateFile( |
1675 |
|
Filename.c_str(), GENERIC_READ, |
1676 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1677 |
|
NULL, OPEN_EXISTING, |
1678 |
|
FILE_ATTRIBUTE_NORMAL | |
1679 |
|
FILE_FLAG_RANDOM_ACCESS, |
1680 |
|
NULL |
1681 |
|
); |
1682 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
1683 |
|
} |
1684 |
#else |
#else |
1685 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1686 |
hFileRead = hFileWrite = fopen(path.c_str(), "r+b"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b"); |
1687 |
if (!hFileRead) { |
if (!hFileRead) { |
1688 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1689 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1690 |
} |
} |
1691 |
#endif |
#endif |
1692 |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1693 |
return true; |
break; |
1694 |
|
case stream_mode_closed: |
1695 |
|
#if POSIX |
1696 |
|
if (hFileRead) close(hFileRead); |
1697 |
|
if (hFileWrite) close(hFileWrite); |
1698 |
|
#elif defined(WIN32) |
1699 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1700 |
|
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1701 |
|
#else |
1702 |
|
if (hFileRead) fclose(hFileRead); |
1703 |
|
if (hFileWrite) fclose(hFileWrite); |
1704 |
|
#endif |
1705 |
|
hFileRead = hFileWrite = 0; |
1706 |
|
break; |
1707 |
default: |
default: |
1708 |
throw Exception("Unknown file access mode"); |
throw Exception("Unknown file access mode"); |
1709 |
} |
} |
1710 |
|
Mode = NewMode; |
1711 |
|
return true; |
1712 |
} |
} |
1713 |
return false; |
return false; |
1714 |
} |
} |
1715 |
|
|
1716 |
|
/** @brief Set the byte order to be used when saving. |
1717 |
|
* |
1718 |
|
* Set the byte order to be used in the file. A value of |
1719 |
|
* endian_little will create a RIFF file, endian_big a RIFX file |
1720 |
|
* and endian_native will create a RIFF file on little-endian |
1721 |
|
* machines and RIFX on big-endian machines. |
1722 |
|
* |
1723 |
|
* @param Endian - endianess to use when file is saved. |
1724 |
|
*/ |
1725 |
|
void File::SetByteOrder(endian_t Endian) { |
1726 |
|
#if WORDS_BIGENDIAN |
1727 |
|
bEndianNative = Endian != endian_little; |
1728 |
|
#else |
1729 |
|
bEndianNative = Endian != endian_big; |
1730 |
|
#endif |
1731 |
|
} |
1732 |
|
|
1733 |
/** @brief Save changes to same file. |
/** @brief Save changes to same file. |
1734 |
* |
* |
1735 |
* Make all changes of all chunks persistent by writing them to the |
* Make all changes of all chunks persistent by writing them to the |
1741 |
* chunk or any kind of IO error occured |
* chunk or any kind of IO error occured |
1742 |
*/ |
*/ |
1743 |
void File::Save() { |
void File::Save() { |
1744 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1745 |
|
if (Layout == layout_flat) |
1746 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1747 |
|
|
1748 |
|
// make sure the RIFF tree is built (from the original file) |
1749 |
|
LoadSubChunksRecursively(); |
1750 |
|
|
1751 |
// reopen file in write mode |
// reopen file in write mode |
1752 |
bool bModeWasChanged = SetMode(stream_mode_read_write); |
SetMode(stream_mode_read_write); |
1753 |
|
|
1754 |
// to be able to save the whole file without loading everything into |
// to be able to save the whole file without loading everything into |
1755 |
// 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 |
1760 |
|
|
1761 |
// 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) |
1762 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1763 |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
std::set<Chunk*>* resizedChunks = _GET_RESIZED_CHUNKS(); |
1764 |
if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)"); |
for (std::set<Chunk*>::const_iterator iter = resizedChunks->begin(), end = resizedChunks->end(); iter != end; ++iter) { |
1765 |
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
if ((*iter)->GetNewSize() == 0) { |
1766 |
if (ulDiff > 0) ulPositiveSizeDiff += ulDiff; |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
1767 |
|
} |
1768 |
|
unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; |
1769 |
|
unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2; |
1770 |
|
if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; |
1771 |
} |
} |
1772 |
|
|
1773 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |
1780 |
// ... and move current data by the same amount towards end of file. |
// ... and move current data by the same amount towards end of file. |
1781 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
1782 |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
1783 |
|
#if defined(WIN32) |
1784 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1785 |
|
#else |
1786 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1787 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
#endif |
1788 |
const unsigned long ulToMove = ulFileSize - ulPos; |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
1789 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1790 |
|
ulPos -= iBytesMoved; |
1791 |
#if POSIX |
#if POSIX |
1792 |
lseek(hFileRead, ulPos, SEEK_SET); |
lseek(hFileRead, ulPos, SEEK_SET); |
1793 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1794 |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1795 |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1796 |
|
#elif defined(WIN32) |
1797 |
|
SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN); |
1798 |
|
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1799 |
|
SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN); |
1800 |
|
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1801 |
#else |
#else |
1802 |
fseek(hFileRead, ulPos, SEEK_SET); |
fseek(hFileRead, ulPos, SEEK_SET); |
1803 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
1810 |
} |
} |
1811 |
|
|
1812 |
// rebuild / rewrite complete RIFF tree |
// rebuild / rewrite complete RIFF tree |
1813 |
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
1814 |
|
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1815 |
|
|
1816 |
// resize file to the final size |
// resize file to the final size |
1817 |
if (ulTotalSize < ulWorkingFileSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1818 |
|
|
1819 |
// forget all resized chunks |
// forget all resized chunks |
1820 |
ResizedChunks.clear(); |
resizedChunks->clear(); |
|
|
|
|
// reopen file in read mode |
|
|
if (bModeWasChanged) SetMode(stream_mode_read); |
|
1821 |
} |
} |
1822 |
|
|
1823 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
1825 |
* Make all changes of all chunks persistent by writing them to another |
* Make all changes of all chunks persistent by writing them to another |
1826 |
* file. <b>Caution:</b> this method is optimized for writing to |
* file. <b>Caution:</b> this method is optimized for writing to |
1827 |
* <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 |
1828 |
* file! Use File::Save() in that case instead! |
* file! Use File::Save() in that case instead! Ignoring this might |
1829 |
|
* result in a corrupted file, especially in case chunks were resized! |
1830 |
* |
* |
1831 |
* After calling this method, this File object will be associated with |
* After calling this method, this File object will be associated with |
1832 |
* the new file (given by \a path) afterwards. |
* the new file (given by \a path) afterwards. |
1834 |
* @param path - path and file name where everything should be written to |
* @param path - path and file name where everything should be written to |
1835 |
*/ |
*/ |
1836 |
void File::Save(const String& path) { |
void File::Save(const String& path) { |
1837 |
|
//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 |
1838 |
|
|
1839 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1840 |
|
if (Layout == layout_flat) |
1841 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1842 |
|
|
1843 |
|
// make sure the RIFF tree is built (from the original file) |
1844 |
|
LoadSubChunksRecursively(); |
1845 |
|
|
1846 |
|
if (!bIsNewFile) SetMode(stream_mode_read); |
1847 |
// 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 |
1848 |
#if POSIX |
#if POSIX |
1849 |
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); |
1850 |
if (hFileWrite < 0) { |
if (hFileWrite < 0) { |
1851 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
1852 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
throw Exception("Could not open file \"" + path + "\" for writing"); |
1853 |
} |
} |
1854 |
|
#elif defined(WIN32) |
1855 |
|
hFileWrite = CreateFile( |
1856 |
|
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1857 |
|
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
1858 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1859 |
|
); |
1860 |
|
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1861 |
|
hFileWrite = hFileRead; |
1862 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1863 |
|
} |
1864 |
#else |
#else |
1865 |
hFileWrite = fopen(path.c_str(), "w+b"); |
hFileWrite = fopen(path.c_str(), "w+b"); |
1866 |
if (!hFileWrite) { |
if (!hFileWrite) { |
1868 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
throw Exception("Could not open file \"" + path + "\" for writing"); |
1869 |
} |
} |
1870 |
#endif // POSIX |
#endif // POSIX |
1871 |
|
Mode = stream_mode_read_write; |
1872 |
|
|
1873 |
// write complete RIFF tree to the other (new) file |
// write complete RIFF tree to the other (new) file |
1874 |
WriteChunk(0, 0); |
unsigned long ulTotalSize = WriteChunk(0, 0); |
1875 |
|
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1876 |
|
|
1877 |
|
// resize file to the final size (if the file was originally larger) |
1878 |
|
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1879 |
|
|
1880 |
// forget all resized chunks |
// forget all resized chunks |
1881 |
ResizedChunks.clear(); |
_GET_RESIZED_CHUNKS()->clear(); |
1882 |
|
|
1883 |
|
#if POSIX |
1884 |
|
if (hFileWrite) close(hFileWrite); |
1885 |
|
#elif defined(WIN32) |
1886 |
|
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1887 |
|
#else |
1888 |
|
if (hFileWrite) fclose(hFileWrite); |
1889 |
|
#endif |
1890 |
|
hFileWrite = hFileRead; |
1891 |
|
|
1892 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1893 |
Filename = path; |
Filename = path; |
1894 |
stream_mode_t oldMode = Mode; |
bIsNewFile = false; |
1895 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1896 |
SetMode(oldMode); // ... so SetMode() has to reopen the file handles. |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
1897 |
} |
} |
1898 |
|
|
1899 |
void File::ResizeFile(unsigned long ulNewSize) { |
void File::ResizeFile(unsigned long ulNewSize) { |
1900 |
#if POSIX |
#if POSIX |
1901 |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
1902 |
throw Exception("Could not resize file \"" + Filename + "\""); |
throw Exception("Could not resize file \"" + Filename + "\""); |
1903 |
|
#elif defined(WIN32) |
1904 |
|
if ( |
1905 |
|
SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER || |
1906 |
|
!SetEndOfFile(hFileWrite) |
1907 |
|
) throw Exception("Could not resize file \"" + Filename + "\""); |
1908 |
#else |
#else |
1909 |
# error Sorry, this version of libgig only supports POSIX systems yet. |
# error Sorry, this version of libgig only supports POSIX and Windows systems yet. |
1910 |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing! |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
1911 |
#endif |
#endif |
1912 |
} |
} |
1913 |
|
|
1915 |
#if DEBUG |
#if DEBUG |
1916 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
1917 |
#endif // DEBUG |
#endif // DEBUG |
1918 |
|
Cleanup(); |
1919 |
|
} |
1920 |
|
|
1921 |
|
/** |
1922 |
|
* Returns @c true if this file has been created new from scratch and |
1923 |
|
* has not been stored to disk yet. |
1924 |
|
*/ |
1925 |
|
bool File::IsNew() const { |
1926 |
|
return bIsNewFile; |
1927 |
|
} |
1928 |
|
|
1929 |
|
void File::Cleanup() { |
1930 |
#if POSIX |
#if POSIX |
1931 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1932 |
|
#elif defined(WIN32) |
1933 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1934 |
#else |
#else |
1935 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1936 |
#endif // POSIX |
#endif // POSIX |
1937 |
|
DeleteChunkList(); |
1938 |
|
pFile = NULL; |
1939 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1940 |
|
delete _GET_RESIZED_CHUNKS(); |
1941 |
} |
} |
1942 |
|
|
1943 |
void File::LogAsResized(Chunk* pResizedChunk) { |
void File::LogAsResized(Chunk* pResizedChunk) { |
1944 |
ResizedChunks.push_back(pResizedChunk); |
_GET_RESIZED_CHUNKS()->insert(pResizedChunk); |
1945 |
|
} |
1946 |
|
|
1947 |
|
void File::UnlogResized(Chunk* pResizedChunk) { |
1948 |
|
_GET_RESIZED_CHUNKS()->erase(pResizedChunk); |
1949 |
} |
} |
1950 |
|
|
1951 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |
1952 |
#if POSIX |
return __GetFileSize(hFileRead); |
1953 |
|
} |
1954 |
|
|
1955 |
|
#if POSIX |
1956 |
|
unsigned long File::__GetFileSize(int hFile) { |
1957 |
struct stat filestat; |
struct stat filestat; |
1958 |
fstat(hFileRead, &filestat); |
fstat(hFile, &filestat); |
1959 |
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 |
|
1960 |
return size; |
return size; |
1961 |
} |
} |
1962 |
|
#elif defined(WIN32) |
1963 |
|
unsigned long File::__GetFileSize(HANDLE hFile) { |
1964 |
|
DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/); |
1965 |
|
if (dwSize == INVALID_FILE_SIZE) |
1966 |
|
throw Exception("Windows FS error: could not determine file size"); |
1967 |
|
return dwSize; |
1968 |
|
} |
1969 |
|
#else // standard C functions |
1970 |
|
unsigned long File::__GetFileSize(FILE* hFile) { |
1971 |
|
long curpos = ftell(hFile); |
1972 |
|
fseek(hFile, 0, SEEK_END); |
1973 |
|
long size = ftell(hFile); |
1974 |
|
fseek(hFile, curpos, SEEK_SET); |
1975 |
|
return size; |
1976 |
|
} |
1977 |
|
#endif |
1978 |
|
|
1979 |
|
|
1980 |
// *************** Exception *************** |
// *************** Exception *************** |