41# define CHUNK_ID_RIFF 0x52494646
42# define CHUNK_ID_RIFX 0x52494658
43# define CHUNK_ID_LIST 0x4C495354
45# define LIST_TYPE_INFO 0x494E464F
46# define CHUNK_ID_ICMT 0x49434D54
47# define CHUNK_ID_ICOP 0x49434F50
48# define CHUNK_ID_ICRD 0x49435244
49# define CHUNK_ID_IENG 0x49454E47
50# define CHUNK_ID_INAM 0x494E414D
51# define CHUNK_ID_IPRD 0x49505244
52# define CHUNK_ID_ISFT 0x49534654
54# define CHUNK_ID_SMPL 0x736D706C
57# define CHUNK_ID_RIFF 0x46464952
58# define CHUNK_ID_RIFX 0x58464952
59# define CHUNK_ID_LIST 0x5453494C
61# define LIST_TYPE_INFO 0x4F464E49
62# define CHUNK_ID_ICMT 0x544D4349
63# define CHUNK_ID_ICOP 0x504F4349
64# define CHUNK_ID_ICRD 0x44524349
65# define CHUNK_ID_IENG 0x474E4549
66# define CHUNK_ID_INAM 0x4D414E49
67# define CHUNK_ID_IPRD 0x44525049
68# define CHUNK_ID_ISFT 0x54465349
70# define CHUNK_ID_SMPL 0x6C706D73
74#define CHUNK_HEADER_SIZE(fileOffsetSize) (4 + fileOffsetSize)
75#define LIST_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize)
76#define RIFF_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize)
104 typedef std::string String;
111 stream_mode_read = 0,
112 stream_mode_read_write = 1,
113 stream_mode_closed = 2
119 stream_end_reached = 1,
170 std::vector<progress_t>
subdivide(
int iSubtasks);
171 std::vector<progress_t>
subdivide(std::vector<float> vSubTaskPortions);
229 mutable std::map<std::thread::id,file_offset_t> byThread;
230 mutable std::mutex mutex;
234 Chunk(File* pFile, List* pParent, uint32_t uiChunkID,
file_offset_t ullBodySize);
238 inline static String convertToString(uint32_t word) {
240 for (
int i = 0; i < 4; i++) {
241 uint8_t
byte = *((uint8_t*)(&word) + i);
261 class List :
public Chunk {
285 typedef std::map<uint32_t,
RIFF::
Chunk*> ChunkMap;
286 typedef std::vector<
Chunk*> ChunkList;
287 typedef std::set<
Chunk*> ChunkSet;
290 ChunkList* pSubChunks;
291 ChunkMap* pSubChunksMap;
292 ChunkList::iterator ChunksIterator;
293 ChunkList::iterator ListIterator;
299 void LoadSubChunks(
progress_t* pProgress = NULL);
300 void LoadSubChunksRecursively(
progress_t* pProgress = NULL);
304 void DeleteChunkList();
327 File(uint32_t FileType);
328 File(
const String& path);
333 String GetFileName()
const;
334 void SetFileName(
const String& path);
347 virtual void Save(
const String& path,
progress_t* pProgress = NULL);
355 struct IO : HandlePair {
357 mutable std::map<std::thread::id,HandlePair> byThread;
358 mutable std::mutex mutex;
373 void __openExistingFile(
const String& path, uint32_t* FileType = NULL);
384 HandlePair& FileHandlePairUnsafeRef();
385 bool SetModeInternal(
stream_mode_t NewMode,
bool* pResetPos);
395 Exception(String format, ...);
396 Exception(String format, va_list arg);
398 virtual ~Exception() {}
402 static String assemble(String format, va_list arg);
File * GetFile() const
Returns pointer to the chunk's File object.
List * GetParent() const
Returns pointer to the chunk's parent list chunk.
file_offset_t WriteInt16(int16_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 16 Bit signed integer words from the buffer pointed by pData to the chunk'...
virtual void __resetPos()
Sets Chunk's read/write position to zero.
void Resize(file_offset_t NewSize)
Resize chunk.
int8_t ReadInt8()
Reads one 8 Bit signed integer word and increments the position within the chunk.
uint32_t ReadUint32()
Reads one 32 Bit unsigned integer word and increments the position within the chunk.
stream_state_t GetState() const
Returns the current state of the chunk object.
file_offset_t SetPos(file_offset_t Where, stream_whence_t Whence=stream_start)
Sets the position within the chunk body, thus within the data portion of the chunk (in bytes).
file_offset_t GetFilePos() const
Current, actual offset in file of current chunk data body read/write position.
void ReleaseChunkData()
Free loaded chunk body from RAM.
file_offset_t Write(void *pData, file_offset_t WordCount, file_offset_t WordSize)
Writes WordCount number of data words with given WordSize from the buffer pointed by pData.
file_offset_t RemainingBytes() const
Returns the number of bytes left to read in the chunk body.
int16_t ReadInt16()
Reads one 16 Bit signed integer word and increments the position within the chunk.
virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t *pProgress=NULL)
Write chunk persistently e.g.
file_offset_t GetPos() const
Current read/write position within the chunk data body (starting with 0).
file_offset_t WriteInt32(int32_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 32 Bit signed integer words from the buffer pointed by pData to the chunk'...
file_offset_t ReadSceptical(void *pData, file_offset_t WordCount, file_offset_t WordSize)
Just an internal wrapper for the main Read() method with additional Exception throwing on errors.
virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize)
Returns the actual total size in bytes (including header) of this Chunk if being stored to a file.
file_offset_t Read(void *pData, file_offset_t WordCount, file_offset_t WordSize)
Reads WordCount number of data words with given WordSize and copies it into a buffer pointed by pData...
uint32_t GetChunkID() const
Chunk ID in unsigned integer representation.
void * LoadChunkData()
Load chunk body into RAM.
String GetChunkIDString() const
Returns the String representation of the chunk's ID (e.g.
uint8_t ReadUint8()
Reads one 8 Bit unsigned integer word and increments the position within the chunk.
file_offset_t WriteInt8(int8_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 8 Bit signed integer words from the buffer pointed by pData to the chunk's...
file_offset_t WriteUint16(uint16_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 16 Bit unsigned integer words from the buffer pointed by pData to the chun...
file_offset_t WriteUint32(uint32_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 32 Bit unsigned integer words from the buffer pointed by pData to the chun...
void ReadString(String &s, int size)
Reads a null-padded string of size characters and copies it into the string s.
uint16_t ReadUint16()
Reads one 16 Bit unsigned integer word and increments the position within the chunk.
file_offset_t GetSize() const
Chunk size in bytes (without header, thus the chunk data body)
int32_t ReadInt32()
Reads one 32 Bit signed integer word and increments the position within the chunk.
file_offset_t WriteUint8(uint8_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 8 Bit unsigned integer words from the buffer pointed by pData to the chunk...
file_offset_t GetNewSize() const
New chunk size if it was modified with Resize(), otherwise value returned will be equal to GetSize().
Handle FileWriteHandle() const
Returns the OS dependent file I/O write handle intended to be used by the calling thread.
HandlePair FileHandlePair() const
Returns the OS dependent file I/O read and write handles intended to be used by the calling thread.
bool SetMode(stream_mode_t NewMode)
Change file access mode.
int GetRequiredFileOffsetSize()
Returns the required size (in bytes) of file offsets stored in the headers of all chunks of this file...
int GetFileOffsetSize() const
Returns the current size (in bytes) of file offsets stored in the headers of all chunks of this file.
File(uint32_t FileType)
Create new RIFF file.
file_offset_t GetCurrentFileSize() const
Returns the current size of this file (in bytes) as it is currently yet stored on disk.
void SetByteOrder(endian_t Endian)
Set the byte order to be used when saving.
bool IsNew() const
Returns true if this file has been created new from scratch and has not been stored to disk yet.
file_offset_t GetRequiredFileSize()
Returns the required size (in bytes) for this RIFF File to be saved to disk.
Handle FileHandle() const
Returns the OS dependent file I/O read handle intended to be used by the calling thread.
bool IsIOPerThread() const
Whether file streams are independent for each thread.
stream_mode_t GetMode() const
Returns the file I/O mode currently being available for the calling thread for this RIFF file (either...
virtual void Save(progress_t *pProgress=NULL)
Save changes to same file.
layout_t Layout
An ordinary RIFF file is always set to layout_standard.
int Handle
OS dependent type serving as file handle / descriptor for OS dependent file I/O operations.
int FileOffsetSize
Size of file offsets (in bytes) when this file was opened (or saved the last time).
void SetIOPerThread(bool enable)
Enable/disable file streams being independent for each thread.
size_t CountSubLists()
Returns number of sublists within the list.
Chunk * GetSubChunk(uint32_t ChunkID)
Returns subchunk with chunk ID ChunkID within this chunk list.
virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize)
Returns the actual total size in bytes (including List chunk header and all subchunks) of this List C...
void MoveSubChunk(Chunk *pSrc, Chunk *pDst)
Moves a sub chunk witin this list.
List * GetFirstSubList()
Returns the first sublist within the list (that is a subchunk with chunk ID "LIST").
List * AddSubList(uint32_t uiListType)
Creates a new list sub chunk.
Chunk * GetFirstSubChunk()
Returns the first subchunk within the list (which may be an ordinary chunk as well as a list chunk).
virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t *pProgress=NULL)
Write list chunk persistently e.g.
void DeleteSubChunk(Chunk *pSubChunk)
Removes a sub chunk.
List * GetSubListAt(size_t pos)
Returns sublist chunk with list type ListType at supplied pos position among all subchunks of type Li...
size_t CountSubChunks()
Returns number of subchunks within the list (including list chunks).
List * GetSubList(uint32_t ListType)
Returns sublist chunk with list type ListType within this chunk list.
Chunk * GetNextSubChunk()
Returns the next subchunk within the list (which may be an ordinary chunk as well as a list chunk).
uint32_t GetListType() const
Returns unsigned integer representation of the list's ID.
String GetListTypeString() const
Returns string representation of the lists's id.
List * GetNextSubList()
Returns the next sublist (that is a subchunk with chunk ID "LIST") within the list.
Chunk * AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize)
Creates a new sub chunk.
virtual void __resetPos()
Sets List Chunk's read/write position to zero and causes all sub chunks to do the same.
Chunk * GetSubChunkAt(size_t pos)
Returns subchunk at supplied pos position within this chunk list.
RIFF specific classes and definitions.
String libraryVersion()
Returns version of this C++ library.
stream_whence_t
File stream position dependent to these relations.
stream_state_t
Current state of the file stream.
String libraryName()
Returns the name of this C++ library.
offset_size_t
Size of RIFF file offsets used in all RIFF chunks' headers.
@ offset_size_64bit
Always use 64 bit offsets (even for files smaller than 4 GB).
@ offset_size_auto
Use 32 bit offsets for files smaller than 4 GB, use 64 bit offsets for files equal or larger than 4 G...
@ offset_size_32bit
Always use 32 bit offsets (even for files larger than 4 GB).
stream_mode_t
Whether file stream is open in read or in read/write mode.
layout_t
General RIFF chunk structure of a RIFF file.
@ layout_standard
Standard RIFF file layout: First chunk in file is a List chunk which contains all other chunks and th...
@ layout_flat
Not a "real" RIFF file: First chunk in file is an ordinary data chunk, not a List chunk,...
endian_t
Alignment of data bytes in memory (system dependant).
uint64_t file_offset_t
Type used by libgig for handling file positioning during file I/O tasks.
Used for indicating the progress of a certain task.
float __range_min
Only for internal usage, do not modify!
std::vector< progress_t > subdivide(int iSubtasks)
Divides this progress task into the requested amount of equal weighted sub-progress tasks and returns...
void(* callback)(progress_t *)
Callback function pointer which has to be assigned to a function for progress notification.
float factor
Reflects current progress as value between 0.0 and 1.0.
void * custom
This pointer can be used for arbitrary data.
float __range_max
Only for internal usage, do not modify!