--- libgig/trunk/src/RIFF.cpp 2007/05/13 10:34:29 1183 +++ libgig/trunk/src/RIFF.cpp 2007/10/05 09:39:12 1383 @@ -33,7 +33,7 @@ // * /// Returns a human readable path of the given chunk. - String __resolveChunkPath(Chunk* pCk) { + static String __resolveChunkPath(Chunk* pCk) { String sPath; for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { if (pChunk->GetChunkID() == CHUNK_ID_LIST) { @@ -235,7 +235,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() { @@ -1268,7 +1268,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) { @@ -1363,11 +1367,14 @@ * 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 that this constructor will create a RIFX file on - * big-endian machines. + * + * 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) { #if defined(WIN32) @@ -1381,39 +1388,6 @@ ListType = FileType; } - - /** @brief Create new RIFF file. - * - * Use this constructor if you want to create a new RIFF file - * completely "from scratch" and also want to specify - * endianess. Note: there must be no empty chunks or empty list - * chunks when trying to make the new RIFF file persistent with - * Save()! - * - * @param FileType - four-byte identifier of the RIFF file type - * @param Endian - endianess used in the new file. A value of - * endian_little will create a RIFF file, - * endian_big a RIFX file, endian_native will - * create a RIFF file on little-endian machines - * and RIFX on big-endian. - * @see AddSubChunk(), AddSubList() - */ - File::File(uint32_t FileType, endian_t Endian) : List(this) { - #if defined(WIN32) - hFileRead = hFileWrite = INVALID_HANDLE_VALUE; - #else - hFileRead = hFileWrite = 0; - #endif - Mode = stream_mode_closed; - #if WORDS_BIGENDIAN - bEndianNative = Endian != endian_little; - #else - bEndianNative = Endian != endian_big; - #endif - ulStartPos = RIFF_HEADER_SIZE; - ListType = FileType; - } - /** @brief Load existing RIFF file. * * Loads an existing RIFF file with all its chunks. @@ -1528,7 +1502,7 @@ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); - throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); + throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); } #else if (hFileRead) fclose(hFileRead); @@ -1562,6 +1536,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 @@ -1703,16 +1694,14 @@ // forget all resized chunks ResizedChunks.clear(); - if (Filename.length() > 0) { - #if POSIX - close(hFileWrite); - #elif defined(WIN32) - CloseHandle(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;