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-2013 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" |
35 |
// * |
// * |
36 |
|
|
37 |
/// Returns a human readable path of the given chunk. |
/// Returns a human readable path of the given chunk. |
38 |
String __resolveChunkPath(Chunk* pCk) { |
static String __resolveChunkPath(Chunk* pCk) { |
39 |
String sPath; |
String sPath; |
40 |
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { |
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { |
41 |
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
60 |
ulPos = 0; |
ulPos = 0; |
61 |
pParent = NULL; |
pParent = NULL; |
62 |
pChunkData = NULL; |
pChunkData = NULL; |
63 |
|
CurrentChunkSize = 0; |
64 |
|
NewChunkSize = 0; |
65 |
ulChunkDataSize = 0; |
ulChunkDataSize = 0; |
66 |
ChunkID = CHUNK_ID_RIFF; |
ChunkID = CHUNK_ID_RIFF; |
67 |
this->pFile = pFile; |
this->pFile = pFile; |
76 |
pParent = Parent; |
pParent = Parent; |
77 |
ulPos = 0; |
ulPos = 0; |
78 |
pChunkData = NULL; |
pChunkData = NULL; |
79 |
|
CurrentChunkSize = 0; |
80 |
|
NewChunkSize = 0; |
81 |
ulChunkDataSize = 0; |
ulChunkDataSize = 0; |
82 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
83 |
} |
} |
88 |
this->pParent = pParent; |
this->pParent = pParent; |
89 |
ulPos = 0; |
ulPos = 0; |
90 |
pChunkData = NULL; |
pChunkData = NULL; |
|
ulChunkDataSize = 0; |
|
91 |
ChunkID = uiChunkID; |
ChunkID = uiChunkID; |
92 |
|
ulChunkDataSize = 0; |
93 |
CurrentChunkSize = 0; |
CurrentChunkSize = 0; |
94 |
NewChunkSize = uiBodySize; |
NewChunkSize = uiBodySize; |
95 |
} |
} |
96 |
|
|
97 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
98 |
if (CurrentChunkSize != NewChunkSize) pFile->UnlogResized(this); |
if (pFile) pFile->UnlogResized(this); |
99 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
100 |
} |
} |
101 |
|
|
103 |
#if DEBUG |
#if DEBUG |
104 |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
105 |
#endif // DEBUG |
#endif // DEBUG |
106 |
|
ChunkID = 0; |
107 |
|
NewChunkSize = CurrentChunkSize = 0; |
108 |
#if POSIX |
#if POSIX |
109 |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
110 |
read(pFile->hFileRead, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
135 |
} |
} |
136 |
#if DEBUG |
#if DEBUG |
137 |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
138 |
std::cout << "ckSize=" << ChunkSize << " "; |
std::cout << "ckSize=" << CurrentChunkSize << " "; |
139 |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; |
140 |
#endif // DEBUG |
#endif // DEBUG |
141 |
NewChunkSize = CurrentChunkSize; |
NewChunkSize = CurrentChunkSize; |
142 |
} |
} |
232 |
#if DEBUG |
#if DEBUG |
233 |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
234 |
#endif // DEBUG |
#endif // DEBUG |
235 |
return CurrentChunkSize - ulPos; |
return (CurrentChunkSize > ulPos) ? CurrentChunkSize - ulPos : 0; |
236 |
} |
} |
237 |
|
|
238 |
/** |
/** |
243 |
* - RIFF::stream_closed : |
* - RIFF::stream_closed : |
244 |
* the data stream was closed somehow, no more reading possible |
* the data stream was closed somehow, no more reading possible |
245 |
* - RIFF::stream_end_reached : |
* - RIFF::stream_end_reached : |
246 |
* alreaady reached the end of the chunk data, no more reading |
* already reached the end of the chunk data, no more reading |
247 |
* possible without SetPos() |
* possible without SetPos() |
248 |
*/ |
*/ |
249 |
stream_state_t Chunk::GetState() { |
stream_state_t Chunk::GetState() { |
281 |
#if DEBUG |
#if DEBUG |
282 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
283 |
#endif // DEBUG |
#endif // DEBUG |
284 |
|
if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
285 |
if (ulPos >= CurrentChunkSize) return 0; |
if (ulPos >= CurrentChunkSize) return 0; |
286 |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
287 |
#if POSIX |
#if POSIX |
367 |
#elif defined(WIN32) |
#elif defined(WIN32) |
368 |
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) { |
369 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
throw Exception("Could not seek to position " + ToString(ulPos) + |
370 |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
371 |
} |
} |
372 |
DWORD writtenWords; |
DWORD writtenWords; |
373 |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
595 |
} |
} |
596 |
|
|
597 |
/** |
/** |
598 |
|
* Reads a null-padded string of size characters and copies it |
599 |
|
* into the string \a s. The position within the chunk will |
600 |
|
* automatically be incremented. |
601 |
|
* |
602 |
|
* @param s destination string |
603 |
|
* @param size number of characters to read |
604 |
|
* @throws RIFF::Exception if an error occured or less than |
605 |
|
* \a size characters could be read! |
606 |
|
*/ |
607 |
|
void Chunk::ReadString(String& s, int size) { |
608 |
|
char* buf = new char[size]; |
609 |
|
ReadSceptical(buf, 1, size); |
610 |
|
s.assign(buf, std::find(buf, buf + size, '\0')); |
611 |
|
delete[] buf; |
612 |
|
} |
613 |
|
|
614 |
|
/** |
615 |
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
616 |
* 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 |
617 |
* actual "physical" file. The position within the chunk will |
* actual "physical" file. The position within the chunk will |
751 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
752 |
*/ |
*/ |
753 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
754 |
if (!pChunkData && pFile->Filename != "") { |
if (!pChunkData && pFile->Filename != "" && ulStartPos != 0) { |
755 |
#if POSIX |
#if POSIX |
756 |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
757 |
#elif defined(WIN32) |
#elif defined(WIN32) |
878 |
#else |
#else |
879 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
880 |
#endif |
#endif |
881 |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
for (unsigned long ulOffset = 0; ulToMove > 0 && iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
882 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
883 |
#if POSIX |
#if POSIX |
884 |
lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
968 |
#if DEBUG |
#if DEBUG |
969 |
std::cout << "List::~List()" << std::endl; |
std::cout << "List::~List()" << std::endl; |
970 |
#endif // DEBUG |
#endif // DEBUG |
971 |
|
DeleteChunkList(); |
972 |
|
} |
973 |
|
|
974 |
|
void List::DeleteChunkList() { |
975 |
if (pSubChunks) { |
if (pSubChunks) { |
976 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
977 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
980 |
iter++; |
iter++; |
981 |
} |
} |
982 |
delete pSubChunks; |
delete pSubChunks; |
983 |
|
pSubChunks = NULL; |
984 |
|
} |
985 |
|
if (pSubChunksMap) { |
986 |
|
delete pSubChunksMap; |
987 |
|
pSubChunksMap = NULL; |
988 |
} |
} |
|
if (pSubChunksMap) delete pSubChunksMap; |
|
989 |
} |
} |
990 |
|
|
991 |
/** |
/** |
1186 |
pSubChunks->push_back(pNewChunk); |
pSubChunks->push_back(pNewChunk); |
1187 |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1188 |
pNewChunk->Resize(uiBodySize); |
pNewChunk->Resize(uiBodySize); |
1189 |
|
NewChunkSize += CHUNK_HEADER_SIZE; |
1190 |
|
pFile->LogAsResized(this); |
1191 |
return pNewChunk; |
return pNewChunk; |
1192 |
} |
} |
1193 |
|
|
1223 |
List* pNewListChunk = new List(pFile, this, uiListType); |
List* pNewListChunk = new List(pFile, this, uiListType); |
1224 |
pSubChunks->push_back(pNewListChunk); |
pSubChunks->push_back(pNewListChunk); |
1225 |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1226 |
|
NewChunkSize += LIST_HEADER_SIZE; |
1227 |
|
pFile->LogAsResized(this); |
1228 |
return pNewListChunk; |
return pNewListChunk; |
1229 |
} |
} |
1230 |
|
|
1261 |
std::cout << "List::Readheader(ulong) "; |
std::cout << "List::Readheader(ulong) "; |
1262 |
#endif // DEBUG |
#endif // DEBUG |
1263 |
Chunk::ReadHeader(fPos); |
Chunk::ReadHeader(fPos); |
1264 |
|
if (CurrentChunkSize < 4) return; |
1265 |
NewChunkSize = CurrentChunkSize -= 4; |
NewChunkSize = CurrentChunkSize -= 4; |
1266 |
#if POSIX |
#if POSIX |
1267 |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1401 |
// *************** File *************** |
// *************** File *************** |
1402 |
// * |
// * |
1403 |
|
|
1404 |
|
//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 |
1405 |
|
#define _GET_RESIZED_CHUNKS() \ |
1406 |
|
(reinterpret_cast<std::set<Chunk*>*>(ResizedChunks.front())) |
1407 |
|
|
1408 |
/** @brief Create new RIFF file. |
/** @brief Create new RIFF file. |
1409 |
* |
* |
1410 |
* 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 |
1419 |
* @param FileType - four-byte identifier of the RIFF file type |
* @param FileType - four-byte identifier of the RIFF file type |
1420 |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1421 |
*/ |
*/ |
1422 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) : List(this), bIsNewFile(true) { |
1423 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1424 |
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1425 |
#if defined(WIN32) |
#if defined(WIN32) |
1426 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1427 |
#else |
#else |
1441 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occured while trying to load the |
1442 |
* given RIFF file |
* given RIFF file |
1443 |
*/ |
*/ |
1444 |
File::File(const String& path) : List(this), Filename(path) { |
File::File(const String& path) : List(this), Filename(path), bIsNewFile(false) { |
1445 |
#if DEBUG |
#if DEBUG |
1446 |
std::cout << "File::File("<<path<<")" << std::endl; |
std::cout << "File::File("<<path<<")" << std::endl; |
1447 |
#endif // DEBUG |
#endif // DEBUG |
1448 |
bEndianNative = true; |
try { |
1449 |
#if POSIX |
bEndianNative = true; |
1450 |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
//HACK: see _GET_RESIZED_CHUNKS() comment |
1451 |
if (hFileRead <= 0) { |
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1452 |
hFileRead = hFileWrite = 0; |
#if POSIX |
1453 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1454 |
} |
if (hFileRead <= 0) { |
1455 |
#elif defined(WIN32) |
hFileRead = hFileWrite = 0; |
1456 |
hFileRead = hFileWrite = CreateFile( |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1457 |
path.c_str(), GENERIC_READ, |
} |
1458 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
#elif defined(WIN32) |
1459 |
NULL, OPEN_EXISTING, |
hFileRead = hFileWrite = CreateFile( |
1460 |
FILE_ATTRIBUTE_NORMAL, NULL |
path.c_str(), GENERIC_READ, |
1461 |
); |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1462 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
NULL, OPEN_EXISTING, |
1463 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
FILE_ATTRIBUTE_NORMAL | |
1464 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
FILE_FLAG_RANDOM_ACCESS, NULL |
1465 |
|
); |
1466 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1467 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1468 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1469 |
|
} |
1470 |
|
#else |
1471 |
|
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1472 |
|
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1473 |
|
#endif // POSIX |
1474 |
|
Mode = stream_mode_read; |
1475 |
|
ulStartPos = RIFF_HEADER_SIZE; |
1476 |
|
ReadHeader(0); |
1477 |
|
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1478 |
|
throw RIFF::Exception("Not a RIFF file"); |
1479 |
|
} |
1480 |
} |
} |
1481 |
#else |
catch (...) { |
1482 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
Cleanup(); |
1483 |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
throw; |
|
#endif // POSIX |
|
|
Mode = stream_mode_read; |
|
|
ulStartPos = RIFF_HEADER_SIZE; |
|
|
ReadHeader(0); |
|
|
if (ChunkID != CHUNK_ID_RIFF) { |
|
|
throw RIFF::Exception("Not a RIFF file"); |
|
1484 |
} |
} |
1485 |
} |
} |
1486 |
|
|
1487 |
String File::GetFileName() { |
String File::GetFileName() { |
1488 |
return Filename; |
return Filename; |
1489 |
} |
} |
1490 |
|
|
1491 |
|
void File::SetFileName(const String& path) { |
1492 |
|
Filename = path; |
1493 |
|
} |
1494 |
|
|
1495 |
stream_mode_t File::GetMode() { |
stream_mode_t File::GetMode() { |
1496 |
return Mode; |
return Mode; |
1523 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
1524 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1525 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1526 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1527 |
|
FILE_FLAG_RANDOM_ACCESS, |
1528 |
|
NULL |
1529 |
); |
); |
1530 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1531 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1553 |
GENERIC_READ | GENERIC_WRITE, |
GENERIC_READ | GENERIC_WRITE, |
1554 |
FILE_SHARE_READ, |
FILE_SHARE_READ, |
1555 |
NULL, OPEN_ALWAYS, |
NULL, OPEN_ALWAYS, |
1556 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1557 |
|
FILE_FLAG_RANDOM_ACCESS, |
1558 |
|
NULL |
1559 |
); |
); |
1560 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1561 |
hFileRead = hFileWrite = CreateFile( |
hFileRead = hFileWrite = CreateFile( |
1562 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
1563 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1564 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1565 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1566 |
|
FILE_FLAG_RANDOM_ACCESS, |
1567 |
|
NULL |
1568 |
); |
); |
1569 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
1570 |
} |
} |
1643 |
|
|
1644 |
// 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) |
1645 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1646 |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
std::set<Chunk*>* resizedChunks = _GET_RESIZED_CHUNKS(); |
1647 |
|
for (std::set<Chunk*>::const_iterator iter = resizedChunks->begin(), end = resizedChunks->end(); iter != end; ++iter) { |
1648 |
if ((*iter)->GetNewSize() == 0) { |
if ((*iter)->GetNewSize() == 0) { |
1649 |
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)); |
1650 |
} |
} |
1651 |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; |
1652 |
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2; |
1653 |
ulPositiveSizeDiff += ulDiff; |
if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; |
|
} |
|
1654 |
} |
} |
1655 |
|
|
1656 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |
1666 |
#if defined(WIN32) |
#if defined(WIN32) |
1667 |
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 |
1668 |
#else |
#else |
1669 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1670 |
#endif |
#endif |
1671 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
1672 |
const unsigned long ulToMove = ulFileSize - ulPos; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1673 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
ulPos -= iBytesMoved; |
1674 |
#if POSIX |
#if POSIX |
1675 |
lseek(hFileRead, ulPos, SEEK_SET); |
lseek(hFileRead, ulPos, SEEK_SET); |
1676 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1700 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1701 |
|
|
1702 |
// forget all resized chunks |
// forget all resized chunks |
1703 |
ResizedChunks.clear(); |
resizedChunks->clear(); |
1704 |
} |
} |
1705 |
|
|
1706 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
1722 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1723 |
LoadSubChunksRecursively(); |
LoadSubChunksRecursively(); |
1724 |
|
|
1725 |
if (Filename.length() > 0) SetMode(stream_mode_read); |
if (!bIsNewFile) SetMode(stream_mode_read); |
1726 |
// 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 |
1727 |
#if POSIX |
#if POSIX |
1728 |
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); |
1733 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1734 |
hFileWrite = CreateFile( |
hFileWrite = CreateFile( |
1735 |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1736 |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
1737 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1738 |
); |
); |
1739 |
if (hFileWrite == INVALID_HANDLE_VALUE) { |
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1740 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
1757 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1758 |
|
|
1759 |
// forget all resized chunks |
// forget all resized chunks |
1760 |
ResizedChunks.clear(); |
_GET_RESIZED_CHUNKS()->clear(); |
1761 |
|
|
1762 |
#if POSIX |
#if POSIX |
1763 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
1770 |
|
|
1771 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1772 |
Filename = path; |
Filename = path; |
1773 |
|
bIsNewFile = false; |
1774 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1775 |
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. |
1776 |
} |
} |
1794 |
#if DEBUG |
#if DEBUG |
1795 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
1796 |
#endif // DEBUG |
#endif // DEBUG |
1797 |
|
Cleanup(); |
1798 |
|
} |
1799 |
|
|
1800 |
|
/** |
1801 |
|
* Returns @c true if this file has been created new from scratch and |
1802 |
|
* has not been stored to disk yet. |
1803 |
|
*/ |
1804 |
|
bool File::IsNew() const { |
1805 |
|
return bIsNewFile; |
1806 |
|
} |
1807 |
|
|
1808 |
|
void File::Cleanup() { |
1809 |
#if POSIX |
#if POSIX |
1810 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1811 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1813 |
#else |
#else |
1814 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1815 |
#endif // POSIX |
#endif // POSIX |
1816 |
|
DeleteChunkList(); |
1817 |
|
pFile = NULL; |
1818 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1819 |
|
delete _GET_RESIZED_CHUNKS(); |
1820 |
} |
} |
1821 |
|
|
1822 |
void File::LogAsResized(Chunk* pResizedChunk) { |
void File::LogAsResized(Chunk* pResizedChunk) { |
1823 |
ResizedChunks.push_back(pResizedChunk); |
_GET_RESIZED_CHUNKS()->insert(pResizedChunk); |
1824 |
} |
} |
1825 |
|
|
1826 |
void File::UnlogResized(Chunk* pResizedChunk) { |
void File::UnlogResized(Chunk* pResizedChunk) { |
1827 |
ResizedChunks.remove(pResizedChunk); |
_GET_RESIZED_CHUNKS()->erase(pResizedChunk); |
1828 |
} |
} |
1829 |
|
|
1830 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |