49 # include <sys/types.h>
50 # include <sys/stat.h>
57 typedef __int8 int8_t;
58 typedef __int16 int16_t;
59 typedef __int32 int32_t;
60 typedef __int64 int64_t;
61 typedef unsigned __int8 uint8_t;
62 typedef unsigned __int16 uint16_t;
63 typedef unsigned __int32 uint32_t;
64 typedef unsigned __int64 uint64_t;
71 typedef unsigned int uint;
77 # define CHUNK_ID_RIFF 0x52494646
78 # define CHUNK_ID_RIFX 0x52494658
79 # define CHUNK_ID_LIST 0x4C495354
80 #else // little endian
81 # define CHUNK_ID_RIFF 0x46464952
82 # define CHUNK_ID_RIFX 0x58464952
83 # define CHUNK_ID_LIST 0x5453494C
84 #endif // WORDS_BIGENDIAN
86 #define CHUNK_HEADER_SIZE 8
87 #define LIST_HEADER_SIZE 12
88 #define RIFF_HEADER_SIZE 12
117 typedef std::string
String;
166 unsigned long Read(
void* pData,
unsigned long WordCount,
unsigned long WordSize);
167 unsigned long ReadInt8(int8_t* pData,
unsigned long WordCount = 1);
168 unsigned long ReadUint8(uint8_t* pData,
unsigned long WordCount = 1);
169 unsigned long ReadInt16(int16_t* pData,
unsigned long WordCount = 1);
170 unsigned long ReadUint16(uint16_t* pData,
unsigned long WordCount = 1);
171 unsigned long ReadInt32(int32_t* pData,
unsigned long WordCount = 1);
172 unsigned long ReadUint32(uint32_t* pData,
unsigned long WordCount = 1);
179 unsigned long Write(
void* pData,
unsigned long WordCount,
unsigned long WordSize);
180 unsigned long WriteInt8(int8_t* pData,
unsigned long WordCount = 1);
181 unsigned long WriteUint8(uint8_t* pData,
unsigned long WordCount = 1);
182 unsigned long WriteInt16(int16_t* pData,
unsigned long WordCount = 1);
183 unsigned long WriteUint16(uint16_t* pData,
unsigned long WordCount = 1);
184 unsigned long WriteInt32(int32_t* pData,
unsigned long WordCount = 1);
185 unsigned long WriteUint32(uint32_t* pData,
unsigned long WordCount = 1);
188 void Resize(
int iNewSize);
202 Chunk(
File* pFile,
List* pParent, uint32_t uiChunkID, uint uiBodySize);
205 unsigned long ReadSceptical(
void* pData,
unsigned long WordCount,
unsigned long WordSize);
207 uint8_t byteCache = *((uint8_t*) Word);
208 *((uint8_t*) Word) = *((uint8_t*) Word + 1);
209 *((uint8_t*) Word + 1) = byteCache;
212 uint8_t byteCache = *((uint8_t*) Word);
213 *((uint8_t*) Word) = *((uint8_t*) Word + 3);
214 *((uint8_t*) Word + 3) = byteCache;
215 byteCache = *((uint8_t*) Word + 1);
216 *((uint8_t*) Word + 1) = *((uint8_t*) Word + 2);
217 *((uint8_t*) Word + 2) = byteCache;
219 inline void swapBytes(
void* Word,
unsigned long WordSize) {
221 unsigned long lo = 0, hi = WordSize - 1;
222 for (; lo < hi; hi--, lo++) {
223 byteCache = *((uint8_t*) Word + lo);
224 *((uint8_t*) Word + lo) = *((uint8_t*) Word + hi);
225 *((uint8_t*) Word + hi) = byteCache;
230 for (
int i = 0; i < 4; i++) {
231 uint8_t byte = *((uint8_t*)(&word) + i);
237 virtual unsigned long WriteChunk(
unsigned long ulWritePos,
unsigned long ulCurrentDataOffset);
284 virtual unsigned long WriteChunk(
unsigned long ulWritePos,
unsigned long ulCurrentDataOffset);
297 File(uint32_t FileType);
298 File(
const String& path);
304 virtual void Save(
const String& path);
328 unsigned long GetFileSize();
329 void ResizeFile(
unsigned long ulNewSize);
331 unsigned long __GetFileSize(
int hFile);
333 unsigned long __GetFileSize(HANDLE hFile);
335 unsigned long __GetFileSize(FILE* hFile);
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset)
Write chunk persistently e.g.
unsigned long WriteUint32(uint32_t *pData, unsigned long WordCount=1)
Writes WordCount number of 32 Bit unsigned integer words from the buffer pointed by pData to the chun...
int16_t ReadInt16()
Reads one 16 Bit signed integer word and increments the position within the chunk.
void UnlogResized(Chunk *pResizedChunk)
void swapBytes_16(void *Word)
stream_whence_t
File stream position dependent to these relations.
unsigned long Read(void *pData, unsigned long WordCount, unsigned long WordSize)
Reads WordCount number of data words with given WordSize and copies it into a buffer pointed by pData...
Chunk * GetFirstSubChunk()
Returns the first subchunk within the list.
String libraryName()
Returns the name of this C++ library.
File(uint32_t FileType)
Create new RIFF file.
unsigned long WriteUint16(uint16_t *pData, unsigned long WordCount=1)
Writes WordCount number of 16 Bit unsigned integer words from the buffer pointed by pData to the chun...
uint32_t GetChunkID()
Chunk ID in unsigned integer representation.
void ReadHeader(unsigned long fPos)
void swapBytes(void *Word, unsigned long WordSize)
stream_state_t
Current state of the file stream.
unsigned long SetPos(unsigned long Where, stream_whence_t Whence=stream_start)
Sets the position within the chunk body, thus within the data portion of the chunk (in bytes)...
void WriteHeader(unsigned long fPos)
String libraryVersion()
Returns version of this C++ library.
List * GetSubList(uint32_t ListType)
Returns sublist chunk with list type ListType within this chunk list.
void DeleteSubChunk(Chunk *pSubChunk)
Removes a sub chunk.
unsigned long WriteInt16(int16_t *pData, unsigned long WordCount=1)
Writes WordCount number of 16 Bit signed integer words from the buffer pointed by pData to the chunk'...
int hFileWrite
handle / descriptor for writing to (some) file
unsigned long RemainingBytes()
Returns the number of bytes left to read in the chunk body.
List * GetFirstSubList()
Returns the first sublist within the list (that is a subchunk with chunk ID "LIST").
Exception(String Message)
stream_mode_t
Whether file stream is open in read or in read/write mode.
std::list< Chunk * > ChunkList
unsigned long GetPos()
Position within the chunk data body.
void SetByteOrder(endian_t Endian)
Set the byte order to be used when saving.
unsigned long GetSize()
Chunk size in bytes (without header, thus the chunk data body)
int8_t ReadInt8()
Reads one 8 Bit signed integer word and increments the position within the chunk. ...
void ReadHeader(unsigned long fPos)
String GetListTypeString()
Returns string representation of the lists's id.
ChunkList::iterator ListIterator
unsigned long WriteInt32(int32_t *pData, unsigned long WordCount=1)
Writes WordCount number of 32 Bit signed integer words from the buffer pointed by pData to the chunk'...
Chunk * GetSubChunk(uint32_t ChunkID)
Returns subchunk with chunk ID ChunkID within this chunk list.
Chunk * GetNextSubChunk()
Returns the next subchunk within the list.
unsigned long ReadSceptical(void *pData, unsigned long WordCount, unsigned long WordSize)
Just an internal wrapper for the main Read() method with additional Exception throwing on errors...
int32_t ReadInt32()
Reads one 32 Bit signed integer word and increments the position within the chunk.
stream_state_t GetState()
Returns the current state of the chunk object.
uint32_t GetListType()
Returns unsigned integer representation of the list's ID.
unsigned long GetFilePos()
Current, actual offset in file.
uint32_t ReadUint32()
Reads one 32 Bit unsigned integer word and increments the position within the chunk.
ChunkList::iterator ChunksIterator
unsigned long Write(void *pData, unsigned long WordCount, unsigned long WordSize)
Writes WordCount number of data words with given WordSize from the buffer pointed by pData...
List * GetParent()
Returns pointer to the chunk's parent list chunk.
void swapBytes_32(void *Word)
Chunk * AddSubChunk(uint32_t uiChunkID, uint uiBodySize)
Creates a new sub chunk.
int hFileRead
handle / descriptor for reading from file
void LogAsResized(Chunk *pResizedChunk)
virtual void __resetPos()
Sets Chunk's read/write position to zero.
uint16_t ReadUint16()
Reads one 16 Bit unsigned integer word and increments the position within the chunk.
virtual void __resetPos()
Sets List Chunk's read/write position to zero and causes all sub chunks to do the same...
uint32_t CurrentChunkSize
endian_t
Alignment of data bytes in memory (system dependant).
void * LoadChunkData()
Load chunk body into RAM.
unsigned long WriteInt8(int8_t *pData, unsigned long WordCount=1)
Writes WordCount number of 8 Bit signed integer words from the buffer pointed by pData to the chunk's...
unsigned long ulChunkDataSize
List * AddSubList(uint32_t uiListType)
Creates a new list sub chunk.
void LoadSubChunksRecursively()
RIFF specific classes and definitions.
List(File *pFile, unsigned long StartPos, List *Parent)
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset)
Write list chunk persistently e.g.
void MoveSubChunk(Chunk *pSrc, Chunk *pDst)
Moves a sub chunk.
unsigned int CountSubChunks()
Returns number of subchunks within the list.
String convertToString(uint32_t word)
bool SetMode(stream_mode_t NewMode)
Change file access mode.
virtual void Save()
Save changes to same file.
Will be thrown whenever an error occurs while handling a RIFF file.
unsigned long WriteUint8(uint8_t *pData, unsigned long WordCount=1)
Writes WordCount number of 8 Bit unsigned integer words from the buffer pointed by pData to the chunk...
void ReleaseChunkData()
Free loaded chunk body from RAM.
unsigned int CountSubLists()
Returns number of sublists within the list.
String GetChunkIDString()
Returns the String representation of the chunk's ID (e.g.
unsigned long GetNewSize()
New chunk size if it was modified with Resize().
void WriteHeader(unsigned long fPos)
std::map< uint32_t, RIFF::Chunk * > ChunkMap
void Resize(int iNewSize)
Resize chunk.
List * GetNextSubList()
Returns the next sublist (that is a subchunk with chunk ID "LIST") within the list.
Chunk(File *pFile, unsigned long StartPos, List *Parent)
uint8_t ReadUint8()
Reads one 8 Bit unsigned integer word and increments the position within the chunk.