1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2005 by Christian Schoenebeck * |
* Copyright (C) 2003-2007 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
29 |
|
|
30 |
namespace RIFF { |
namespace RIFF { |
31 |
|
|
32 |
|
// *************** Internal functions ************** |
33 |
|
// * |
34 |
|
|
35 |
|
/// Returns a human readable path of the given chunk. |
36 |
|
String __resolveChunkPath(Chunk* pCk) { |
37 |
|
String sPath; |
38 |
|
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { |
39 |
|
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
40 |
|
List* pList = (List*) pChunk; |
41 |
|
sPath = "->'" + pList->GetListTypeString() + "'" + sPath; |
42 |
|
} else { |
43 |
|
sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath; |
44 |
|
} |
45 |
|
} |
46 |
|
return sPath; |
47 |
|
} |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
// *************** Chunk ************** |
// *************** Chunk ************** |
52 |
// * |
// * |
53 |
|
|
89 |
} |
} |
90 |
|
|
91 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
92 |
|
if (CurrentChunkSize != NewChunkSize) pFile->UnlogResized(this); |
93 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
94 |
} |
} |
95 |
|
|
101 |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
102 |
read(pFile->hFileRead, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
103 |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
104 |
|
#elif defined(WIN32) |
105 |
|
if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
106 |
|
DWORD dwBytesRead; |
107 |
|
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
108 |
|
ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL); |
109 |
#else |
#else |
110 |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
111 |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
113 |
#endif // POSIX |
#endif // POSIX |
114 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
115 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
116 |
bEndianNative = false; |
pFile->bEndianNative = false; |
117 |
} |
} |
118 |
#else // little endian |
#else // little endian |
119 |
if (ChunkID == CHUNK_ID_RIFX) { |
if (ChunkID == CHUNK_ID_RIFX) { |
154 |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
155 |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
156 |
} |
} |
157 |
|
#elif defined(WIN32) |
158 |
|
if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
159 |
|
DWORD dwBytesWritten; |
160 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
161 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL); |
162 |
|
} |
163 |
#else |
#else |
164 |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
165 |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
243 |
std::cout << "Chunk::GetState()" << std::endl; |
std::cout << "Chunk::GetState()" << std::endl; |
244 |
#endif // DEBUG |
#endif // DEBUG |
245 |
#if POSIX |
#if POSIX |
246 |
if (pFile->hFileRead == 0) return stream_closed; |
if (pFile->hFileRead == 0) return stream_closed; |
247 |
|
#elif defined (WIN32) |
248 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) |
249 |
|
return stream_closed; |
250 |
#else |
#else |
251 |
if (pFile->hFileRead == NULL) return stream_closed; |
if (pFile->hFileRead == NULL) return stream_closed; |
252 |
#endif // POSIX |
#endif // POSIX |
280 |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
281 |
if (readWords < 1) return 0; |
if (readWords < 1) return 0; |
282 |
readWords /= WordSize; |
readWords /= WordSize; |
283 |
|
#elif defined(WIN32) |
284 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
285 |
|
DWORD readWords; |
286 |
|
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); |
287 |
|
if (readWords < 1) return 0; |
288 |
|
readWords /= WordSize; |
289 |
#else // standard C functions |
#else // standard C functions |
290 |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
291 |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
355 |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
356 |
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"); |
357 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
358 |
|
#elif defined(WIN32) |
359 |
|
if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
360 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
361 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
362 |
|
} |
363 |
|
DWORD writtenWords; |
364 |
|
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
365 |
|
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
366 |
|
writtenWords /= WordSize; |
367 |
#else // standard C functions |
#else // standard C functions |
368 |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
369 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
throw Exception("Could not seek to position " + ToString(ulPos) + |
725 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
726 |
*/ |
*/ |
727 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
728 |
if (!pChunkData) { |
if (!pChunkData && pFile->Filename != "") { |
729 |
#if POSIX |
#if POSIX |
730 |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
731 |
|
#elif defined(WIN32) |
732 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL; |
733 |
#else |
#else |
734 |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
735 |
#endif // POSIX |
#endif // POSIX |
739 |
memset(pChunkData, 0, ulBufferSize); |
memset(pChunkData, 0, ulBufferSize); |
740 |
#if POSIX |
#if POSIX |
741 |
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
742 |
|
#elif defined(WIN32) |
743 |
|
DWORD readWords; |
744 |
|
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); |
745 |
#else |
#else |
746 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
747 |
#endif // POSIX |
#endif // POSIX |
794 |
* @see File::Save() |
* @see File::Save() |
795 |
*/ |
*/ |
796 |
void Chunk::Resize(int iNewSize) { |
void Chunk::Resize(int iNewSize) { |
797 |
if (iNewSize <= 0) throw Exception("Chunk size must be at least one byte"); |
if (iNewSize <= 0) |
798 |
|
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this)); |
799 |
if (NewChunkSize == iNewSize) return; |
if (NewChunkSize == iNewSize) return; |
800 |
NewChunkSize = iNewSize; |
NewChunkSize = iNewSize; |
801 |
pFile->LogAsResized(this); |
pFile->LogAsResized(this); |
830 |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
831 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
832 |
} |
} |
833 |
|
#elif defined(WIN32) |
834 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN); |
835 |
|
DWORD dwBytesWritten; |
836 |
|
WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL); |
837 |
|
if (dwBytesWritten != NewChunkSize) { |
838 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
839 |
|
} |
840 |
#else |
#else |
841 |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
842 |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
847 |
// 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 |
848 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
849 |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
850 |
|
#if defined(WIN32) |
851 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
852 |
|
#else |
853 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
854 |
|
#endif |
855 |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
856 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
857 |
#if POSIX |
#if POSIX |
859 |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
860 |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
861 |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
862 |
|
#elif defined(WIN32) |
863 |
|
SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
864 |
|
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
865 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
866 |
|
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
867 |
#else |
#else |
868 |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
869 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
889 |
#if POSIX |
#if POSIX |
890 |
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
891 |
write(pFile->hFileWrite, &cPadByte, 1); |
write(pFile->hFileWrite, &cPadByte, 1); |
892 |
|
#elif defined(WIN32) |
893 |
|
SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN); |
894 |
|
DWORD dwBytesWritten; |
895 |
|
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
896 |
#else |
#else |
897 |
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
898 |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
1155 |
return pNewChunk; |
return pNewChunk; |
1156 |
} |
} |
1157 |
|
|
1158 |
|
/** @brief Moves a sub chunk. |
1159 |
|
* |
1160 |
|
* Moves a sub chunk from one position in a list to another |
1161 |
|
* position in the same list. The pSrc chunk is placed before the |
1162 |
|
* pDst chunk. |
1163 |
|
* |
1164 |
|
* @param pSrc - sub chunk to be moved |
1165 |
|
* @param pDst - the position to move to. pSrc will be placed |
1166 |
|
* before pDst. If pDst is 0, pSrc will be placed |
1167 |
|
* last in list. |
1168 |
|
*/ |
1169 |
|
void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) { |
1170 |
|
if (!pSubChunks) LoadSubChunks(); |
1171 |
|
pSubChunks->remove(pSrc); |
1172 |
|
ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst); |
1173 |
|
pSubChunks->insert(iter, pSrc); |
1174 |
|
} |
1175 |
|
|
1176 |
/** @brief Creates a new list sub chunk. |
/** @brief Creates a new list sub chunk. |
1177 |
* |
* |
1178 |
* 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 |
1194 |
* |
* |
1195 |
* 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 |
1196 |
* 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 |
1197 |
* 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, |
1198 |
* change persistent at any time. |
* all its subchunks (if any) will be removed recursively as well. You |
1199 |
|
* should call File::Save() to make this change persistent at any time. |
1200 |
* |
* |
1201 |
* @param pSubChunk - sub chunk or sub list chunk to be removed |
* @param pSubChunk - sub chunk or sub list chunk to be removed |
1202 |
*/ |
*/ |
1227 |
#if POSIX |
#if POSIX |
1228 |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1229 |
read(pFile->hFileRead, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1230 |
|
#elif defined(WIN32) |
1231 |
|
SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1232 |
|
DWORD dwBytesRead; |
1233 |
|
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
1234 |
#else |
#else |
1235 |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1236 |
fread(&ListType, 4, 1, pFile->hFileRead); |
fread(&ListType, 4, 1, pFile->hFileRead); |
1251 |
#if POSIX |
#if POSIX |
1252 |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1253 |
write(pFile->hFileWrite, &ListType, 4); |
write(pFile->hFileWrite, &ListType, 4); |
1254 |
|
#elif defined(WIN32) |
1255 |
|
SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1256 |
|
DWORD dwBytesWritten; |
1257 |
|
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
1258 |
#else |
#else |
1259 |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1260 |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
1294 |
} |
} |
1295 |
} |
} |
1296 |
|
|
1297 |
|
void List::LoadSubChunksRecursively() { |
1298 |
|
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList()) |
1299 |
|
pList->LoadSubChunksRecursively(); |
1300 |
|
} |
1301 |
|
|
1302 |
/** @brief Write list chunk persistently e.g. to disk. |
/** @brief Write list chunk persistently e.g. to disk. |
1303 |
* |
* |
1304 |
* Stores the list chunk persistently to its actual "physical" file. All |
* Stores the list chunk persistently to its actual "physical" file. All |
1363 |
* 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 |
1364 |
* "from scratch". Note: there must be no empty chunks or empty list |
* "from scratch". Note: there must be no empty chunks or empty list |
1365 |
* chunks when trying to make the new RIFF file persistent with Save()! |
* chunks when trying to make the new RIFF file persistent with Save()! |
1366 |
|
* Note that this constructor will create a RIFX file on |
1367 |
|
* big-endian machines. |
1368 |
* |
* |
1369 |
* @param FileType - four-byte identifier of the RIFF file type |
* @param FileType - four-byte identifier of the RIFF file type |
1370 |
* @see AddSubChunk(), AddSubList() |
* @see AddSubChunk(), AddSubList() |
1371 |
*/ |
*/ |
1372 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) : List(this) { |
1373 |
|
#if defined(WIN32) |
1374 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1375 |
|
#else |
1376 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1377 |
|
#endif |
1378 |
Mode = stream_mode_closed; |
Mode = stream_mode_closed; |
1379 |
bEndianNative = true; |
bEndianNative = true; |
1380 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1381 |
ListType = FileType; |
ListType = FileType; |
1382 |
} |
} |
1383 |
|
|
1384 |
|
|
1385 |
|
/** @brief Create new RIFF file. |
1386 |
|
* |
1387 |
|
* Use this constructor if you want to create a new RIFF file |
1388 |
|
* completely "from scratch" and also want to specify |
1389 |
|
* endianess. Note: there must be no empty chunks or empty list |
1390 |
|
* chunks when trying to make the new RIFF file persistent with |
1391 |
|
* Save()! |
1392 |
|
* |
1393 |
|
* @param FileType - four-byte identifier of the RIFF file type |
1394 |
|
* @param Endian - endianess used in the new file. A value of |
1395 |
|
* endian_little will create a RIFF file, |
1396 |
|
* endian_big a RIFX file, endian_native will |
1397 |
|
* create a RIFF file on little-endian machines |
1398 |
|
* and RIFX on big-endian. |
1399 |
|
* @see AddSubChunk(), AddSubList() |
1400 |
|
*/ |
1401 |
|
File::File(uint32_t FileType, endian_t Endian) : List(this) { |
1402 |
|
#if defined(WIN32) |
1403 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1404 |
|
#else |
1405 |
|
hFileRead = hFileWrite = 0; |
1406 |
|
#endif |
1407 |
|
Mode = stream_mode_closed; |
1408 |
|
#if WORDS_BIGENDIAN |
1409 |
|
bEndianNative = Endian != endian_little; |
1410 |
|
#else |
1411 |
|
bEndianNative = Endian != endian_big; |
1412 |
|
#endif |
1413 |
|
ulStartPos = RIFF_HEADER_SIZE; |
1414 |
|
ListType = FileType; |
1415 |
|
} |
1416 |
|
|
1417 |
/** @brief Load existing RIFF file. |
/** @brief Load existing RIFF file. |
1418 |
* |
* |
1419 |
* Loads an existing RIFF file with all its chunks. |
* Loads an existing RIFF file with all its chunks. |
1433 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1434 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1435 |
} |
} |
1436 |
|
#elif defined(WIN32) |
1437 |
|
hFileRead = hFileWrite = CreateFile( |
1438 |
|
path.c_str(), GENERIC_READ, |
1439 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1440 |
|
NULL, OPEN_EXISTING, |
1441 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1442 |
|
); |
1443 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1444 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1445 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1446 |
|
} |
1447 |
#else |
#else |
1448 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1449 |
if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1450 |
#endif // POSIX |
#endif // POSIX |
1451 |
Mode = stream_mode_read; |
Mode = stream_mode_read; |
1452 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1485 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1486 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1487 |
} |
} |
1488 |
|
#elif defined(WIN32) |
1489 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1490 |
|
hFileRead = hFileWrite = CreateFile( |
1491 |
|
Filename.c_str(), GENERIC_READ, |
1492 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1493 |
|
NULL, OPEN_EXISTING, |
1494 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1495 |
|
); |
1496 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1497 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1498 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1499 |
|
} |
1500 |
#else |
#else |
1501 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1502 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1503 |
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"); |
1504 |
#endif |
#endif |
1505 |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1512 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1513 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1514 |
} |
} |
1515 |
|
#elif defined(WIN32) |
1516 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1517 |
|
hFileRead = hFileWrite = CreateFile( |
1518 |
|
Filename.c_str(), |
1519 |
|
GENERIC_READ | GENERIC_WRITE, |
1520 |
|
FILE_SHARE_READ, |
1521 |
|
NULL, OPEN_ALWAYS, |
1522 |
|
FILE_ATTRIBUTE_NORMAL, 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, NULL |
1530 |
|
); |
1531 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1532 |
|
} |
1533 |
#else |
#else |
1534 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1535 |
hFileRead = hFileWrite = fopen(path.c_str(), "r+b"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b"); |
1536 |
if (!hFileRead) { |
if (!hFileRead) { |
1537 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1538 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1539 |
} |
} |
1540 |
#endif |
#endif |
1544 |
#if POSIX |
#if POSIX |
1545 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1546 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
1547 |
|
#elif defined(WIN32) |
1548 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1549 |
|
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1550 |
#else |
#else |
1551 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1552 |
if (hFileWrite) fclose(hFileWrite); |
if (hFileWrite) fclose(hFileWrite); |
1573 |
* chunk or any kind of IO error occured |
* chunk or any kind of IO error occured |
1574 |
*/ |
*/ |
1575 |
void File::Save() { |
void File::Save() { |
1576 |
|
// make sure the RIFF tree is built (from the original file) |
1577 |
|
LoadSubChunksRecursively(); |
1578 |
|
|
1579 |
// reopen file in write mode |
// reopen file in write mode |
1580 |
SetMode(stream_mode_read_write); |
SetMode(stream_mode_read_write); |
1581 |
|
|
1589 |
// 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) |
1590 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1591 |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
1592 |
if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)"); |
if ((*iter)->GetNewSize() == 0) { |
1593 |
|
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
1594 |
|
} |
1595 |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
1596 |
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
1597 |
ulPositiveSizeDiff += ulDiff; |
ulPositiveSizeDiff += ulDiff; |
1608 |
// ... and move current data by the same amount towards end of file. |
// ... and move current data by the same amount towards end of file. |
1609 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
1610 |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
1611 |
int iBytesMoved = 1; |
#if defined(WIN32) |
1612 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1613 |
|
#else |
1614 |
|
int iBytesMoved = 1; |
1615 |
|
#endif |
1616 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
1617 |
const unsigned long ulToMove = ulFileSize - ulPos; |
const unsigned long ulToMove = ulFileSize - ulPos; |
1618 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
1621 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1622 |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1623 |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1624 |
|
#elif defined(WIN32) |
1625 |
|
SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN); |
1626 |
|
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1627 |
|
SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN); |
1628 |
|
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1629 |
#else |
#else |
1630 |
fseek(hFileRead, ulPos, SEEK_SET); |
fseek(hFileRead, ulPos, SEEK_SET); |
1631 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
1664 |
void File::Save(const String& path) { |
void File::Save(const String& path) { |
1665 |
//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 |
1666 |
|
|
1667 |
|
// make sure the RIFF tree is built (from the original file) |
1668 |
|
LoadSubChunksRecursively(); |
1669 |
|
|
1670 |
if (Filename.length() > 0) SetMode(stream_mode_read); |
if (Filename.length() > 0) SetMode(stream_mode_read); |
1671 |
// 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 |
1672 |
#if POSIX |
#if POSIX |
1675 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
1676 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
throw Exception("Could not open file \"" + path + "\" for writing"); |
1677 |
} |
} |
1678 |
|
#elif defined(WIN32) |
1679 |
|
hFileWrite = CreateFile( |
1680 |
|
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1681 |
|
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL |
1682 |
|
); |
1683 |
|
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1684 |
|
hFileWrite = hFileRead; |
1685 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1686 |
|
} |
1687 |
#else |
#else |
1688 |
hFileWrite = fopen(path.c_str(), "w+b"); |
hFileWrite = fopen(path.c_str(), "w+b"); |
1689 |
if (!hFileWrite) { |
if (!hFileWrite) { |
1706 |
if (Filename.length() > 0) { |
if (Filename.length() > 0) { |
1707 |
#if POSIX |
#if POSIX |
1708 |
close(hFileWrite); |
close(hFileWrite); |
1709 |
|
#elif defined(WIN32) |
1710 |
|
CloseHandle(hFileWrite); |
1711 |
#else |
#else |
1712 |
fclose(hFileWrite); |
fclose(hFileWrite); |
1713 |
#endif |
#endif |
1724 |
#if POSIX |
#if POSIX |
1725 |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
1726 |
throw Exception("Could not resize file \"" + Filename + "\""); |
throw Exception("Could not resize file \"" + Filename + "\""); |
1727 |
|
#elif defined(WIN32) |
1728 |
|
if ( |
1729 |
|
SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER || |
1730 |
|
!SetEndOfFile(hFileWrite) |
1731 |
|
) throw Exception("Could not resize file \"" + Filename + "\""); |
1732 |
#else |
#else |
1733 |
# error Sorry, this version of libgig only supports POSIX systems yet. |
# error Sorry, this version of libgig only supports POSIX and Windows systems yet. |
1734 |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
1735 |
#endif |
#endif |
1736 |
} |
} |
1741 |
#endif // DEBUG |
#endif // DEBUG |
1742 |
#if POSIX |
#if POSIX |
1743 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1744 |
|
#elif defined(WIN32) |
1745 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1746 |
#else |
#else |
1747 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1748 |
#endif // POSIX |
#endif // POSIX |
1752 |
ResizedChunks.push_back(pResizedChunk); |
ResizedChunks.push_back(pResizedChunk); |
1753 |
} |
} |
1754 |
|
|
1755 |
|
void File::UnlogResized(Chunk* pResizedChunk) { |
1756 |
|
ResizedChunks.remove(pResizedChunk); |
1757 |
|
} |
1758 |
|
|
1759 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |
1760 |
return __GetFileSize(hFileRead); |
return __GetFileSize(hFileRead); |
1761 |
} |
} |
1767 |
long size = filestat.st_size; |
long size = filestat.st_size; |
1768 |
return size; |
return size; |
1769 |
} |
} |
1770 |
|
#elif defined(WIN32) |
1771 |
|
unsigned long File::__GetFileSize(HANDLE hFile) { |
1772 |
|
DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/); |
1773 |
|
if (dwSize == INVALID_FILE_SIZE) |
1774 |
|
throw Exception("Windows FS error: could not determine file size"); |
1775 |
|
return dwSize; |
1776 |
|
} |
1777 |
#else // standard C functions |
#else // standard C functions |
1778 |
unsigned long File::__GetFileSize(FILE* hFile) { |
unsigned long File::__GetFileSize(FILE* hFile) { |
1779 |
long curpos = ftell(hFile); |
long curpos = ftell(hFile); |