2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2007 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" |
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 |
} |
} |
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); |
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 |
} |
} |
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 |
/** |
/** |
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 |
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) |
#elif defined(WIN32) |
1235 |
std::cout << "List::Readheader(ulong) "; |
std::cout << "List::Readheader(ulong) "; |
1236 |
#endif // DEBUG |
#endif // DEBUG |
1237 |
Chunk::ReadHeader(fPos); |
Chunk::ReadHeader(fPos); |
1238 |
|
if (CurrentChunkSize < 8) return; |
1239 |
NewChunkSize = CurrentChunkSize -= 4; |
NewChunkSize = CurrentChunkSize -= 4; |
1240 |
#if POSIX |
#if POSIX |
1241 |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1439 |
Mode = stream_mode_read; |
Mode = stream_mode_read; |
1440 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1441 |
ReadHeader(0); |
ReadHeader(0); |
1442 |
if (ChunkID != CHUNK_ID_RIFF) { |
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1443 |
throw RIFF::Exception("Not a RIFF file"); |
throw RIFF::Exception("Not a RIFF file"); |
1444 |
} |
} |
1445 |
} |
} |
1603 |
if ((*iter)->GetNewSize() == 0) { |
if ((*iter)->GetNewSize() == 0) { |
1604 |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
1605 |
} |
} |
1606 |
unsigned long ulDiff = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2 - (*iter)->GetSize() - (*iter)->GetSize() % 2; |
unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; |
1607 |
if (ulDiff > 0) ulPositiveSizeDiff += ulDiff; |
unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2; |
1608 |
|
if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; |
1609 |
} |
} |
1610 |
|
|
1611 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |