--- libgig/trunk/src/RIFF.cpp 2005/11/09 20:04:11 800 +++ libgig/trunk/src/RIFF.cpp 2013/11/25 02:22:38 2482 @@ -1,8 +1,8 @@ /*************************************************************************** * * - * libgig - C++ cross-platform Gigasampler format file loader library * + * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2005 by Christian Schoenebeck * + * Copyright (C) 2003-2013 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -21,6 +21,8 @@ * MA 02111-1307 USA * ***************************************************************************/ +#include +#include #include #include "RIFF.h" @@ -29,6 +31,25 @@ namespace RIFF { +// *************** Internal functions ************** +// * + + /// Returns a human readable path of the given chunk. + static String __resolveChunkPath(Chunk* pCk) { + String sPath; + for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { + if (pChunk->GetChunkID() == CHUNK_ID_LIST) { + List* pList = (List*) pChunk; + sPath = "->'" + pList->GetListTypeString() + "'" + sPath; + } else { + sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath; + } + } + return sPath; + } + + + // *************** Chunk ************** // * @@ -39,6 +60,8 @@ ulPos = 0; pParent = NULL; pChunkData = NULL; + CurrentChunkSize = 0; + NewChunkSize = 0; ulChunkDataSize = 0; ChunkID = CHUNK_ID_RIFF; this->pFile = pFile; @@ -53,6 +76,8 @@ pParent = Parent; ulPos = 0; pChunkData = NULL; + CurrentChunkSize = 0; + NewChunkSize = 0; ulChunkDataSize = 0; ReadHeader(StartPos); } @@ -63,13 +88,14 @@ this->pParent = pParent; ulPos = 0; pChunkData = NULL; - ulChunkDataSize = 0; ChunkID = uiChunkID; + ulChunkDataSize = 0; CurrentChunkSize = 0; NewChunkSize = uiBodySize; } Chunk::~Chunk() { + if (pFile) pFile->UnlogResized(this); if (pChunkData) delete[] pChunkData; } @@ -77,10 +103,17 @@ #if DEBUG std::cout << "Chunk::Readheader(" << fPos << ") "; #endif // DEBUG + ChunkID = 0; + NewChunkSize = CurrentChunkSize = 0; #if POSIX if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { read(pFile->hFileRead, &ChunkID, 4); read(pFile->hFileRead, &CurrentChunkSize, 4); + #elif defined(WIN32) + if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { + DWORD dwBytesRead; + ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); + ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL); #else if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { fread(&ChunkID, 4, 1, pFile->hFileRead); @@ -88,7 +121,7 @@ #endif // POSIX #if WORDS_BIGENDIAN if (ChunkID == CHUNK_ID_RIFF) { - bEndianNative = false; + pFile->bEndianNative = false; } #else // little endian if (ChunkID == CHUNK_ID_RIFX) { @@ -102,8 +135,8 @@ } #if DEBUG std::cout << "ckID=" << convertToString(ChunkID) << " "; - std::cout << "ckSize=" << ChunkSize << " "; - std::cout << "bEndianNative=" << bEndianNative << std::endl; + std::cout << "ckSize=" << CurrentChunkSize << " "; + std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; #endif // DEBUG NewChunkSize = CurrentChunkSize; } @@ -129,6 +162,12 @@ write(pFile->hFileWrite, &uiNewChunkID, 4); write(pFile->hFileWrite, &uiNewChunkSize, 4); } + #elif defined(WIN32) + if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { + DWORD dwBytesWritten; + WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); + WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL); + } #else if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); @@ -193,7 +232,7 @@ #if DEBUG std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; #endif // DEBUG - return CurrentChunkSize - ulPos; + return (CurrentChunkSize > ulPos) ? CurrentChunkSize - ulPos : 0; } /** @@ -204,7 +243,7 @@ * - RIFF::stream_closed : * the data stream was closed somehow, no more reading possible * - RIFF::stream_end_reached : - * alreaady reached the end of the chunk data, no more reading + * already reached the end of the chunk data, no more reading * possible without SetPos() */ stream_state_t Chunk::GetState() { @@ -212,7 +251,10 @@ std::cout << "Chunk::GetState()" << std::endl; #endif // DEBUG #if POSIX - if (pFile->hFileRead == 0) return stream_closed; + if (pFile->hFileRead == 0) return stream_closed; + #elif defined (WIN32) + if (pFile->hFileRead == INVALID_HANDLE_VALUE) + return stream_closed; #else if (pFile->hFileRead == NULL) return stream_closed; #endif // POSIX @@ -239,6 +281,7 @@ #if DEBUG std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; #endif // DEBUG + if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) if (ulPos >= CurrentChunkSize) return 0; if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; #if POSIX @@ -246,6 +289,12 @@ unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); if (readWords < 1) return 0; readWords /= WordSize; + #elif defined(WIN32) + if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; + DWORD readWords; + ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); + if (readWords < 1) return 0; + readWords /= WordSize; #else // standard C functions if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); @@ -315,6 +364,15 @@ unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); writtenWords /= WordSize; + #elif defined(WIN32) + if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { + throw Exception("Could not seek to position " + ToString(ulPos) + + " in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); + } + DWORD writtenWords; + WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); + if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); + writtenWords /= WordSize; #else // standard C functions if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { throw Exception("Could not seek to position " + ToString(ulPos) + @@ -537,6 +595,23 @@ } /** + * Reads a null-padded string of size characters and copies it + * into the string \a s. The position within the chunk will + * automatically be incremented. + * + * @param s destination string + * @param size number of characters to read + * @throws RIFF::Exception if an error occured or less than + * \a size characters could be read! + */ + void Chunk::ReadString(String& s, int size) { + char* buf = new char[size]; + ReadSceptical(buf, 1, size); + s.assign(buf, std::find(buf, buf + size, '\0')); + delete[] buf; + } + + /** * Writes \a WordCount number of 32 Bit unsigned integer words from the * buffer pointed by \a pData to the chunk's body, directly to the * actual "physical" file. The position within the chunk will @@ -676,9 +751,11 @@ * @see ReleaseChunkData() */ void* Chunk::LoadChunkData() { - if (!pChunkData) { + if (!pChunkData && pFile->Filename != "" && ulStartPos != 0) { #if POSIX if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; + #elif defined(WIN32) + if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL; #else if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; #endif // POSIX @@ -688,6 +765,9 @@ memset(pChunkData, 0, ulBufferSize); #if POSIX unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); + #elif defined(WIN32) + DWORD readWords; + ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); #else unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); #endif // POSIX @@ -740,7 +820,8 @@ * @see File::Save() */ void Chunk::Resize(int iNewSize) { - if (iNewSize <= 0) throw Exception("Chunk size must be at least one byte"); + if (iNewSize <= 0) + throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this)); if (NewChunkSize == iNewSize) return; NewChunkSize = iNewSize; pFile->LogAsResized(this); @@ -775,6 +856,13 @@ if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { throw Exception("Writing Chunk data (from RAM) failed"); } + #elif defined(WIN32) + SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN); + DWORD dwBytesWritten; + WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL); + if (dwBytesWritten != NewChunkSize) { + throw Exception("Writing Chunk data (from RAM) failed"); + } #else fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { @@ -785,14 +873,23 @@ // move chunk data from the end of the file to the appropriate position int8_t* pCopyBuffer = new int8_t[4096]; unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; + #if defined(WIN32) + DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured + #else int iBytesMoved = 1; - for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { + #endif + for (unsigned long ulOffset = 0; ulToMove > 0 && iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; #if POSIX lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); + #elif defined(WIN32) + SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN); + ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); + SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN); + WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); #else fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); @@ -818,6 +915,10 @@ #if POSIX lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); write(pFile->hFileWrite, &cPadByte, 1); + #elif defined(WIN32) + SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN); + DWORD dwBytesWritten; + WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); #else fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); fwrite(&cPadByte, 1, 1, pFile->hFileWrite); @@ -867,6 +968,10 @@ #if DEBUG std::cout << "List::~List()" << std::endl; #endif // DEBUG + DeleteChunkList(); + } + + void List::DeleteChunkList() { if (pSubChunks) { ChunkList::iterator iter = pSubChunks->begin(); ChunkList::iterator end = pSubChunks->end(); @@ -875,8 +980,12 @@ iter++; } delete pSubChunks; + pSubChunks = NULL; + } + if (pSubChunksMap) { + delete pSubChunksMap; + pSubChunksMap = NULL; } - if (pSubChunksMap) delete pSubChunksMap; } /** @@ -1077,9 +1186,29 @@ pSubChunks->push_back(pNewChunk); (*pSubChunksMap)[uiChunkID] = pNewChunk; pNewChunk->Resize(uiBodySize); + NewChunkSize += CHUNK_HEADER_SIZE; + pFile->LogAsResized(this); return pNewChunk; } + /** @brief Moves a sub chunk. + * + * Moves a sub chunk from one position in a list to another + * position in the same list. The pSrc chunk is placed before the + * pDst chunk. + * + * @param pSrc - sub chunk to be moved + * @param pDst - the position to move to. pSrc will be placed + * before pDst. If pDst is 0, pSrc will be placed + * last in list. + */ + void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) { + if (!pSubChunks) LoadSubChunks(); + pSubChunks->remove(pSrc); + ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst); + pSubChunks->insert(iter, pSrc); + } + /** @brief Creates a new list sub chunk. * * Creates and adds a new list sub chunk to this list chunk. Note that @@ -1094,6 +1223,8 @@ List* pNewListChunk = new List(pFile, this, uiListType); pSubChunks->push_back(pNewListChunk); (*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; + NewChunkSize += LIST_HEADER_SIZE; + pFile->LogAsResized(this); return pNewListChunk; } @@ -1101,8 +1232,9 @@ * * Removes the sub chunk given by \a pSubChunk from this list and frees * it completely from RAM. The given chunk can either be a normal sub - * chunk or a list sub chunk. You should call File::Save() to make this - * change persistent at any time. + * chunk or a list sub chunk. In case the given chunk is a list chunk, + * all its subchunks (if any) will be removed recursively as well. You + * should call File::Save() to make this change persistent at any time. * * @param pSubChunk - sub chunk or sub list chunk to be removed */ @@ -1129,10 +1261,15 @@ std::cout << "List::Readheader(ulong) "; #endif // DEBUG Chunk::ReadHeader(fPos); + if (CurrentChunkSize < 4) return; NewChunkSize = CurrentChunkSize -= 4; #if POSIX lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); read(pFile->hFileRead, &ListType, 4); + #elif defined(WIN32) + SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); + DWORD dwBytesRead; + ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); #else fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); fread(&ListType, 4, 1, pFile->hFileRead); @@ -1153,6 +1290,10 @@ #if POSIX lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); write(pFile->hFileWrite, &ListType, 4); + #elif defined(WIN32) + SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); + DWORD dwBytesWritten; + WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); #else fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); fwrite(&ListType, 4, 1, pFile->hFileWrite); @@ -1166,7 +1307,11 @@ if (!pSubChunks) { pSubChunks = new ChunkList(); pSubChunksMap = new ChunkMap(); + #if defined(WIN32) + if (pFile->hFileRead == INVALID_HANDLE_VALUE) return; + #else if (!pFile->hFileRead) return; + #endif unsigned long uiOriginalPos = GetPos(); SetPos(0); // jump to beginning of list chunk body while (RemainingBytes() >= CHUNK_HEADER_SIZE) { @@ -1192,6 +1337,11 @@ } } + void List::LoadSubChunksRecursively() { + for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList()) + pList->LoadSubChunksRecursively(); + } + /** @brief Write list chunk persistently e.g. to disk. * * Stores the list chunk persistently to its actual "physical" file. All @@ -1251,17 +1401,32 @@ // *************** File *************** // * +//HACK: to avoid breaking DLL compatibility to older versions of libgig we roll the new std::set into the old std::list container, should be replaced on member variable level soon though +#define _GET_RESIZED_CHUNKS() \ + (reinterpret_cast*>(ResizedChunks.front())) + /** @brief Create new RIFF file. * * Use this constructor if you want to create a new RIFF file completely * "from scratch". Note: there must be no empty chunks or empty list * chunks when trying to make the new RIFF file persistent with Save()! * + * Note: by default, the RIFF file will be saved in native endian + * format; that is, as a RIFF file on little-endian machines and + * as a RIFX file on big-endian. To change this behaviour, call + * SetByteOrder() before calling Save(). + * * @param FileType - four-byte identifier of the RIFF file type - * @see AddSubChunk(), AddSubList() + * @see AddSubChunk(), AddSubList(), SetByteOrder() */ - File::File(uint32_t FileType) : List(this) { + File::File(uint32_t FileType) : List(this), bIsNewFile(true) { + //HACK: see _GET_RESIZED_CHUNKS() comment + ResizedChunks.push_back(reinterpret_cast(new std::set)); + #if defined(WIN32) + hFileRead = hFileWrite = INVALID_HANDLE_VALUE; + #else hFileRead = hFileWrite = 0; + #endif Mode = stream_mode_closed; bEndianNative = true; ulStartPos = RIFF_HEADER_SIZE; @@ -1276,32 +1441,56 @@ * @throws RIFF::Exception if error occured while trying to load the * given RIFF file */ - File::File(const String& path) : List(this), Filename(path) { - #if DEBUG - std::cout << "File::File("<(new std::set)); + #if POSIX + hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); + if (hFileRead <= 0) { + hFileRead = hFileWrite = 0; + throw RIFF::Exception("Can't open \"" + path + "\""); + } + #elif defined(WIN32) + hFileRead = hFileWrite = CreateFile( + path.c_str(), GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | + FILE_FLAG_RANDOM_ACCESS, NULL + ); + if (hFileRead == INVALID_HANDLE_VALUE) { + hFileRead = hFileWrite = INVALID_HANDLE_VALUE; + throw RIFF::Exception("Can't open \"" + path + "\""); + } + #else + hFileRead = hFileWrite = fopen(path.c_str(), "rb"); + if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); + #endif // POSIX + Mode = stream_mode_read; + ulStartPos = RIFF_HEADER_SIZE; + ReadHeader(0); + if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { + throw RIFF::Exception("Not a RIFF file"); + } } - #else - hFileRead = hFileWrite = fopen(path.c_str(), "rb"); - if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); - #endif // POSIX - Mode = stream_mode_read; - ulStartPos = RIFF_HEADER_SIZE; - ReadHeader(0); - if (ChunkID != CHUNK_ID_RIFF) { - throw RIFF::Exception("Not a RIFF file"); + catch (...) { + Cleanup(); + throw; } } String File::GetFileName() { return Filename; } + + void File::SetFileName(const String& path) { + Filename = path; + } stream_mode_t File::GetMode() { return Mode; @@ -1328,9 +1517,23 @@ hFileRead = hFileWrite = 0; throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); } + #elif defined(WIN32) + if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); + hFileRead = hFileWrite = CreateFile( + Filename.c_str(), GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | + FILE_FLAG_RANDOM_ACCESS, + NULL + ); + if (hFileRead == INVALID_HANDLE_VALUE) { + hFileRead = hFileWrite = INVALID_HANDLE_VALUE; + throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); + } #else if (hFileRead) fclose(hFileRead); - hFileRead = hFileWrite = fopen(path.c_str(), "rb"); + hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); if (!hFileRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); #endif __resetPos(); // reset read/write position of ALL 'Chunk' objects @@ -1343,11 +1546,33 @@ hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); } + #elif defined(WIN32) + if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); + hFileRead = hFileWrite = CreateFile( + Filename.c_str(), + GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ, + NULL, OPEN_ALWAYS, + FILE_ATTRIBUTE_NORMAL | + FILE_FLAG_RANDOM_ACCESS, + NULL + ); + if (hFileRead == INVALID_HANDLE_VALUE) { + hFileRead = hFileWrite = CreateFile( + Filename.c_str(), GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | + FILE_FLAG_RANDOM_ACCESS, + NULL + ); + throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); + } #else if (hFileRead) fclose(hFileRead); - hFileRead = hFileWrite = fopen(path.c_str(), "r+b"); + hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b"); if (!hFileRead) { - hFileRead = hFileWrite = fopen(path.c_str(), "rb"); + hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); } #endif @@ -1357,6 +1582,9 @@ #if POSIX if (hFileRead) close(hFileRead); if (hFileWrite) close(hFileWrite); + #elif defined(WIN32) + if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); + if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); #else if (hFileRead) fclose(hFileRead); if (hFileWrite) fclose(hFileWrite); @@ -1372,6 +1600,23 @@ return false; } + /** @brief Set the byte order to be used when saving. + * + * Set the byte order to be used in the file. A value of + * endian_little will create a RIFF file, endian_big a RIFX file + * and endian_native will create a RIFF file on little-endian + * machines and RIFX on big-endian machines. + * + * @param Endian - endianess to use when file is saved. + */ + void File::SetByteOrder(endian_t Endian) { + #if WORDS_BIGENDIAN + bEndianNative = Endian != endian_little; + #else + bEndianNative = Endian != endian_big; + #endif + } + /** @brief Save changes to same file. * * Make all changes of all chunks persistent by writing them to the @@ -1383,6 +1628,9 @@ * chunk or any kind of IO error occured */ void File::Save() { + // make sure the RIFF tree is built (from the original file) + LoadSubChunksRecursively(); + // reopen file in write mode SetMode(stream_mode_read_write); @@ -1395,12 +1643,14 @@ // first we sum up all positive chunk size changes (and skip all negative ones) unsigned long ulPositiveSizeDiff = 0; - for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { - if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)"); - if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { - unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte - ulPositiveSizeDiff += ulDiff; + std::set* resizedChunks = _GET_RESIZED_CHUNKS(); + for (std::set::const_iterator iter = resizedChunks->begin(), end = resizedChunks->end(); iter != end; ++iter) { + if ((*iter)->GetNewSize() == 0) { + throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); } + unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; + unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2; + if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; } unsigned long ulWorkingFileSize = GetFileSize(); @@ -1413,15 +1663,24 @@ // ... and move current data by the same amount towards end of file. int8_t* pCopyBuffer = new int8_t[4096]; const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; + #if defined(WIN32) + DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured + #else int iBytesMoved = 1; - for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { - const unsigned long ulToMove = ulFileSize - ulPos; - iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; + #endif + for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { + iBytesMoved = (ulPos < 4096) ? ulPos : 4096; + ulPos -= iBytesMoved; #if POSIX lseek(hFileRead, ulPos, SEEK_SET); iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); + #elif defined(WIN32) + SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN); + ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); + SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN); + WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); #else fseek(hFileRead, ulPos, SEEK_SET); iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); @@ -1441,7 +1700,7 @@ if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); // forget all resized chunks - ResizedChunks.clear(); + resizedChunks->clear(); } /** @brief Save changes to another file. @@ -1460,7 +1719,10 @@ void File::Save(const String& path) { //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 - if (Filename.length() > 0) SetMode(stream_mode_read); + // make sure the RIFF tree is built (from the original file) + LoadSubChunksRecursively(); + + if (!bIsNewFile) SetMode(stream_mode_read); // open the other (new) file for writing and truncate it to zero size #if POSIX hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); @@ -1468,6 +1730,16 @@ hFileWrite = hFileRead; throw Exception("Could not open file \"" + path + "\" for writing"); } + #elif defined(WIN32) + hFileWrite = CreateFile( + path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, + NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | + FILE_FLAG_RANDOM_ACCESS, NULL + ); + if (hFileWrite == INVALID_HANDLE_VALUE) { + hFileWrite = hFileRead; + throw Exception("Could not open file \"" + path + "\" for writing"); + } #else hFileWrite = fopen(path.c_str(), "w+b"); if (!hFileWrite) { @@ -1485,19 +1757,20 @@ if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); // forget all resized chunks - ResizedChunks.clear(); + _GET_RESIZED_CHUNKS()->clear(); - if (Filename.length() > 0) { - #if POSIX - close(hFileWrite); - #else - fclose(hFileWrite); - #endif - hFileWrite = hFileRead; - } + #if POSIX + if (hFileWrite) close(hFileWrite); + #elif defined(WIN32) + if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); + #else + if (hFileWrite) fclose(hFileWrite); + #endif + hFileWrite = hFileRead; // associate new file with this File object from now on Filename = path; + bIsNewFile = false; Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. } @@ -1506,8 +1779,13 @@ #if POSIX if (ftruncate(hFileWrite, ulNewSize) < 0) throw Exception("Could not resize file \"" + Filename + "\""); + #elif defined(WIN32) + if ( + SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER || + !SetEndOfFile(hFileWrite) + ) throw Exception("Could not resize file \"" + Filename + "\""); #else - # error Sorry, this version of libgig only supports POSIX systems yet. + # error Sorry, this version of libgig only supports POSIX and Windows systems yet. # error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! #endif } @@ -1516,15 +1794,37 @@ #if DEBUG std::cout << "File::~File()" << std::endl; #endif // DEBUG + Cleanup(); + } + + /** + * Returns @c true if this file has been created new from scratch and + * has not been stored to disk yet. + */ + bool File::IsNew() const { + return bIsNewFile; + } + + void File::Cleanup() { #if POSIX if (hFileRead) close(hFileRead); + #elif defined(WIN32) + if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); #else if (hFileRead) fclose(hFileRead); #endif // POSIX + DeleteChunkList(); + pFile = NULL; + //HACK: see _GET_RESIZED_CHUNKS() comment + delete _GET_RESIZED_CHUNKS(); } void File::LogAsResized(Chunk* pResizedChunk) { - ResizedChunks.push_back(pResizedChunk); + _GET_RESIZED_CHUNKS()->insert(pResizedChunk); + } + + void File::UnlogResized(Chunk* pResizedChunk) { + _GET_RESIZED_CHUNKS()->erase(pResizedChunk); } unsigned long File::GetFileSize() { @@ -1538,6 +1838,13 @@ long size = filestat.st_size; return size; } + #elif defined(WIN32) + unsigned long File::__GetFileSize(HANDLE hFile) { + DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/); + if (dwSize == INVALID_FILE_SIZE) + throw Exception("Windows FS error: could not determine file size"); + return dwSize; + } #else // standard C functions unsigned long File::__GetFileSize(FILE* hFile) { long curpos = ftell(hFile);