22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
#include <algorithm> |
#include <algorithm> |
25 |
|
#include <set> |
26 |
#include <string.h> |
#include <string.h> |
27 |
|
|
28 |
#include "RIFF.h" |
#include "RIFF.h" |
1384 |
// *************** File *************** |
// *************** File *************** |
1385 |
// * |
// * |
1386 |
|
|
1387 |
|
//HACK: to avoid breaking DLL compatibility to older versions of libgig we roll the new std::set<Chunk*> into the old std::list<Chunk*> container, should be replaced on member variable level soon though |
1388 |
|
#define _GET_RESIZED_CHUNKS() \ |
1389 |
|
(reinterpret_cast<std::set<Chunk*>*>(ResizedChunks.front())) |
1390 |
|
|
1391 |
/** @brief Create new RIFF file. |
/** @brief Create new RIFF file. |
1392 |
* |
* |
1393 |
* Use this constructor if you want to create a new RIFF file completely |
* Use this constructor if you want to create a new RIFF file completely |
1403 |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1404 |
*/ |
*/ |
1405 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) : List(this) { |
1406 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1407 |
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1408 |
#if defined(WIN32) |
#if defined(WIN32) |
1409 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1410 |
#else |
#else |
1429 |
std::cout << "File::File("<<path<<")" << std::endl; |
std::cout << "File::File("<<path<<")" << std::endl; |
1430 |
#endif // DEBUG |
#endif // DEBUG |
1431 |
bEndianNative = true; |
bEndianNative = true; |
1432 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1433 |
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1434 |
#if POSIX |
#if POSIX |
1435 |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1436 |
if (hFileRead <= 0) { |
if (hFileRead <= 0) { |
1616 |
|
|
1617 |
// 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) |
1618 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1619 |
for (std::set<Chunk*>::const_iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
std::set<Chunk*>* resizedChunks = _GET_RESIZED_CHUNKS(); |
1620 |
|
for (std::set<Chunk*>::const_iterator iter = resizedChunks->begin(), end = resizedChunks->end(); iter != end; ++iter) { |
1621 |
if ((*iter)->GetNewSize() == 0) { |
if ((*iter)->GetNewSize() == 0) { |
1622 |
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)); |
1623 |
} |
} |
1673 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1674 |
|
|
1675 |
// forget all resized chunks |
// forget all resized chunks |
1676 |
ResizedChunks.clear(); |
resizedChunks->clear(); |
1677 |
} |
} |
1678 |
|
|
1679 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
1730 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1731 |
|
|
1732 |
// forget all resized chunks |
// forget all resized chunks |
1733 |
ResizedChunks.clear(); |
_GET_RESIZED_CHUNKS()->clear(); |
1734 |
|
|
1735 |
#if POSIX |
#if POSIX |
1736 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
1775 |
#endif // POSIX |
#endif // POSIX |
1776 |
DeleteChunkList(); |
DeleteChunkList(); |
1777 |
pFile = NULL; |
pFile = NULL; |
1778 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1779 |
|
delete _GET_RESIZED_CHUNKS(); |
1780 |
} |
} |
1781 |
|
|
1782 |
void File::LogAsResized(Chunk* pResizedChunk) { |
void File::LogAsResized(Chunk* pResizedChunk) { |
1783 |
ResizedChunks.insert(pResizedChunk); |
_GET_RESIZED_CHUNKS()->insert(pResizedChunk); |
1784 |
} |
} |
1785 |
|
|
1786 |
void File::UnlogResized(Chunk* pResizedChunk) { |
void File::UnlogResized(Chunk* pResizedChunk) { |
1787 |
ResizedChunks.erase(pResizedChunk); |
_GET_RESIZED_CHUNKS()->erase(pResizedChunk); |
1788 |
} |
} |
1789 |
|
|
1790 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |