54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
// *************** progress_t *************** |
58 |
|
// * |
59 |
|
|
60 |
|
progress_t::progress_t() { |
61 |
|
callback = NULL; |
62 |
|
custom = NULL; |
63 |
|
__range_min = 0.0f; |
64 |
|
__range_max = 1.0f; |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
// *************** Chunk ************** |
// *************** Chunk ************** |
70 |
// * |
// * |
71 |
|
|
851 |
* chunk should be written to |
* chunk should be written to |
852 |
* @param ulCurrentDataOffset - offset of current (old) data within |
* @param ulCurrentDataOffset - offset of current (old) data within |
853 |
* the file |
* the file |
854 |
|
* @param pProgress - optional: callback function for progress notification |
855 |
* @returns new write position in the "physical" file, that is |
* @returns new write position in the "physical" file, that is |
856 |
* \a ulWritePos incremented by this chunk's new size |
* \a ulWritePos incremented by this chunk's new size |
857 |
* (including its header size of course) |
* (including its header size of course) |
858 |
*/ |
*/ |
859 |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset, progress_t* pProgress) { |
860 |
const unsigned long ulOriginalPos = ulWritePos; |
const unsigned long ulOriginalPos = ulWritePos; |
861 |
ulWritePos += CHUNK_HEADER_SIZE; |
ulWritePos += CHUNK_HEADER_SIZE; |
862 |
|
|
922 |
CurrentChunkSize = NewChunkSize; |
CurrentChunkSize = NewChunkSize; |
923 |
WriteHeader(ulOriginalPos); |
WriteHeader(ulOriginalPos); |
924 |
|
|
925 |
|
__notify_progress(pProgress, 1.0); // notify done |
926 |
|
|
927 |
// update chunk's position pointers |
// update chunk's position pointers |
928 |
ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE; |
ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE; |
929 |
ulPos = 0; |
ulPos = 0; |
1351 |
#endif // POSIX |
#endif // POSIX |
1352 |
} |
} |
1353 |
|
|
1354 |
void List::LoadSubChunks() { |
void List::LoadSubChunks(progress_t* pProgress) { |
1355 |
#if DEBUG |
#if DEBUG |
1356 |
std::cout << "List::LoadSubChunks()"; |
std::cout << "List::LoadSubChunks()"; |
1357 |
#endif // DEBUG |
#endif // DEBUG |
1386 |
} |
} |
1387 |
SetPos(uiOriginalPos); // restore position before this call |
SetPos(uiOriginalPos); // restore position before this call |
1388 |
} |
} |
1389 |
|
__notify_progress(pProgress, 1.0); // notify done |
1390 |
} |
} |
1391 |
|
|
1392 |
void List::LoadSubChunksRecursively() { |
void List::LoadSubChunksRecursively(progress_t* pProgress) { |
1393 |
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList()) |
const int n = CountSubLists(); |
1394 |
pList->LoadSubChunksRecursively(); |
int i = 0; |
1395 |
|
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) { |
1396 |
|
// divide local progress into subprogress |
1397 |
|
progress_t subprogress; |
1398 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1399 |
|
// do the actual work |
1400 |
|
pList->LoadSubChunksRecursively(&subprogress); |
1401 |
|
} |
1402 |
|
__notify_progress(pProgress, 1.0); // notify done |
1403 |
} |
} |
1404 |
|
|
1405 |
/** @brief Write list chunk persistently e.g. to disk. |
/** @brief Write list chunk persistently e.g. to disk. |
1412 |
* list chunk should be written to |
* list chunk should be written to |
1413 |
* @param ulCurrentDataOffset - offset of current (old) data within |
* @param ulCurrentDataOffset - offset of current (old) data within |
1414 |
* the file |
* the file |
1415 |
|
* @param pProgress - optional: callback function for progress notification |
1416 |
* @returns new write position in the "physical" file, that is |
* @returns new write position in the "physical" file, that is |
1417 |
* \a ulWritePos incremented by this list chunk's new size |
* \a ulWritePos incremented by this list chunk's new size |
1418 |
* (including its header size of course) |
* (including its header size of course) |
1419 |
*/ |
*/ |
1420 |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset, progress_t* pProgress) { |
1421 |
const unsigned long ulOriginalPos = ulWritePos; |
const unsigned long ulOriginalPos = ulWritePos; |
1422 |
ulWritePos += LIST_HEADER_SIZE; |
ulWritePos += LIST_HEADER_SIZE; |
1423 |
|
|
1426 |
|
|
1427 |
// write all subchunks (including sub list chunks) recursively |
// write all subchunks (including sub list chunks) recursively |
1428 |
if (pSubChunks) { |
if (pSubChunks) { |
1429 |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
int i = 0; |
1430 |
ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset); |
const int n = pSubChunks->size(); |
1431 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) { |
1432 |
|
// divide local progress into subprogress for loading current Instrument |
1433 |
|
progress_t subprogress; |
1434 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1435 |
|
// do the actual work |
1436 |
|
ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset, &subprogress); |
1437 |
} |
} |
1438 |
} |
} |
1439 |
|
|
1444 |
// offset of this list chunk in new written file may have changed |
// offset of this list chunk in new written file may have changed |
1445 |
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
1446 |
|
|
1447 |
|
__notify_progress(pProgress, 1.0); // notify done |
1448 |
|
|
1449 |
return ulWritePos; |
return ulWritePos; |
1450 |
} |
} |
1451 |
|
|
1777 |
* than it will have at the end of the saving process, in case chunks |
* than it will have at the end of the saving process, in case chunks |
1778 |
* were grown. |
* were grown. |
1779 |
* |
* |
1780 |
|
* @param pProgress - optional: callback function for progress notification |
1781 |
* @throws RIFF::Exception if there is an empty chunk or empty list |
* @throws RIFF::Exception if there is an empty chunk or empty list |
1782 |
* chunk or any kind of IO error occured |
* chunk or any kind of IO error occured |
1783 |
*/ |
*/ |
1784 |
void File::Save() { |
void File::Save(progress_t* pProgress) { |
1785 |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1786 |
if (Layout == layout_flat) |
if (Layout == layout_flat) |
1787 |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1788 |
|
|
1789 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1790 |
LoadSubChunksRecursively(); |
{ |
1791 |
|
// divide progress into subprogress |
1792 |
|
progress_t subprogress; |
1793 |
|
__divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress |
1794 |
|
// do the actual work |
1795 |
|
LoadSubChunksRecursively(&subprogress); |
1796 |
|
// notify subprogress done |
1797 |
|
__notify_progress(&subprogress, 1.f); |
1798 |
|
} |
1799 |
|
|
1800 |
// reopen file in write mode |
// reopen file in write mode |
1801 |
SetMode(stream_mode_read_write); |
SetMode(stream_mode_read_write); |
1823 |
|
|
1824 |
// if there are positive size changes... |
// if there are positive size changes... |
1825 |
if (ulPositiveSizeDiff > 0) { |
if (ulPositiveSizeDiff > 0) { |
1826 |
|
// divide progress into subprogress |
1827 |
|
progress_t subprogress; |
1828 |
|
__divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress |
1829 |
|
|
1830 |
// ... we enlarge this file first ... |
// ... we enlarge this file first ... |
1831 |
ulWorkingFileSize += ulPositiveSizeDiff; |
ulWorkingFileSize += ulPositiveSizeDiff; |
1832 |
ResizeFile(ulWorkingFileSize); |
ResizeFile(ulWorkingFileSize); |
1838 |
#else |
#else |
1839 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1840 |
#endif |
#endif |
1841 |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
for (unsigned long ulPos = ulFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) { |
1842 |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1843 |
ulPos -= iBytesMoved; |
ulPos -= iBytesMoved; |
1844 |
#if POSIX |
#if POSIX |
1857 |
fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1858 |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
1859 |
#endif |
#endif |
1860 |
|
if (!(iNotif % 8) && iBytesMoved > 0) |
1861 |
|
__notify_progress(&subprogress, float(ulFileSize - ulPos) / float(ulFileSize)); |
1862 |
} |
} |
1863 |
delete[] pCopyBuffer; |
delete[] pCopyBuffer; |
1864 |
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
1865 |
|
|
1866 |
|
__notify_progress(&subprogress, 1.f); // notify subprogress done |
1867 |
} |
} |
1868 |
|
|
1869 |
// rebuild / rewrite complete RIFF tree |
// rebuild / rewrite complete RIFF tree ... |
1870 |
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
|
1871 |
|
// divide progress into subprogress |
1872 |
|
progress_t subprogress; |
1873 |
|
__divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress |
1874 |
|
// do the actual work |
1875 |
|
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff, &subprogress); |
1876 |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1877 |
|
// notify subprogress done |
1878 |
|
__notify_progress(&subprogress, 1.f); |
1879 |
|
|
1880 |
// resize file to the final size |
// resize file to the final size |
1881 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1882 |
|
|
1883 |
// forget all resized chunks |
// forget all resized chunks |
1884 |
resizedChunks->clear(); |
resizedChunks->clear(); |
1885 |
|
|
1886 |
|
__notify_progress(pProgress, 1.0); // notify done |
1887 |
} |
} |
1888 |
|
|
1889 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
1898 |
* the new file (given by \a path) afterwards. |
* the new file (given by \a path) afterwards. |
1899 |
* |
* |
1900 |
* @param path - path and file name where everything should be written to |
* @param path - path and file name where everything should be written to |
1901 |
|
* @param pProgress - optional: callback function for progress notification |
1902 |
*/ |
*/ |
1903 |
void File::Save(const String& path) { |
void File::Save(const String& path, progress_t* pProgress) { |
1904 |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
1905 |
|
|
1906 |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1908 |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1909 |
|
|
1910 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1911 |
LoadSubChunksRecursively(); |
{ |
1912 |
|
// divide progress into subprogress |
1913 |
|
progress_t subprogress; |
1914 |
|
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress |
1915 |
|
// do the actual work |
1916 |
|
LoadSubChunksRecursively(&subprogress); |
1917 |
|
// notify subprogress done |
1918 |
|
__notify_progress(&subprogress, 1.f); |
1919 |
|
} |
1920 |
|
|
1921 |
if (!bIsNewFile) SetMode(stream_mode_read); |
if (!bIsNewFile) SetMode(stream_mode_read); |
1922 |
// open the other (new) file for writing and truncate it to zero size |
// open the other (new) file for writing and truncate it to zero size |
1947 |
Mode = stream_mode_read_write; |
Mode = stream_mode_read_write; |
1948 |
|
|
1949 |
// write complete RIFF tree to the other (new) file |
// write complete RIFF tree to the other (new) file |
1950 |
unsigned long ulTotalSize = WriteChunk(0, 0); |
unsigned long ulTotalSize; |
1951 |
|
{ |
1952 |
|
// divide progress into subprogress |
1953 |
|
progress_t subprogress; |
1954 |
|
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress |
1955 |
|
// do the actual work |
1956 |
|
ulTotalSize = WriteChunk(0, 0, &subprogress); |
1957 |
|
// notify subprogress done |
1958 |
|
__notify_progress(&subprogress, 1.f); |
1959 |
|
} |
1960 |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1961 |
|
|
1962 |
// resize file to the final size (if the file was originally larger) |
// resize file to the final size (if the file was originally larger) |
1979 |
bIsNewFile = false; |
bIsNewFile = false; |
1980 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1981 |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
1982 |
|
|
1983 |
|
__notify_progress(pProgress, 1.0); // notify done |
1984 |
} |
} |
1985 |
|
|
1986 |
void File::ResizeFile(unsigned long ulNewSize) { |
void File::ResizeFile(unsigned long ulNewSize) { |