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-2009 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 <string.h> |
#include <string.h> |
26 |
|
|
27 |
#include "RIFF.h" |
#include "RIFF.h" |
30 |
|
|
31 |
namespace RIFF { |
namespace RIFF { |
32 |
|
|
33 |
|
// *************** Internal functions ************** |
34 |
|
// * |
35 |
|
|
36 |
|
/// Returns a human readable path of the given chunk. |
37 |
|
static String __resolveChunkPath(Chunk* pCk) { |
38 |
|
String sPath; |
39 |
|
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { |
40 |
|
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
41 |
|
List* pList = (List*) pChunk; |
42 |
|
sPath = "->'" + pList->GetListTypeString() + "'" + sPath; |
43 |
|
} else { |
44 |
|
sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath; |
45 |
|
} |
46 |
|
} |
47 |
|
return sPath; |
48 |
|
} |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
// *************** Chunk ************** |
// *************** Chunk ************** |
53 |
// * |
// * |
54 |
|
|
59 |
ulPos = 0; |
ulPos = 0; |
60 |
pParent = NULL; |
pParent = NULL; |
61 |
pChunkData = NULL; |
pChunkData = NULL; |
62 |
|
CurrentChunkSize = 0; |
63 |
|
NewChunkSize = 0; |
64 |
ulChunkDataSize = 0; |
ulChunkDataSize = 0; |
65 |
ChunkID = CHUNK_ID_RIFF; |
ChunkID = CHUNK_ID_RIFF; |
66 |
this->pFile = pFile; |
this->pFile = pFile; |
75 |
pParent = Parent; |
pParent = Parent; |
76 |
ulPos = 0; |
ulPos = 0; |
77 |
pChunkData = NULL; |
pChunkData = NULL; |
78 |
|
CurrentChunkSize = 0; |
79 |
|
NewChunkSize = 0; |
80 |
ulChunkDataSize = 0; |
ulChunkDataSize = 0; |
81 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
82 |
} |
} |
87 |
this->pParent = pParent; |
this->pParent = pParent; |
88 |
ulPos = 0; |
ulPos = 0; |
89 |
pChunkData = NULL; |
pChunkData = NULL; |
|
ulChunkDataSize = 0; |
|
90 |
ChunkID = uiChunkID; |
ChunkID = uiChunkID; |
91 |
|
ulChunkDataSize = 0; |
92 |
CurrentChunkSize = 0; |
CurrentChunkSize = 0; |
93 |
NewChunkSize = uiBodySize; |
NewChunkSize = uiBodySize; |
94 |
} |
} |
95 |
|
|
96 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
97 |
|
if (pFile) pFile->UnlogResized(this); |
98 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
99 |
} |
} |
100 |
|
|
102 |
#if DEBUG |
#if DEBUG |
103 |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
104 |
#endif // DEBUG |
#endif // DEBUG |
105 |
|
ChunkID = 0; |
106 |
|
NewChunkSize = CurrentChunkSize = 0; |
107 |
#if POSIX |
#if POSIX |
108 |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
109 |
read(pFile->hFileRead, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
110 |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
111 |
|
#elif defined(WIN32) |
112 |
|
if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
113 |
|
DWORD dwBytesRead; |
114 |
|
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
115 |
|
ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL); |
116 |
#else |
#else |
117 |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
118 |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
120 |
#endif // POSIX |
#endif // POSIX |
121 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
122 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
123 |
bEndianNative = false; |
pFile->bEndianNative = false; |
124 |
} |
} |
125 |
#else // little endian |
#else // little endian |
126 |
if (ChunkID == CHUNK_ID_RIFX) { |
if (ChunkID == CHUNK_ID_RIFX) { |
134 |
} |
} |
135 |
#if DEBUG |
#if DEBUG |
136 |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
137 |
std::cout << "ckSize=" << ChunkSize << " "; |
std::cout << "ckSize=" << CurrentChunkSize << " "; |
138 |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; |
139 |
#endif // DEBUG |
#endif // DEBUG |
140 |
NewChunkSize = CurrentChunkSize; |
NewChunkSize = CurrentChunkSize; |
141 |
} |
} |
161 |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
162 |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
163 |
} |
} |
164 |
|
#elif defined(WIN32) |
165 |
|
if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
166 |
|
DWORD dwBytesWritten; |
167 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
168 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL); |
169 |
|
} |
170 |
#else |
#else |
171 |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
172 |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
231 |
#if DEBUG |
#if DEBUG |
232 |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
233 |
#endif // DEBUG |
#endif // DEBUG |
234 |
return CurrentChunkSize - ulPos; |
return (CurrentChunkSize > ulPos) ? CurrentChunkSize - ulPos : 0; |
235 |
} |
} |
236 |
|
|
237 |
/** |
/** |
242 |
* - RIFF::stream_closed : |
* - RIFF::stream_closed : |
243 |
* the data stream was closed somehow, no more reading possible |
* the data stream was closed somehow, no more reading possible |
244 |
* - RIFF::stream_end_reached : |
* - RIFF::stream_end_reached : |
245 |
* alreaady reached the end of the chunk data, no more reading |
* already reached the end of the chunk data, no more reading |
246 |
* possible without SetPos() |
* possible without SetPos() |
247 |
*/ |
*/ |
248 |
stream_state_t Chunk::GetState() { |
stream_state_t Chunk::GetState() { |
250 |
std::cout << "Chunk::GetState()" << std::endl; |
std::cout << "Chunk::GetState()" << std::endl; |
251 |
#endif // DEBUG |
#endif // DEBUG |
252 |
#if POSIX |
#if POSIX |
253 |
if (pFile->hFileRead == 0) return stream_closed; |
if (pFile->hFileRead == 0) return stream_closed; |
254 |
|
#elif defined (WIN32) |
255 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) |
256 |
|
return stream_closed; |
257 |
#else |
#else |
258 |
if (pFile->hFileRead == NULL) return stream_closed; |
if (pFile->hFileRead == NULL) return stream_closed; |
259 |
#endif // POSIX |
#endif // POSIX |
280 |
#if DEBUG |
#if DEBUG |
281 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
282 |
#endif // DEBUG |
#endif // DEBUG |
283 |
|
if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
284 |
if (ulPos >= CurrentChunkSize) return 0; |
if (ulPos >= CurrentChunkSize) return 0; |
285 |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
286 |
#if POSIX |
#if POSIX |
288 |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
289 |
if (readWords < 1) return 0; |
if (readWords < 1) return 0; |
290 |
readWords /= WordSize; |
readWords /= WordSize; |
291 |
|
#elif defined(WIN32) |
292 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
293 |
|
DWORD readWords; |
294 |
|
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); |
295 |
|
if (readWords < 1) return 0; |
296 |
|
readWords /= WordSize; |
297 |
#else // standard C functions |
#else // standard C functions |
298 |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
299 |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
363 |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
364 |
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"); |
365 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
366 |
|
#elif defined(WIN32) |
367 |
|
if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
368 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
369 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
370 |
|
} |
371 |
|
DWORD writtenWords; |
372 |
|
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
373 |
|
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
374 |
|
writtenWords /= WordSize; |
375 |
#else // standard C functions |
#else // standard C functions |
376 |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
377 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
throw Exception("Could not seek to position " + ToString(ulPos) + |
733 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
734 |
*/ |
*/ |
735 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
736 |
if (!pChunkData && pFile->Filename != "") { |
if (!pChunkData && pFile->Filename != "" && ulStartPos != 0) { |
737 |
#if POSIX |
#if POSIX |
738 |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
739 |
|
#elif defined(WIN32) |
740 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL; |
741 |
#else |
#else |
742 |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
743 |
#endif // POSIX |
#endif // POSIX |
747 |
memset(pChunkData, 0, ulBufferSize); |
memset(pChunkData, 0, ulBufferSize); |
748 |
#if POSIX |
#if POSIX |
749 |
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
750 |
|
#elif defined(WIN32) |
751 |
|
DWORD readWords; |
752 |
|
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); |
753 |
#else |
#else |
754 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
755 |
#endif // POSIX |
#endif // POSIX |
802 |
* @see File::Save() |
* @see File::Save() |
803 |
*/ |
*/ |
804 |
void Chunk::Resize(int iNewSize) { |
void Chunk::Resize(int iNewSize) { |
805 |
if (iNewSize <= 0) throw Exception("Chunk size must be at least one byte"); |
if (iNewSize <= 0) |
806 |
|
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this)); |
807 |
if (NewChunkSize == iNewSize) return; |
if (NewChunkSize == iNewSize) return; |
808 |
NewChunkSize = iNewSize; |
NewChunkSize = iNewSize; |
809 |
pFile->LogAsResized(this); |
pFile->LogAsResized(this); |
838 |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
839 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
840 |
} |
} |
841 |
|
#elif defined(WIN32) |
842 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN); |
843 |
|
DWORD dwBytesWritten; |
844 |
|
WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL); |
845 |
|
if (dwBytesWritten != NewChunkSize) { |
846 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
847 |
|
} |
848 |
#else |
#else |
849 |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
850 |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
855 |
// 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 |
856 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
857 |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
858 |
|
#if defined(WIN32) |
859 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
860 |
|
#else |
861 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
862 |
|
#endif |
863 |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
864 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
865 |
#if POSIX |
#if POSIX |
867 |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
868 |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
869 |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
870 |
|
#elif defined(WIN32) |
871 |
|
SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
872 |
|
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
873 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
874 |
|
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
875 |
#else |
#else |
876 |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
877 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
897 |
#if POSIX |
#if POSIX |
898 |
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
899 |
write(pFile->hFileWrite, &cPadByte, 1); |
write(pFile->hFileWrite, &cPadByte, 1); |
900 |
|
#elif defined(WIN32) |
901 |
|
SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN); |
902 |
|
DWORD dwBytesWritten; |
903 |
|
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
904 |
#else |
#else |
905 |
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
906 |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
950 |
#if DEBUG |
#if DEBUG |
951 |
std::cout << "List::~List()" << std::endl; |
std::cout << "List::~List()" << std::endl; |
952 |
#endif // DEBUG |
#endif // DEBUG |
953 |
|
DeleteChunkList(); |
954 |
|
} |
955 |
|
|
956 |
|
void List::DeleteChunkList() { |
957 |
if (pSubChunks) { |
if (pSubChunks) { |
958 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
959 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
962 |
iter++; |
iter++; |
963 |
} |
} |
964 |
delete pSubChunks; |
delete pSubChunks; |
965 |
|
pSubChunks = NULL; |
966 |
|
} |
967 |
|
if (pSubChunksMap) { |
968 |
|
delete pSubChunksMap; |
969 |
|
pSubChunksMap = NULL; |
970 |
} |
} |
|
if (pSubChunksMap) delete pSubChunksMap; |
|
971 |
} |
} |
972 |
|
|
973 |
/** |
/** |
1168 |
pSubChunks->push_back(pNewChunk); |
pSubChunks->push_back(pNewChunk); |
1169 |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1170 |
pNewChunk->Resize(uiBodySize); |
pNewChunk->Resize(uiBodySize); |
1171 |
|
NewChunkSize += CHUNK_HEADER_SIZE; |
1172 |
|
pFile->LogAsResized(this); |
1173 |
return pNewChunk; |
return pNewChunk; |
1174 |
} |
} |
1175 |
|
|
1176 |
|
/** @brief Moves a sub chunk. |
1177 |
|
* |
1178 |
|
* Moves a sub chunk from one position in a list to another |
1179 |
|
* position in the same list. The pSrc chunk is placed before the |
1180 |
|
* pDst chunk. |
1181 |
|
* |
1182 |
|
* @param pSrc - sub chunk to be moved |
1183 |
|
* @param pDst - the position to move to. pSrc will be placed |
1184 |
|
* before pDst. If pDst is 0, pSrc will be placed |
1185 |
|
* last in list. |
1186 |
|
*/ |
1187 |
|
void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) { |
1188 |
|
if (!pSubChunks) LoadSubChunks(); |
1189 |
|
pSubChunks->remove(pSrc); |
1190 |
|
ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst); |
1191 |
|
pSubChunks->insert(iter, pSrc); |
1192 |
|
} |
1193 |
|
|
1194 |
/** @brief Creates a new list sub chunk. |
/** @brief Creates a new list sub chunk. |
1195 |
* |
* |
1196 |
* 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 |
1205 |
List* pNewListChunk = new List(pFile, this, uiListType); |
List* pNewListChunk = new List(pFile, this, uiListType); |
1206 |
pSubChunks->push_back(pNewListChunk); |
pSubChunks->push_back(pNewListChunk); |
1207 |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1208 |
|
NewChunkSize += LIST_HEADER_SIZE; |
1209 |
|
pFile->LogAsResized(this); |
1210 |
return pNewListChunk; |
return pNewListChunk; |
1211 |
} |
} |
1212 |
|
|
1214 |
* |
* |
1215 |
* 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 |
1216 |
* 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 |
1217 |
* 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, |
1218 |
* change persistent at any time. |
* all its subchunks (if any) will be removed recursively as well. You |
1219 |
|
* should call File::Save() to make this change persistent at any time. |
1220 |
* |
* |
1221 |
* @param pSubChunk - sub chunk or sub list chunk to be removed |
* @param pSubChunk - sub chunk or sub list chunk to be removed |
1222 |
*/ |
*/ |
1243 |
std::cout << "List::Readheader(ulong) "; |
std::cout << "List::Readheader(ulong) "; |
1244 |
#endif // DEBUG |
#endif // DEBUG |
1245 |
Chunk::ReadHeader(fPos); |
Chunk::ReadHeader(fPos); |
1246 |
|
if (CurrentChunkSize < 8) return; |
1247 |
NewChunkSize = CurrentChunkSize -= 4; |
NewChunkSize = CurrentChunkSize -= 4; |
1248 |
#if POSIX |
#if POSIX |
1249 |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1250 |
read(pFile->hFileRead, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1251 |
|
#elif defined(WIN32) |
1252 |
|
SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1253 |
|
DWORD dwBytesRead; |
1254 |
|
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
1255 |
#else |
#else |
1256 |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1257 |
fread(&ListType, 4, 1, pFile->hFileRead); |
fread(&ListType, 4, 1, pFile->hFileRead); |
1272 |
#if POSIX |
#if POSIX |
1273 |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1274 |
write(pFile->hFileWrite, &ListType, 4); |
write(pFile->hFileWrite, &ListType, 4); |
1275 |
|
#elif defined(WIN32) |
1276 |
|
SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1277 |
|
DWORD dwBytesWritten; |
1278 |
|
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
1279 |
#else |
#else |
1280 |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1281 |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
1289 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1290 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1291 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1292 |
|
#if defined(WIN32) |
1293 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) return; |
1294 |
|
#else |
1295 |
if (!pFile->hFileRead) return; |
if (!pFile->hFileRead) return; |
1296 |
|
#endif |
1297 |
unsigned long uiOriginalPos = GetPos(); |
unsigned long uiOriginalPos = GetPos(); |
1298 |
SetPos(0); // jump to beginning of list chunk body |
SetPos(0); // jump to beginning of list chunk body |
1299 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
1319 |
} |
} |
1320 |
} |
} |
1321 |
|
|
1322 |
|
void List::LoadSubChunksRecursively() { |
1323 |
|
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList()) |
1324 |
|
pList->LoadSubChunksRecursively(); |
1325 |
|
} |
1326 |
|
|
1327 |
/** @brief Write list chunk persistently e.g. to disk. |
/** @brief Write list chunk persistently e.g. to disk. |
1328 |
* |
* |
1329 |
* Stores the list chunk persistently to its actual "physical" file. All |
* Stores the list chunk persistently to its actual "physical" file. All |
1389 |
* "from scratch". Note: there must be no empty chunks or empty list |
* "from scratch". Note: there must be no empty chunks or empty list |
1390 |
* chunks when trying to make the new RIFF file persistent with Save()! |
* chunks when trying to make the new RIFF file persistent with Save()! |
1391 |
* |
* |
1392 |
|
* Note: by default, the RIFF file will be saved in native endian |
1393 |
|
* format; that is, as a RIFF file on little-endian machines and |
1394 |
|
* as a RIFX file on big-endian. To change this behaviour, call |
1395 |
|
* SetByteOrder() before calling Save(). |
1396 |
|
* |
1397 |
* @param FileType - four-byte identifier of the RIFF file type |
* @param FileType - four-byte identifier of the RIFF file type |
1398 |
* @see AddSubChunk(), AddSubList() |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1399 |
*/ |
*/ |
1400 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) : List(this) { |
1401 |
|
#if defined(WIN32) |
1402 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1403 |
|
#else |
1404 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1405 |
|
#endif |
1406 |
Mode = stream_mode_closed; |
Mode = stream_mode_closed; |
1407 |
bEndianNative = true; |
bEndianNative = true; |
1408 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1428 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1429 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1430 |
} |
} |
1431 |
|
#elif defined(WIN32) |
1432 |
|
hFileRead = hFileWrite = CreateFile( |
1433 |
|
path.c_str(), GENERIC_READ, |
1434 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1435 |
|
NULL, OPEN_EXISTING, |
1436 |
|
FILE_ATTRIBUTE_NORMAL | |
1437 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1438 |
|
); |
1439 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1440 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1441 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1442 |
|
} |
1443 |
#else |
#else |
1444 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1445 |
if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1446 |
#endif // POSIX |
#endif // POSIX |
1447 |
Mode = stream_mode_read; |
Mode = stream_mode_read; |
1448 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1449 |
ReadHeader(0); |
ReadHeader(0); |
1450 |
if (ChunkID != CHUNK_ID_RIFF) { |
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1451 |
throw RIFF::Exception("Not a RIFF file"); |
throw RIFF::Exception("Not a RIFF file"); |
1452 |
} |
} |
1453 |
} |
} |
1481 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1482 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1483 |
} |
} |
1484 |
|
#elif defined(WIN32) |
1485 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1486 |
|
hFileRead = hFileWrite = CreateFile( |
1487 |
|
Filename.c_str(), GENERIC_READ, |
1488 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1489 |
|
NULL, OPEN_EXISTING, |
1490 |
|
FILE_ATTRIBUTE_NORMAL | |
1491 |
|
FILE_FLAG_RANDOM_ACCESS, |
1492 |
|
NULL |
1493 |
|
); |
1494 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1495 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1496 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1497 |
|
} |
1498 |
#else |
#else |
1499 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1500 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1501 |
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"); |
1502 |
#endif |
#endif |
1503 |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1510 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1511 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1512 |
} |
} |
1513 |
|
#elif defined(WIN32) |
1514 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1515 |
|
hFileRead = hFileWrite = CreateFile( |
1516 |
|
Filename.c_str(), |
1517 |
|
GENERIC_READ | GENERIC_WRITE, |
1518 |
|
FILE_SHARE_READ, |
1519 |
|
NULL, OPEN_ALWAYS, |
1520 |
|
FILE_ATTRIBUTE_NORMAL | |
1521 |
|
FILE_FLAG_RANDOM_ACCESS, |
1522 |
|
NULL |
1523 |
|
); |
1524 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1525 |
|
hFileRead = hFileWrite = CreateFile( |
1526 |
|
Filename.c_str(), GENERIC_READ, |
1527 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1528 |
|
NULL, OPEN_EXISTING, |
1529 |
|
FILE_ATTRIBUTE_NORMAL | |
1530 |
|
FILE_FLAG_RANDOM_ACCESS, |
1531 |
|
NULL |
1532 |
|
); |
1533 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
1534 |
|
} |
1535 |
#else |
#else |
1536 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1537 |
hFileRead = hFileWrite = fopen(path.c_str(), "r+b"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b"); |
1538 |
if (!hFileRead) { |
if (!hFileRead) { |
1539 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1540 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1541 |
} |
} |
1542 |
#endif |
#endif |
1546 |
#if POSIX |
#if POSIX |
1547 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1548 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
1549 |
|
#elif defined(WIN32) |
1550 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1551 |
|
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1552 |
#else |
#else |
1553 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1554 |
if (hFileWrite) fclose(hFileWrite); |
if (hFileWrite) fclose(hFileWrite); |
1564 |
return false; |
return false; |
1565 |
} |
} |
1566 |
|
|
1567 |
|
/** @brief Set the byte order to be used when saving. |
1568 |
|
* |
1569 |
|
* Set the byte order to be used in the file. A value of |
1570 |
|
* endian_little will create a RIFF file, endian_big a RIFX file |
1571 |
|
* and endian_native will create a RIFF file on little-endian |
1572 |
|
* machines and RIFX on big-endian machines. |
1573 |
|
* |
1574 |
|
* @param Endian - endianess to use when file is saved. |
1575 |
|
*/ |
1576 |
|
void File::SetByteOrder(endian_t Endian) { |
1577 |
|
#if WORDS_BIGENDIAN |
1578 |
|
bEndianNative = Endian != endian_little; |
1579 |
|
#else |
1580 |
|
bEndianNative = Endian != endian_big; |
1581 |
|
#endif |
1582 |
|
} |
1583 |
|
|
1584 |
/** @brief Save changes to same file. |
/** @brief Save changes to same file. |
1585 |
* |
* |
1586 |
* Make all changes of all chunks persistent by writing them to the |
* Make all changes of all chunks persistent by writing them to the |
1592 |
* chunk or any kind of IO error occured |
* chunk or any kind of IO error occured |
1593 |
*/ |
*/ |
1594 |
void File::Save() { |
void File::Save() { |
1595 |
|
// make sure the RIFF tree is built (from the original file) |
1596 |
|
LoadSubChunksRecursively(); |
1597 |
|
|
1598 |
// reopen file in write mode |
// reopen file in write mode |
1599 |
SetMode(stream_mode_read_write); |
SetMode(stream_mode_read_write); |
1600 |
|
|
1607 |
|
|
1608 |
// 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) |
1609 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1610 |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
for (std::set<Chunk*>::const_iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
1611 |
if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)"); |
if ((*iter)->GetNewSize() == 0) { |
1612 |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
|
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
|
|
ulPositiveSizeDiff += ulDiff; |
|
1613 |
} |
} |
1614 |
|
unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; |
1615 |
|
unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2; |
1616 |
|
if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; |
1617 |
} |
} |
1618 |
|
|
1619 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |
1626 |
// ... and move current data by the same amount towards end of file. |
// ... and move current data by the same amount towards end of file. |
1627 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
1628 |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
1629 |
|
#if defined(WIN32) |
1630 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1631 |
|
#else |
1632 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1633 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
#endif |
1634 |
const unsigned long ulToMove = ulFileSize - ulPos; |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
1635 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1636 |
|
ulPos -= iBytesMoved; |
1637 |
#if POSIX |
#if POSIX |
1638 |
lseek(hFileRead, ulPos, SEEK_SET); |
lseek(hFileRead, ulPos, SEEK_SET); |
1639 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1640 |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1641 |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1642 |
|
#elif defined(WIN32) |
1643 |
|
SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN); |
1644 |
|
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1645 |
|
SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN); |
1646 |
|
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1647 |
#else |
#else |
1648 |
fseek(hFileRead, ulPos, SEEK_SET); |
fseek(hFileRead, ulPos, SEEK_SET); |
1649 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
1682 |
void File::Save(const String& path) { |
void File::Save(const String& path) { |
1683 |
//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 |
1684 |
|
|
1685 |
|
// make sure the RIFF tree is built (from the original file) |
1686 |
|
LoadSubChunksRecursively(); |
1687 |
|
|
1688 |
if (Filename.length() > 0) SetMode(stream_mode_read); |
if (Filename.length() > 0) SetMode(stream_mode_read); |
1689 |
// 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 |
1690 |
#if POSIX |
#if POSIX |
1693 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
1694 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
throw Exception("Could not open file \"" + path + "\" for writing"); |
1695 |
} |
} |
1696 |
|
#elif defined(WIN32) |
1697 |
|
hFileWrite = CreateFile( |
1698 |
|
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1699 |
|
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
1700 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1701 |
|
); |
1702 |
|
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1703 |
|
hFileWrite = hFileRead; |
1704 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1705 |
|
} |
1706 |
#else |
#else |
1707 |
hFileWrite = fopen(path.c_str(), "w+b"); |
hFileWrite = fopen(path.c_str(), "w+b"); |
1708 |
if (!hFileWrite) { |
if (!hFileWrite) { |
1722 |
// forget all resized chunks |
// forget all resized chunks |
1723 |
ResizedChunks.clear(); |
ResizedChunks.clear(); |
1724 |
|
|
1725 |
if (Filename.length() > 0) { |
#if POSIX |
1726 |
#if POSIX |
if (hFileWrite) close(hFileWrite); |
1727 |
close(hFileWrite); |
#elif defined(WIN32) |
1728 |
#else |
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1729 |
fclose(hFileWrite); |
#else |
1730 |
#endif |
if (hFileWrite) fclose(hFileWrite); |
1731 |
hFileWrite = hFileRead; |
#endif |
1732 |
} |
hFileWrite = hFileRead; |
1733 |
|
|
1734 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1735 |
Filename = path; |
Filename = path; |
1741 |
#if POSIX |
#if POSIX |
1742 |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
1743 |
throw Exception("Could not resize file \"" + Filename + "\""); |
throw Exception("Could not resize file \"" + Filename + "\""); |
1744 |
|
#elif defined(WIN32) |
1745 |
|
if ( |
1746 |
|
SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER || |
1747 |
|
!SetEndOfFile(hFileWrite) |
1748 |
|
) throw Exception("Could not resize file \"" + Filename + "\""); |
1749 |
#else |
#else |
1750 |
# error Sorry, this version of libgig only supports POSIX systems yet. |
# error Sorry, this version of libgig only supports POSIX and Windows systems yet. |
1751 |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
1752 |
#endif |
#endif |
1753 |
} |
} |
1758 |
#endif // DEBUG |
#endif // DEBUG |
1759 |
#if POSIX |
#if POSIX |
1760 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1761 |
|
#elif defined(WIN32) |
1762 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1763 |
#else |
#else |
1764 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1765 |
#endif // POSIX |
#endif // POSIX |
1766 |
|
DeleteChunkList(); |
1767 |
|
pFile = NULL; |
1768 |
} |
} |
1769 |
|
|
1770 |
void File::LogAsResized(Chunk* pResizedChunk) { |
void File::LogAsResized(Chunk* pResizedChunk) { |
1771 |
ResizedChunks.push_back(pResizedChunk); |
ResizedChunks.insert(pResizedChunk); |
1772 |
|
} |
1773 |
|
|
1774 |
|
void File::UnlogResized(Chunk* pResizedChunk) { |
1775 |
|
ResizedChunks.erase(pResizedChunk); |
1776 |
} |
} |
1777 |
|
|
1778 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |
1786 |
long size = filestat.st_size; |
long size = filestat.st_size; |
1787 |
return size; |
return size; |
1788 |
} |
} |
1789 |
|
#elif defined(WIN32) |
1790 |
|
unsigned long File::__GetFileSize(HANDLE hFile) { |
1791 |
|
DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/); |
1792 |
|
if (dwSize == INVALID_FILE_SIZE) |
1793 |
|
throw Exception("Windows FS error: could not determine file size"); |
1794 |
|
return dwSize; |
1795 |
|
} |
1796 |
#else // standard C functions |
#else // standard C functions |
1797 |
unsigned long File::__GetFileSize(FILE* hFile) { |
unsigned long File::__GetFileSize(FILE* hFile) { |
1798 |
long curpos = ftell(hFile); |
long curpos = ftell(hFile); |