2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2006 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> |
27 |
|
|
28 |
#include "RIFF.h" |
#include "RIFF.h" |
31 |
|
|
32 |
namespace RIFF { |
namespace RIFF { |
33 |
|
|
34 |
|
// *************** Internal functions ************** |
35 |
|
// * |
36 |
|
|
37 |
|
/// Returns a human readable path of the given chunk. |
38 |
|
static String __resolveChunkPath(Chunk* pCk) { |
39 |
|
String sPath; |
40 |
|
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { |
41 |
|
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
42 |
|
List* pList = (List*) pChunk; |
43 |
|
sPath = "->'" + pList->GetListTypeString() + "'" + sPath; |
44 |
|
} else { |
45 |
|
sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath; |
46 |
|
} |
47 |
|
} |
48 |
|
return sPath; |
49 |
|
} |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
// *************** Chunk ************** |
// *************** Chunk ************** |
54 |
// * |
// * |
55 |
|
|
60 |
ulPos = 0; |
ulPos = 0; |
61 |
pParent = NULL; |
pParent = NULL; |
62 |
pChunkData = NULL; |
pChunkData = NULL; |
63 |
|
CurrentChunkSize = 0; |
64 |
|
NewChunkSize = 0; |
65 |
ulChunkDataSize = 0; |
ulChunkDataSize = 0; |
66 |
ChunkID = CHUNK_ID_RIFF; |
ChunkID = CHUNK_ID_RIFF; |
67 |
this->pFile = pFile; |
this->pFile = pFile; |
76 |
pParent = Parent; |
pParent = Parent; |
77 |
ulPos = 0; |
ulPos = 0; |
78 |
pChunkData = NULL; |
pChunkData = NULL; |
79 |
|
CurrentChunkSize = 0; |
80 |
|
NewChunkSize = 0; |
81 |
ulChunkDataSize = 0; |
ulChunkDataSize = 0; |
82 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
83 |
} |
} |
88 |
this->pParent = pParent; |
this->pParent = pParent; |
89 |
ulPos = 0; |
ulPos = 0; |
90 |
pChunkData = NULL; |
pChunkData = NULL; |
|
ulChunkDataSize = 0; |
|
91 |
ChunkID = uiChunkID; |
ChunkID = uiChunkID; |
92 |
|
ulChunkDataSize = 0; |
93 |
CurrentChunkSize = 0; |
CurrentChunkSize = 0; |
94 |
NewChunkSize = uiBodySize; |
NewChunkSize = uiBodySize; |
95 |
} |
} |
96 |
|
|
97 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
98 |
|
if (pFile) pFile->UnlogResized(this); |
99 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
100 |
} |
} |
101 |
|
|
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); |
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); |
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 |
751 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
752 |
*/ |
*/ |
753 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
754 |
if (!pChunkData && pFile->Filename != "") { |
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 |
|
#elif defined(WIN32) |
758 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL; |
759 |
#else |
#else |
760 |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
761 |
#endif // POSIX |
#endif // POSIX |
765 |
memset(pChunkData, 0, ulBufferSize); |
memset(pChunkData, 0, ulBufferSize); |
766 |
#if POSIX |
#if POSIX |
767 |
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
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 |
#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 |
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; |
if (NewChunkSize == iNewSize) return; |
826 |
NewChunkSize = iNewSize; |
NewChunkSize = iNewSize; |
827 |
pFile->LogAsResized(this); |
pFile->LogAsResized(this); |
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); |
915 |
#if POSIX |
#if POSIX |
916 |
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
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); |
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
924 |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
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 |
/** |
/** |
1186 |
pSubChunks->push_back(pNewChunk); |
pSubChunks->push_back(pNewChunk); |
1187 |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1188 |
pNewChunk->Resize(uiBodySize); |
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. |
1195 |
|
* |
1196 |
|
* Moves a sub chunk from one position in a 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 Creates a new list sub chunk. |
/** @brief Creates a new list sub chunk. |
1213 |
* |
* |
1214 |
* 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 |
1223 |
List* pNewListChunk = new List(pFile, this, uiListType); |
List* pNewListChunk = new List(pFile, this, uiListType); |
1224 |
pSubChunks->push_back(pNewListChunk); |
pSubChunks->push_back(pNewListChunk); |
1225 |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1226 |
|
NewChunkSize += LIST_HEADER_SIZE; |
1227 |
|
pFile->LogAsResized(this); |
1228 |
return pNewListChunk; |
return pNewListChunk; |
1229 |
} |
} |
1230 |
|
|
1232 |
* |
* |
1233 |
* 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 |
1234 |
* 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 |
1235 |
* 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, |
1236 |
* change persistent at any time. |
* all its subchunks (if any) will be removed recursively as well. You |
1237 |
|
* should call File::Save() to make this change persistent at any time. |
1238 |
* |
* |
1239 |
* @param pSubChunk - sub chunk or sub list chunk to be removed |
* @param pSubChunk - sub chunk or sub list chunk to be removed |
1240 |
*/ |
*/ |
1261 |
std::cout << "List::Readheader(ulong) "; |
std::cout << "List::Readheader(ulong) "; |
1262 |
#endif // DEBUG |
#endif // DEBUG |
1263 |
Chunk::ReadHeader(fPos); |
Chunk::ReadHeader(fPos); |
1264 |
|
if (CurrentChunkSize < 4) return; |
1265 |
NewChunkSize = CurrentChunkSize -= 4; |
NewChunkSize = CurrentChunkSize -= 4; |
1266 |
#if POSIX |
#if POSIX |
1267 |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1268 |
read(pFile->hFileRead, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1269 |
|
#elif defined(WIN32) |
1270 |
|
SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1271 |
|
DWORD dwBytesRead; |
1272 |
|
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
1273 |
#else |
#else |
1274 |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1275 |
fread(&ListType, 4, 1, pFile->hFileRead); |
fread(&ListType, 4, 1, pFile->hFileRead); |
1290 |
#if POSIX |
#if POSIX |
1291 |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1292 |
write(pFile->hFileWrite, &ListType, 4); |
write(pFile->hFileWrite, &ListType, 4); |
1293 |
|
#elif defined(WIN32) |
1294 |
|
SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1295 |
|
DWORD dwBytesWritten; |
1296 |
|
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
1297 |
#else |
#else |
1298 |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1299 |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
1307 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1308 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1309 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1310 |
|
#if defined(WIN32) |
1311 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) return; |
1312 |
|
#else |
1313 |
if (!pFile->hFileRead) return; |
if (!pFile->hFileRead) return; |
1314 |
|
#endif |
1315 |
unsigned long uiOriginalPos = GetPos(); |
unsigned long uiOriginalPos = GetPos(); |
1316 |
SetPos(0); // jump to beginning of list chunk body |
SetPos(0); // jump to beginning of list chunk body |
1317 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
1401 |
// *************** File *************** |
// *************** File *************** |
1402 |
// * |
// * |
1403 |
|
|
1404 |
|
//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 |
1405 |
|
#define _GET_RESIZED_CHUNKS() \ |
1406 |
|
(reinterpret_cast<std::set<Chunk*>*>(ResizedChunks.front())) |
1407 |
|
|
1408 |
/** @brief Create new RIFF file. |
/** @brief Create new RIFF file. |
1409 |
* |
* |
1410 |
* 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 |
1411 |
* "from scratch". Note: there must be no empty chunks or empty list |
* "from scratch". Note: there must be no empty chunks or empty list |
1412 |
* chunks when trying to make the new RIFF file persistent with Save()! |
* chunks when trying to make the new RIFF file persistent with Save()! |
1413 |
* |
* |
1414 |
|
* Note: by default, the RIFF file will be saved in native endian |
1415 |
|
* format; that is, as a RIFF file on little-endian machines and |
1416 |
|
* as a RIFX file on big-endian. To change this behaviour, call |
1417 |
|
* SetByteOrder() before calling Save(). |
1418 |
|
* |
1419 |
* @param FileType - four-byte identifier of the RIFF file type |
* @param FileType - four-byte identifier of the RIFF file type |
1420 |
* @see AddSubChunk(), AddSubList() |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1421 |
*/ |
*/ |
1422 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) |
1423 |
|
: List(this), bIsNewFile(true), Layout(layout_standard) |
1424 |
|
{ |
1425 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1426 |
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1427 |
|
#if defined(WIN32) |
1428 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1429 |
|
#else |
1430 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1431 |
|
#endif |
1432 |
Mode = stream_mode_closed; |
Mode = stream_mode_closed; |
1433 |
bEndianNative = true; |
bEndianNative = true; |
1434 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1443 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occured while trying to load the |
1444 |
* given RIFF file |
* given RIFF file |
1445 |
*/ |
*/ |
1446 |
File::File(const String& path) : List(this), Filename(path) { |
File::File(const String& path) |
1447 |
#if DEBUG |
: List(this), Filename(path), bIsNewFile(false), Layout(layout_standard) |
1448 |
std::cout << "File::File("<<path<<")" << std::endl; |
{ |
1449 |
#endif // DEBUG |
#if DEBUG |
1450 |
|
std::cout << "File::File("<<path<<")" << std::endl; |
1451 |
|
#endif // DEBUG |
1452 |
bEndianNative = true; |
bEndianNative = true; |
1453 |
|
try { |
1454 |
|
__openExistingFile(path); |
1455 |
|
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1456 |
|
throw RIFF::Exception("Not a RIFF file"); |
1457 |
|
} |
1458 |
|
} |
1459 |
|
catch (...) { |
1460 |
|
Cleanup(); |
1461 |
|
throw; |
1462 |
|
} |
1463 |
|
} |
1464 |
|
|
1465 |
|
/** @brief Load existing RIFF-like file. |
1466 |
|
* |
1467 |
|
* Loads an existing file, which is not a "real" RIFF file, but similar to |
1468 |
|
* an ordinary RIFF file. |
1469 |
|
* |
1470 |
|
* A "real" RIFF file contains at top level a List chunk either with chunk |
1471 |
|
* ID "RIFF" or "RIFX". The simple constructor above expects this to be |
1472 |
|
* case, and if it finds the toplevel List chunk to have another chunk ID |
1473 |
|
* than one of those two expected ones, it would throw an Exception and |
1474 |
|
* would refuse to load the file accordingly. |
1475 |
|
* |
1476 |
|
* Since there are however a lot of file formats which use the same simple |
1477 |
|
* principles of the RIFF format, with another toplevel List chunk ID |
1478 |
|
* though, you can use this alternative constructor here to be able to load |
1479 |
|
* and handle those files in the same way as you would do with "real" RIFF |
1480 |
|
* files. |
1481 |
|
* |
1482 |
|
* @param path - path and file name of the RIFF-alike file to be opened |
1483 |
|
* @param FileType - expected toplevel List chunk ID (this is the very |
1484 |
|
* first chunk found in the file) |
1485 |
|
* @param Endian - whether the file uses little endian or big endian layout |
1486 |
|
* @param layout - general file structure type |
1487 |
|
* @throws RIFF::Exception if error occured while trying to load the |
1488 |
|
* given RIFF-alike file |
1489 |
|
*/ |
1490 |
|
File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout) |
1491 |
|
: List(this), Filename(path), bIsNewFile(false), Layout(layout) |
1492 |
|
{ |
1493 |
|
SetByteOrder(Endian); |
1494 |
|
try { |
1495 |
|
__openExistingFile(path, &FileType); |
1496 |
|
} |
1497 |
|
catch (...) { |
1498 |
|
Cleanup(); |
1499 |
|
throw; |
1500 |
|
} |
1501 |
|
} |
1502 |
|
|
1503 |
|
/** |
1504 |
|
* Opens an already existing RIFF file or RIFF-alike file. This method |
1505 |
|
* shall only be called once (in a File class constructor). |
1506 |
|
* |
1507 |
|
* @param path - path and file name of the RIFF file or RIFF-alike file to |
1508 |
|
* be opened |
1509 |
|
* @param FileType - (optional) expected chunk ID of first chunk in file |
1510 |
|
* @throws RIFF::Exception if error occured while trying to load the |
1511 |
|
* given RIFF file or RIFF-alike file |
1512 |
|
*/ |
1513 |
|
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
1514 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1515 |
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1516 |
#if POSIX |
#if POSIX |
1517 |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1518 |
if (hFileRead <= 0) { |
if (hFileRead <= 0) { |
1519 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1520 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1521 |
} |
} |
1522 |
|
#elif defined(WIN32) |
1523 |
|
hFileRead = hFileWrite = CreateFile( |
1524 |
|
path.c_str(), GENERIC_READ, |
1525 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1526 |
|
NULL, OPEN_EXISTING, |
1527 |
|
FILE_ATTRIBUTE_NORMAL | |
1528 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1529 |
|
); |
1530 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1531 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1532 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1533 |
|
} |
1534 |
#else |
#else |
1535 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1536 |
if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1537 |
#endif // POSIX |
#endif // POSIX |
1538 |
Mode = stream_mode_read; |
Mode = stream_mode_read; |
1539 |
ulStartPos = RIFF_HEADER_SIZE; |
switch (Layout) { |
1540 |
ReadHeader(0); |
case layout_standard: // this is a normal RIFF file |
1541 |
if (ChunkID != CHUNK_ID_RIFF) { |
ulStartPos = RIFF_HEADER_SIZE; |
1542 |
throw RIFF::Exception("Not a RIFF file"); |
ReadHeader(0); |
1543 |
|
if (FileType && ChunkID != *FileType) |
1544 |
|
throw RIFF::Exception("Invalid file container ID"); |
1545 |
|
break; |
1546 |
|
case layout_flat: // non-standard RIFF-alike file |
1547 |
|
ulStartPos = 0; |
1548 |
|
NewChunkSize = CurrentChunkSize = GetFileSize(); |
1549 |
|
if (FileType) { |
1550 |
|
uint32_t ckid; |
1551 |
|
if (Read(&ckid, 4, 1) != 4) { |
1552 |
|
throw RIFF::Exception("Invalid file header ID (premature end of header)"); |
1553 |
|
} else if (ckid != *FileType) { |
1554 |
|
String s = " (expected '" + convertToString(*FileType) + "' but got '" + convertToString(ckid) + "')"; |
1555 |
|
throw RIFF::Exception("Invalid file header ID" + s); |
1556 |
|
} |
1557 |
|
SetPos(0); // reset to first byte of file |
1558 |
|
} |
1559 |
|
LoadSubChunks(); |
1560 |
|
break; |
1561 |
} |
} |
1562 |
} |
} |
1563 |
|
|
1564 |
String File::GetFileName() { |
String File::GetFileName() { |
1565 |
return Filename; |
return Filename; |
1566 |
} |
} |
1567 |
|
|
1568 |
|
void File::SetFileName(const String& path) { |
1569 |
|
Filename = path; |
1570 |
|
} |
1571 |
|
|
1572 |
stream_mode_t File::GetMode() { |
stream_mode_t File::GetMode() { |
1573 |
return Mode; |
return Mode; |
1574 |
} |
} |
1575 |
|
|
1576 |
|
layout_t File::GetLayout() const { |
1577 |
|
return Layout; |
1578 |
|
} |
1579 |
|
|
1580 |
/** @brief Change file access mode. |
/** @brief Change file access mode. |
1581 |
* |
* |
1582 |
* 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 |
1598 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1599 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1600 |
} |
} |
1601 |
|
#elif defined(WIN32) |
1602 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1603 |
|
hFileRead = hFileWrite = CreateFile( |
1604 |
|
Filename.c_str(), GENERIC_READ, |
1605 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1606 |
|
NULL, OPEN_EXISTING, |
1607 |
|
FILE_ATTRIBUTE_NORMAL | |
1608 |
|
FILE_FLAG_RANDOM_ACCESS, |
1609 |
|
NULL |
1610 |
|
); |
1611 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1612 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1613 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1614 |
|
} |
1615 |
#else |
#else |
1616 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1617 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1618 |
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"); |
1619 |
#endif |
#endif |
1620 |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1627 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1628 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1629 |
} |
} |
1630 |
|
#elif defined(WIN32) |
1631 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1632 |
|
hFileRead = hFileWrite = CreateFile( |
1633 |
|
Filename.c_str(), |
1634 |
|
GENERIC_READ | GENERIC_WRITE, |
1635 |
|
FILE_SHARE_READ, |
1636 |
|
NULL, OPEN_ALWAYS, |
1637 |
|
FILE_ATTRIBUTE_NORMAL | |
1638 |
|
FILE_FLAG_RANDOM_ACCESS, |
1639 |
|
NULL |
1640 |
|
); |
1641 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1642 |
|
hFileRead = hFileWrite = CreateFile( |
1643 |
|
Filename.c_str(), GENERIC_READ, |
1644 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1645 |
|
NULL, OPEN_EXISTING, |
1646 |
|
FILE_ATTRIBUTE_NORMAL | |
1647 |
|
FILE_FLAG_RANDOM_ACCESS, |
1648 |
|
NULL |
1649 |
|
); |
1650 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
1651 |
|
} |
1652 |
#else |
#else |
1653 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1654 |
hFileRead = hFileWrite = fopen(path.c_str(), "r+b"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b"); |
1655 |
if (!hFileRead) { |
if (!hFileRead) { |
1656 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1657 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1658 |
} |
} |
1659 |
#endif |
#endif |
1663 |
#if POSIX |
#if POSIX |
1664 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1665 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
1666 |
|
#elif defined(WIN32) |
1667 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1668 |
|
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1669 |
#else |
#else |
1670 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1671 |
if (hFileWrite) fclose(hFileWrite); |
if (hFileWrite) fclose(hFileWrite); |
1681 |
return false; |
return false; |
1682 |
} |
} |
1683 |
|
|
1684 |
|
/** @brief Set the byte order to be used when saving. |
1685 |
|
* |
1686 |
|
* Set the byte order to be used in the file. A value of |
1687 |
|
* endian_little will create a RIFF file, endian_big a RIFX file |
1688 |
|
* and endian_native will create a RIFF file on little-endian |
1689 |
|
* machines and RIFX on big-endian machines. |
1690 |
|
* |
1691 |
|
* @param Endian - endianess to use when file is saved. |
1692 |
|
*/ |
1693 |
|
void File::SetByteOrder(endian_t Endian) { |
1694 |
|
#if WORDS_BIGENDIAN |
1695 |
|
bEndianNative = Endian != endian_little; |
1696 |
|
#else |
1697 |
|
bEndianNative = Endian != endian_big; |
1698 |
|
#endif |
1699 |
|
} |
1700 |
|
|
1701 |
/** @brief Save changes to same file. |
/** @brief Save changes to same file. |
1702 |
* |
* |
1703 |
* Make all changes of all chunks persistent by writing them to the |
* Make all changes of all chunks persistent by writing them to the |
1709 |
* chunk or any kind of IO error occured |
* chunk or any kind of IO error occured |
1710 |
*/ |
*/ |
1711 |
void File::Save() { |
void File::Save() { |
1712 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1713 |
|
if (Layout == layout_flat) |
1714 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1715 |
|
|
1716 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1717 |
LoadSubChunksRecursively(); |
LoadSubChunksRecursively(); |
1718 |
|
|
1728 |
|
|
1729 |
// 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) |
1730 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1731 |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
std::set<Chunk*>* resizedChunks = _GET_RESIZED_CHUNKS(); |
1732 |
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) { |
1733 |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
if ((*iter)->GetNewSize() == 0) { |
1734 |
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
|
ulPositiveSizeDiff += ulDiff; |
|
1735 |
} |
} |
1736 |
|
unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; |
1737 |
|
unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2; |
1738 |
|
if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; |
1739 |
} |
} |
1740 |
|
|
1741 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |
1748 |
// ... and move current data by the same amount towards end of file. |
// ... and move current data by the same amount towards end of file. |
1749 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
1750 |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
1751 |
|
#if defined(WIN32) |
1752 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1753 |
|
#else |
1754 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1755 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
#endif |
1756 |
const unsigned long ulToMove = ulFileSize - ulPos; |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
1757 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1758 |
|
ulPos -= iBytesMoved; |
1759 |
#if POSIX |
#if POSIX |
1760 |
lseek(hFileRead, ulPos, SEEK_SET); |
lseek(hFileRead, ulPos, SEEK_SET); |
1761 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1762 |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1763 |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1764 |
|
#elif defined(WIN32) |
1765 |
|
SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN); |
1766 |
|
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1767 |
|
SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN); |
1768 |
|
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1769 |
#else |
#else |
1770 |
fseek(hFileRead, ulPos, SEEK_SET); |
fseek(hFileRead, ulPos, SEEK_SET); |
1771 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
1785 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1786 |
|
|
1787 |
// forget all resized chunks |
// forget all resized chunks |
1788 |
ResizedChunks.clear(); |
resizedChunks->clear(); |
1789 |
} |
} |
1790 |
|
|
1791 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
1804 |
void File::Save(const String& path) { |
void File::Save(const String& path) { |
1805 |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
1806 |
|
|
1807 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1808 |
|
if (Layout == layout_flat) |
1809 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1810 |
|
|
1811 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1812 |
LoadSubChunksRecursively(); |
LoadSubChunksRecursively(); |
1813 |
|
|
1814 |
if (Filename.length() > 0) SetMode(stream_mode_read); |
if (!bIsNewFile) SetMode(stream_mode_read); |
1815 |
// 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 |
1816 |
#if POSIX |
#if POSIX |
1817 |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
1819 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
1820 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
throw Exception("Could not open file \"" + path + "\" for writing"); |
1821 |
} |
} |
1822 |
|
#elif defined(WIN32) |
1823 |
|
hFileWrite = CreateFile( |
1824 |
|
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1825 |
|
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
1826 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1827 |
|
); |
1828 |
|
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1829 |
|
hFileWrite = hFileRead; |
1830 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1831 |
|
} |
1832 |
#else |
#else |
1833 |
hFileWrite = fopen(path.c_str(), "w+b"); |
hFileWrite = fopen(path.c_str(), "w+b"); |
1834 |
if (!hFileWrite) { |
if (!hFileWrite) { |
1846 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1847 |
|
|
1848 |
// forget all resized chunks |
// forget all resized chunks |
1849 |
ResizedChunks.clear(); |
_GET_RESIZED_CHUNKS()->clear(); |
1850 |
|
|
1851 |
if (Filename.length() > 0) { |
#if POSIX |
1852 |
#if POSIX |
if (hFileWrite) close(hFileWrite); |
1853 |
close(hFileWrite); |
#elif defined(WIN32) |
1854 |
#else |
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1855 |
fclose(hFileWrite); |
#else |
1856 |
#endif |
if (hFileWrite) fclose(hFileWrite); |
1857 |
hFileWrite = hFileRead; |
#endif |
1858 |
} |
hFileWrite = hFileRead; |
1859 |
|
|
1860 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1861 |
Filename = path; |
Filename = path; |
1862 |
|
bIsNewFile = false; |
1863 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1864 |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
1865 |
} |
} |
1868 |
#if POSIX |
#if POSIX |
1869 |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
1870 |
throw Exception("Could not resize file \"" + Filename + "\""); |
throw Exception("Could not resize file \"" + Filename + "\""); |
1871 |
|
#elif defined(WIN32) |
1872 |
|
if ( |
1873 |
|
SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER || |
1874 |
|
!SetEndOfFile(hFileWrite) |
1875 |
|
) throw Exception("Could not resize file \"" + Filename + "\""); |
1876 |
#else |
#else |
1877 |
# error Sorry, this version of libgig only supports POSIX systems yet. |
# error Sorry, this version of libgig only supports POSIX and Windows systems yet. |
1878 |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
1879 |
#endif |
#endif |
1880 |
} |
} |
1883 |
#if DEBUG |
#if DEBUG |
1884 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
1885 |
#endif // DEBUG |
#endif // DEBUG |
1886 |
|
Cleanup(); |
1887 |
|
} |
1888 |
|
|
1889 |
|
/** |
1890 |
|
* Returns @c true if this file has been created new from scratch and |
1891 |
|
* has not been stored to disk yet. |
1892 |
|
*/ |
1893 |
|
bool File::IsNew() const { |
1894 |
|
return bIsNewFile; |
1895 |
|
} |
1896 |
|
|
1897 |
|
void File::Cleanup() { |
1898 |
#if POSIX |
#if POSIX |
1899 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1900 |
|
#elif defined(WIN32) |
1901 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1902 |
#else |
#else |
1903 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1904 |
#endif // POSIX |
#endif // POSIX |
1905 |
|
DeleteChunkList(); |
1906 |
|
pFile = NULL; |
1907 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1908 |
|
delete _GET_RESIZED_CHUNKS(); |
1909 |
} |
} |
1910 |
|
|
1911 |
void File::LogAsResized(Chunk* pResizedChunk) { |
void File::LogAsResized(Chunk* pResizedChunk) { |
1912 |
ResizedChunks.push_back(pResizedChunk); |
_GET_RESIZED_CHUNKS()->insert(pResizedChunk); |
1913 |
|
} |
1914 |
|
|
1915 |
|
void File::UnlogResized(Chunk* pResizedChunk) { |
1916 |
|
_GET_RESIZED_CHUNKS()->erase(pResizedChunk); |
1917 |
} |
} |
1918 |
|
|
1919 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |
1927 |
long size = filestat.st_size; |
long size = filestat.st_size; |
1928 |
return size; |
return size; |
1929 |
} |
} |
1930 |
|
#elif defined(WIN32) |
1931 |
|
unsigned long File::__GetFileSize(HANDLE hFile) { |
1932 |
|
DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/); |
1933 |
|
if (dwSize == INVALID_FILE_SIZE) |
1934 |
|
throw Exception("Windows FS error: could not determine file size"); |
1935 |
|
return dwSize; |
1936 |
|
} |
1937 |
#else // standard C functions |
#else // standard C functions |
1938 |
unsigned long File::__GetFileSize(FILE* hFile) { |
unsigned long File::__GetFileSize(FILE* hFile) { |
1939 |
long curpos = ftell(hFile); |
long curpos = ftell(hFile); |