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;
197 unsigned long Read(
void* pData,
unsigned long WordCount,
unsigned long WordSize);
198 unsigned long ReadInt8(int8_t* pData,
unsigned long WordCount = 1);
199 unsigned long ReadUint8(uint8_t* pData,
unsigned long WordCount = 1);
200 unsigned long ReadInt16(int16_t* pData,
unsigned long WordCount = 1);
201 unsigned long ReadUint16(uint16_t* pData,
unsigned long WordCount = 1);
202 unsigned long ReadInt32(int32_t* pData,
unsigned long WordCount = 1);
203 unsigned long ReadUint32(uint32_t* pData,
unsigned long WordCount = 1);
211 unsigned long Write(
void* pData,
unsigned long WordCount,
unsigned long WordSize);
212 unsigned long WriteInt8(int8_t* pData,
unsigned long WordCount = 1);
213 unsigned long WriteUint8(uint8_t* pData,
unsigned long WordCount = 1);
214 unsigned long WriteInt16(int16_t* pData,
unsigned long WordCount = 1);
215 unsigned long WriteUint16(uint16_t* pData,
unsigned long WordCount = 1);
216 unsigned long WriteInt32(int32_t* pData,
unsigned long WordCount = 1);
217 unsigned long WriteUint32(uint32_t* pData,
unsigned long WordCount = 1);
220 void Resize(
int iNewSize);
234 Chunk(
File* pFile,
List* pParent, uint32_t uiChunkID, uint uiBodySize);
237 unsigned long ReadSceptical(
void* pData,
unsigned long WordCount,
unsigned long WordSize);
239 uint8_t byteCache = *((uint8_t*) Word);
240 *((uint8_t*) Word) = *((uint8_t*) Word + 1);
241 *((uint8_t*) Word + 1) = byteCache;
244 uint8_t byteCache = *((uint8_t*) Word);
245 *((uint8_t*) Word) = *((uint8_t*) Word + 3);
246 *((uint8_t*) Word + 3) = byteCache;
247 byteCache = *((uint8_t*) Word + 1);
248 *((uint8_t*) Word + 1) = *((uint8_t*) Word + 2);
249 *((uint8_t*) Word + 2) = byteCache;
251 inline void swapBytes(
void* Word,
unsigned long WordSize) {
253 unsigned long lo = 0, hi = WordSize - 1;
254 for (; lo < hi; hi--, lo++) {
255 byteCache = *((uint8_t*) Word + lo);
256 *((uint8_t*) Word + lo) = *((uint8_t*) Word + hi);
257 *((uint8_t*) Word + hi) = byteCache;
262 for (
int i = 0; i < 4; i++) {
263 uint8_t byte = *((uint8_t*)(&word) + i);
269 virtual unsigned long WriteChunk(
unsigned long ulWritePos,
unsigned long ulCurrentDataOffset);
317 virtual unsigned long WriteChunk(
unsigned long ulWritePos,
unsigned long ulCurrentDataOffset);
330 File(uint32_t FileType);
331 File(
const String& path);
341 virtual void Save(
const String& path);
367 void __openExistingFile(
const String& path, uint32_t* FileType = NULL);
368 unsigned long GetFileSize();
369 void ResizeFile(
unsigned long ulNewSize);
371 unsigned long __GetFileSize(
int hFile);
373 unsigned long __GetFileSize(HANDLE hFile);
375 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.
layout_t Layout
An ordinary RIFF file is always set to layout_standard.
layout_t GetLayout() const
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.
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.
layout_t
General chunk structure of a file.
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
File * GetFile()
Returns pointer to the chunk's File object.
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
Not a "real" RIFF file: First chunk in file is an ordinary data chunk, not a List chunk...
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 SetFileName(const String &path)
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...
Standard RIFF file layout: First chunk in file is a List chunk which contains all other chunks and th...
unsigned long GetSize() const
Chunk size in bytes (without header, thus the chunk data body)
unsigned long ulChunkDataSize
List * AddSubList(uint32_t uiListType)
Creates a new list sub chunk.
void LoadSubChunksRecursively()
RIFF specific classes and definitions.
bool IsNew() const
Returns true if this file has been created new from scratch and has not been stored to disk yet...
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 witin this list.
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.