125 |
read(pFile->hFileRead, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
126 |
read(pFile->hFileRead, &ullCurrentChunkSize, pFile->FileOffsetSize); |
read(pFile->hFileRead, &ullCurrentChunkSize, pFile->FileOffsetSize); |
127 |
#elif defined(WIN32) |
#elif defined(WIN32) |
128 |
if (SetFilePointerEx(pFile->hFileRead, filePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
LARGE_INTEGER liFilePos; |
129 |
|
liFilePos.QuadPart = filePos; |
130 |
|
if (SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
131 |
DWORD dwBytesRead; |
DWORD dwBytesRead; |
132 |
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
133 |
ReadFile(pFile->hFileRead, &ullCurrentChunkSize, pFile->FileOffsetSize, &dwBytesRead, NULL); |
ReadFile(pFile->hFileRead, &ullCurrentChunkSize, pFile->FileOffsetSize, &dwBytesRead, NULL); |
186 |
write(pFile->hFileWrite, &ullNewChunkSize, pFile->FileOffsetSize); |
write(pFile->hFileWrite, &ullNewChunkSize, pFile->FileOffsetSize); |
187 |
} |
} |
188 |
#elif defined(WIN32) |
#elif defined(WIN32) |
189 |
if (SetFilePointerEx(pFile->hFileWrite, filePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
LARGE_INTEGER liFilePos; |
190 |
|
liFilePos.QuadPart = filePos; |
191 |
|
if (SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
192 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
193 |
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
194 |
WriteFile(pFile->hFileWrite, &ullNewChunkSize, pFile->FileOffsetSize, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &ullNewChunkSize, pFile->FileOffsetSize, &dwBytesWritten, NULL); |
313 |
* @param WordCount number of data words to read |
* @param WordCount number of data words to read |
314 |
* @param WordSize size of each data word to read |
* @param WordSize size of each data word to read |
315 |
* @returns number of successfully read data words or 0 if end |
* @returns number of successfully read data words or 0 if end |
316 |
* of file reached or error occured |
* of file reached or error occurred |
317 |
*/ |
*/ |
318 |
file_offset_t Chunk::Read(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
file_offset_t Chunk::Read(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
319 |
#if DEBUG |
#if DEBUG |
333 |
} |
} |
334 |
readWords /= WordSize; |
readWords /= WordSize; |
335 |
#elif defined(WIN32) |
#elif defined(WIN32) |
336 |
if (!SetFilePointerEx(pFile->hFileRead, ullStartPos + ullPos, NULL/*new pos pointer*/, FILE_BEGIN)) |
LARGE_INTEGER liFilePos; |
337 |
|
liFilePos.QuadPart = ullStartPos + ullPos; |
338 |
|
if (!SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) |
339 |
return 0; |
return 0; |
340 |
DWORD readWords; |
DWORD readWords; |
341 |
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); //FIXME: does not work for reading buffers larger than 2GB (even though this should rarely be the case in practice) |
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); //FIXME: does not work for reading buffers larger than 2GB (even though this should rarely be the case in practice) |
382 |
* @param WordSize size of each data word to write |
* @param WordSize size of each data word to write |
383 |
* @returns number of successfully written data words |
* @returns number of successfully written data words |
384 |
* @throws RIFF::Exception if write operation would exceed current |
* @throws RIFF::Exception if write operation would exceed current |
385 |
* chunk size or any IO error occured |
* chunk size or any IO error occurred |
386 |
* @see Resize() |
* @see Resize() |
387 |
*/ |
*/ |
388 |
file_offset_t Chunk::Write(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
file_offset_t Chunk::Write(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
419 |
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"); |
420 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
421 |
#elif defined(WIN32) |
#elif defined(WIN32) |
422 |
if (!SetFilePointerEx(pFile->hFileWrite, ullStartPos + ullPos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
LARGE_INTEGER liFilePos; |
423 |
|
liFilePos.QuadPart = ullStartPos + ullPos; |
424 |
|
if (!SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
425 |
throw Exception("Could not seek to position " + ToString(ullPos) + |
throw Exception("Could not seek to position " + ToString(ullPos) + |
426 |
" in chunk (" + ToString(ullStartPos + ullPos) + " in file)"); |
" in chunk (" + ToString(ullStartPos + ullPos) + " in file)"); |
427 |
} |
} |
455 |
* @param pData destination buffer |
* @param pData destination buffer |
456 |
* @param WordCount number of 8 Bit signed integers to read |
* @param WordCount number of 8 Bit signed integers to read |
457 |
* @returns number of read integers |
* @returns number of read integers |
458 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
459 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
460 |
*/ |
*/ |
461 |
file_offset_t Chunk::ReadInt8(int8_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::ReadInt8(int8_t* pData, file_offset_t WordCount) { |
476 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
477 |
* @param WordCount number of 8 Bit signed integers to write |
* @param WordCount number of 8 Bit signed integers to write |
478 |
* @returns number of written integers |
* @returns number of written integers |
479 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
480 |
* @see Resize() |
* @see Resize() |
481 |
*/ |
*/ |
482 |
file_offset_t Chunk::WriteInt8(int8_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteInt8(int8_t* pData, file_offset_t WordCount) { |
492 |
* @param pData destination buffer |
* @param pData destination buffer |
493 |
* @param WordCount number of 8 Bit unsigned integers to read |
* @param WordCount number of 8 Bit unsigned integers to read |
494 |
* @returns number of read integers |
* @returns number of read integers |
495 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
496 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
497 |
*/ |
*/ |
498 |
file_offset_t Chunk::ReadUint8(uint8_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::ReadUint8(uint8_t* pData, file_offset_t WordCount) { |
513 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
514 |
* @param WordCount number of 8 Bit unsigned integers to write |
* @param WordCount number of 8 Bit unsigned integers to write |
515 |
* @returns number of written integers |
* @returns number of written integers |
516 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
517 |
* @see Resize() |
* @see Resize() |
518 |
*/ |
*/ |
519 |
file_offset_t Chunk::WriteUint8(uint8_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteUint8(uint8_t* pData, file_offset_t WordCount) { |
529 |
* @param pData destination buffer |
* @param pData destination buffer |
530 |
* @param WordCount number of 16 Bit signed integers to read |
* @param WordCount number of 16 Bit signed integers to read |
531 |
* @returns number of read integers |
* @returns number of read integers |
532 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
533 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
534 |
*/ |
*/ |
535 |
file_offset_t Chunk::ReadInt16(int16_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::ReadInt16(int16_t* pData, file_offset_t WordCount) { |
550 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
551 |
* @param WordCount number of 16 Bit signed integers to write |
* @param WordCount number of 16 Bit signed integers to write |
552 |
* @returns number of written integers |
* @returns number of written integers |
553 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
554 |
* @see Resize() |
* @see Resize() |
555 |
*/ |
*/ |
556 |
file_offset_t Chunk::WriteInt16(int16_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteInt16(int16_t* pData, file_offset_t WordCount) { |
566 |
* @param pData destination buffer |
* @param pData destination buffer |
567 |
* @param WordCount number of 8 Bit unsigned integers to read |
* @param WordCount number of 8 Bit unsigned integers to read |
568 |
* @returns number of read integers |
* @returns number of read integers |
569 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
570 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
571 |
*/ |
*/ |
572 |
file_offset_t Chunk::ReadUint16(uint16_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::ReadUint16(uint16_t* pData, file_offset_t WordCount) { |
587 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
588 |
* @param WordCount number of 16 Bit unsigned integers to write |
* @param WordCount number of 16 Bit unsigned integers to write |
589 |
* @returns number of written integers |
* @returns number of written integers |
590 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
591 |
* @see Resize() |
* @see Resize() |
592 |
*/ |
*/ |
593 |
file_offset_t Chunk::WriteUint16(uint16_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteUint16(uint16_t* pData, file_offset_t WordCount) { |
603 |
* @param pData destination buffer |
* @param pData destination buffer |
604 |
* @param WordCount number of 32 Bit signed integers to read |
* @param WordCount number of 32 Bit signed integers to read |
605 |
* @returns number of read integers |
* @returns number of read integers |
606 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
607 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
608 |
*/ |
*/ |
609 |
file_offset_t Chunk::ReadInt32(int32_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::ReadInt32(int32_t* pData, file_offset_t WordCount) { |
624 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
625 |
* @param WordCount number of 32 Bit signed integers to write |
* @param WordCount number of 32 Bit signed integers to write |
626 |
* @returns number of written integers |
* @returns number of written integers |
627 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
628 |
* @see Resize() |
* @see Resize() |
629 |
*/ |
*/ |
630 |
file_offset_t Chunk::WriteInt32(int32_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteInt32(int32_t* pData, file_offset_t WordCount) { |
640 |
* @param pData destination buffer |
* @param pData destination buffer |
641 |
* @param WordCount number of 32 Bit unsigned integers to read |
* @param WordCount number of 32 Bit unsigned integers to read |
642 |
* @returns number of read integers |
* @returns number of read integers |
643 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
644 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
645 |
*/ |
*/ |
646 |
file_offset_t Chunk::ReadUint32(uint32_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::ReadUint32(uint32_t* pData, file_offset_t WordCount) { |
657 |
* |
* |
658 |
* @param s destination string |
* @param s destination string |
659 |
* @param size number of characters to read |
* @param size number of characters to read |
660 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
661 |
* \a size characters could be read! |
* \a size characters could be read! |
662 |
*/ |
*/ |
663 |
void Chunk::ReadString(String& s, int size) { |
void Chunk::ReadString(String& s, int size) { |
678 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
679 |
* @param WordCount number of 32 Bit unsigned integers to write |
* @param WordCount number of 32 Bit unsigned integers to write |
680 |
* @returns number of written integers |
* @returns number of written integers |
681 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
682 |
* @see Resize() |
* @see Resize() |
683 |
*/ |
*/ |
684 |
file_offset_t Chunk::WriteUint32(uint32_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteUint32(uint32_t* pData, file_offset_t WordCount) { |
690 |
* the chunk. |
* the chunk. |
691 |
* |
* |
692 |
* @returns read integer word |
* @returns read integer word |
693 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
694 |
*/ |
*/ |
695 |
int8_t Chunk::ReadInt8() { |
int8_t Chunk::ReadInt8() { |
696 |
#if DEBUG |
#if DEBUG |
706 |
* within the chunk. |
* within the chunk. |
707 |
* |
* |
708 |
* @returns read integer word |
* @returns read integer word |
709 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
710 |
*/ |
*/ |
711 |
uint8_t Chunk::ReadUint8() { |
uint8_t Chunk::ReadUint8() { |
712 |
#if DEBUG |
#if DEBUG |
723 |
* needed. |
* needed. |
724 |
* |
* |
725 |
* @returns read integer word |
* @returns read integer word |
726 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
727 |
*/ |
*/ |
728 |
int16_t Chunk::ReadInt16() { |
int16_t Chunk::ReadInt16() { |
729 |
#if DEBUG |
#if DEBUG |
740 |
* needed. |
* needed. |
741 |
* |
* |
742 |
* @returns read integer word |
* @returns read integer word |
743 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
744 |
*/ |
*/ |
745 |
uint16_t Chunk::ReadUint16() { |
uint16_t Chunk::ReadUint16() { |
746 |
#if DEBUG |
#if DEBUG |
757 |
* needed. |
* needed. |
758 |
* |
* |
759 |
* @returns read integer word |
* @returns read integer word |
760 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
761 |
*/ |
*/ |
762 |
int32_t Chunk::ReadInt32() { |
int32_t Chunk::ReadInt32() { |
763 |
#if DEBUG |
#if DEBUG |
774 |
* needed. |
* needed. |
775 |
* |
* |
776 |
* @returns read integer word |
* @returns read integer word |
777 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
778 |
*/ |
*/ |
779 |
uint32_t Chunk::ReadUint32() { |
uint32_t Chunk::ReadUint32() { |
780 |
#if DEBUG |
#if DEBUG |
811 |
#if POSIX |
#if POSIX |
812 |
if (lseek(pFile->hFileRead, ullStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ullStartPos, SEEK_SET) == -1) return NULL; |
813 |
#elif defined(WIN32) |
#elif defined(WIN32) |
814 |
if (!SetFilePointerEx(pFile->hFileRead, ullStartPos, NULL/*new pos pointer*/, FILE_BEGIN)) return NULL; |
LARGE_INTEGER liFilePos; |
815 |
|
liFilePos.QuadPart = ullStartPos; |
816 |
|
if (!SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) return NULL; |
817 |
#else |
#else |
818 |
if (fseeko(pFile->hFileRead, ullStartPos, SEEK_SET)) return NULL; |
if (fseeko(pFile->hFileRead, ullStartPos, SEEK_SET)) return NULL; |
819 |
#endif // POSIX |
#endif // POSIX |
874 |
* boundary! |
* boundary! |
875 |
* |
* |
876 |
* @param NewSize - new chunk body size in bytes (must be greater than zero) |
* @param NewSize - new chunk body size in bytes (must be greater than zero) |
877 |
* @throws RIFF::Exception if \a NewSize is less than 1 or Unrealistic large |
* @throws RIFF::Exception if \a NewSize is less than 1 or unrealistic large |
878 |
* @see File::Save() |
* @see File::Save() |
879 |
*/ |
*/ |
880 |
void Chunk::Resize(file_offset_t NewSize) { |
void Chunk::Resize(file_offset_t NewSize) { |
917 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
918 |
} |
} |
919 |
#elif defined(WIN32) |
#elif defined(WIN32) |
920 |
SetFilePointerEx(pFile->hFileWrite, ullWritePos, NULL/*new pos pointer*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
921 |
|
liFilePos.QuadPart = ullWritePos; |
922 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
923 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
924 |
WriteFile(pFile->hFileWrite, pChunkData, ullNewChunkSize, &dwBytesWritten, NULL); //FIXME: won't save chunks larger than 2GB ! |
WriteFile(pFile->hFileWrite, pChunkData, ullNewChunkSize, &dwBytesWritten, NULL); //FIXME: won't save chunks larger than 2GB ! |
925 |
if (dwBytesWritten != ullNewChunkSize) { |
if (dwBytesWritten != ullNewChunkSize) { |
941 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
942 |
#endif |
#endif |
943 |
for (file_offset_t ullOffset = 0; ullToMove > 0 && iBytesMoved > 0; ullOffset += iBytesMoved, ullToMove -= iBytesMoved) { |
for (file_offset_t ullOffset = 0; ullToMove > 0 && iBytesMoved > 0; ullOffset += iBytesMoved, ullToMove -= iBytesMoved) { |
944 |
iBytesMoved = (ullToMove < 4096) ? ullToMove : 4096; |
iBytesMoved = (ullToMove < 4096) ? int(ullToMove) : 4096; |
945 |
#if POSIX |
#if POSIX |
946 |
lseek(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
lseek(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
947 |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = (int) read(pFile->hFileRead, pCopyBuffer, (size_t) iBytesMoved); |
948 |
lseek(pFile->hFileWrite, ullWritePos + ullOffset, SEEK_SET); |
lseek(pFile->hFileWrite, ullWritePos + ullOffset, SEEK_SET); |
949 |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = (int) write(pFile->hFileWrite, pCopyBuffer, (size_t) iBytesMoved); |
950 |
#elif defined(WIN32) |
#elif defined(WIN32) |
951 |
SetFilePointerEx(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, NULL/*new pos pointer*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
952 |
|
liFilePos.QuadPart = ullStartPos + ullCurrentDataOffset + ullOffset; |
953 |
|
SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
954 |
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
955 |
SetFilePointerEx(pFile->hFileWrite, ullWritePos + ullOffset, NULL/*new pos pointer*/, FILE_BEGIN); |
liFilePos.QuadPart = ullWritePos + ullOffset; |
956 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
957 |
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
958 |
#else |
#else |
959 |
fseeko(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
fseeko(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
983 |
lseek(pFile->hFileWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
lseek(pFile->hFileWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
984 |
write(pFile->hFileWrite, &cPadByte, 1); |
write(pFile->hFileWrite, &cPadByte, 1); |
985 |
#elif defined(WIN32) |
#elif defined(WIN32) |
986 |
SetFilePointerEx(pFile->hFileWrite, ullStartPos + ullNewChunkSize, NULL/*new pos pointer*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
987 |
|
liFilePos.QuadPart = ullStartPos + ullNewChunkSize; |
988 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
989 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
990 |
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
991 |
#else |
#else |
1079 |
/** |
/** |
1080 |
* Returns sublist chunk with list type <i>\a ListType</i> within this |
* Returns sublist chunk with list type <i>\a ListType</i> within this |
1081 |
* chunk list. Use this method if you expect only one sublist chunk of |
* chunk list. Use this method if you expect only one sublist chunk of |
1082 |
* that type in the list. It there are more than one, it's undetermined |
* that type in the list. If there are more than one, it's undetermined |
1083 |
* which one of them will be returned! If there are no sublists with |
* which one of them will be returned! If there are no sublists with |
1084 |
* that desired list type, NULL will be returned. |
* that desired list type, NULL will be returned. |
1085 |
* |
* |
1105 |
} |
} |
1106 |
|
|
1107 |
/** |
/** |
1108 |
* Returns the first subchunk within the list. You have to call this |
* Returns the first subchunk within the list (which may be an ordinary |
1109 |
|
* chunk as well as a list chunk). You have to call this |
1110 |
* method before you can call GetNextSubChunk(). Recall it when you want |
* method before you can call GetNextSubChunk(). Recall it when you want |
1111 |
* to start from the beginning of the list again. |
* to start from the beginning of the list again. |
1112 |
* |
* |
1123 |
} |
} |
1124 |
|
|
1125 |
/** |
/** |
1126 |
* Returns the next subchunk within the list. You have to call |
* Returns the next subchunk within the list (which may be an ordinary |
1127 |
|
* chunk as well as a list chunk). You have to call |
1128 |
* GetFirstSubChunk() before you can use this method! |
* GetFirstSubChunk() before you can use this method! |
1129 |
* |
* |
1130 |
* @returns pointer to the next subchunk within the list or NULL if |
* @returns pointer to the next subchunk within the list or NULL if |
1186 |
} |
} |
1187 |
|
|
1188 |
/** |
/** |
1189 |
* Returns number of subchunks within the list. |
* Returns number of subchunks within the list (including list chunks). |
1190 |
*/ |
*/ |
1191 |
unsigned int List::CountSubChunks() { |
size_t List::CountSubChunks() { |
1192 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1193 |
return pSubChunks->size(); |
return pSubChunks->size(); |
1194 |
} |
} |
1197 |
* Returns number of subchunks within the list with chunk ID |
* Returns number of subchunks within the list with chunk ID |
1198 |
* <i>\a ChunkId</i>. |
* <i>\a ChunkId</i>. |
1199 |
*/ |
*/ |
1200 |
unsigned int List::CountSubChunks(uint32_t ChunkID) { |
size_t List::CountSubChunks(uint32_t ChunkID) { |
1201 |
unsigned int result = 0; |
size_t result = 0; |
1202 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1203 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1204 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1214 |
/** |
/** |
1215 |
* Returns number of sublists within the list. |
* Returns number of sublists within the list. |
1216 |
*/ |
*/ |
1217 |
unsigned int List::CountSubLists() { |
size_t List::CountSubLists() { |
1218 |
return CountSubChunks(CHUNK_ID_LIST); |
return CountSubChunks(CHUNK_ID_LIST); |
1219 |
} |
} |
1220 |
|
|
1222 |
* Returns number of sublists within the list with list type |
* Returns number of sublists within the list with list type |
1223 |
* <i>\a ListType</i> |
* <i>\a ListType</i> |
1224 |
*/ |
*/ |
1225 |
unsigned int List::CountSubLists(uint32_t ListType) { |
size_t List::CountSubLists(uint32_t ListType) { |
1226 |
unsigned int result = 0; |
size_t result = 0; |
1227 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1228 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1229 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1385 |
lseek(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
lseek(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1386 |
read(pFile->hFileRead, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1387 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1388 |
SetFilePointerEx(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), NULL/*new pos pointer*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
1389 |
|
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1390 |
|
SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1391 |
DWORD dwBytesRead; |
DWORD dwBytesRead; |
1392 |
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
1393 |
#else |
#else |
1411 |
lseek(pFile->hFileWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
lseek(pFile->hFileWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1412 |
write(pFile->hFileWrite, &ListType, 4); |
write(pFile->hFileWrite, &ListType, 4); |
1413 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1414 |
SetFilePointerEx(pFile->hFileWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), NULL/*new pos pointer*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
1415 |
|
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1416 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1417 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
1418 |
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
1419 |
#else |
#else |
1461 |
} |
} |
1462 |
|
|
1463 |
void List::LoadSubChunksRecursively(progress_t* pProgress) { |
void List::LoadSubChunksRecursively(progress_t* pProgress) { |
1464 |
const int n = CountSubLists(); |
const int n = (int) CountSubLists(); |
1465 |
int i = 0; |
int i = 0; |
1466 |
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) { |
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) { |
1467 |
// divide local progress into subprogress |
// divide local progress into subprogress |
1497 |
|
|
1498 |
// write all subchunks (including sub list chunks) recursively |
// write all subchunks (including sub list chunks) recursively |
1499 |
if (pSubChunks) { |
if (pSubChunks) { |
1500 |
int i = 0; |
size_t i = 0; |
1501 |
const int n = pSubChunks->size(); |
const size_t n = pSubChunks->size(); |
1502 |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) { |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) { |
1503 |
// divide local progress into subprogress for loading current Instrument |
// divide local progress into subprogress for loading current Instrument |
1504 |
progress_t subprogress; |
progress_t subprogress; |
1576 |
* Loads an existing RIFF file with all its chunks. |
* Loads an existing RIFF file with all its chunks. |
1577 |
* |
* |
1578 |
* @param path - path and file name of the RIFF file to open |
* @param path - path and file name of the RIFF file to open |
1579 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occurred while trying to load the |
1580 |
* given RIFF file |
* given RIFF file |
1581 |
*/ |
*/ |
1582 |
File::File(const String& path) |
File::File(const String& path) |
1623 |
* @param Endian - whether the file uses little endian or big endian layout |
* @param Endian - whether the file uses little endian or big endian layout |
1624 |
* @param layout - general file structure type |
* @param layout - general file structure type |
1625 |
* @param fileOffsetSize - (optional) preference how to deal with large files |
* @param fileOffsetSize - (optional) preference how to deal with large files |
1626 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occurred while trying to load the |
1627 |
* given RIFF-alike file |
* given RIFF-alike file |
1628 |
*/ |
*/ |
1629 |
File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize) |
File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize) |
1650 |
* @param path - path and file name of the RIFF file or RIFF-alike file to |
* @param path - path and file name of the RIFF file or RIFF-alike file to |
1651 |
* be opened |
* be opened |
1652 |
* @param FileType - (optional) expected chunk ID of first chunk in file |
* @param FileType - (optional) expected chunk ID of first chunk in file |
1653 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occurred while trying to load the |
1654 |
* given RIFF file or RIFF-alike file |
* given RIFF file or RIFF-alike file |
1655 |
*/ |
*/ |
1656 |
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
1854 |
* |
* |
1855 |
* @param pProgress - optional: callback function for progress notification |
* @param pProgress - optional: callback function for progress notification |
1856 |
* @throws RIFF::Exception if there is an empty chunk or empty list |
* @throws RIFF::Exception if there is an empty chunk or empty list |
1857 |
* chunk or any kind of IO error occured |
* chunk or any kind of IO error occurred |
1858 |
*/ |
*/ |
1859 |
void File::Save(progress_t* pProgress) { |
void File::Save(progress_t* pProgress) { |
1860 |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1910 |
#if defined(WIN32) |
#if defined(WIN32) |
1911 |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1912 |
#else |
#else |
1913 |
int iBytesMoved = 1; |
ssize_t iBytesMoved = 1; |
1914 |
#endif |
#endif |
1915 |
for (file_offset_t ullPos = workingFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) { |
for (file_offset_t ullPos = workingFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) { |
1916 |
iBytesMoved = (ullPos < 4096) ? ullPos : 4096; |
iBytesMoved = (ullPos < 4096) ? ullPos : 4096; |
1921 |
lseek(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET); |
lseek(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET); |
1922 |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1923 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1924 |
SetFilePointerEx(hFileRead, ullPos, NULL/*new pos pointer*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
1925 |
|
liFilePos.QuadPart = ullPos; |
1926 |
|
SetFilePointerEx(hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1927 |
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1928 |
SetFilePointerEx(hFileWrite, ullPos + positiveSizeDiff, NULL/*new pos pointer*/, FILE_BEGIN); |
liFilePos.QuadPart = ullPos + positiveSizeDiff; |
1929 |
|
SetFilePointerEx(hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1930 |
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1931 |
#else |
#else |
1932 |
fseeko(hFileRead, ullPos, SEEK_SET); |
fseeko(hFileRead, ullPos, SEEK_SET); |
2066 |
if (ftruncate(hFileWrite, ullNewSize) < 0) |
if (ftruncate(hFileWrite, ullNewSize) < 0) |
2067 |
throw Exception("Could not resize file \"" + Filename + "\""); |
throw Exception("Could not resize file \"" + Filename + "\""); |
2068 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2069 |
|
LARGE_INTEGER liFilePos; |
2070 |
|
liFilePos.QuadPart = ullNewSize; |
2071 |
if ( |
if ( |
2072 |
!SetFilePointerEx(hFileWrite, ullNewSize, NULL/*new pos pointer*/, FILE_BEGIN) || |
!SetFilePointerEx(hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN) || |
2073 |
!SetEndOfFile(hFileWrite) |
!SetEndOfFile(hFileWrite) |
2074 |
) throw Exception("Could not resize file \"" + Filename + "\""); |
) throw Exception("Could not resize file \"" + Filename + "\""); |
2075 |
#else |
#else |
2231 |
} |
} |
2232 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2233 |
file_offset_t File::__GetFileSize(HANDLE hFile) const { |
file_offset_t File::__GetFileSize(HANDLE hFile) const { |
2234 |
PLARGE_INTEGER size; |
LARGE_INTEGER size; |
2235 |
if (!GetFileSizeEx(hFile, &size)) |
if (!GetFileSizeEx(hFile, &size)) |
2236 |
throw Exception("Windows FS error: could not determine file size"); |
throw Exception("Windows FS error: could not determine file size"); |
2237 |
return size; |
return size.QuadPart; |
2238 |
} |
} |
2239 |
#else // standard C functions |
#else // standard C functions |
2240 |
file_offset_t File::__GetFileSize(FILE* hFile) const { |
file_offset_t File::__GetFileSize(FILE* hFile) const { |