1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2005 by Christian Schoenebeck * |
* Copyright (C) 2003-2006 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 * |
72 |
/* just symbol prototyping */ |
/* just symbol prototyping */ |
73 |
class Chunk; |
class Chunk; |
74 |
class List; |
class List; |
75 |
|
class File; |
76 |
|
|
77 |
typedef std::string String; |
typedef std::string String; |
78 |
|
|
79 |
|
/** Whether file stream is open in read or in read/write mode. */ |
80 |
|
typedef enum { |
81 |
|
stream_mode_read = 0, |
82 |
|
stream_mode_read_write = 1, |
83 |
|
stream_mode_closed = 2 |
84 |
|
} stream_mode_t; |
85 |
|
|
86 |
/** Current state of the file stream. */ |
/** Current state of the file stream. */ |
87 |
typedef enum { |
typedef enum { |
88 |
stream_ready = 0, |
stream_ready = 0, |
101 |
/** Provides convenient methods to access data of RIFF chunks in general. */ |
/** Provides convenient methods to access data of RIFF chunks in general. */ |
102 |
class Chunk { |
class Chunk { |
103 |
public: |
public: |
104 |
#if POSIX |
Chunk(File* pFile, unsigned long StartPos, List* Parent); |
|
Chunk(int hFile, unsigned long StartPos, bool EndianNative, List* Parent); |
|
|
#else |
|
|
Chunk(FILE* hFile, unsigned long StartPos, bool EndianNative, List* Parent); |
|
|
#endif // POSIX |
|
105 |
String GetChunkIDString(); |
String GetChunkIDString(); |
106 |
uint32_t GetChunkID() { return ChunkID; }; ///< Chunk ID in unsigned integer representation. |
uint32_t GetChunkID() { return ChunkID; }; ///< Chunk ID in unsigned integer representation. |
107 |
List* GetParent() { return pParent; }; ///< Returns pointer to the chunk's parent list chunk. |
List* GetParent() { return pParent; }; ///< Returns pointer to the chunk's parent list chunk. |
108 |
unsigned long GetSize() { return ChunkSize; }; ///< Chunk size in bytes (without header, thus the chunk data body) |
unsigned long GetSize() { return CurrentChunkSize; }; ///< Chunk size in bytes (without header, thus the chunk data body) |
109 |
|
unsigned long GetNewSize() { return NewChunkSize; }; ///< New chunk size if it was modified with Resize(). |
110 |
unsigned long GetPos() { return ulPos; }; ///< Position within the chunk data body |
unsigned long GetPos() { return ulPos; }; ///< Position within the chunk data body |
111 |
unsigned long GetFilePos() { return ulStartPos + ulPos; }; ///< Current, actual offset in file. |
unsigned long GetFilePos() { return ulStartPos + ulPos; }; ///< Current, actual offset in file. |
112 |
unsigned long SetPos(unsigned long Where, stream_whence_t Whence = stream_start); |
unsigned long SetPos(unsigned long Where, stream_whence_t Whence = stream_start); |
125 |
uint16_t ReadUint16(); |
uint16_t ReadUint16(); |
126 |
int32_t ReadInt32(); |
int32_t ReadInt32(); |
127 |
uint32_t ReadUint32(); |
uint32_t ReadUint32(); |
128 |
void* LoadChunkData(); ///< Load the whole chunk body in memory (on success returns a pointer to the data in RAM, else NULL). |
unsigned long Write(void* pData, unsigned long WordCount, unsigned long WordSize); |
129 |
void ReleaseChunkData(); ///< Free loaded chunk body data from memory (RAM). |
unsigned long WriteInt8(int8_t* pData, unsigned long WordCount = 1); |
130 |
|
unsigned long WriteUint8(uint8_t* pData, unsigned long WordCount = 1); |
131 |
|
unsigned long WriteInt16(int16_t* pData, unsigned long WordCount = 1); |
132 |
|
unsigned long WriteUint16(uint16_t* pData, unsigned long WordCount = 1); |
133 |
|
unsigned long WriteInt32(int32_t* pData, unsigned long WordCount = 1); |
134 |
|
unsigned long WriteUint32(uint32_t* pData, unsigned long WordCount = 1); |
135 |
|
void* LoadChunkData(); |
136 |
|
void ReleaseChunkData(); |
137 |
|
void Resize(int iNewSize); |
138 |
virtual ~Chunk(); |
virtual ~Chunk(); |
139 |
protected: |
protected: |
140 |
uint32_t ChunkID; |
uint32_t ChunkID; |
141 |
uint32_t ChunkSize; /* in bytes */ |
uint32_t CurrentChunkSize; /* in bytes */ |
142 |
|
uint32_t NewChunkSize; /* in bytes (if chunk was scheduled to be resized) */ |
143 |
List* pParent; |
List* pParent; |
144 |
#if POSIX |
File* pFile; |
|
int hFile; |
|
|
#else |
|
|
FILE* hFile; |
|
|
#endif // POSIX |
|
145 |
unsigned long ulStartPos; /* actual position in file where chunk (without header) starts */ |
unsigned long ulStartPos; /* actual position in file where chunk (without header) starts */ |
146 |
unsigned long ulPos; /* # of bytes from ulStartPos */ |
unsigned long ulPos; /* # of bytes from ulStartPos */ |
|
bool bEndianNative; |
|
147 |
uint8_t* pChunkData; |
uint8_t* pChunkData; |
148 |
|
unsigned long ulChunkDataSize; |
149 |
|
|
150 |
Chunk(); |
Chunk(File* pFile); |
151 |
|
Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize); |
152 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(unsigned long fPos); |
153 |
|
void WriteHeader(unsigned long fPos); |
154 |
unsigned long ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize); |
unsigned long ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize); |
155 |
inline void swapBytes_16(void* Word) { |
inline void swapBytes_16(void* Word) { |
156 |
uint8_t byteCache = *((uint8_t*) Word); |
uint8_t byteCache = *((uint8_t*) Word); |
183 |
} |
} |
184 |
return result; |
return result; |
185 |
} |
} |
186 |
|
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
187 |
|
virtual void __resetPos(); ///< Sets Chunk's read/write position to zero. |
188 |
|
|
189 |
|
friend class List; |
190 |
}; |
}; |
191 |
|
|
192 |
/** Provides convenient methods to access data of RIFF list chunks and their subchunks. */ |
/** Provides convenient methods to access data of RIFF list chunks and their subchunks. */ |
193 |
class List : public Chunk { |
class List : public Chunk { |
194 |
public: |
public: |
195 |
#if POSIX |
List(File* pFile, unsigned long StartPos, List* Parent); |
|
List(int hFile, unsigned long StartPos, bool EndianNative, List* Parent); |
|
|
#else |
|
|
List(FILE* hFile, unsigned long StartPos, bool EndianNative, List* Parent); |
|
|
#endif // POSIX |
|
196 |
String GetListTypeString(); |
String GetListTypeString(); |
197 |
uint32_t GetListType() { return ListType; } ///< Returns unsigned integer representation of the list's ID |
uint32_t GetListType() { return ListType; } ///< Returns unsigned integer representation of the list's ID |
198 |
Chunk* GetSubChunk(uint32_t ChunkID); |
Chunk* GetSubChunk(uint32_t ChunkID); |
205 |
unsigned int CountSubChunks(uint32_t ChunkID); |
unsigned int CountSubChunks(uint32_t ChunkID); |
206 |
unsigned int CountSubLists(); |
unsigned int CountSubLists(); |
207 |
unsigned int CountSubLists(uint32_t ListType); |
unsigned int CountSubLists(uint32_t ListType); |
208 |
|
Chunk* AddSubChunk(uint32_t uiChunkID, uint uiBodySize); |
209 |
|
List* AddSubList(uint32_t uiListType); |
210 |
|
void DeleteSubChunk(Chunk* pSubChunk); |
211 |
virtual ~List(); |
virtual ~List(); |
212 |
protected: |
protected: |
213 |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
219 |
ChunkList::iterator ChunksIterator; |
ChunkList::iterator ChunksIterator; |
220 |
ChunkList::iterator ListIterator; |
ChunkList::iterator ListIterator; |
221 |
|
|
222 |
List(); |
List(File* pFile); |
223 |
|
List(File* pFile, List* pParent, uint32_t uiListID); |
224 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(unsigned long fPos); |
225 |
|
void WriteHeader(unsigned long fPos); |
226 |
void LoadSubChunks(); |
void LoadSubChunks(); |
227 |
|
void LoadSubChunksRecursively(); |
228 |
|
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
229 |
|
virtual void __resetPos(); ///< Sets List Chunk's read/write position to zero and causes all sub chunks to do the same. |
230 |
}; |
}; |
231 |
|
|
232 |
/** Parses arbitrary RIFF files and provides together with it's base classes convenient methods to walk through the RIFF tree. */ |
/** Parses arbitrary RIFF files and provides together with it's base classes convenient methods to walk through the RIFF tree. */ |
233 |
class File : public List { |
class File : public List { |
234 |
public: |
public: |
235 |
|
File(uint32_t FileType); |
236 |
File(const String& path); |
File(const String& path); |
237 |
|
stream_mode_t GetMode(); |
238 |
|
bool SetMode(stream_mode_t NewMode); |
239 |
|
String GetFileName(); |
240 |
|
virtual void Save(); |
241 |
|
virtual void Save(const String& path); |
242 |
virtual ~File(); |
virtual ~File(); |
243 |
|
protected: |
244 |
|
#if POSIX |
245 |
|
int hFileRead; ///< handle / descriptor for reading from file |
246 |
|
int hFileWrite; ///< handle / descriptor for writing to (some) file |
247 |
|
#else |
248 |
|
FILE* hFileRead; ///< handle / descriptor for reading from file |
249 |
|
FILE* hFileWrite; ///< handle / descriptor for writing to (some) file |
250 |
|
#endif // POSIX |
251 |
|
String Filename; |
252 |
|
bool bEndianNative; |
253 |
|
|
254 |
|
void LogAsResized(Chunk* pResizedChunk); |
255 |
|
friend class Chunk; |
256 |
|
friend class List; |
257 |
private: |
private: |
258 |
|
stream_mode_t Mode; |
259 |
|
ChunkList ResizedChunks; ///< All chunks which have been resized (enlarged / shortened). |
260 |
|
|
261 |
unsigned long GetFileSize(); |
unsigned long GetFileSize(); |
262 |
|
void ResizeFile(unsigned long ulNewSize); |
263 |
|
#if POSIX |
264 |
|
unsigned long __GetFileSize(int hFile); |
265 |
|
#else |
266 |
|
unsigned long __GetFileSize(FILE* hFile); |
267 |
|
#endif |
268 |
}; |
}; |
269 |
|
|
270 |
/** Will be thrown whenever an error occurs while parsing a RIFF file. */ |
/** Will be thrown whenever an error occurs while parsing a RIFF file. */ |
277 |
virtual ~Exception() {}; |
virtual ~Exception() {}; |
278 |
}; |
}; |
279 |
|
|
280 |
|
String libraryName(); |
281 |
|
String libraryVersion(); |
282 |
|
|
283 |
} // namespace RIFF |
} // namespace RIFF |
284 |
#endif // __RIFF_H__ |
#endif // __RIFF_H__ |