2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2007 by Christian Schoenebeck * |
* Copyright (C) 2003-2014 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
|
#include <algorithm> |
25 |
|
#include <set> |
26 |
#include <string.h> |
#include <string.h> |
27 |
|
|
28 |
#include "RIFF.h" |
#include "RIFF.h" |
29 |
|
|
30 |
#include "helper.h" |
#include "helper.h" |
31 |
|
|
32 |
|
#if POSIX |
33 |
|
# include <errno.h> |
34 |
|
#endif |
35 |
|
|
36 |
namespace RIFF { |
namespace RIFF { |
37 |
|
|
38 |
// *************** Internal functions ************** |
// *************** Internal functions ************** |
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 |
|
|
76 |
ulPos = 0; |
ulPos = 0; |
77 |
pParent = NULL; |
pParent = NULL; |
78 |
pChunkData = NULL; |
pChunkData = NULL; |
79 |
|
CurrentChunkSize = 0; |
80 |
|
NewChunkSize = 0; |
81 |
ulChunkDataSize = 0; |
ulChunkDataSize = 0; |
82 |
ChunkID = CHUNK_ID_RIFF; |
ChunkID = CHUNK_ID_RIFF; |
83 |
this->pFile = pFile; |
this->pFile = pFile; |
92 |
pParent = Parent; |
pParent = Parent; |
93 |
ulPos = 0; |
ulPos = 0; |
94 |
pChunkData = NULL; |
pChunkData = NULL; |
95 |
|
CurrentChunkSize = 0; |
96 |
|
NewChunkSize = 0; |
97 |
ulChunkDataSize = 0; |
ulChunkDataSize = 0; |
98 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
99 |
} |
} |
104 |
this->pParent = pParent; |
this->pParent = pParent; |
105 |
ulPos = 0; |
ulPos = 0; |
106 |
pChunkData = NULL; |
pChunkData = NULL; |
|
ulChunkDataSize = 0; |
|
107 |
ChunkID = uiChunkID; |
ChunkID = uiChunkID; |
108 |
|
ulChunkDataSize = 0; |
109 |
CurrentChunkSize = 0; |
CurrentChunkSize = 0; |
110 |
NewChunkSize = uiBodySize; |
NewChunkSize = uiBodySize; |
111 |
} |
} |
112 |
|
|
113 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
114 |
pFile->UnlogResized(this); |
if (pFile) pFile->UnlogResized(this); |
115 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
116 |
} |
} |
117 |
|
|
119 |
#if DEBUG |
#if DEBUG |
120 |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
121 |
#endif // DEBUG |
#endif // DEBUG |
122 |
|
ChunkID = 0; |
123 |
|
NewChunkSize = CurrentChunkSize = 0; |
124 |
#if POSIX |
#if POSIX |
125 |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
126 |
read(pFile->hFileRead, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
151 |
} |
} |
152 |
#if DEBUG |
#if DEBUG |
153 |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
154 |
std::cout << "ckSize=" << ChunkSize << " "; |
std::cout << "ckSize=" << CurrentChunkSize << " "; |
155 |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; |
156 |
#endif // DEBUG |
#endif // DEBUG |
157 |
NewChunkSize = CurrentChunkSize; |
NewChunkSize = CurrentChunkSize; |
158 |
} |
} |
248 |
#if DEBUG |
#if DEBUG |
249 |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
250 |
#endif // DEBUG |
#endif // DEBUG |
251 |
return CurrentChunkSize - ulPos; |
return (CurrentChunkSize > ulPos) ? CurrentChunkSize - ulPos : 0; |
252 |
} |
} |
253 |
|
|
254 |
/** |
/** |
297 |
#if DEBUG |
#if DEBUG |
298 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
299 |
#endif // DEBUG |
#endif // DEBUG |
300 |
|
//if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
301 |
if (ulPos >= CurrentChunkSize) return 0; |
if (ulPos >= CurrentChunkSize) return 0; |
302 |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
303 |
#if POSIX |
#if POSIX |
304 |
if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
305 |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
ssize_t readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
306 |
if (readWords < 1) return 0; |
if (readWords < 1) { |
307 |
|
#if DEBUG |
308 |
|
std::cerr << "POSIX read() failed: " << strerror(errno) << std::endl << std::flush; |
309 |
|
#endif // DEBUG |
310 |
|
return 0; |
311 |
|
} |
312 |
readWords /= WordSize; |
readWords /= WordSize; |
313 |
#elif defined(WIN32) |
#elif defined(WIN32) |
314 |
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
318 |
readWords /= WordSize; |
readWords /= WordSize; |
319 |
#else // standard C functions |
#else // standard C functions |
320 |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
321 |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
size_t readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
322 |
#endif // POSIX |
#endif // POSIX |
323 |
if (!pFile->bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
324 |
switch (WordSize) { |
switch (WordSize) { |
616 |
} |
} |
617 |
|
|
618 |
/** |
/** |
619 |
|
* Reads a null-padded string of size characters and copies it |
620 |
|
* into the string \a s. The position within the chunk will |
621 |
|
* automatically be incremented. |
622 |
|
* |
623 |
|
* @param s destination string |
624 |
|
* @param size number of characters to read |
625 |
|
* @throws RIFF::Exception if an error occured or less than |
626 |
|
* \a size characters could be read! |
627 |
|
*/ |
628 |
|
void Chunk::ReadString(String& s, int size) { |
629 |
|
char* buf = new char[size]; |
630 |
|
ReadSceptical(buf, 1, size); |
631 |
|
s.assign(buf, std::find(buf, buf + size, '\0')); |
632 |
|
delete[] buf; |
633 |
|
} |
634 |
|
|
635 |
|
/** |
636 |
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
637 |
* buffer pointed by \a pData to the chunk's body, directly to the |
* buffer pointed by \a pData to the chunk's body, directly to the |
638 |
* actual "physical" file. The position within the chunk will |
* actual "physical" file. The position within the chunk will |
772 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
773 |
*/ |
*/ |
774 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
775 |
if (!pChunkData && pFile->Filename != "") { |
if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) { |
776 |
#if POSIX |
#if POSIX |
777 |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
778 |
#elif defined(WIN32) |
#elif defined(WIN32) |
856 |
* chunk should be written to |
* chunk should be written to |
857 |
* @param ulCurrentDataOffset - offset of current (old) data within |
* @param ulCurrentDataOffset - offset of current (old) data within |
858 |
* the file |
* the file |
859 |
|
* @param pProgress - optional: callback function for progress notification |
860 |
* @returns new write position in the "physical" file, that is |
* @returns new write position in the "physical" file, that is |
861 |
* \a ulWritePos incremented by this chunk's new size |
* \a ulWritePos incremented by this chunk's new size |
862 |
* (including its header size of course) |
* (including its header size of course) |
863 |
*/ |
*/ |
864 |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset, progress_t* pProgress) { |
865 |
const unsigned long ulOriginalPos = ulWritePos; |
const unsigned long ulOriginalPos = ulWritePos; |
866 |
ulWritePos += CHUNK_HEADER_SIZE; |
ulWritePos += CHUNK_HEADER_SIZE; |
867 |
|
|
900 |
#else |
#else |
901 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
902 |
#endif |
#endif |
903 |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
for (unsigned long ulOffset = 0; ulToMove > 0 && iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
904 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
905 |
#if POSIX |
#if POSIX |
906 |
lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
927 |
CurrentChunkSize = NewChunkSize; |
CurrentChunkSize = NewChunkSize; |
928 |
WriteHeader(ulOriginalPos); |
WriteHeader(ulOriginalPos); |
929 |
|
|
930 |
|
__notify_progress(pProgress, 1.0); // notify done |
931 |
|
|
932 |
// update chunk's position pointers |
// update chunk's position pointers |
933 |
ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE; |
ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE; |
934 |
ulPos = 0; |
ulPos = 0; |
992 |
#if DEBUG |
#if DEBUG |
993 |
std::cout << "List::~List()" << std::endl; |
std::cout << "List::~List()" << std::endl; |
994 |
#endif // DEBUG |
#endif // DEBUG |
995 |
|
DeleteChunkList(); |
996 |
|
} |
997 |
|
|
998 |
|
void List::DeleteChunkList() { |
999 |
if (pSubChunks) { |
if (pSubChunks) { |
1000 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1001 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1004 |
iter++; |
iter++; |
1005 |
} |
} |
1006 |
delete pSubChunks; |
delete pSubChunks; |
1007 |
|
pSubChunks = NULL; |
1008 |
|
} |
1009 |
|
if (pSubChunksMap) { |
1010 |
|
delete pSubChunksMap; |
1011 |
|
pSubChunksMap = NULL; |
1012 |
} |
} |
|
if (pSubChunksMap) delete pSubChunksMap; |
|
1013 |
} |
} |
1014 |
|
|
1015 |
/** |
/** |
1215 |
return pNewChunk; |
return pNewChunk; |
1216 |
} |
} |
1217 |
|
|
1218 |
/** @brief Moves a sub chunk. |
/** @brief Moves a sub chunk witin this list. |
1219 |
* |
* |
1220 |
* Moves a sub chunk from one position in a list to another |
* Moves a sub chunk from one position in this list to another |
1221 |
* position in the same list. The pSrc chunk is placed before the |
* position in the same list. The pSrc chunk is placed before the |
1222 |
* pDst chunk. |
* pDst chunk. |
1223 |
* |
* |
1233 |
pSubChunks->insert(iter, pSrc); |
pSubChunks->insert(iter, pSrc); |
1234 |
} |
} |
1235 |
|
|
1236 |
|
/** @brief Moves a sub chunk from this list to another list. |
1237 |
|
* |
1238 |
|
* Moves a sub chunk from this list list to the end of another |
1239 |
|
* list. |
1240 |
|
* |
1241 |
|
* @param pSrc - sub chunk to be moved |
1242 |
|
* @param pDst - destination list where the chunk shall be moved to |
1243 |
|
*/ |
1244 |
|
void List::MoveSubChunk(Chunk* pSrc, List* pNewParent) { |
1245 |
|
if (pNewParent == this || !pNewParent) return; |
1246 |
|
if (!pSubChunks) LoadSubChunks(); |
1247 |
|
if (!pNewParent->pSubChunks) pNewParent->LoadSubChunks(); |
1248 |
|
pSubChunks->remove(pSrc); |
1249 |
|
pNewParent->pSubChunks->push_back(pSrc); |
1250 |
|
// update chunk id map of this List |
1251 |
|
if ((*pSubChunksMap)[pSrc->GetChunkID()] == pSrc) { |
1252 |
|
pSubChunksMap->erase(pSrc->GetChunkID()); |
1253 |
|
// try to find another chunk of the same chunk ID |
1254 |
|
ChunkList::iterator iter = pSubChunks->begin(); |
1255 |
|
ChunkList::iterator end = pSubChunks->end(); |
1256 |
|
for (; iter != end; ++iter) { |
1257 |
|
if ((*iter)->GetChunkID() == pSrc->GetChunkID()) { |
1258 |
|
(*pSubChunksMap)[pSrc->GetChunkID()] = *iter; |
1259 |
|
break; // we're done, stop search |
1260 |
|
} |
1261 |
|
} |
1262 |
|
} |
1263 |
|
// update chunk id map of other list |
1264 |
|
if (!(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()]) |
1265 |
|
(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()] = pSrc; |
1266 |
|
} |
1267 |
|
|
1268 |
/** @brief Creates a new list sub chunk. |
/** @brief Creates a new list sub chunk. |
1269 |
* |
* |
1270 |
* 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 |
1317 |
std::cout << "List::Readheader(ulong) "; |
std::cout << "List::Readheader(ulong) "; |
1318 |
#endif // DEBUG |
#endif // DEBUG |
1319 |
Chunk::ReadHeader(fPos); |
Chunk::ReadHeader(fPos); |
1320 |
|
if (CurrentChunkSize < 4) return; |
1321 |
NewChunkSize = CurrentChunkSize -= 4; |
NewChunkSize = CurrentChunkSize -= 4; |
1322 |
#if POSIX |
#if POSIX |
1323 |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1356 |
#endif // POSIX |
#endif // POSIX |
1357 |
} |
} |
1358 |
|
|
1359 |
void List::LoadSubChunks() { |
void List::LoadSubChunks(progress_t* pProgress) { |
1360 |
#if DEBUG |
#if DEBUG |
1361 |
std::cout << "List::LoadSubChunks()"; |
std::cout << "List::LoadSubChunks()"; |
1362 |
#endif // DEBUG |
#endif // DEBUG |
1391 |
} |
} |
1392 |
SetPos(uiOriginalPos); // restore position before this call |
SetPos(uiOriginalPos); // restore position before this call |
1393 |
} |
} |
1394 |
|
__notify_progress(pProgress, 1.0); // notify done |
1395 |
} |
} |
1396 |
|
|
1397 |
void List::LoadSubChunksRecursively() { |
void List::LoadSubChunksRecursively(progress_t* pProgress) { |
1398 |
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList()) |
const int n = CountSubLists(); |
1399 |
pList->LoadSubChunksRecursively(); |
int i = 0; |
1400 |
|
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) { |
1401 |
|
// divide local progress into subprogress |
1402 |
|
progress_t subprogress; |
1403 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1404 |
|
// do the actual work |
1405 |
|
pList->LoadSubChunksRecursively(&subprogress); |
1406 |
|
} |
1407 |
|
__notify_progress(pProgress, 1.0); // notify done |
1408 |
} |
} |
1409 |
|
|
1410 |
/** @brief Write list chunk persistently e.g. to disk. |
/** @brief Write list chunk persistently e.g. to disk. |
1417 |
* list chunk should be written to |
* list chunk should be written to |
1418 |
* @param ulCurrentDataOffset - offset of current (old) data within |
* @param ulCurrentDataOffset - offset of current (old) data within |
1419 |
* the file |
* the file |
1420 |
|
* @param pProgress - optional: callback function for progress notification |
1421 |
* @returns new write position in the "physical" file, that is |
* @returns new write position in the "physical" file, that is |
1422 |
* \a ulWritePos incremented by this list chunk's new size |
* \a ulWritePos incremented by this list chunk's new size |
1423 |
* (including its header size of course) |
* (including its header size of course) |
1424 |
*/ |
*/ |
1425 |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset, progress_t* pProgress) { |
1426 |
const unsigned long ulOriginalPos = ulWritePos; |
const unsigned long ulOriginalPos = ulWritePos; |
1427 |
ulWritePos += LIST_HEADER_SIZE; |
ulWritePos += LIST_HEADER_SIZE; |
1428 |
|
|
1431 |
|
|
1432 |
// write all subchunks (including sub list chunks) recursively |
// write all subchunks (including sub list chunks) recursively |
1433 |
if (pSubChunks) { |
if (pSubChunks) { |
1434 |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
int i = 0; |
1435 |
ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset); |
const int n = pSubChunks->size(); |
1436 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) { |
1437 |
|
// divide local progress into subprogress for loading current Instrument |
1438 |
|
progress_t subprogress; |
1439 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1440 |
|
// do the actual work |
1441 |
|
ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset, &subprogress); |
1442 |
} |
} |
1443 |
} |
} |
1444 |
|
|
1449 |
// offset of this list chunk in new written file may have changed |
// offset of this list chunk in new written file may have changed |
1450 |
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
1451 |
|
|
1452 |
|
__notify_progress(pProgress, 1.0); // notify done |
1453 |
|
|
1454 |
return ulWritePos; |
return ulWritePos; |
1455 |
} |
} |
1456 |
|
|
1475 |
// *************** File *************** |
// *************** File *************** |
1476 |
// * |
// * |
1477 |
|
|
1478 |
|
//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 |
1479 |
|
#define _GET_RESIZED_CHUNKS() \ |
1480 |
|
(reinterpret_cast<std::set<Chunk*>*>(ResizedChunks.front())) |
1481 |
|
|
1482 |
/** @brief Create new RIFF file. |
/** @brief Create new RIFF file. |
1483 |
* |
* |
1484 |
* 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 |
1493 |
* @param FileType - four-byte identifier of the RIFF file type |
* @param FileType - four-byte identifier of the RIFF file type |
1494 |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1495 |
*/ |
*/ |
1496 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) |
1497 |
|
: List(this), bIsNewFile(true), Layout(layout_standard) |
1498 |
|
{ |
1499 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1500 |
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1501 |
#if defined(WIN32) |
#if defined(WIN32) |
1502 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1503 |
#else |
#else |
1517 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occured while trying to load the |
1518 |
* given RIFF file |
* given RIFF file |
1519 |
*/ |
*/ |
1520 |
File::File(const String& path) : List(this), Filename(path) { |
File::File(const String& path) |
1521 |
#if DEBUG |
: List(this), Filename(path), bIsNewFile(false), Layout(layout_standard) |
1522 |
std::cout << "File::File("<<path<<")" << std::endl; |
{ |
1523 |
#endif // DEBUG |
#if DEBUG |
1524 |
|
std::cout << "File::File("<<path<<")" << std::endl; |
1525 |
|
#endif // DEBUG |
1526 |
bEndianNative = true; |
bEndianNative = true; |
1527 |
|
try { |
1528 |
|
__openExistingFile(path); |
1529 |
|
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1530 |
|
throw RIFF::Exception("Not a RIFF file"); |
1531 |
|
} |
1532 |
|
} |
1533 |
|
catch (...) { |
1534 |
|
Cleanup(); |
1535 |
|
throw; |
1536 |
|
} |
1537 |
|
} |
1538 |
|
|
1539 |
|
/** @brief Load existing RIFF-like file. |
1540 |
|
* |
1541 |
|
* Loads an existing file, which is not a "real" RIFF file, but similar to |
1542 |
|
* an ordinary RIFF file. |
1543 |
|
* |
1544 |
|
* A "real" RIFF file contains at top level a List chunk either with chunk |
1545 |
|
* ID "RIFF" or "RIFX". The simple constructor above expects this to be |
1546 |
|
* case, and if it finds the toplevel List chunk to have another chunk ID |
1547 |
|
* than one of those two expected ones, it would throw an Exception and |
1548 |
|
* would refuse to load the file accordingly. |
1549 |
|
* |
1550 |
|
* Since there are however a lot of file formats which use the same simple |
1551 |
|
* principles of the RIFF format, with another toplevel List chunk ID |
1552 |
|
* though, you can use this alternative constructor here to be able to load |
1553 |
|
* and handle those files in the same way as you would do with "real" RIFF |
1554 |
|
* files. |
1555 |
|
* |
1556 |
|
* @param path - path and file name of the RIFF-alike file to be opened |
1557 |
|
* @param FileType - expected toplevel List chunk ID (this is the very |
1558 |
|
* first chunk found in the file) |
1559 |
|
* @param Endian - whether the file uses little endian or big endian layout |
1560 |
|
* @param layout - general file structure type |
1561 |
|
* @throws RIFF::Exception if error occured while trying to load the |
1562 |
|
* given RIFF-alike file |
1563 |
|
*/ |
1564 |
|
File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout) |
1565 |
|
: List(this), Filename(path), bIsNewFile(false), Layout(layout) |
1566 |
|
{ |
1567 |
|
SetByteOrder(Endian); |
1568 |
|
try { |
1569 |
|
__openExistingFile(path, &FileType); |
1570 |
|
} |
1571 |
|
catch (...) { |
1572 |
|
Cleanup(); |
1573 |
|
throw; |
1574 |
|
} |
1575 |
|
} |
1576 |
|
|
1577 |
|
/** |
1578 |
|
* Opens an already existing RIFF file or RIFF-alike file. This method |
1579 |
|
* shall only be called once (in a File class constructor). |
1580 |
|
* |
1581 |
|
* @param path - path and file name of the RIFF file or RIFF-alike file to |
1582 |
|
* be opened |
1583 |
|
* @param FileType - (optional) expected chunk ID of first chunk in file |
1584 |
|
* @throws RIFF::Exception if error occured while trying to load the |
1585 |
|
* given RIFF file or RIFF-alike file |
1586 |
|
*/ |
1587 |
|
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
1588 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1589 |
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1590 |
#if POSIX |
#if POSIX |
1591 |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1592 |
if (hFileRead <= 0) { |
if (hFileRead == -1) { |
1593 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1594 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
String sError = strerror(errno); |
1595 |
|
throw RIFF::Exception("Can't open \"" + path + "\": " + sError); |
1596 |
} |
} |
1597 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1598 |
hFileRead = hFileWrite = CreateFile( |
hFileRead = hFileWrite = CreateFile( |
1611 |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1612 |
#endif // POSIX |
#endif // POSIX |
1613 |
Mode = stream_mode_read; |
Mode = stream_mode_read; |
1614 |
ulStartPos = RIFF_HEADER_SIZE; |
switch (Layout) { |
1615 |
ReadHeader(0); |
case layout_standard: // this is a normal RIFF file |
1616 |
if (ChunkID != CHUNK_ID_RIFF) { |
ulStartPos = RIFF_HEADER_SIZE; |
1617 |
throw RIFF::Exception("Not a RIFF file"); |
ReadHeader(0); |
1618 |
|
if (FileType && ChunkID != *FileType) |
1619 |
|
throw RIFF::Exception("Invalid file container ID"); |
1620 |
|
break; |
1621 |
|
case layout_flat: // non-standard RIFF-alike file |
1622 |
|
ulStartPos = 0; |
1623 |
|
NewChunkSize = CurrentChunkSize = GetFileSize(); |
1624 |
|
if (FileType) { |
1625 |
|
uint32_t ckid; |
1626 |
|
if (Read(&ckid, 4, 1) != 4) { |
1627 |
|
throw RIFF::Exception("Invalid file header ID (premature end of header)"); |
1628 |
|
} else if (ckid != *FileType) { |
1629 |
|
String s = " (expected '" + convertToString(*FileType) + "' but got '" + convertToString(ckid) + "')"; |
1630 |
|
throw RIFF::Exception("Invalid file header ID" + s); |
1631 |
|
} |
1632 |
|
SetPos(0); // reset to first byte of file |
1633 |
|
} |
1634 |
|
LoadSubChunks(); |
1635 |
|
break; |
1636 |
} |
} |
1637 |
} |
} |
1638 |
|
|
1639 |
String File::GetFileName() { |
String File::GetFileName() { |
1640 |
return Filename; |
return Filename; |
1641 |
} |
} |
1642 |
|
|
1643 |
|
void File::SetFileName(const String& path) { |
1644 |
|
Filename = path; |
1645 |
|
} |
1646 |
|
|
1647 |
stream_mode_t File::GetMode() { |
stream_mode_t File::GetMode() { |
1648 |
return Mode; |
return Mode; |
1649 |
} |
} |
1650 |
|
|
1651 |
|
layout_t File::GetLayout() const { |
1652 |
|
return Layout; |
1653 |
|
} |
1654 |
|
|
1655 |
/** @brief Change file access mode. |
/** @brief Change file access mode. |
1656 |
* |
* |
1657 |
* Changes files access mode either to read-only mode or to read/write |
* Changes files access mode either to read-only mode or to read/write |
1669 |
#if POSIX |
#if POSIX |
1670 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1671 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1672 |
if (hFileRead < 0) { |
if (hFileRead == -1) { |
1673 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1674 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
String sError = strerror(errno); |
1675 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode: " + sError); |
1676 |
} |
} |
1677 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1678 |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1699 |
#if POSIX |
#if POSIX |
1700 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1701 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
1702 |
if (hFileRead < 0) { |
if (hFileRead == -1) { |
1703 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1704 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
String sError = strerror(errno); |
1705 |
|
throw Exception("Could not open file \"" + Filename + "\" in read+write mode: " + sError); |
1706 |
} |
} |
1707 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1708 |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1782 |
* 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 |
1783 |
* were grown. |
* were grown. |
1784 |
* |
* |
1785 |
|
* @param pProgress - optional: callback function for progress notification |
1786 |
* @throws RIFF::Exception if there is an empty chunk or empty list |
* @throws RIFF::Exception if there is an empty chunk or empty list |
1787 |
* chunk or any kind of IO error occured |
* chunk or any kind of IO error occured |
1788 |
*/ |
*/ |
1789 |
void File::Save() { |
void File::Save(progress_t* pProgress) { |
1790 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1791 |
|
if (Layout == layout_flat) |
1792 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1793 |
|
|
1794 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1795 |
LoadSubChunksRecursively(); |
{ |
1796 |
|
// divide progress into subprogress |
1797 |
|
progress_t subprogress; |
1798 |
|
__divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress |
1799 |
|
// do the actual work |
1800 |
|
LoadSubChunksRecursively(&subprogress); |
1801 |
|
// notify subprogress done |
1802 |
|
__notify_progress(&subprogress, 1.f); |
1803 |
|
} |
1804 |
|
|
1805 |
// reopen file in write mode |
// reopen file in write mode |
1806 |
SetMode(stream_mode_read_write); |
SetMode(stream_mode_read_write); |
1814 |
|
|
1815 |
// 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) |
1816 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1817 |
for (std::set<Chunk*>::const_iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
std::set<Chunk*>* resizedChunks = _GET_RESIZED_CHUNKS(); |
1818 |
|
for (std::set<Chunk*>::const_iterator iter = resizedChunks->begin(), end = resizedChunks->end(); iter != end; ++iter) { |
1819 |
if ((*iter)->GetNewSize() == 0) { |
if ((*iter)->GetNewSize() == 0) { |
1820 |
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)); |
1821 |
} |
} |
1822 |
unsigned long ulDiff = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2 - (*iter)->GetSize() - (*iter)->GetSize() % 2; |
unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; |
1823 |
if (ulDiff > 0) ulPositiveSizeDiff += ulDiff; |
unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2; |
1824 |
|
if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; |
1825 |
} |
} |
1826 |
|
|
1827 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |
1828 |
|
|
1829 |
// if there are positive size changes... |
// if there are positive size changes... |
1830 |
if (ulPositiveSizeDiff > 0) { |
if (ulPositiveSizeDiff > 0) { |
1831 |
|
// divide progress into subprogress |
1832 |
|
progress_t subprogress; |
1833 |
|
__divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress |
1834 |
|
|
1835 |
// ... we enlarge this file first ... |
// ... we enlarge this file first ... |
1836 |
ulWorkingFileSize += ulPositiveSizeDiff; |
ulWorkingFileSize += ulPositiveSizeDiff; |
1837 |
ResizeFile(ulWorkingFileSize); |
ResizeFile(ulWorkingFileSize); |
1843 |
#else |
#else |
1844 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1845 |
#endif |
#endif |
1846 |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
for (unsigned long ulPos = ulFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) { |
1847 |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1848 |
ulPos -= iBytesMoved; |
ulPos -= iBytesMoved; |
1849 |
#if POSIX |
#if POSIX |
1862 |
fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1863 |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
1864 |
#endif |
#endif |
1865 |
|
if (!(iNotif % 8) && iBytesMoved > 0) |
1866 |
|
__notify_progress(&subprogress, float(ulFileSize - ulPos) / float(ulFileSize)); |
1867 |
} |
} |
1868 |
delete[] pCopyBuffer; |
delete[] pCopyBuffer; |
1869 |
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"); |
1870 |
|
|
1871 |
|
__notify_progress(&subprogress, 1.f); // notify subprogress done |
1872 |
} |
} |
1873 |
|
|
1874 |
// rebuild / rewrite complete RIFF tree |
// rebuild / rewrite complete RIFF tree ... |
1875 |
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
|
1876 |
|
// divide progress into subprogress |
1877 |
|
progress_t subprogress; |
1878 |
|
__divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress |
1879 |
|
// do the actual work |
1880 |
|
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff, &subprogress); |
1881 |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1882 |
|
// notify subprogress done |
1883 |
|
__notify_progress(&subprogress, 1.f); |
1884 |
|
|
1885 |
// resize file to the final size |
// resize file to the final size |
1886 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1887 |
|
|
1888 |
// forget all resized chunks |
// forget all resized chunks |
1889 |
ResizedChunks.clear(); |
resizedChunks->clear(); |
1890 |
|
|
1891 |
|
__notify_progress(pProgress, 1.0); // notify done |
1892 |
} |
} |
1893 |
|
|
1894 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
1903 |
* the new file (given by \a path) afterwards. |
* the new file (given by \a path) afterwards. |
1904 |
* |
* |
1905 |
* @param path - path and file name where everything should be written to |
* @param path - path and file name where everything should be written to |
1906 |
|
* @param pProgress - optional: callback function for progress notification |
1907 |
*/ |
*/ |
1908 |
void File::Save(const String& path) { |
void File::Save(const String& path, progress_t* pProgress) { |
1909 |
//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 |
1910 |
|
|
1911 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1912 |
|
if (Layout == layout_flat) |
1913 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1914 |
|
|
1915 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1916 |
LoadSubChunksRecursively(); |
{ |
1917 |
|
// divide progress into subprogress |
1918 |
|
progress_t subprogress; |
1919 |
|
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress |
1920 |
|
// do the actual work |
1921 |
|
LoadSubChunksRecursively(&subprogress); |
1922 |
|
// notify subprogress done |
1923 |
|
__notify_progress(&subprogress, 1.f); |
1924 |
|
} |
1925 |
|
|
1926 |
if (Filename.length() > 0) SetMode(stream_mode_read); |
if (!bIsNewFile) SetMode(stream_mode_read); |
1927 |
// 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 |
1928 |
#if POSIX |
#if POSIX |
1929 |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
1930 |
if (hFileWrite < 0) { |
if (hFileWrite == -1) { |
1931 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
1932 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
String sError = strerror(errno); |
1933 |
|
throw Exception("Could not open file \"" + path + "\" for writing: " + sError); |
1934 |
} |
} |
1935 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1936 |
hFileWrite = CreateFile( |
hFileWrite = CreateFile( |
1952 |
Mode = stream_mode_read_write; |
Mode = stream_mode_read_write; |
1953 |
|
|
1954 |
// write complete RIFF tree to the other (new) file |
// write complete RIFF tree to the other (new) file |
1955 |
unsigned long ulTotalSize = WriteChunk(0, 0); |
unsigned long ulTotalSize; |
1956 |
|
{ |
1957 |
|
// divide progress into subprogress |
1958 |
|
progress_t subprogress; |
1959 |
|
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress |
1960 |
|
// do the actual work |
1961 |
|
ulTotalSize = WriteChunk(0, 0, &subprogress); |
1962 |
|
// notify subprogress done |
1963 |
|
__notify_progress(&subprogress, 1.f); |
1964 |
|
} |
1965 |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1966 |
|
|
1967 |
// resize file to the final size (if the file was originally larger) |
// resize file to the final size (if the file was originally larger) |
1968 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1969 |
|
|
1970 |
// forget all resized chunks |
// forget all resized chunks |
1971 |
ResizedChunks.clear(); |
_GET_RESIZED_CHUNKS()->clear(); |
1972 |
|
|
1973 |
#if POSIX |
#if POSIX |
1974 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
1981 |
|
|
1982 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1983 |
Filename = path; |
Filename = path; |
1984 |
|
bIsNewFile = false; |
1985 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1986 |
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. |
1987 |
|
|
1988 |
|
__notify_progress(pProgress, 1.0); // notify done |
1989 |
} |
} |
1990 |
|
|
1991 |
void File::ResizeFile(unsigned long ulNewSize) { |
void File::ResizeFile(unsigned long ulNewSize) { |
2007 |
#if DEBUG |
#if DEBUG |
2008 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
2009 |
#endif // DEBUG |
#endif // DEBUG |
2010 |
|
Cleanup(); |
2011 |
|
} |
2012 |
|
|
2013 |
|
/** |
2014 |
|
* Returns @c true if this file has been created new from scratch and |
2015 |
|
* has not been stored to disk yet. |
2016 |
|
*/ |
2017 |
|
bool File::IsNew() const { |
2018 |
|
return bIsNewFile; |
2019 |
|
} |
2020 |
|
|
2021 |
|
void File::Cleanup() { |
2022 |
#if POSIX |
#if POSIX |
2023 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
2024 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2026 |
#else |
#else |
2027 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
2028 |
#endif // POSIX |
#endif // POSIX |
2029 |
|
DeleteChunkList(); |
2030 |
|
pFile = NULL; |
2031 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
2032 |
|
delete _GET_RESIZED_CHUNKS(); |
2033 |
} |
} |
2034 |
|
|
2035 |
void File::LogAsResized(Chunk* pResizedChunk) { |
void File::LogAsResized(Chunk* pResizedChunk) { |
2036 |
ResizedChunks.insert(pResizedChunk); |
_GET_RESIZED_CHUNKS()->insert(pResizedChunk); |
2037 |
} |
} |
2038 |
|
|
2039 |
void File::UnlogResized(Chunk* pResizedChunk) { |
void File::UnlogResized(Chunk* pResizedChunk) { |
2040 |
ResizedChunks.erase(pResizedChunk); |
_GET_RESIZED_CHUNKS()->erase(pResizedChunk); |
2041 |
} |
} |
2042 |
|
|
2043 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |