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
81 # define LIST_TYPE_INFO 0x494E464F
82 # define CHUNK_ID_ICMT 0x49434D54
83 # define CHUNK_ID_ICOP 0x49434F50
84 # define CHUNK_ID_ICRD 0x49435244
85 # define CHUNK_ID_IENG 0x49454E47
86 # define CHUNK_ID_INAM 0x494E414D
87 # define CHUNK_ID_IPRD 0x49505244
88 # define CHUNK_ID_ISFT 0x49534654
90 # define CHUNK_ID_SMPL 0x736D706C
92 #else // little endian
93 # define CHUNK_ID_RIFF 0x46464952
94 # define CHUNK_ID_RIFX 0x58464952
95 # define CHUNK_ID_LIST 0x5453494C
97 # define LIST_TYPE_INFO 0x4F464E49
98 # define CHUNK_ID_ICMT 0x544D4349
99 # define CHUNK_ID_ICOP 0x504F4349
100 # define CHUNK_ID_ICRD 0x44524349
101 # define CHUNK_ID_IENG 0x474E4549
102 # define CHUNK_ID_INAM 0x4D414E49
103 # define CHUNK_ID_IPRD 0x44525049
104 # define CHUNK_ID_ISFT 0x54465349
106 # define CHUNK_ID_SMPL 0x6C706D73
108 #endif // WORDS_BIGENDIAN
110 #define CHUNK_HEADER_SIZE 8
111 #define LIST_HEADER_SIZE 12
112 #define RIFF_HEADER_SIZE 12
141 typedef std::string
String;
190 unsigned long Read(
void* pData,
unsigned long WordCount,
unsigned long WordSize);
191 unsigned long ReadInt8(int8_t* pData,
unsigned long WordCount = 1);
192 unsigned long ReadUint8(uint8_t* pData,
unsigned long WordCount = 1);
193 unsigned long ReadInt16(int16_t* pData,
unsigned long WordCount = 1);
194 unsigned long ReadUint16(uint16_t* pData,
unsigned long WordCount = 1);
195 unsigned long ReadInt32(int32_t* pData,
unsigned long WordCount = 1);
196 unsigned long ReadUint32(uint32_t* pData,
unsigned long WordCount = 1);
204 unsigned long Write(
void* pData,
unsigned long WordCount,
unsigned long WordSize);
205 unsigned long WriteInt8(int8_t* pData,
unsigned long WordCount = 1);
206 unsigned long WriteUint8(uint8_t* pData,
unsigned long WordCount = 1);
207 unsigned long WriteInt16(int16_t* pData,
unsigned long WordCount = 1);
208 unsigned long WriteUint16(uint16_t* pData,
unsigned long WordCount = 1);
209 unsigned long WriteInt32(int32_t* pData,
unsigned long WordCount = 1);
210 unsigned long WriteUint32(uint32_t* pData,
unsigned long WordCount = 1);
213 void Resize(
int iNewSize);
230 unsigned long ReadSceptical(
void* pData,
unsigned long WordCount,
unsigned long WordSize);
232 uint8_t byteCache = *((uint8_t*) Word);
233 *((uint8_t*) Word) = *((uint8_t*) Word + 1);
234 *((uint8_t*) Word + 1) = byteCache;
237 uint8_t byteCache = *((uint8_t*) Word);
238 *((uint8_t*) Word) = *((uint8_t*) Word + 3);
239 *((uint8_t*) Word + 3) = byteCache;
240 byteCache = *((uint8_t*) Word + 1);
241 *((uint8_t*) Word + 1) = *((uint8_t*) Word + 2);
242 *((uint8_t*) Word + 2) = byteCache;
244 inline void swapBytes(
void* Word,
unsigned long WordSize) {
246 unsigned long lo = 0, hi = WordSize - 1;
247 for (; lo < hi; hi--, lo++) {
248 byteCache = *((uint8_t*) Word + lo);
249 *((uint8_t*) Word + lo) = *((uint8_t*) Word + hi);
250 *((uint8_t*) Word + hi) = byteCache;
255 for (
int i = 0; i < 4; i++) {
256 uint8_t byte = *((uint8_t*)(&word) + i);
262 virtual unsigned long WriteChunk(
unsigned long ulWritePos,
unsigned long ulCurrentDataOffset);
309 virtual unsigned long WriteChunk(
unsigned long ulWritePos,
unsigned long ulCurrentDataOffset);
322 File(uint32_t FileType);
353 unsigned long GetFileSize();
354 void ResizeFile(
unsigned long ulNewSize);
356 unsigned long __GetFileSize(
int hFile);
358 unsigned long __GetFileSize(HANDLE hFile);
360 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 ReadString(String &s, int size)
Reads a null-padded string of size characters and copies it into the string s.
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()
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.