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, |
82 |
|
stream_mode_read_write |
83 |
|
} stream_mode_t; |
84 |
|
|
85 |
/** Current state of the file stream. */ |
/** Current state of the file stream. */ |
86 |
typedef enum { |
typedef enum { |
87 |
stream_ready = 0, |
stream_ready = 0, |
100 |
/** Provides convenient methods to access data of RIFF chunks in general. */ |
/** Provides convenient methods to access data of RIFF chunks in general. */ |
101 |
class Chunk { |
class Chunk { |
102 |
public: |
public: |
103 |
#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 |
|
104 |
String GetChunkIDString(); |
String GetChunkIDString(); |
105 |
uint32_t GetChunkID() { return ChunkID; }; ///< Chunk ID in unsigned integer representation. |
uint32_t GetChunkID() { return ChunkID; }; ///< Chunk ID in unsigned integer representation. |
106 |
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. |
107 |
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) |
108 |
|
unsigned long GetNewSize() { return NewChunkSize; }; ///< New chunk size if it was modified with Resize(). |
109 |
unsigned long GetPos() { return ulPos; }; ///< Position within the chunk data body |
unsigned long GetPos() { return ulPos; }; ///< Position within the chunk data body |
110 |
unsigned long GetFilePos() { return ulStartPos + ulPos; }; ///< Current, actual offset in file. |
unsigned long GetFilePos() { return ulStartPos + ulPos; }; ///< Current, actual offset in file. |
111 |
unsigned long SetPos(unsigned long Where, stream_whence_t Whence = stream_start); |
unsigned long SetPos(unsigned long Where, stream_whence_t Whence = stream_start); |
124 |
uint16_t ReadUint16(); |
uint16_t ReadUint16(); |
125 |
int32_t ReadInt32(); |
int32_t ReadInt32(); |
126 |
uint32_t ReadUint32(); |
uint32_t ReadUint32(); |
127 |
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); |
128 |
void ReleaseChunkData(); ///< Free loaded chunk body data from memory (RAM). |
unsigned long WriteInt8(int8_t* pData, unsigned long WordCount = 1); |
129 |
|
unsigned long WriteUint8(uint8_t* pData, unsigned long WordCount = 1); |
130 |
|
unsigned long WriteInt16(int16_t* pData, unsigned long WordCount = 1); |
131 |
|
unsigned long WriteUint16(uint16_t* pData, unsigned long WordCount = 1); |
132 |
|
unsigned long WriteInt32(int32_t* pData, unsigned long WordCount = 1); |
133 |
|
unsigned long WriteUint32(uint32_t* pData, unsigned long WordCount = 1); |
134 |
|
void* LoadChunkData(); |
135 |
|
void ReleaseChunkData(); |
136 |
|
void Resize(int iNewSize); |
137 |
virtual ~Chunk(); |
virtual ~Chunk(); |
138 |
protected: |
protected: |
139 |
uint32_t ChunkID; |
uint32_t ChunkID; |
140 |
uint32_t ChunkSize; /* in bytes */ |
uint32_t CurrentChunkSize; /* in bytes */ |
141 |
|
uint32_t NewChunkSize; /* in bytes (if chunk was scheduled to be resized) */ |
142 |
List* pParent; |
List* pParent; |
143 |
#if POSIX |
File* pFile; |
|
int hFile; |
|
|
#else |
|
|
FILE* hFile; |
|
|
#endif // POSIX |
|
144 |
unsigned long ulStartPos; /* actual position in file where chunk (without header) starts */ |
unsigned long ulStartPos; /* actual position in file where chunk (without header) starts */ |
145 |
unsigned long ulPos; /* # of bytes from ulStartPos */ |
unsigned long ulPos; /* # of bytes from ulStartPos */ |
|
bool bEndianNative; |
|
146 |
uint8_t* pChunkData; |
uint8_t* pChunkData; |
147 |
|
|
148 |
Chunk(); |
Chunk(File* pFile); |
149 |
|
Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize); |
150 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(unsigned long fPos); |
151 |
|
void WriteHeader(unsigned long fPos); |
152 |
unsigned long ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize); |
unsigned long ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize); |
153 |
inline void swapBytes_16(void* Word) { |
inline void swapBytes_16(void* Word) { |
154 |
uint8_t byteCache = *((uint8_t*) Word); |
uint8_t byteCache = *((uint8_t*) Word); |
181 |
} |
} |
182 |
return result; |
return result; |
183 |
} |
} |
184 |
|
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
185 |
|
virtual void __resetPos(); ///< Sets Chunk's read/write position to zero. |
186 |
|
|
187 |
|
friend class List; |
188 |
}; |
}; |
189 |
|
|
190 |
/** 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. */ |
191 |
class List : public Chunk { |
class List : public Chunk { |
192 |
public: |
public: |
193 |
#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 |
|
194 |
String GetListTypeString(); |
String GetListTypeString(); |
195 |
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 |
196 |
Chunk* GetSubChunk(uint32_t ChunkID); |
Chunk* GetSubChunk(uint32_t ChunkID); |
203 |
unsigned int CountSubChunks(uint32_t ChunkID); |
unsigned int CountSubChunks(uint32_t ChunkID); |
204 |
unsigned int CountSubLists(); |
unsigned int CountSubLists(); |
205 |
unsigned int CountSubLists(uint32_t ListType); |
unsigned int CountSubLists(uint32_t ListType); |
206 |
|
Chunk* AddSubChunk(uint32_t uiChunkID, uint uiBodySize); |
207 |
|
List* AddSubList(uint32_t uiListType); |
208 |
|
void DeleteSubChunk(Chunk* pSubChunk); |
209 |
virtual ~List(); |
virtual ~List(); |
210 |
protected: |
protected: |
211 |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
217 |
ChunkList::iterator ChunksIterator; |
ChunkList::iterator ChunksIterator; |
218 |
ChunkList::iterator ListIterator; |
ChunkList::iterator ListIterator; |
219 |
|
|
220 |
List(); |
List(File* pFile); |
221 |
|
List(File* pFile, List* pParent, uint32_t uiListID); |
222 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(unsigned long fPos); |
223 |
|
void WriteHeader(unsigned long fPos); |
224 |
void LoadSubChunks(); |
void LoadSubChunks(); |
225 |
|
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
226 |
|
virtual void __resetPos(); ///< Sets List Chunk's read/write position to zero and causes all sub chunks to do the same. |
227 |
}; |
}; |
228 |
|
|
229 |
/** 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. */ |
230 |
class File : public List { |
class File : public List { |
231 |
public: |
public: |
232 |
|
File(); |
233 |
File(const String& path); |
File(const String& path); |
234 |
|
stream_mode_t GetMode(); |
235 |
|
bool SetMode(stream_mode_t NewMode); |
236 |
|
String GetFileName(); |
237 |
|
virtual void Save(); |
238 |
|
virtual void Save(const String& path); |
239 |
virtual ~File(); |
virtual ~File(); |
240 |
const String Filename; |
protected: |
241 |
|
#if POSIX |
242 |
|
int hFileRead; ///< handle / descriptor for reading from file |
243 |
|
int hFileWrite; ///< handle / descriptor for writing to (some) file |
244 |
|
#else |
245 |
|
FILE* hFileRead; ///< handle / descriptor for reading from file |
246 |
|
FILE* hFileWrite; ///< handle / descriptor for writing to (some) file |
247 |
|
#endif // POSIX |
248 |
|
String Filename; |
249 |
|
bool bEndianNative; |
250 |
|
|
251 |
|
void LogAsResized(Chunk* pResizedChunk); |
252 |
|
friend class Chunk; |
253 |
|
friend class List; |
254 |
private: |
private: |
255 |
|
stream_mode_t Mode; |
256 |
|
ChunkList ResizedChunks; ///< All chunks which have been resized (enlarged / shortened). |
257 |
|
|
258 |
unsigned long GetFileSize(); |
unsigned long GetFileSize(); |
259 |
|
void ResizeFile(unsigned long ulNewSize); |
260 |
}; |
}; |
261 |
|
|
262 |
/** Will be thrown whenever an error occurs while parsing a RIFF file. */ |
/** Will be thrown whenever an error occurs while parsing a RIFF file. */ |