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" |
90 |
} |
} |
91 |
|
|
92 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
93 |
if (CurrentChunkSize != NewChunkSize) pFile->UnlogResized(this); |
pFile->UnlogResized(this); |
94 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
95 |
} |
} |
96 |
|
|
1153 |
pSubChunks->push_back(pNewChunk); |
pSubChunks->push_back(pNewChunk); |
1154 |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1155 |
pNewChunk->Resize(uiBodySize); |
pNewChunk->Resize(uiBodySize); |
1156 |
|
NewChunkSize += CHUNK_HEADER_SIZE; |
1157 |
|
pFile->LogAsResized(this); |
1158 |
return pNewChunk; |
return pNewChunk; |
1159 |
} |
} |
1160 |
|
|
1190 |
List* pNewListChunk = new List(pFile, this, uiListType); |
List* pNewListChunk = new List(pFile, this, uiListType); |
1191 |
pSubChunks->push_back(pNewListChunk); |
pSubChunks->push_back(pNewListChunk); |
1192 |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1193 |
|
NewChunkSize += LIST_HEADER_SIZE; |
1194 |
|
pFile->LogAsResized(this); |
1195 |
return pNewListChunk; |
return pNewListChunk; |
1196 |
} |
} |
1197 |
|
|
1591 |
|
|
1592 |
// 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) |
1593 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1594 |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
for (std::set<Chunk*>::const_iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
1595 |
if ((*iter)->GetNewSize() == 0) { |
if ((*iter)->GetNewSize() == 0) { |
1596 |
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)); |
1597 |
} |
} |
1598 |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
unsigned long ulDiff = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2 - (*iter)->GetSize() - (*iter)->GetSize() % 2; |
1599 |
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
if (ulDiff > 0) ulPositiveSizeDiff += ulDiff; |
|
ulPositiveSizeDiff += ulDiff; |
|
|
} |
|
1600 |
} |
} |
1601 |
|
|
1602 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |
1614 |
#else |
#else |
1615 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1616 |
#endif |
#endif |
1617 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
1618 |
const unsigned long ulToMove = ulFileSize - ulPos; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1619 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
ulPos -= iBytesMoved; |
1620 |
#if POSIX |
#if POSIX |
1621 |
lseek(hFileRead, ulPos, SEEK_SET); |
lseek(hFileRead, ulPos, SEEK_SET); |
1622 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1749 |
} |
} |
1750 |
|
|
1751 |
void File::LogAsResized(Chunk* pResizedChunk) { |
void File::LogAsResized(Chunk* pResizedChunk) { |
1752 |
ResizedChunks.push_back(pResizedChunk); |
ResizedChunks.insert(pResizedChunk); |
1753 |
} |
} |
1754 |
|
|
1755 |
void File::UnlogResized(Chunk* pResizedChunk) { |
void File::UnlogResized(Chunk* pResizedChunk) { |
1756 |
ResizedChunks.remove(pResizedChunk); |
ResizedChunks.erase(pResizedChunk); |
1757 |
} |
} |
1758 |
|
|
1759 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |