29 |
|
|
30 |
namespace RIFF { |
namespace RIFF { |
31 |
|
|
32 |
|
// *************** Internal functions ************** |
33 |
|
// * |
34 |
|
|
35 |
|
/// Returns a human readable path of the given chunk. |
36 |
|
static 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 |
|
pFile->UnlogResized(this); |
93 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
94 |
} |
} |
95 |
|
|
235 |
* - RIFF::stream_closed : |
* - RIFF::stream_closed : |
236 |
* the data stream was closed somehow, no more reading possible |
* the data stream was closed somehow, no more reading possible |
237 |
* - RIFF::stream_end_reached : |
* - RIFF::stream_end_reached : |
238 |
* alreaady reached the end of the chunk data, no more reading |
* already reached the end of the chunk data, no more reading |
239 |
* possible without SetPos() |
* possible without SetPos() |
240 |
*/ |
*/ |
241 |
stream_state_t Chunk::GetState() { |
stream_state_t Chunk::GetState() { |
358 |
#elif defined(WIN32) |
#elif defined(WIN32) |
359 |
if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
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) + |
throw Exception("Could not seek to position " + ToString(ulPos) + |
361 |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
362 |
} |
} |
363 |
DWORD writtenWords; |
DWORD writtenWords; |
364 |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
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); |
1152 |
pSubChunks->push_back(pNewChunk); |
pSubChunks->push_back(pNewChunk); |
1153 |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1154 |
pNewChunk->Resize(uiBodySize); |
pNewChunk->Resize(uiBodySize); |
1155 |
|
NewChunkSize += CHUNK_HEADER_SIZE; |
1156 |
|
pFile->LogAsResized(this); |
1157 |
return pNewChunk; |
return pNewChunk; |
1158 |
} |
} |
1159 |
|
|
1160 |
|
/** @brief Moves a sub chunk. |
1161 |
|
* |
1162 |
|
* Moves a sub chunk from one position in a list to another |
1163 |
|
* position in the same list. The pSrc chunk is placed before the |
1164 |
|
* pDst chunk. |
1165 |
|
* |
1166 |
|
* @param pSrc - sub chunk to be moved |
1167 |
|
* @param pDst - the position to move to. pSrc will be placed |
1168 |
|
* before pDst. If pDst is 0, pSrc will be placed |
1169 |
|
* last in list. |
1170 |
|
*/ |
1171 |
|
void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) { |
1172 |
|
if (!pSubChunks) LoadSubChunks(); |
1173 |
|
pSubChunks->remove(pSrc); |
1174 |
|
ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst); |
1175 |
|
pSubChunks->insert(iter, pSrc); |
1176 |
|
} |
1177 |
|
|
1178 |
/** @brief Creates a new list sub chunk. |
/** @brief Creates a new list sub chunk. |
1179 |
* |
* |
1180 |
* 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 |
1189 |
List* pNewListChunk = new List(pFile, this, uiListType); |
List* pNewListChunk = new List(pFile, this, uiListType); |
1190 |
pSubChunks->push_back(pNewListChunk); |
pSubChunks->push_back(pNewListChunk); |
1191 |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1192 |
|
NewChunkSize += LIST_HEADER_SIZE; |
1193 |
|
pFile->LogAsResized(this); |
1194 |
return pNewListChunk; |
return pNewListChunk; |
1195 |
} |
} |
1196 |
|
|
1272 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1273 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1274 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1275 |
|
#if defined(WIN32) |
1276 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) return; |
1277 |
|
#else |
1278 |
if (!pFile->hFileRead) return; |
if (!pFile->hFileRead) return; |
1279 |
|
#endif |
1280 |
unsigned long uiOriginalPos = GetPos(); |
unsigned long uiOriginalPos = GetPos(); |
1281 |
SetPos(0); // jump to beginning of list chunk body |
SetPos(0); // jump to beginning of list chunk body |
1282 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
1372 |
* "from scratch". Note: there must be no empty chunks or empty list |
* "from scratch". Note: there must be no empty chunks or empty list |
1373 |
* chunks when trying to make the new RIFF file persistent with Save()! |
* chunks when trying to make the new RIFF file persistent with Save()! |
1374 |
* |
* |
1375 |
|
* Note: by default, the RIFF file will be saved in native endian |
1376 |
|
* format; that is, as a RIFF file on little-endian machines and |
1377 |
|
* as a RIFX file on big-endian. To change this behaviour, call |
1378 |
|
* SetByteOrder() before calling Save(). |
1379 |
|
* |
1380 |
* @param FileType - four-byte identifier of the RIFF file type |
* @param FileType - four-byte identifier of the RIFF file type |
1381 |
* @see AddSubChunk(), AddSubList() |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1382 |
*/ |
*/ |
1383 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) : List(this) { |
1384 |
#if defined(WIN32) |
#if defined(WIN32) |
1416 |
path.c_str(), GENERIC_READ, |
path.c_str(), GENERIC_READ, |
1417 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1418 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1419 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1420 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1421 |
); |
); |
1422 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1423 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1470 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
1471 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1472 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1473 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1474 |
|
FILE_FLAG_RANDOM_ACCESS, |
1475 |
|
NULL |
1476 |
); |
); |
1477 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1478 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1500 |
GENERIC_READ | GENERIC_WRITE, |
GENERIC_READ | GENERIC_WRITE, |
1501 |
FILE_SHARE_READ, |
FILE_SHARE_READ, |
1502 |
NULL, OPEN_ALWAYS, |
NULL, OPEN_ALWAYS, |
1503 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1504 |
|
FILE_FLAG_RANDOM_ACCESS, |
1505 |
|
NULL |
1506 |
); |
); |
1507 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1508 |
hFileRead = hFileWrite = CreateFile( |
hFileRead = hFileWrite = CreateFile( |
1509 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
1510 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1511 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1512 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1513 |
|
FILE_FLAG_RANDOM_ACCESS, |
1514 |
|
NULL |
1515 |
); |
); |
1516 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
1517 |
} |
} |
1518 |
#else |
#else |
1519 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1547 |
return false; |
return false; |
1548 |
} |
} |
1549 |
|
|
1550 |
|
/** @brief Set the byte order to be used when saving. |
1551 |
|
* |
1552 |
|
* Set the byte order to be used in the file. A value of |
1553 |
|
* endian_little will create a RIFF file, endian_big a RIFX file |
1554 |
|
* and endian_native will create a RIFF file on little-endian |
1555 |
|
* machines and RIFX on big-endian machines. |
1556 |
|
* |
1557 |
|
* @param Endian - endianess to use when file is saved. |
1558 |
|
*/ |
1559 |
|
void File::SetByteOrder(endian_t Endian) { |
1560 |
|
#if WORDS_BIGENDIAN |
1561 |
|
bEndianNative = Endian != endian_little; |
1562 |
|
#else |
1563 |
|
bEndianNative = Endian != endian_big; |
1564 |
|
#endif |
1565 |
|
} |
1566 |
|
|
1567 |
/** @brief Save changes to same file. |
/** @brief Save changes to same file. |
1568 |
* |
* |
1569 |
* Make all changes of all chunks persistent by writing them to the |
* Make all changes of all chunks persistent by writing them to the |
1590 |
|
|
1591 |
// 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) |
1592 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1593 |
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) { |
1594 |
if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)"); |
if ((*iter)->GetNewSize() == 0) { |
1595 |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
|
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
|
|
ulPositiveSizeDiff += ulDiff; |
|
1596 |
} |
} |
1597 |
|
unsigned long ulDiff = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2 - (*iter)->GetSize() - (*iter)->GetSize() % 2; |
1598 |
|
if (ulDiff > 0) ulPositiveSizeDiff += ulDiff; |
1599 |
} |
} |
1600 |
|
|
1601 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |
1611 |
#if defined(WIN32) |
#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 |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1613 |
#else |
#else |
1614 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1615 |
#endif |
#endif |
1616 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
1617 |
const unsigned long ulToMove = ulFileSize - ulPos; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1618 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
ulPos -= iBytesMoved; |
1619 |
#if POSIX |
#if POSIX |
1620 |
lseek(hFileRead, ulPos, SEEK_SET); |
lseek(hFileRead, ulPos, SEEK_SET); |
1621 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1678 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1679 |
hFileWrite = CreateFile( |
hFileWrite = CreateFile( |
1680 |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1681 |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
1682 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1683 |
); |
); |
1684 |
if (hFileWrite == INVALID_HANDLE_VALUE) { |
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1685 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
1704 |
// forget all resized chunks |
// forget all resized chunks |
1705 |
ResizedChunks.clear(); |
ResizedChunks.clear(); |
1706 |
|
|
1707 |
if (Filename.length() > 0) { |
#if POSIX |
1708 |
#if POSIX |
if (hFileWrite) close(hFileWrite); |
1709 |
close(hFileWrite); |
#elif defined(WIN32) |
1710 |
#elif defined(WIN32) |
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1711 |
CloseHandle(hFileWrite); |
#else |
1712 |
#else |
if (hFileWrite) fclose(hFileWrite); |
1713 |
fclose(hFileWrite); |
#endif |
1714 |
#endif |
hFileWrite = hFileRead; |
|
hFileWrite = hFileRead; |
|
|
} |
|
1715 |
|
|
1716 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1717 |
Filename = path; |
Filename = path; |
1748 |
} |
} |
1749 |
|
|
1750 |
void File::LogAsResized(Chunk* pResizedChunk) { |
void File::LogAsResized(Chunk* pResizedChunk) { |
1751 |
ResizedChunks.push_back(pResizedChunk); |
ResizedChunks.insert(pResizedChunk); |
1752 |
|
} |
1753 |
|
|
1754 |
|
void File::UnlogResized(Chunk* pResizedChunk) { |
1755 |
|
ResizedChunks.erase(pResizedChunk); |
1756 |
} |
} |
1757 |
|
|
1758 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |