2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2007 by Christian Schoenebeck * |
* Copyright (C) 2003-2021 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
36 |
# define DEBUG 0 |
# define DEBUG 0 |
37 |
#endif |
#endif |
38 |
|
|
39 |
|
#ifndef OVERRIDE |
40 |
|
# if defined(__cplusplus) && __cplusplus >= 201103L |
41 |
|
# define OVERRIDE override |
42 |
|
# else |
43 |
|
# define OVERRIDE |
44 |
|
# endif |
45 |
|
#endif |
46 |
|
|
47 |
#include <string> |
#include <string> |
48 |
#include <list> |
#include <list> |
|
#include <set> |
|
49 |
#include <map> |
#include <map> |
50 |
|
#include <set> |
51 |
|
#include <vector> |
52 |
#include <iostream> |
#include <iostream> |
53 |
|
#include <stdarg.h> |
54 |
|
#include <thread> |
55 |
|
#include <mutex> |
56 |
|
|
57 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
58 |
# include <config.h> |
# include <config.h> |
65 |
# include <unistd.h> |
# include <unistd.h> |
66 |
#endif // POSIX |
#endif // POSIX |
67 |
|
|
68 |
|
#if defined _MSC_VER && _MSC_VER < 1600 |
69 |
|
// Visual C++ 2008 doesn't have stdint.h |
70 |
|
typedef __int8 int8_t; |
71 |
|
typedef __int16 int16_t; |
72 |
|
typedef __int32 int32_t; |
73 |
|
typedef __int64 int64_t; |
74 |
|
typedef unsigned __int8 uint8_t; |
75 |
|
typedef unsigned __int16 uint16_t; |
76 |
|
typedef unsigned __int32 uint32_t; |
77 |
|
typedef unsigned __int64 uint64_t; |
78 |
|
#else |
79 |
#include <stdint.h> |
#include <stdint.h> |
80 |
|
#endif |
81 |
|
|
82 |
#ifdef WIN32 |
#ifdef WIN32 |
83 |
|
# if (_WIN32 && !_WIN64) || (__GNUC__ && !(__x86_64__ || __ppc64__)) /* if 32 bit windows compilation */ |
84 |
|
# if _WIN32_WINNT < 0x0501 |
85 |
|
# undef _WIN32_WINNT |
86 |
|
# define _WIN32_WINNT 0x0501 /* Win XP (no service pack): required for 32 bit compilation for GetFileSizeEx() to be declared by windows.h */ |
87 |
|
# endif |
88 |
|
# endif |
89 |
# include <windows.h> |
# include <windows.h> |
90 |
typedef unsigned int uint; |
typedef unsigned int uint; |
91 |
#endif // WIN32 |
#endif // WIN32 |
96 |
# define CHUNK_ID_RIFF 0x52494646 |
# define CHUNK_ID_RIFF 0x52494646 |
97 |
# define CHUNK_ID_RIFX 0x52494658 |
# define CHUNK_ID_RIFX 0x52494658 |
98 |
# define CHUNK_ID_LIST 0x4C495354 |
# define CHUNK_ID_LIST 0x4C495354 |
99 |
|
|
100 |
|
# define LIST_TYPE_INFO 0x494E464F |
101 |
|
# define CHUNK_ID_ICMT 0x49434D54 |
102 |
|
# define CHUNK_ID_ICOP 0x49434F50 |
103 |
|
# define CHUNK_ID_ICRD 0x49435244 |
104 |
|
# define CHUNK_ID_IENG 0x49454E47 |
105 |
|
# define CHUNK_ID_INAM 0x494E414D |
106 |
|
# define CHUNK_ID_IPRD 0x49505244 |
107 |
|
# define CHUNK_ID_ISFT 0x49534654 |
108 |
|
|
109 |
|
# define CHUNK_ID_SMPL 0x736D706C |
110 |
|
|
111 |
#else // little endian |
#else // little endian |
112 |
# define CHUNK_ID_RIFF 0x46464952 |
# define CHUNK_ID_RIFF 0x46464952 |
113 |
# define CHUNK_ID_RIFX 0x58464952 |
# define CHUNK_ID_RIFX 0x58464952 |
114 |
# define CHUNK_ID_LIST 0x5453494C |
# define CHUNK_ID_LIST 0x5453494C |
|
#endif // WORDS_BIGENDIAN |
|
115 |
|
|
116 |
#define CHUNK_HEADER_SIZE 8 |
# define LIST_TYPE_INFO 0x4F464E49 |
117 |
#define LIST_HEADER_SIZE 12 |
# define CHUNK_ID_ICMT 0x544D4349 |
118 |
#define RIFF_HEADER_SIZE 12 |
# define CHUNK_ID_ICOP 0x504F4349 |
119 |
|
# define CHUNK_ID_ICRD 0x44524349 |
120 |
|
# define CHUNK_ID_IENG 0x474E4549 |
121 |
|
# define CHUNK_ID_INAM 0x4D414E49 |
122 |
|
# define CHUNK_ID_IPRD 0x44525049 |
123 |
|
# define CHUNK_ID_ISFT 0x54465349 |
124 |
|
|
125 |
|
# define CHUNK_ID_SMPL 0x6C706D73 |
126 |
|
|
127 |
|
#endif // WORDS_BIGENDIAN |
128 |
|
|
129 |
|
#define CHUNK_HEADER_SIZE(fileOffsetSize) (4 + fileOffsetSize) |
130 |
|
#define LIST_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize) |
131 |
|
#define RIFF_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize) |
132 |
|
|
133 |
/** |
/** |
134 |
* @brief RIFF specific classes and definitions |
* @brief RIFF specific classes and definitions |
158 |
|
|
159 |
typedef std::string String; |
typedef std::string String; |
160 |
|
|
161 |
|
/** Type used by libgig for handling file positioning during file I/O tasks. */ |
162 |
|
typedef uint64_t file_offset_t; |
163 |
|
|
164 |
/** Whether file stream is open in read or in read/write mode. */ |
/** Whether file stream is open in read or in read/write mode. */ |
165 |
typedef enum { |
enum stream_mode_t { |
166 |
stream_mode_read = 0, |
stream_mode_read = 0, |
167 |
stream_mode_read_write = 1, |
stream_mode_read_write = 1, |
168 |
stream_mode_closed = 2 |
stream_mode_closed = 2 |
169 |
} stream_mode_t; |
}; |
170 |
|
|
171 |
/** Current state of the file stream. */ |
/** Current state of the file stream. */ |
172 |
typedef enum { |
enum stream_state_t { |
173 |
stream_ready = 0, |
stream_ready = 0, |
174 |
stream_end_reached = 1, |
stream_end_reached = 1, |
175 |
stream_closed = 2 |
stream_closed = 2 |
176 |
} stream_state_t; |
}; |
177 |
|
|
178 |
/** File stream position dependent to these relations. */ |
/** File stream position dependent to these relations. */ |
179 |
typedef enum { |
enum stream_whence_t { |
180 |
stream_start = 0, |
stream_start = 0, |
181 |
stream_curpos = 1, |
stream_curpos = 1, |
182 |
stream_backward = 2, |
stream_backward = 2, |
183 |
stream_end = 3 |
stream_end = 3 |
184 |
} stream_whence_t; |
}; |
185 |
|
|
186 |
/** Alignment of data bytes in memory (system dependant). */ |
/** Alignment of data bytes in memory (system dependant). */ |
187 |
typedef enum { |
enum endian_t { |
188 |
endian_little = 0, |
endian_little = 0, |
189 |
endian_big = 1, |
endian_big = 1, |
190 |
endian_native = 2 |
endian_native = 2 |
191 |
} endian_t; |
}; |
192 |
|
|
193 |
|
/** General RIFF chunk structure of a RIFF file. */ |
194 |
|
enum layout_t { |
195 |
|
layout_standard = 0, ///< Standard RIFF file layout: First chunk in file is a List chunk which contains all other chunks and there are no chunks outside the scope of that very first (List) chunk. |
196 |
|
layout_flat = 1 ///< Not a "real" RIFF file: First chunk in file is an ordinary data chunk, not a List chunk, and there might be other chunks after that first chunk. |
197 |
|
}; |
198 |
|
|
199 |
|
/** Size of RIFF file offsets used in all RIFF chunks' headers. @see File::GetFileOffsetSize() */ |
200 |
|
enum offset_size_t { |
201 |
|
offset_size_auto = 0, ///< Use 32 bit offsets for files smaller than 4 GB, use 64 bit offsets for files equal or larger than 4 GB. |
202 |
|
offset_size_32bit = 4, ///< Always use 32 bit offsets (even for files larger than 4 GB). |
203 |
|
offset_size_64bit = 8 ///< Always use 64 bit offsets (even for files smaller than 4 GB). |
204 |
|
}; |
205 |
|
|
206 |
|
/** |
207 |
|
* @brief Used for indicating the progress of a certain task. |
208 |
|
* |
209 |
|
* The function pointer argument has to be supplied with a valid |
210 |
|
* function of the given signature which will then be called on |
211 |
|
* progress changes. An equivalent progress_t structure will be passed |
212 |
|
* back as argument to the callback function on each progress change. |
213 |
|
* The factor field of the supplied progress_t structure will then |
214 |
|
* reflect the current progress as value between 0.0 and 1.0. You might |
215 |
|
* want to use the custom field for data needed in your callback |
216 |
|
* function. |
217 |
|
*/ |
218 |
|
struct progress_t { |
219 |
|
void (*callback)(progress_t*); ///< Callback function pointer which has to be assigned to a function for progress notification. |
220 |
|
float factor; ///< Reflects current progress as value between 0.0 and 1.0. |
221 |
|
void* custom; ///< This pointer can be used for arbitrary data. |
222 |
|
float __range_min; ///< Only for internal usage, do not modify! |
223 |
|
float __range_max; ///< Only for internal usage, do not modify! |
224 |
|
progress_t(); |
225 |
|
std::vector<progress_t> subdivide(int iSubtasks); |
226 |
|
std::vector<progress_t> subdivide(std::vector<float> vSubTaskPortions); |
227 |
|
}; |
228 |
|
|
229 |
/** @brief Ordinary RIFF Chunk |
/** @brief Ordinary RIFF Chunk |
230 |
* |
* |
233 |
*/ |
*/ |
234 |
class Chunk { |
class Chunk { |
235 |
public: |
public: |
236 |
Chunk(File* pFile, unsigned long StartPos, List* Parent); |
Chunk(File* pFile, file_offset_t StartPos, List* Parent); |
237 |
String GetChunkIDString(); |
String GetChunkIDString() const; |
238 |
uint32_t GetChunkID() { return ChunkID; }; ///< Chunk ID in unsigned integer representation. |
uint32_t GetChunkID() const { return ChunkID; } ///< Chunk ID in unsigned integer representation. |
239 |
List* GetParent() { return pParent; }; ///< Returns pointer to the chunk's parent list chunk. |
File* GetFile() const { return pFile; } ///< Returns pointer to the chunk's File object. |
240 |
unsigned long GetSize() { return CurrentChunkSize; }; ///< Chunk size in bytes (without header, thus the chunk data body) |
List* GetParent() const { return pParent; } ///< Returns pointer to the chunk's parent list chunk. |
241 |
unsigned long GetNewSize() { return NewChunkSize; }; ///< New chunk size if it was modified with Resize(). |
file_offset_t GetSize() const { return ullCurrentChunkSize; } ///< Chunk size in bytes (without header, thus the chunk data body) |
242 |
unsigned long GetPos() { return ulPos; }; ///< Position within the chunk data body |
file_offset_t GetNewSize() const { return ullNewChunkSize; } ///< New chunk size if it was modified with Resize(), otherwise value returned will be equal to GetSize(). |
243 |
unsigned long GetFilePos() { return ulStartPos + ulPos; }; ///< Current, actual offset in file. |
file_offset_t GetPos() const; |
244 |
unsigned long SetPos(unsigned long Where, stream_whence_t Whence = stream_start); |
file_offset_t GetFilePos() const; |
245 |
unsigned long RemainingBytes(); |
file_offset_t SetPos(file_offset_t Where, stream_whence_t Whence = stream_start); |
246 |
stream_state_t GetState(); |
file_offset_t RemainingBytes() const; |
247 |
unsigned long Read(void* pData, unsigned long WordCount, unsigned long WordSize); |
stream_state_t GetState() const; |
248 |
unsigned long ReadInt8(int8_t* pData, unsigned long WordCount = 1); |
file_offset_t Read(void* pData, file_offset_t WordCount, file_offset_t WordSize); |
249 |
unsigned long ReadUint8(uint8_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt8(int8_t* pData, file_offset_t WordCount = 1); |
250 |
unsigned long ReadInt16(int16_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadUint8(uint8_t* pData, file_offset_t WordCount = 1); |
251 |
unsigned long ReadUint16(uint16_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt16(int16_t* pData, file_offset_t WordCount = 1); |
252 |
unsigned long ReadInt32(int32_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadUint16(uint16_t* pData, file_offset_t WordCount = 1); |
253 |
unsigned long ReadUint32(uint32_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt32(int32_t* pData, file_offset_t WordCount = 1); |
254 |
|
file_offset_t ReadUint32(uint32_t* pData, file_offset_t WordCount = 1); |
255 |
int8_t ReadInt8(); |
int8_t ReadInt8(); |
256 |
uint8_t ReadUint8(); |
uint8_t ReadUint8(); |
257 |
int16_t ReadInt16(); |
int16_t ReadInt16(); |
258 |
uint16_t ReadUint16(); |
uint16_t ReadUint16(); |
259 |
int32_t ReadInt32(); |
int32_t ReadInt32(); |
260 |
uint32_t ReadUint32(); |
uint32_t ReadUint32(); |
261 |
unsigned long Write(void* pData, unsigned long WordCount, unsigned long WordSize); |
void ReadString(String& s, int size); |
262 |
unsigned long WriteInt8(int8_t* pData, unsigned long WordCount = 1); |
file_offset_t Write(void* pData, file_offset_t WordCount, file_offset_t WordSize); |
263 |
unsigned long WriteUint8(uint8_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt8(int8_t* pData, file_offset_t WordCount = 1); |
264 |
unsigned long WriteInt16(int16_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteUint8(uint8_t* pData, file_offset_t WordCount = 1); |
265 |
unsigned long WriteUint16(uint16_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt16(int16_t* pData, file_offset_t WordCount = 1); |
266 |
unsigned long WriteInt32(int32_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteUint16(uint16_t* pData, file_offset_t WordCount = 1); |
267 |
unsigned long WriteUint32(uint32_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt32(int32_t* pData, file_offset_t WordCount = 1); |
268 |
|
file_offset_t WriteUint32(uint32_t* pData, file_offset_t WordCount = 1); |
269 |
void* LoadChunkData(); |
void* LoadChunkData(); |
270 |
void ReleaseChunkData(); |
void ReleaseChunkData(); |
271 |
void Resize(int iNewSize); |
void Resize(file_offset_t NewSize); |
272 |
virtual ~Chunk(); |
virtual ~Chunk(); |
273 |
protected: |
protected: |
274 |
uint32_t ChunkID; |
uint32_t ChunkID; |
275 |
uint32_t CurrentChunkSize; /* in bytes */ |
file_offset_t ullCurrentChunkSize; /* in bytes */ |
276 |
uint32_t NewChunkSize; /* in bytes (if chunk was scheduled to be resized) */ |
file_offset_t ullNewChunkSize; /* in bytes (if chunk was scheduled to be resized) */ |
277 |
List* pParent; |
List* pParent; |
278 |
File* pFile; |
File* pFile; |
279 |
unsigned long ulStartPos; /* actual position in file where chunk (without header) starts */ |
file_offset_t ullStartPos; /* actual position in file where chunk data (without header) starts */ |
|
unsigned long ulPos; /* # of bytes from ulStartPos */ |
|
280 |
uint8_t* pChunkData; |
uint8_t* pChunkData; |
281 |
unsigned long ulChunkDataSize; |
file_offset_t ullChunkDataSize; |
282 |
|
struct ChunkPos { /* current read/write position as # of bytes from ulStartPos */ |
283 |
|
file_offset_t ullPos; /* used if File::IsIOPerThread() is false (default) */ |
284 |
|
mutable std::map<std::thread::id,file_offset_t> byThread; /* used if File::IsIOPerThread() is true */ |
285 |
|
mutable std::mutex mutex; /* for protecting concurrent changes on byThread map itself */ |
286 |
|
} chunkPos; |
287 |
|
|
288 |
Chunk(File* pFile); |
Chunk(File* pFile); |
289 |
Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize); |
Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize); |
290 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(file_offset_t filePos); |
291 |
void WriteHeader(unsigned long fPos); |
void WriteHeader(file_offset_t filePos); |
292 |
unsigned long ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize); |
file_offset_t ReadSceptical(void* pData, file_offset_t WordCount, file_offset_t WordSize); |
293 |
inline void swapBytes_16(void* Word) { |
inline static String convertToString(uint32_t word) { |
|
uint8_t byteCache = *((uint8_t*) Word); |
|
|
*((uint8_t*) Word) = *((uint8_t*) Word + 1); |
|
|
*((uint8_t*) Word + 1) = byteCache; |
|
|
} |
|
|
inline void swapBytes_32(void* Word) { |
|
|
uint8_t byteCache = *((uint8_t*) Word); |
|
|
*((uint8_t*) Word) = *((uint8_t*) Word + 3); |
|
|
*((uint8_t*) Word + 3) = byteCache; |
|
|
byteCache = *((uint8_t*) Word + 1); |
|
|
*((uint8_t*) Word + 1) = *((uint8_t*) Word + 2); |
|
|
*((uint8_t*) Word + 2) = byteCache; |
|
|
} |
|
|
inline void swapBytes(void* Word, unsigned long WordSize) { |
|
|
uint8_t byteCache; |
|
|
unsigned long lo = 0, hi = WordSize - 1; |
|
|
for (; lo < hi; hi--, lo++) { |
|
|
byteCache = *((uint8_t*) Word + lo); |
|
|
*((uint8_t*) Word + lo) = *((uint8_t*) Word + hi); |
|
|
*((uint8_t*) Word + hi) = byteCache; |
|
|
} |
|
|
} |
|
|
inline String convertToString(uint32_t word) { |
|
294 |
String result; |
String result; |
295 |
for (int i = 0; i < 4; i++) { |
for (int i = 0; i < 4; i++) { |
296 |
uint8_t byte = *((uint8_t*)(&word) + i); |
uint8_t byte = *((uint8_t*)(&word) + i); |
299 |
} |
} |
300 |
return result; |
return result; |
301 |
} |
} |
302 |
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize); |
303 |
|
virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL); |
304 |
virtual void __resetPos(); ///< Sets Chunk's read/write position to zero. |
virtual void __resetPos(); ///< Sets Chunk's read/write position to zero. |
305 |
|
|
306 |
friend class List; |
friend class List; |
307 |
|
private: |
308 |
|
file_offset_t& GetPosUnsafeRef(); |
309 |
}; |
}; |
310 |
|
|
311 |
/** @brief RIFF List Chunk |
/** @brief RIFF List Chunk |
315 |
*/ |
*/ |
316 |
class List : public Chunk { |
class List : public Chunk { |
317 |
public: |
public: |
318 |
List(File* pFile, unsigned long StartPos, List* Parent); |
List(File* pFile, file_offset_t StartPos, List* Parent); |
319 |
String GetListTypeString(); |
String GetListTypeString() const; |
320 |
uint32_t GetListType() { return ListType; } ///< Returns unsigned integer representation of the list's ID |
uint32_t GetListType() const { return ListType; } ///< Returns unsigned integer representation of the list's ID |
321 |
Chunk* GetSubChunk(uint32_t ChunkID); |
Chunk* GetSubChunk(uint32_t ChunkID); |
322 |
List* GetSubList(uint32_t ListType); |
List* GetSubList(uint32_t ListType); |
323 |
Chunk* GetFirstSubChunk(); |
Chunk* GetFirstSubChunk(); |
324 |
Chunk* GetNextSubChunk(); |
Chunk* GetNextSubChunk(); |
325 |
List* GetFirstSubList(); |
List* GetFirstSubList(); |
326 |
List* GetNextSubList(); |
List* GetNextSubList(); |
327 |
unsigned int CountSubChunks(); |
size_t CountSubChunks(); |
328 |
unsigned int CountSubChunks(uint32_t ChunkID); |
size_t CountSubChunks(uint32_t ChunkID); |
329 |
unsigned int CountSubLists(); |
size_t CountSubLists(); |
330 |
unsigned int CountSubLists(uint32_t ListType); |
size_t CountSubLists(uint32_t ListType); |
331 |
Chunk* AddSubChunk(uint32_t uiChunkID, uint uiBodySize); |
Chunk* AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize); |
332 |
List* AddSubList(uint32_t uiListType); |
List* AddSubList(uint32_t uiListType); |
333 |
void DeleteSubChunk(Chunk* pSubChunk); |
void DeleteSubChunk(Chunk* pSubChunk); |
334 |
void MoveSubChunk(Chunk* pSrc, Chunk* pDst); |
void MoveSubChunk(Chunk* pSrc, Chunk* pDst); // read API doc comments !!! |
335 |
|
void MoveSubChunk(Chunk* pSrc, List* pNewParent); |
336 |
virtual ~List(); |
virtual ~List(); |
337 |
protected: |
protected: |
338 |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
339 |
typedef std::list<Chunk*> ChunkList; |
typedef std::list<Chunk*> ChunkList; |
340 |
|
typedef std::set<Chunk*> ChunkSet; |
341 |
|
|
342 |
uint32_t ListType; |
uint32_t ListType; |
343 |
ChunkList* pSubChunks; |
ChunkList* pSubChunks; |
347 |
|
|
348 |
List(File* pFile); |
List(File* pFile); |
349 |
List(File* pFile, List* pParent, uint32_t uiListID); |
List(File* pFile, List* pParent, uint32_t uiListID); |
350 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(file_offset_t filePos); |
351 |
void WriteHeader(unsigned long fPos); |
void WriteHeader(file_offset_t filePos); |
352 |
void LoadSubChunks(); |
void LoadSubChunks(progress_t* pProgress = NULL); |
353 |
void LoadSubChunksRecursively(); |
void LoadSubChunksRecursively(progress_t* pProgress = NULL); |
354 |
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize); |
355 |
|
virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL); |
356 |
virtual void __resetPos(); ///< Sets List Chunk's read/write position to zero and causes all sub chunks to do the same. |
virtual void __resetPos(); ///< Sets List Chunk's read/write position to zero and causes all sub chunks to do the same. |
357 |
|
void DeleteChunkList(); |
358 |
}; |
}; |
359 |
|
|
360 |
/** @brief RIFF File |
/** @brief RIFF File |
365 |
*/ |
*/ |
366 |
class File : public List { |
class File : public List { |
367 |
public: |
public: |
368 |
|
/** |
369 |
|
* OS dependent type serving as file handle / descriptor for OS |
370 |
|
* dependent file I/O operations. |
371 |
|
*/ |
372 |
|
#if POSIX |
373 |
|
typedef int Handle; |
374 |
|
#elif defined(WIN32) |
375 |
|
typedef HANDLE Handle; |
376 |
|
#else |
377 |
|
typedef FILE* Handle; |
378 |
|
#endif |
379 |
|
|
380 |
File(uint32_t FileType); |
File(uint32_t FileType); |
381 |
File(const String& path); |
File(const String& path); |
382 |
stream_mode_t GetMode(); |
File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize = offset_size_auto); |
383 |
|
stream_mode_t GetMode() const; |
384 |
bool SetMode(stream_mode_t NewMode); |
bool SetMode(stream_mode_t NewMode); |
385 |
void SetByteOrder(endian_t Endian); |
void SetByteOrder(endian_t Endian); |
386 |
String GetFileName(); |
String GetFileName() const; |
387 |
virtual void Save(); |
void SetFileName(const String& path); |
388 |
virtual void Save(const String& path); |
Handle FileHandle() const; |
389 |
|
bool IsNew() const; |
390 |
|
layout_t GetLayout() const; |
391 |
|
file_offset_t GetCurrentFileSize() const; |
392 |
|
file_offset_t GetRequiredFileSize(); |
393 |
|
file_offset_t GetRequiredFileSize(offset_size_t fileOffsetSize); |
394 |
|
int GetFileOffsetSize() const; |
395 |
|
int GetRequiredFileOffsetSize(); |
396 |
|
bool IsIOPerThread() const; |
397 |
|
void SetIOPerThread(bool enable); |
398 |
|
|
399 |
|
virtual void Save(progress_t* pProgress = NULL); |
400 |
|
virtual void Save(const String& path, progress_t* pProgress = NULL); |
401 |
virtual ~File(); |
virtual ~File(); |
402 |
protected: |
protected: |
403 |
#if POSIX |
struct HandlePair { |
404 |
int hFileRead; ///< handle / descriptor for reading from file |
Handle hRead; ///< handle / descriptor for reading from file |
405 |
int hFileWrite; ///< handle / descriptor for writing to (some) file |
Handle hWrite; ///< handle / descriptor for writing to (some) file |
406 |
#elif defined(WIN32) |
stream_mode_t Mode; ///< State of handles (ro, rw, closed). |
407 |
HANDLE hFileRead; ///< handle / descriptor for reading from file |
}; |
408 |
HANDLE hFileWrite; ///< handle / descriptor for writing to (some) file |
struct IO : HandlePair { |
409 |
#else |
mutable std::map<std::thread::id,HandlePair> byThread; ///< Optional: Individual file handle pair for each thread separately. |
410 |
FILE* hFileRead; ///< handle / descriptor for reading from file |
mutable std::mutex mutex; ///< For protecting concurrent changes on the @c byThread map itself and replacing handles. |
411 |
FILE* hFileWrite; ///< handle / descriptor for writing to (some) file |
} io; |
|
#endif // POSIX |
|
412 |
String Filename; |
String Filename; |
413 |
bool bEndianNative; |
bool bEndianNative; |
414 |
|
bool bIsNewFile; |
415 |
|
layout_t Layout; ///< An ordinary RIFF file is always set to layout_standard. |
416 |
|
offset_size_t FileOffsetPreference; |
417 |
|
int FileOffsetSize; ///< Size of file offsets (in bytes) when this file was opened (or saved the last time). |
418 |
|
|
419 |
|
Handle FileWriteHandle() const; |
420 |
|
HandlePair FileHandlePair() const; |
421 |
|
|
|
void LogAsResized(Chunk* pResizedChunk); |
|
|
void UnlogResized(Chunk* pResizedChunk); |
|
422 |
friend class Chunk; |
friend class Chunk; |
423 |
friend class List; |
friend class List; |
424 |
private: |
private: |
425 |
stream_mode_t Mode; |
void __openExistingFile(const String& path, uint32_t* FileType = NULL); |
426 |
std::set<Chunk*> ResizedChunks; ///< All chunks which have been resized (enlarged / shortened). |
void ResizeFile(file_offset_t ullNewSize); |
|
|
|
|
unsigned long GetFileSize(); |
|
|
void ResizeFile(unsigned long ulNewSize); |
|
427 |
#if POSIX |
#if POSIX |
428 |
unsigned long __GetFileSize(int hFile); |
file_offset_t __GetFileSize(int hFile) const; |
429 |
#elif defined(WIN32) |
#elif defined(WIN32) |
430 |
unsigned long __GetFileSize(HANDLE hFile); |
file_offset_t __GetFileSize(HANDLE hFile) const; |
431 |
#else |
#else |
432 |
unsigned long __GetFileSize(FILE* hFile); |
file_offset_t __GetFileSize(FILE* hFile) const; |
433 |
#endif |
#endif |
434 |
|
int FileOffsetSizeFor(file_offset_t fileSize) const; |
435 |
|
void Cleanup(); |
436 |
|
HandlePair& FileHandlePairUnsafeRef(); |
437 |
}; |
}; |
438 |
|
|
439 |
/** |
/** |
443 |
public: |
public: |
444 |
String Message; |
String Message; |
445 |
|
|
446 |
Exception(String Message) { Exception::Message = Message; }; |
Exception(String format, ...); |
447 |
|
Exception(String format, va_list arg); |
448 |
void PrintMessage(); |
void PrintMessage(); |
449 |
virtual ~Exception() {}; |
virtual ~Exception() {} |
450 |
|
|
451 |
|
protected: |
452 |
|
Exception(); |
453 |
|
static String assemble(String format, va_list arg); |
454 |
}; |
}; |
455 |
|
|
456 |
String libraryName(); |
String libraryName(); |