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-2014 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 |
|
#ifdef __GNUC__ |
48 |
|
# define LIBGIG_DEPRECATED(msg) __attribute__ ((deprecated(msg))) |
49 |
|
#else |
50 |
|
# define LIBGIG_DEPRECATED |
51 |
|
#endif |
52 |
|
|
53 |
#include <string> |
#include <string> |
54 |
#include <list> |
#include <list> |
55 |
#include <map> |
#include <map> |
56 |
|
#include <set> |
57 |
|
#include <vector> |
58 |
#include <iostream> |
#include <iostream> |
59 |
|
#include <stdarg.h> |
60 |
|
#include <thread> |
61 |
|
#include <mutex> |
62 |
|
|
63 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
64 |
# include <config.h> |
# include <config.h> |
71 |
# include <unistd.h> |
# include <unistd.h> |
72 |
#endif // POSIX |
#endif // POSIX |
73 |
|
|
74 |
#ifdef _MSC_VER |
#if defined _MSC_VER && _MSC_VER < 1600 |
75 |
// Visual C++ 2008 doesn't have stdint.h |
// Visual C++ 2008 doesn't have stdint.h |
76 |
typedef __int8 int8_t; |
typedef __int8 int8_t; |
77 |
typedef __int16 int16_t; |
typedef __int16 int16_t; |
86 |
#endif |
#endif |
87 |
|
|
88 |
#ifdef WIN32 |
#ifdef WIN32 |
89 |
|
# if (_WIN32 && !_WIN64) || (__GNUC__ && !(__x86_64__ || __ppc64__)) /* if 32 bit windows compilation */ |
90 |
|
# if _WIN32_WINNT < 0x0501 |
91 |
|
# undef _WIN32_WINNT |
92 |
|
# define _WIN32_WINNT 0x0501 /* Win XP (no service pack): required for 32 bit compilation for GetFileSizeEx() to be declared by windows.h */ |
93 |
|
# endif |
94 |
|
# endif |
95 |
# include <windows.h> |
# include <windows.h> |
96 |
typedef unsigned int uint; |
typedef unsigned int uint; |
97 |
#endif // WIN32 |
#endif // WIN32 |
132 |
|
|
133 |
#endif // WORDS_BIGENDIAN |
#endif // WORDS_BIGENDIAN |
134 |
|
|
135 |
#define CHUNK_HEADER_SIZE 8 |
#define CHUNK_HEADER_SIZE(fileOffsetSize) (4 + fileOffsetSize) |
136 |
#define LIST_HEADER_SIZE 12 |
#define LIST_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize) |
137 |
#define RIFF_HEADER_SIZE 12 |
#define RIFF_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize) |
|
|
|
138 |
|
|
139 |
/** |
/** |
140 |
* @brief RIFF specific classes and definitions |
* @brief RIFF specific classes and definitions |
164 |
|
|
165 |
typedef std::string String; |
typedef std::string String; |
166 |
|
|
167 |
|
/** Type used by libgig for handling file positioning during file I/O tasks. */ |
168 |
|
typedef uint64_t file_offset_t; |
169 |
|
|
170 |
/** Whether file stream is open in read or in read/write mode. */ |
/** Whether file stream is open in read or in read/write mode. */ |
171 |
typedef enum { |
enum stream_mode_t { |
172 |
stream_mode_read = 0, |
stream_mode_read = 0, |
173 |
stream_mode_read_write = 1, |
stream_mode_read_write = 1, |
174 |
stream_mode_closed = 2 |
stream_mode_closed = 2 |
175 |
} stream_mode_t; |
}; |
176 |
|
|
177 |
/** Current state of the file stream. */ |
/** Current state of the file stream. */ |
178 |
typedef enum { |
enum stream_state_t { |
179 |
stream_ready = 0, |
stream_ready = 0, |
180 |
stream_end_reached = 1, |
stream_end_reached = 1, |
181 |
stream_closed = 2 |
stream_closed = 2 |
182 |
} stream_state_t; |
}; |
183 |
|
|
184 |
/** File stream position dependent to these relations. */ |
/** File stream position dependent to these relations. */ |
185 |
typedef enum { |
enum stream_whence_t { |
186 |
stream_start = 0, |
stream_start = 0, |
187 |
stream_curpos = 1, |
stream_curpos = 1, |
188 |
stream_backward = 2, |
stream_backward = 2, |
189 |
stream_end = 3 |
stream_end = 3 |
190 |
} stream_whence_t; |
}; |
191 |
|
|
192 |
/** Alignment of data bytes in memory (system dependant). */ |
/** Alignment of data bytes in memory (system dependant). */ |
193 |
typedef enum { |
enum endian_t { |
194 |
endian_little = 0, |
endian_little = 0, |
195 |
endian_big = 1, |
endian_big = 1, |
196 |
endian_native = 2 |
endian_native = 2 |
197 |
} endian_t; |
}; |
198 |
|
|
199 |
/** General chunk structure of a file. */ |
/** General RIFF chunk structure of a RIFF file. */ |
200 |
enum layout_t { |
enum layout_t { |
201 |
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. |
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. |
202 |
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. |
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. |
203 |
}; |
}; |
204 |
|
|
205 |
|
/** Size of RIFF file offsets used in all RIFF chunks' headers. @see File::GetFileOffsetSize() */ |
206 |
|
enum offset_size_t { |
207 |
|
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. |
208 |
|
offset_size_32bit = 4, ///< Always use 32 bit offsets (even for files larger than 4 GB). |
209 |
|
offset_size_64bit = 8 ///< Always use 64 bit offsets (even for files smaller than 4 GB). |
210 |
|
}; |
211 |
|
|
212 |
|
/** |
213 |
|
* @brief Used for indicating the progress of a certain task. |
214 |
|
* |
215 |
|
* The function pointer argument has to be supplied with a valid |
216 |
|
* function of the given signature which will then be called on |
217 |
|
* progress changes. An equivalent progress_t structure will be passed |
218 |
|
* back as argument to the callback function on each progress change. |
219 |
|
* The factor field of the supplied progress_t structure will then |
220 |
|
* reflect the current progress as value between 0.0 and 1.0. You might |
221 |
|
* want to use the custom field for data needed in your callback |
222 |
|
* function. |
223 |
|
*/ |
224 |
|
struct progress_t { |
225 |
|
void (*callback)(progress_t*); ///< Callback function pointer which has to be assigned to a function for progress notification. |
226 |
|
float factor; ///< Reflects current progress as value between 0.0 and 1.0. |
227 |
|
void* custom; ///< This pointer can be used for arbitrary data. |
228 |
|
float __range_min; ///< Only for internal usage, do not modify! |
229 |
|
float __range_max; ///< Only for internal usage, do not modify! |
230 |
|
progress_t(); |
231 |
|
std::vector<progress_t> subdivide(int iSubtasks); |
232 |
|
std::vector<progress_t> subdivide(std::vector<float> vSubTaskPortions); |
233 |
|
}; |
234 |
|
|
235 |
/** @brief Ordinary RIFF Chunk |
/** @brief Ordinary RIFF Chunk |
236 |
* |
* |
237 |
* Provides convenient methods to access data of ordinary RIFF chunks |
* Provides convenient methods to access data of ordinary RIFF chunks |
239 |
*/ |
*/ |
240 |
class Chunk { |
class Chunk { |
241 |
public: |
public: |
242 |
Chunk(File* pFile, unsigned long StartPos, List* Parent); |
Chunk(File* pFile, file_offset_t StartPos, List* Parent); |
243 |
String GetChunkIDString(); |
String GetChunkIDString() const; |
244 |
uint32_t GetChunkID() { return ChunkID; } ///< Chunk ID in unsigned integer representation. |
uint32_t GetChunkID() const { return ChunkID; } ///< Chunk ID in unsigned integer representation. |
245 |
File* GetFile() { return pFile; } ///< Returns pointer to the chunk's File object. |
File* GetFile() const { return pFile; } ///< Returns pointer to the chunk's File object. |
246 |
List* GetParent() { return pParent; } ///< Returns pointer to the chunk's parent list chunk. |
List* GetParent() const { return pParent; } ///< Returns pointer to the chunk's parent list chunk. |
247 |
unsigned long GetSize() const { return CurrentChunkSize; } ///< Chunk size in bytes (without header, thus the chunk data body) |
file_offset_t GetSize() const { return ullCurrentChunkSize; } ///< Chunk size in bytes (without header, thus the chunk data body) |
248 |
unsigned long GetNewSize() { return NewChunkSize; } ///< New chunk size if it was modified with Resize(). |
file_offset_t GetNewSize() const { return ullNewChunkSize; } ///< New chunk size if it was modified with Resize(), otherwise value returned will be equal to GetSize(). |
249 |
unsigned long GetPos() { return ulPos; } ///< Position within the chunk data body |
file_offset_t GetPos() const; |
250 |
unsigned long GetFilePos() { return ulStartPos + ulPos; } ///< Current, actual offset in file. |
file_offset_t GetFilePos() const; |
251 |
unsigned long SetPos(unsigned long Where, stream_whence_t Whence = stream_start); |
file_offset_t SetPos(file_offset_t Where, stream_whence_t Whence = stream_start); |
252 |
unsigned long RemainingBytes(); |
file_offset_t RemainingBytes() const; |
253 |
stream_state_t GetState(); |
stream_state_t GetState() const; |
254 |
unsigned long Read(void* pData, unsigned long WordCount, unsigned long WordSize); |
file_offset_t Read(void* pData, file_offset_t WordCount, file_offset_t WordSize); |
255 |
unsigned long ReadInt8(int8_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt8(int8_t* pData, file_offset_t WordCount = 1); |
256 |
unsigned long ReadUint8(uint8_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadUint8(uint8_t* pData, file_offset_t WordCount = 1); |
257 |
unsigned long ReadInt16(int16_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt16(int16_t* pData, file_offset_t WordCount = 1); |
258 |
unsigned long ReadUint16(uint16_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadUint16(uint16_t* pData, file_offset_t WordCount = 1); |
259 |
unsigned long ReadInt32(int32_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt32(int32_t* pData, file_offset_t WordCount = 1); |
260 |
unsigned long ReadUint32(uint32_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadUint32(uint32_t* pData, file_offset_t WordCount = 1); |
261 |
int8_t ReadInt8(); |
int8_t ReadInt8(); |
262 |
uint8_t ReadUint8(); |
uint8_t ReadUint8(); |
263 |
int16_t ReadInt16(); |
int16_t ReadInt16(); |
265 |
int32_t ReadInt32(); |
int32_t ReadInt32(); |
266 |
uint32_t ReadUint32(); |
uint32_t ReadUint32(); |
267 |
void ReadString(String& s, int size); |
void ReadString(String& s, int size); |
268 |
unsigned long Write(void* pData, unsigned long WordCount, unsigned long WordSize); |
file_offset_t Write(void* pData, file_offset_t WordCount, file_offset_t WordSize); |
269 |
unsigned long WriteInt8(int8_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt8(int8_t* pData, file_offset_t WordCount = 1); |
270 |
unsigned long WriteUint8(uint8_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteUint8(uint8_t* pData, file_offset_t WordCount = 1); |
271 |
unsigned long WriteInt16(int16_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt16(int16_t* pData, file_offset_t WordCount = 1); |
272 |
unsigned long WriteUint16(uint16_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteUint16(uint16_t* pData, file_offset_t WordCount = 1); |
273 |
unsigned long WriteInt32(int32_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt32(int32_t* pData, file_offset_t WordCount = 1); |
274 |
unsigned long WriteUint32(uint32_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteUint32(uint32_t* pData, file_offset_t WordCount = 1); |
275 |
void* LoadChunkData(); |
void* LoadChunkData(); |
276 |
void ReleaseChunkData(); |
void ReleaseChunkData(); |
277 |
void Resize(int iNewSize); |
void Resize(file_offset_t NewSize); |
278 |
virtual ~Chunk(); |
virtual ~Chunk(); |
279 |
protected: |
protected: |
280 |
uint32_t ChunkID; |
uint32_t ChunkID; |
281 |
uint32_t CurrentChunkSize; /* in bytes */ |
file_offset_t ullCurrentChunkSize; /* in bytes */ |
282 |
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) */ |
283 |
List* pParent; |
List* pParent; |
284 |
File* pFile; |
File* pFile; |
285 |
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 */ |
|
286 |
uint8_t* pChunkData; |
uint8_t* pChunkData; |
287 |
unsigned long ulChunkDataSize; |
file_offset_t ullChunkDataSize; |
288 |
|
struct ChunkPos { /* current read/write position as # of bytes from ulStartPos */ |
289 |
|
file_offset_t ullPos; /* used if File::IsIOPerThread() is false (default) */ |
290 |
|
mutable std::map<std::thread::id,file_offset_t> byThread; /* used if File::IsIOPerThread() is true */ |
291 |
|
mutable std::mutex mutex; /* for protecting concurrent changes on byThread map itself */ |
292 |
|
} chunkPos; |
293 |
|
|
294 |
Chunk(File* pFile); |
Chunk(File* pFile); |
295 |
Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize); |
Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize); |
296 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(file_offset_t filePos); |
297 |
void WriteHeader(unsigned long fPos); |
void WriteHeader(file_offset_t filePos); |
298 |
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); |
299 |
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) { |
|
300 |
String result; |
String result; |
301 |
for (int i = 0; i < 4; i++) { |
for (int i = 0; i < 4; i++) { |
302 |
uint8_t byte = *((uint8_t*)(&word) + i); |
uint8_t byte = *((uint8_t*)(&word) + i); |
305 |
} |
} |
306 |
return result; |
return result; |
307 |
} |
} |
308 |
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize); |
309 |
|
virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL); |
310 |
virtual void __resetPos(); ///< Sets Chunk's read/write position to zero. |
virtual void __resetPos(); ///< Sets Chunk's read/write position to zero. |
311 |
|
|
312 |
friend class List; |
friend class List; |
313 |
|
private: |
314 |
|
file_offset_t& GetPosUnsafeRef(); |
315 |
}; |
}; |
316 |
|
|
317 |
/** @brief RIFF List Chunk |
/** @brief RIFF List Chunk |
321 |
*/ |
*/ |
322 |
class List : public Chunk { |
class List : public Chunk { |
323 |
public: |
public: |
324 |
List(File* pFile, unsigned long StartPos, List* Parent); |
List(File* pFile, file_offset_t StartPos, List* Parent); |
325 |
String GetListTypeString(); |
String GetListTypeString() const; |
326 |
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 |
327 |
|
Chunk* GetSubChunkAt(size_t pos); |
328 |
Chunk* GetSubChunk(uint32_t ChunkID); |
Chunk* GetSubChunk(uint32_t ChunkID); |
329 |
|
List* GetSubListAt(size_t pos); |
330 |
List* GetSubList(uint32_t ListType); |
List* GetSubList(uint32_t ListType); |
331 |
Chunk* GetFirstSubChunk(); |
Chunk* GetFirstSubChunk() LIBGIG_DEPRECATED("Use GetSubChunkAt() instead."); |
332 |
Chunk* GetNextSubChunk(); |
Chunk* GetNextSubChunk() LIBGIG_DEPRECATED("Use GetSubChunkAt() instead."); |
333 |
List* GetFirstSubList(); |
List* GetFirstSubList() LIBGIG_DEPRECATED("Use GetSubListAt() instead."); |
334 |
List* GetNextSubList(); |
List* GetNextSubList() LIBGIG_DEPRECATED("Use GetSubListAt() instead."); |
335 |
unsigned int CountSubChunks(); |
size_t CountSubChunks(); |
336 |
unsigned int CountSubChunks(uint32_t ChunkID); |
size_t CountSubChunks(uint32_t ChunkID); |
337 |
unsigned int CountSubLists(); |
size_t CountSubLists(); |
338 |
unsigned int CountSubLists(uint32_t ListType); |
size_t CountSubLists(uint32_t ListType); |
339 |
Chunk* AddSubChunk(uint32_t uiChunkID, uint uiBodySize); |
Chunk* AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize); |
340 |
List* AddSubList(uint32_t uiListType); |
List* AddSubList(uint32_t uiListType); |
341 |
void DeleteSubChunk(Chunk* pSubChunk); |
void DeleteSubChunk(Chunk* pSubChunk); |
342 |
void MoveSubChunk(Chunk* pSrc, Chunk* pDst); |
void MoveSubChunk(Chunk* pSrc, Chunk* pDst); // read API doc comments !!! |
343 |
|
void MoveSubChunk(Chunk* pSrc, List* pNewParent); |
344 |
virtual ~List(); |
virtual ~List(); |
345 |
protected: |
protected: |
346 |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
347 |
typedef std::list<Chunk*> ChunkList; |
typedef std::vector<Chunk*> ChunkList; |
348 |
|
typedef std::set<Chunk*> ChunkSet; |
349 |
|
|
350 |
uint32_t ListType; |
uint32_t ListType; |
351 |
ChunkList* pSubChunks; |
ChunkList* pSubChunks; |
355 |
|
|
356 |
List(File* pFile); |
List(File* pFile); |
357 |
List(File* pFile, List* pParent, uint32_t uiListID); |
List(File* pFile, List* pParent, uint32_t uiListID); |
358 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(file_offset_t filePos); |
359 |
void WriteHeader(unsigned long fPos); |
void WriteHeader(file_offset_t filePos); |
360 |
void LoadSubChunks(); |
void LoadSubChunks(progress_t* pProgress = NULL); |
361 |
void LoadSubChunksRecursively(); |
void LoadSubChunksRecursively(progress_t* pProgress = NULL); |
362 |
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize); |
363 |
|
virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL); |
364 |
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. |
365 |
void DeleteChunkList(); |
void DeleteChunkList(); |
366 |
}; |
}; |
373 |
*/ |
*/ |
374 |
class File : public List { |
class File : public List { |
375 |
public: |
public: |
376 |
|
/** |
377 |
|
* OS dependent type serving as file handle / descriptor for OS |
378 |
|
* dependent file I/O operations. |
379 |
|
*/ |
380 |
|
#if POSIX |
381 |
|
typedef int Handle; |
382 |
|
#elif defined(WIN32) |
383 |
|
typedef HANDLE Handle; |
384 |
|
#else |
385 |
|
typedef FILE* Handle; |
386 |
|
#endif |
387 |
|
|
388 |
File(uint32_t FileType); |
File(uint32_t FileType); |
389 |
File(const String& path); |
File(const String& path); |
390 |
File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout); |
File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize = offset_size_auto); |
391 |
stream_mode_t GetMode(); |
stream_mode_t GetMode() const; |
392 |
bool SetMode(stream_mode_t NewMode); |
bool SetMode(stream_mode_t NewMode); |
393 |
void SetByteOrder(endian_t Endian); |
void SetByteOrder(endian_t Endian); |
394 |
String GetFileName(); |
String GetFileName() const; |
395 |
void SetFileName(const String& path); |
void SetFileName(const String& path); |
396 |
|
Handle FileHandle() const; |
397 |
bool IsNew() const; |
bool IsNew() const; |
398 |
layout_t GetLayout() const; |
layout_t GetLayout() const; |
399 |
virtual void Save(); |
file_offset_t GetCurrentFileSize() const; |
400 |
virtual void Save(const String& path); |
file_offset_t GetRequiredFileSize(); |
401 |
|
file_offset_t GetRequiredFileSize(offset_size_t fileOffsetSize); |
402 |
|
int GetFileOffsetSize() const; |
403 |
|
int GetRequiredFileOffsetSize(); |
404 |
|
bool IsIOPerThread() const; |
405 |
|
void SetIOPerThread(bool enable); |
406 |
|
|
407 |
|
virtual void Save(progress_t* pProgress = NULL); |
408 |
|
virtual void Save(const String& path, progress_t* pProgress = NULL); |
409 |
virtual ~File(); |
virtual ~File(); |
410 |
protected: |
protected: |
411 |
#if POSIX |
struct HandlePair { |
412 |
int hFileRead; ///< handle / descriptor for reading from file |
Handle hRead; ///< handle / descriptor for reading from file |
413 |
int hFileWrite; ///< handle / descriptor for writing to (some) file |
Handle hWrite; ///< handle / descriptor for writing to (some) file |
414 |
#elif defined(WIN32) |
stream_mode_t Mode; ///< State of handles (ro, rw, closed). |
415 |
HANDLE hFileRead; ///< handle / descriptor for reading from file |
}; |
416 |
HANDLE hFileWrite; ///< handle / descriptor for writing to (some) file |
struct IO : HandlePair { |
417 |
#else |
bool isPerThread; ///< Whether SetIOPerThread(true) was called. |
418 |
FILE* hFileRead; ///< handle / descriptor for reading from file |
mutable std::map<std::thread::id,HandlePair> byThread; ///< Optional: Individual file handle pair for each thread separately. |
419 |
FILE* hFileWrite; ///< handle / descriptor for writing to (some) file |
mutable std::mutex mutex; ///< For protecting concurrent changes on the @c byThread map itself and replacing handles. |
420 |
#endif // POSIX |
} io; |
421 |
String Filename; |
String Filename; |
422 |
bool bEndianNative; |
bool bEndianNative; |
423 |
bool bIsNewFile; |
bool bIsNewFile; |
424 |
layout_t Layout; ///< An ordinary RIFF file is always set to layout_standard. |
layout_t Layout; ///< An ordinary RIFF file is always set to layout_standard. |
425 |
|
offset_size_t FileOffsetPreference; |
426 |
|
int FileOffsetSize; ///< Size of file offsets (in bytes) when this file was opened (or saved the last time). |
427 |
|
|
428 |
|
Handle FileWriteHandle() const; |
429 |
|
HandlePair FileHandlePair() const; |
430 |
|
|
|
void LogAsResized(Chunk* pResizedChunk); |
|
|
void UnlogResized(Chunk* pResizedChunk); |
|
431 |
friend class Chunk; |
friend class Chunk; |
432 |
friend class List; |
friend class List; |
433 |
private: |
private: |
|
stream_mode_t Mode; |
|
|
ChunkList ResizedChunks; ///< All chunks which have been resized (enlarged / shortened). |
|
|
|
|
434 |
void __openExistingFile(const String& path, uint32_t* FileType = NULL); |
void __openExistingFile(const String& path, uint32_t* FileType = NULL); |
435 |
unsigned long GetFileSize(); |
void ResizeFile(file_offset_t ullNewSize); |
|
void ResizeFile(unsigned long ulNewSize); |
|
436 |
#if POSIX |
#if POSIX |
437 |
unsigned long __GetFileSize(int hFile); |
file_offset_t __GetFileSize(int hFile) const; |
438 |
#elif defined(WIN32) |
#elif defined(WIN32) |
439 |
unsigned long __GetFileSize(HANDLE hFile); |
file_offset_t __GetFileSize(HANDLE hFile) const; |
440 |
#else |
#else |
441 |
unsigned long __GetFileSize(FILE* hFile); |
file_offset_t __GetFileSize(FILE* hFile) const; |
442 |
#endif |
#endif |
443 |
|
int FileOffsetSizeFor(file_offset_t fileSize) const; |
444 |
void Cleanup(); |
void Cleanup(); |
445 |
|
HandlePair& FileHandlePairUnsafeRef(); |
446 |
|
bool SetModeInternal(stream_mode_t NewMode, bool* pResetPos); |
447 |
}; |
}; |
448 |
|
|
449 |
/** |
/** |
453 |
public: |
public: |
454 |
String Message; |
String Message; |
455 |
|
|
456 |
Exception(String Message) { Exception::Message = Message; } |
Exception(String format, ...); |
457 |
|
Exception(String format, va_list arg); |
458 |
void PrintMessage(); |
void PrintMessage(); |
459 |
virtual ~Exception() {} |
virtual ~Exception() {} |
460 |
|
|
461 |
|
protected: |
462 |
|
Exception(); |
463 |
|
static String assemble(String format, va_list arg); |
464 |
}; |
}; |
465 |
|
|
466 |
String libraryName(); |
String libraryName(); |