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 * |
81 |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
82 |
read(pFile->hFileRead, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
83 |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
84 |
|
#elif defined(WIN32) |
85 |
|
if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
86 |
|
DWORD dwBytesRead; |
87 |
|
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
88 |
|
ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL); |
89 |
#else |
#else |
90 |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
91 |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
134 |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
135 |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
136 |
} |
} |
137 |
|
#elif defined(WIN32) |
138 |
|
if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
139 |
|
DWORD dwBytesWritten; |
140 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
141 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL); |
142 |
|
} |
143 |
#else |
#else |
144 |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
145 |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
223 |
std::cout << "Chunk::GetState()" << std::endl; |
std::cout << "Chunk::GetState()" << std::endl; |
224 |
#endif // DEBUG |
#endif // DEBUG |
225 |
#if POSIX |
#if POSIX |
226 |
if (pFile->hFileRead == 0) return stream_closed; |
if (pFile->hFileRead == 0) return stream_closed; |
227 |
|
#elif defined (WIN32) |
228 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) |
229 |
|
return stream_closed; |
230 |
#else |
#else |
231 |
if (pFile->hFileRead == NULL) return stream_closed; |
if (pFile->hFileRead == NULL) return stream_closed; |
232 |
#endif // POSIX |
#endif // POSIX |
260 |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
261 |
if (readWords < 1) return 0; |
if (readWords < 1) return 0; |
262 |
readWords /= WordSize; |
readWords /= WordSize; |
263 |
|
#elif defined(WIN32) |
264 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
265 |
|
DWORD readWords; |
266 |
|
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); |
267 |
|
if (readWords < 1) return 0; |
268 |
|
readWords /= WordSize; |
269 |
#else // standard C functions |
#else // standard C functions |
270 |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
271 |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
335 |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
336 |
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
337 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
338 |
|
#elif defined(WIN32) |
339 |
|
if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
340 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
341 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
342 |
|
} |
343 |
|
DWORD writtenWords; |
344 |
|
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
345 |
|
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
346 |
|
writtenWords /= WordSize; |
347 |
#else // standard C functions |
#else // standard C functions |
348 |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
349 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
throw Exception("Could not seek to position " + ToString(ulPos) + |
708 |
if (!pChunkData && pFile->Filename != "") { |
if (!pChunkData && pFile->Filename != "") { |
709 |
#if POSIX |
#if POSIX |
710 |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
711 |
|
#elif defined(WIN32) |
712 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL; |
713 |
#else |
#else |
714 |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
715 |
#endif // POSIX |
#endif // POSIX |
719 |
memset(pChunkData, 0, ulBufferSize); |
memset(pChunkData, 0, ulBufferSize); |
720 |
#if POSIX |
#if POSIX |
721 |
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
722 |
|
#elif defined(WIN32) |
723 |
|
DWORD readWords; |
724 |
|
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); |
725 |
#else |
#else |
726 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
727 |
#endif // POSIX |
#endif // POSIX |
809 |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
810 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
811 |
} |
} |
812 |
|
#elif defined(WIN32) |
813 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN); |
814 |
|
DWORD dwBytesWritten; |
815 |
|
WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL); |
816 |
|
if (dwBytesWritten != NewChunkSize) { |
817 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
818 |
|
} |
819 |
#else |
#else |
820 |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
821 |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
826 |
// move chunk data from the end of the file to the appropriate position |
// move chunk data from the end of the file to the appropriate position |
827 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
828 |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
829 |
|
#if defined(WIN32) |
830 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
831 |
|
#else |
832 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
833 |
|
#endif |
834 |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
835 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
836 |
#if POSIX |
#if POSIX |
838 |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
839 |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
840 |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
841 |
|
#elif defined(WIN32) |
842 |
|
SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
843 |
|
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
844 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
845 |
|
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
846 |
#else |
#else |
847 |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
848 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
868 |
#if POSIX |
#if POSIX |
869 |
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
870 |
write(pFile->hFileWrite, &cPadByte, 1); |
write(pFile->hFileWrite, &cPadByte, 1); |
871 |
|
#elif defined(WIN32) |
872 |
|
SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN); |
873 |
|
DWORD dwBytesWritten; |
874 |
|
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
875 |
#else |
#else |
876 |
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
877 |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
1187 |
#if POSIX |
#if POSIX |
1188 |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1189 |
read(pFile->hFileRead, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1190 |
|
#elif defined(WIN32) |
1191 |
|
SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1192 |
|
DWORD dwBytesRead; |
1193 |
|
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
1194 |
#else |
#else |
1195 |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1196 |
fread(&ListType, 4, 1, pFile->hFileRead); |
fread(&ListType, 4, 1, pFile->hFileRead); |
1211 |
#if POSIX |
#if POSIX |
1212 |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1213 |
write(pFile->hFileWrite, &ListType, 4); |
write(pFile->hFileWrite, &ListType, 4); |
1214 |
|
#elif defined(WIN32) |
1215 |
|
SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1216 |
|
DWORD dwBytesWritten; |
1217 |
|
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
1218 |
#else |
#else |
1219 |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1220 |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
1328 |
* @see AddSubChunk(), AddSubList() |
* @see AddSubChunk(), AddSubList() |
1329 |
*/ |
*/ |
1330 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) : List(this) { |
1331 |
|
#if defined(WIN32) |
1332 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1333 |
|
#else |
1334 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1335 |
|
#endif |
1336 |
Mode = stream_mode_closed; |
Mode = stream_mode_closed; |
1337 |
bEndianNative = true; |
bEndianNative = true; |
1338 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1358 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1359 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1360 |
} |
} |
1361 |
|
#elif defined(WIN32) |
1362 |
|
hFileRead = hFileWrite = CreateFile( |
1363 |
|
path.c_str(), GENERIC_READ, |
1364 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1365 |
|
NULL, OPEN_EXISTING, |
1366 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1367 |
|
); |
1368 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1369 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1370 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1371 |
|
} |
1372 |
#else |
#else |
1373 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1374 |
if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1375 |
#endif // POSIX |
#endif // POSIX |
1376 |
Mode = stream_mode_read; |
Mode = stream_mode_read; |
1377 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1410 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1411 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1412 |
} |
} |
1413 |
|
#elif defined(WIN32) |
1414 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1415 |
|
hFileRead = hFileWrite = CreateFile( |
1416 |
|
Filename.c_str(), GENERIC_READ, |
1417 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1418 |
|
NULL, OPEN_EXISTING, |
1419 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1420 |
|
); |
1421 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1422 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1423 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1424 |
|
} |
1425 |
#else |
#else |
1426 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1427 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1428 |
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"); |
1429 |
#endif |
#endif |
1430 |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1437 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1438 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1439 |
} |
} |
1440 |
|
#elif defined(WIN32) |
1441 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1442 |
|
hFileRead = hFileWrite = CreateFile( |
1443 |
|
Filename.c_str(), |
1444 |
|
GENERIC_READ | GENERIC_WRITE, |
1445 |
|
FILE_SHARE_READ, |
1446 |
|
NULL, OPEN_ALWAYS, |
1447 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1448 |
|
); |
1449 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1450 |
|
hFileRead = hFileWrite = CreateFile( |
1451 |
|
Filename.c_str(), GENERIC_READ, |
1452 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1453 |
|
NULL, OPEN_EXISTING, |
1454 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1455 |
|
); |
1456 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1457 |
|
} |
1458 |
#else |
#else |
1459 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1460 |
hFileRead = hFileWrite = fopen(path.c_str(), "r+b"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b"); |
1461 |
if (!hFileRead) { |
if (!hFileRead) { |
1462 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1463 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1464 |
} |
} |
1465 |
#endif |
#endif |
1469 |
#if POSIX |
#if POSIX |
1470 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1471 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
1472 |
|
#elif defined(WIN32) |
1473 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1474 |
|
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1475 |
#else |
#else |
1476 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1477 |
if (hFileWrite) fclose(hFileWrite); |
if (hFileWrite) fclose(hFileWrite); |
1531 |
// ... and move current data by the same amount towards end of file. |
// ... and move current data by the same amount towards end of file. |
1532 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
1533 |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
1534 |
int iBytesMoved = 1; |
#if defined(WIN32) |
1535 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1536 |
|
#else |
1537 |
|
int iBytesMoved = 1; |
1538 |
|
#endif |
1539 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
1540 |
const unsigned long ulToMove = ulFileSize - ulPos; |
const unsigned long ulToMove = ulFileSize - ulPos; |
1541 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
1544 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1545 |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1546 |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1547 |
|
#elif defined(WIN32) |
1548 |
|
SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN); |
1549 |
|
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1550 |
|
SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN); |
1551 |
|
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1552 |
#else |
#else |
1553 |
fseek(hFileRead, ulPos, SEEK_SET); |
fseek(hFileRead, ulPos, SEEK_SET); |
1554 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
1598 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
1599 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
throw Exception("Could not open file \"" + path + "\" for writing"); |
1600 |
} |
} |
1601 |
|
#elif defined(WIN32) |
1602 |
|
hFileWrite = CreateFile( |
1603 |
|
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1604 |
|
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL |
1605 |
|
); |
1606 |
|
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1607 |
|
hFileWrite = hFileRead; |
1608 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1609 |
|
} |
1610 |
#else |
#else |
1611 |
hFileWrite = fopen(path.c_str(), "w+b"); |
hFileWrite = fopen(path.c_str(), "w+b"); |
1612 |
if (!hFileWrite) { |
if (!hFileWrite) { |
1629 |
if (Filename.length() > 0) { |
if (Filename.length() > 0) { |
1630 |
#if POSIX |
#if POSIX |
1631 |
close(hFileWrite); |
close(hFileWrite); |
1632 |
|
#elif defined(WIN32) |
1633 |
|
CloseHandle(hFileWrite); |
1634 |
#else |
#else |
1635 |
fclose(hFileWrite); |
fclose(hFileWrite); |
1636 |
#endif |
#endif |
1647 |
#if POSIX |
#if POSIX |
1648 |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
1649 |
throw Exception("Could not resize file \"" + Filename + "\""); |
throw Exception("Could not resize file \"" + Filename + "\""); |
1650 |
|
#elif defined(WIN32) |
1651 |
|
if ( |
1652 |
|
SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER || |
1653 |
|
!SetEndOfFile(hFileWrite) |
1654 |
|
) throw Exception("Could not resize file \"" + Filename + "\""); |
1655 |
#else |
#else |
1656 |
# error Sorry, this version of libgig only supports POSIX systems yet. |
# error Sorry, this version of libgig only supports POSIX and Windows systems yet. |
1657 |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
1658 |
#endif |
#endif |
1659 |
} |
} |
1664 |
#endif // DEBUG |
#endif // DEBUG |
1665 |
#if POSIX |
#if POSIX |
1666 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1667 |
|
#elif defined(WIN32) |
1668 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1669 |
#else |
#else |
1670 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1671 |
#endif // POSIX |
#endif // POSIX |
1686 |
long size = filestat.st_size; |
long size = filestat.st_size; |
1687 |
return size; |
return size; |
1688 |
} |
} |
1689 |
|
#elif defined(WIN32) |
1690 |
|
unsigned long File::__GetFileSize(HANDLE hFile) { |
1691 |
|
DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/); |
1692 |
|
if (dwSize == INVALID_FILE_SIZE) |
1693 |
|
throw Exception("Windows FS error: could not determine file size"); |
1694 |
|
return dwSize; |
1695 |
|
} |
1696 |
#else // standard C functions |
#else // standard C functions |
1697 |
unsigned long File::__GetFileSize(FILE* hFile) { |
unsigned long File::__GetFileSize(FILE* hFile) { |
1698 |
long curpos = ftell(hFile); |
long curpos = ftell(hFile); |