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 |
|
|
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); |
1155 |
return pNewChunk; |
return pNewChunk; |
1156 |
} |
} |
1157 |
|
|
1158 |
|
/** @brief Moves a sub chunk. |
1159 |
|
* |
1160 |
|
* Moves a sub chunk from one position in a list to another |
1161 |
|
* position in the same list. The pSrc chunk is placed before the |
1162 |
|
* pDst chunk. |
1163 |
|
* |
1164 |
|
* @param pSrc - sub chunk to be moved |
1165 |
|
* @param pDst - the position to move to. pSrc will be placed |
1166 |
|
* before pDst. If pDst is 0, pSrc will be placed |
1167 |
|
* last in list. |
1168 |
|
*/ |
1169 |
|
void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) { |
1170 |
|
if (!pSubChunks) LoadSubChunks(); |
1171 |
|
pSubChunks->remove(pSrc); |
1172 |
|
ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst); |
1173 |
|
pSubChunks->insert(iter, pSrc); |
1174 |
|
} |
1175 |
|
|
1176 |
/** @brief Creates a new list sub chunk. |
/** @brief Creates a new list sub chunk. |
1177 |
* |
* |
1178 |
* 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 |
1268 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1269 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1270 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1271 |
|
#if defined(WIN32) |
1272 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) return; |
1273 |
|
#else |
1274 |
if (!pFile->hFileRead) return; |
if (!pFile->hFileRead) return; |
1275 |
|
#endif |
1276 |
unsigned long uiOriginalPos = GetPos(); |
unsigned long uiOriginalPos = GetPos(); |
1277 |
SetPos(0); // jump to beginning of list chunk body |
SetPos(0); // jump to beginning of list chunk body |
1278 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
1368 |
* "from scratch". Note: there must be no empty chunks or empty list |
* "from scratch". Note: there must be no empty chunks or empty list |
1369 |
* chunks when trying to make the new RIFF file persistent with Save()! |
* chunks when trying to make the new RIFF file persistent with Save()! |
1370 |
* |
* |
1371 |
|
* Note: by default, the RIFF file will be saved in native endian |
1372 |
|
* format; that is, as a RIFF file on little-endian machines and |
1373 |
|
* as a RIFX file on big-endian. To change this behaviour, call |
1374 |
|
* SetByteOrder() before calling Save(). |
1375 |
|
* |
1376 |
* @param FileType - four-byte identifier of the RIFF file type |
* @param FileType - four-byte identifier of the RIFF file type |
1377 |
* @see AddSubChunk(), AddSubList() |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1378 |
*/ |
*/ |
1379 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) : List(this) { |
1380 |
#if defined(WIN32) |
#if defined(WIN32) |
1412 |
path.c_str(), GENERIC_READ, |
path.c_str(), GENERIC_READ, |
1413 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1414 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1415 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1416 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1417 |
); |
); |
1418 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1419 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1466 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
1467 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1468 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1469 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1470 |
|
FILE_FLAG_RANDOM_ACCESS, |
1471 |
|
NULL |
1472 |
); |
); |
1473 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1474 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1496 |
GENERIC_READ | GENERIC_WRITE, |
GENERIC_READ | GENERIC_WRITE, |
1497 |
FILE_SHARE_READ, |
FILE_SHARE_READ, |
1498 |
NULL, OPEN_ALWAYS, |
NULL, OPEN_ALWAYS, |
1499 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1500 |
|
FILE_FLAG_RANDOM_ACCESS, |
1501 |
|
NULL |
1502 |
); |
); |
1503 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1504 |
hFileRead = hFileWrite = CreateFile( |
hFileRead = hFileWrite = CreateFile( |
1505 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
1506 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1507 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1508 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1509 |
|
FILE_FLAG_RANDOM_ACCESS, |
1510 |
|
NULL |
1511 |
); |
); |
1512 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
1513 |
} |
} |
1514 |
#else |
#else |
1515 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1543 |
return false; |
return false; |
1544 |
} |
} |
1545 |
|
|
1546 |
|
/** @brief Set the byte order to be used when saving. |
1547 |
|
* |
1548 |
|
* Set the byte order to be used in the file. A value of |
1549 |
|
* endian_little will create a RIFF file, endian_big a RIFX file |
1550 |
|
* and endian_native will create a RIFF file on little-endian |
1551 |
|
* machines and RIFX on big-endian machines. |
1552 |
|
* |
1553 |
|
* @param Endian - endianess to use when file is saved. |
1554 |
|
*/ |
1555 |
|
void File::SetByteOrder(endian_t Endian) { |
1556 |
|
#if WORDS_BIGENDIAN |
1557 |
|
bEndianNative = Endian != endian_little; |
1558 |
|
#else |
1559 |
|
bEndianNative = Endian != endian_big; |
1560 |
|
#endif |
1561 |
|
} |
1562 |
|
|
1563 |
/** @brief Save changes to same file. |
/** @brief Save changes to same file. |
1564 |
* |
* |
1565 |
* Make all changes of all chunks persistent by writing them to the |
* Make all changes of all chunks persistent by writing them to the |
1588 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1589 |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
1590 |
if ((*iter)->GetNewSize() == 0) { |
if ((*iter)->GetNewSize() == 0) { |
1591 |
// just to make the exception message a bit more verbose: resolve the chunk's path |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
|
String sChunkPath; |
|
|
for (Chunk* pChunk = *iter; pChunk; pChunk = pChunk->GetParent()) { |
|
|
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
|
|
List* pList = (List*) pChunk; |
|
|
sChunkPath = "->'" + pList->GetListTypeString() + "'" + sChunkPath; |
|
|
} else { |
|
|
sChunkPath = "->'" + pChunk->GetChunkIDString() + "'" + sChunkPath; |
|
|
} |
|
|
} |
|
|
throw Exception("There is at least one empty chunk (zero size): " + sChunkPath); |
|
1592 |
} |
} |
1593 |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
1594 |
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
1609 |
#if defined(WIN32) |
#if defined(WIN32) |
1610 |
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 |
1611 |
#else |
#else |
1612 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1613 |
#endif |
#endif |
1614 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
1615 |
const unsigned long ulToMove = ulFileSize - ulPos; |
const unsigned long ulToMove = ulFileSize - ulPos; |
1676 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1677 |
hFileWrite = CreateFile( |
hFileWrite = CreateFile( |
1678 |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1679 |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
1680 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1681 |
); |
); |
1682 |
if (hFileWrite == INVALID_HANDLE_VALUE) { |
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1683 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
1702 |
// forget all resized chunks |
// forget all resized chunks |
1703 |
ResizedChunks.clear(); |
ResizedChunks.clear(); |
1704 |
|
|
1705 |
if (Filename.length() > 0) { |
#if POSIX |
1706 |
#if POSIX |
if (hFileWrite) close(hFileWrite); |
1707 |
close(hFileWrite); |
#elif defined(WIN32) |
1708 |
#elif defined(WIN32) |
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1709 |
CloseHandle(hFileWrite); |
#else |
1710 |
#else |
if (hFileWrite) fclose(hFileWrite); |
1711 |
fclose(hFileWrite); |
#endif |
1712 |
#endif |
hFileWrite = hFileRead; |
|
hFileWrite = hFileRead; |
|
|
} |
|
1713 |
|
|
1714 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1715 |
Filename = path; |
Filename = path; |