40 # if defined(__cplusplus) && __cplusplus >= 201103L
41 # define OVERRIDE override
60 # include <sys/types.h>
61 # include <sys/stat.h>
66 #if defined _MSC_VER && _MSC_VER < 1600
68 typedef __int8 int8_t;
69 typedef __int16 int16_t;
70 typedef __int32 int32_t;
71 typedef __int64 int64_t;
72 typedef unsigned __int8 uint8_t;
73 typedef unsigned __int16 uint16_t;
74 typedef unsigned __int32 uint32_t;
75 typedef unsigned __int64 uint64_t;
81 # if (_WIN32 && !_WIN64) || (__GNUC__ && !(__x86_64__ || __ppc64__))
82 # if _WIN32_WINNT < 0x0501
84 # define _WIN32_WINNT 0x0501
88 typedef unsigned int uint;
94 # define CHUNK_ID_RIFF 0x52494646
95 # define CHUNK_ID_RIFX 0x52494658
96 # define CHUNK_ID_LIST 0x4C495354
98 # define LIST_TYPE_INFO 0x494E464F
99 # define CHUNK_ID_ICMT 0x49434D54
100 # define CHUNK_ID_ICOP 0x49434F50
101 # define CHUNK_ID_ICRD 0x49435244
102 # define CHUNK_ID_IENG 0x49454E47
103 # define CHUNK_ID_INAM 0x494E414D
104 # define CHUNK_ID_IPRD 0x49505244
105 # define CHUNK_ID_ISFT 0x49534654
107 # define CHUNK_ID_SMPL 0x736D706C
109 #else // little endian
110 # define CHUNK_ID_RIFF 0x46464952
111 # define CHUNK_ID_RIFX 0x58464952
112 # define CHUNK_ID_LIST 0x5453494C
114 # define LIST_TYPE_INFO 0x4F464E49
115 # define CHUNK_ID_ICMT 0x544D4349
116 # define CHUNK_ID_ICOP 0x504F4349
117 # define CHUNK_ID_ICRD 0x44524349
118 # define CHUNK_ID_IENG 0x474E4549
119 # define CHUNK_ID_INAM 0x4D414E49
120 # define CHUNK_ID_IPRD 0x44525049
121 # define CHUNK_ID_ISFT 0x54465349
123 # define CHUNK_ID_SMPL 0x6C706D73
125 #endif // WORDS_BIGENDIAN
127 #define CHUNK_HEADER_SIZE(fileOffsetSize) (4 + fileOffsetSize)
128 #define LIST_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize)
129 #define RIFF_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize)
157 typedef std::string String;
164 stream_mode_read = 0,
165 stream_mode_read_write = 1,
166 stream_mode_closed = 2
172 stream_end_reached = 1,
223 std::vector<progress_t>
subdivide(
int iSubtasks);
224 std::vector<progress_t>
subdivide(std::vector<float> vSubTaskPortions);
236 uint32_t GetChunkID()
const {
return ChunkID; }
237 File* GetFile()
const {
return pFile; }
238 List* GetParent()
const {
return pParent; }
239 file_offset_t GetSize()
const {
return ullCurrentChunkSize; }
242 file_offset_t GetFilePos()
const {
return ullStartPos + ullPos; }
287 inline static String convertToString(uint32_t word) {
289 for (
int i = 0; i < 4; i++) {
290 uint8_t
byte = *((uint8_t*)(&word) + i);
312 uint32_t GetListType()
const {
return ListType; }
330 typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap;
331 typedef std::list<Chunk*> ChunkList;
332 typedef std::set<Chunk*> ChunkSet;
335 ChunkList* pSubChunks;
336 ChunkMap* pSubChunksMap;
337 ChunkList::iterator ChunksIterator;
338 ChunkList::iterator ListIterator;
344 void LoadSubChunks(
progress_t* pProgress = NULL);
345 void LoadSubChunksRecursively(
progress_t* pProgress = NULL);
349 void DeleteChunkList();
360 File(uint32_t FileType);
361 File(
const String& path);
366 String GetFileName()
const;
367 void SetFileName(
const String& path);
377 virtual void Save(
const String& path,
progress_t* pProgress = NULL);
402 void __openExistingFile(
const String& path, uint32_t* FileType = NULL);
429 static String assemble(String format, va_list arg);
Chunk * GetNextSubChunk()
Returns the next subchunk within the list (which may be an ordinary chunk as well as a list chunk).
size_t CountSubChunks()
Returns number of subchunks within the list (including list chunks).
file_offset_t GetCurrentFileSize() const
Returns the current size of this file (in bytes) as it is currently yet stored on disk.
int GetRequiredFileOffsetSize()
Returns the required size (in bytes) of file offsets stored in the headers of all chunks of this file...
virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t *pProgress=NULL)
Write chunk persistently e.g.
int hFileWrite
handle / descriptor for writing to (some) file
virtual void __resetPos()
Sets List Chunk's read/write position to zero and causes all sub chunks to do the same.
uint8_t ReadUint8()
Reads one 8 Bit unsigned integer word and increments the position within the chunk.
uint16_t ReadUint16()
Reads one 16 Bit unsigned integer word and increments the position within the chunk.
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.
List * GetFirstSubList()
Returns the first sublist within the list (that is a subchunk with chunk ID "LIST").
bool IsNew() const
Returns true if this file has been created new from scratch and has not been stored to disk yet.
List * GetNextSubList()
Returns the next sublist (that is a subchunk with chunk ID "LIST") within the list.
void * custom
This pointer can be used for arbitrary data.
void ReleaseChunkData()
Free loaded chunk body from RAM.
Chunk * AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize)
Creates a new sub chunk.
Used for indicating the progress of a certain task.
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...
layout_t
General RIFF chunk structure of a RIFF file.
void MoveSubChunk(Chunk *pSrc, Chunk *pDst)
Moves a sub chunk witin this list.
Chunk * GetFirstSubChunk()
Returns the first subchunk within the list (which may be an ordinary chunk as well as a list chunk).
List * AddSubList(uint32_t uiListType)
Creates a new list sub chunk.
endian_t
Alignment of data bytes in memory (system dependant).
int FileOffsetSize
Size of file offsets (in bytes) when this file was opened (or saved the last time).
@ 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...
float factor
Reflects current progress as value between 0.0 and 1.0.
file_offset_t RemainingBytes() const
Returns the number of bytes left to read in the chunk body.
offset_size_t
Size of RIFF file offsets used in all RIFF chunks' headers.
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 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 WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t *pProgress=NULL)
Write list chunk persistently e.g.
virtual void Save(progress_t *pProgress=NULL)
Save changes to same file.
String GetChunkIDString() const
Returns the String representation of the chunk's ID (e.g.
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).
layout_t Layout
An ordinary RIFF file is always set to layout_standard.
file_offset_t GetRequiredFileSize()
Returns the required size (in bytes) for this RIFF File to be saved to disk.
int8_t ReadInt8()
Reads one 8 Bit signed integer word and increments the position within the chunk.
void(* callback)(progress_t *)
Callback function pointer which has to be assigned to a function for progress notification.
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'...
void ReadString(String &s, int size)
Reads a null-padded string of size characters and copies it into the string s.
void Resize(file_offset_t NewSize)
Resize chunk.
stream_whence_t
File stream position dependent to these relations.
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...
size_t CountSubLists()
Returns number of sublists within the list.
stream_state_t
Current state of the file stream.
void SetByteOrder(endian_t Endian)
Set the byte order to be used when saving.
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'...
float __range_max
Only for internal usage, do not modify!
String libraryName()
Returns the name of this C++ library.
@ layout_flat
Not a "real" RIFF file: First chunk in file is an ordinary data chunk, not a List chunk,...
@ offset_size_64bit
Always use 64 bit offsets (even for files smaller than 4 GB).
float __range_min
Only for internal usage, do not modify!
stream_state_t GetState() const
Returns the current state of the chunk object.
List * GetSubList(uint32_t ListType)
Returns sublist chunk with list type ListType within this chunk list.
stream_mode_t
Whether file stream is open in read or in read/write mode.
String GetListTypeString() const
Returns string representation of the lists's id.
void DeleteSubChunk(Chunk *pSubChunk)
Removes a sub chunk.
int GetFileOffsetSize() const
Returns the current size (in bytes) of file offsets stored in the headers of all chunks of this file.
virtual void __resetPos()
Sets Chunk's read/write position to zero.
String libraryVersion()
Returns version of this C++ library.
int hFileRead
handle / descriptor for reading from file
@ offset_size_32bit
Always use 32 bit offsets (even for files larger than 4 GB).
std::vector< progress_t > subdivide(int iSubtasks)
Divides this progress task into the requested amount of equal weighted sub-progress tasks and returns...
@ layout_standard
Standard RIFF file layout: First chunk in file is a List chunk which contains all other chunks and th...
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...
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 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...
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...
File(uint32_t FileType)
Create new RIFF file.
uint64_t file_offset_t
Type used by libgig for handling file positioning during file I/O tasks.
int32_t ReadInt32()
Reads one 32 Bit signed integer word and increments the position within the chunk.
void * LoadChunkData()
Load chunk body into RAM.
RIFF specific classes and definitions.
uint32_t ReadUint32()
Reads one 32 Bit unsigned integer word and increments the position within the chunk.
Will be thrown whenever an error occurs while handling a RIFF file.
Chunk * GetSubChunk(uint32_t ChunkID)
Returns subchunk with chunk ID ChunkID within this chunk list.
bool SetMode(stream_mode_t NewMode)
Change file access mode.
int16_t ReadInt16()
Reads one 16 Bit signed integer word and increments the position within the chunk.