/[svn]/libgig/trunk/src/RIFF.h
ViewVC logotype

Annotation of /libgig/trunk/src/RIFF.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 780 - (hide annotations) (download) (as text)
Sun Sep 25 13:40:37 2005 UTC (18 years, 6 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 12144 byte(s)
* added write support (only to the RIFF classes yet)

1 schoenebeck 2 /***************************************************************************
2     * *
3     * libgig - C++ cross-platform Gigasampler format file loader library *
4     * *
5 schoenebeck 384 * Copyright (C) 2003-2005 by Christian Schoenebeck *
6     * <cuse@users.sourceforge.net> *
7 schoenebeck 2 * *
8     * This library is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This library is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this library; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #ifndef __RIFF_H__
25     #define __RIFF_H__
26    
27     #define POSIX 1
28     #define DEBUG 0
29    
30     #include <string>
31     #include <list>
32     #include <map>
33     #include <iostream>
34    
35 schoenebeck 11 #ifdef HAVE_CONFIG_H
36     # include <config.h>
37     #endif
38    
39 schoenebeck 2 #if POSIX
40     # include <sys/types.h>
41     # include <sys/stat.h>
42     # include <fcntl.h>
43     # include <unistd.h>
44     #endif // POSIX
45    
46     #include <stdint.h>
47    
48     //typedef unsigned char uint8_t;
49     //typedef unsigned short uint16_t;
50     //typedef unsigned int uint32_t;
51    
52     #include <stdio.h>
53    
54     #if WORDS_BIGENDIAN
55     # define CHUNK_ID_RIFF 0x52494646
56     # define CHUNK_ID_RIFX 0x52494658
57     # define CHUNK_ID_LIST 0x4C495354
58     #else // little endian
59     # define CHUNK_ID_RIFF 0x46464952
60     # define CHUNK_ID_RIFX 0x58464952
61     # define CHUNK_ID_LIST 0x5453494C
62     #endif // WORDS_BIGENDIAN
63    
64     #define CHUNK_HEADER_SIZE 8
65     #define LIST_HEADER_SIZE 12
66     #define RIFF_HEADER_SIZE 12
67    
68    
69     /** RIFF specific classes and definitions */
70     namespace RIFF {
71    
72     /* just symbol prototyping */
73     class Chunk;
74     class List;
75 schoenebeck 780 class File;
76 schoenebeck 2
77     typedef std::string String;
78    
79 schoenebeck 780 /** 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 schoenebeck 2 /** Current state of the file stream. */
86     typedef enum {
87     stream_ready = 0,
88     stream_end_reached = 1,
89     stream_closed = 2
90     } stream_state_t;
91    
92     /** File stream position dependent to these relations. */
93     typedef enum {
94     stream_start = 0,
95     stream_curpos = 1,
96     stream_backward = 2,
97     stream_end = 3
98     } stream_whence_t;
99    
100     /** Provides convenient methods to access data of RIFF chunks in general. */
101     class Chunk {
102     public:
103 schoenebeck 780 Chunk(File* pFile, unsigned long StartPos, List* Parent);
104 schoenebeck 2 String GetChunkIDString();
105 schoenebeck 11 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.
107 schoenebeck 780 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 schoenebeck 11 unsigned long GetPos() { return ulPos; }; ///< Position within the chunk data body
110 schoenebeck 2 unsigned long GetFilePos() { return ulStartPos + ulPos; }; ///< Current, actual offset in file.
111     unsigned long SetPos(unsigned long Where, stream_whence_t Whence = stream_start);
112     unsigned long RemainingBytes();
113     stream_state_t GetState();
114     unsigned long Read(void* pData, unsigned long WordCount, unsigned long WordSize);
115     unsigned long ReadInt8(int8_t* pData, unsigned long WordCount = 1);
116     unsigned long ReadUint8(uint8_t* pData, unsigned long WordCount = 1);
117     unsigned long ReadInt16(int16_t* pData, unsigned long WordCount = 1);
118     unsigned long ReadUint16(uint16_t* pData, unsigned long WordCount = 1);
119     unsigned long ReadInt32(int32_t* pData, unsigned long WordCount = 1);
120     unsigned long ReadUint32(uint32_t* pData, unsigned long WordCount = 1);
121     int8_t ReadInt8();
122     uint8_t ReadUint8();
123     int16_t ReadInt16();
124     uint16_t ReadUint16();
125     int32_t ReadInt32();
126     uint32_t ReadUint32();
127 schoenebeck 780 unsigned long Write(void* pData, unsigned long WordCount, unsigned long WordSize);
128     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 schoenebeck 384 virtual ~Chunk();
138 schoenebeck 2 protected:
139     uint32_t ChunkID;
140 schoenebeck 780 uint32_t CurrentChunkSize; /* in bytes */
141     uint32_t NewChunkSize; /* in bytes (if chunk was scheduled to be resized) */
142 schoenebeck 2 List* pParent;
143 schoenebeck 780 File* pFile;
144 schoenebeck 2 unsigned long ulStartPos; /* actual position in file where chunk (without header) starts */
145     unsigned long ulPos; /* # of bytes from ulStartPos */
146     uint8_t* pChunkData;
147    
148 schoenebeck 780 Chunk(File* pFile);
149     Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize);
150 schoenebeck 2 void ReadHeader(unsigned long fPos);
151 schoenebeck 780 void WriteHeader(unsigned long fPos);
152 schoenebeck 2 unsigned long ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize);
153     inline void swapBytes_16(void* Word) {
154     uint8_t byteCache = *((uint8_t*) Word);
155     *((uint8_t*) Word) = *((uint8_t*) Word + 1);
156     *((uint8_t*) Word + 1) = byteCache;
157     }
158     inline void swapBytes_32(void* Word) {
159     uint8_t byteCache = *((uint8_t*) Word);
160     *((uint8_t*) Word) = *((uint8_t*) Word + 3);
161     *((uint8_t*) Word + 3) = byteCache;
162     byteCache = *((uint8_t*) Word + 1);
163     *((uint8_t*) Word + 1) = *((uint8_t*) Word + 2);
164     *((uint8_t*) Word + 2) = byteCache;
165     }
166     inline void swapBytes(void* Word, unsigned long WordSize) {
167     uint8_t byteCache;
168     unsigned long lo = 0, hi = WordSize - 1;
169     for (; lo < hi; hi--, lo++) {
170     byteCache = *((uint8_t*) Word + lo);
171     *((uint8_t*) Word + lo) = *((uint8_t*) Word + hi);
172     *((uint8_t*) Word + hi) = byteCache;
173     }
174     }
175     inline String convertToString(uint32_t word) {
176     String result;
177     for (int i = 0; i < 4; i++) {
178     uint8_t byte = *((uint8_t*)(&word) + i);
179     char c = byte;
180     result += c;
181     }
182     return result;
183     }
184 schoenebeck 780 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 schoenebeck 2 };
189    
190     /** Provides convenient methods to access data of RIFF list chunks and their subchunks. */
191     class List : public Chunk {
192     public:
193 schoenebeck 780 List(File* pFile, unsigned long StartPos, List* Parent);
194 schoenebeck 2 String GetListTypeString();
195 schoenebeck 11 uint32_t GetListType() { return ListType; } ///< Returns unsigned integer representation of the list's ID
196     Chunk* GetSubChunk(uint32_t ChunkID);
197     List* GetSubList(uint32_t ListType);
198 schoenebeck 2 Chunk* GetFirstSubChunk();
199     Chunk* GetNextSubChunk();
200     List* GetFirstSubList();
201     List* GetNextSubList();
202     unsigned int CountSubChunks();
203     unsigned int CountSubChunks(uint32_t ChunkID);
204     unsigned int CountSubLists();
205     unsigned int CountSubLists(uint32_t ListType);
206 schoenebeck 780 Chunk* AddSubChunk(uint32_t uiChunkID, uint uiBodySize);
207     List* AddSubList(uint32_t uiListType);
208     void DeleteSubChunk(Chunk* pSubChunk);
209 schoenebeck 384 virtual ~List();
210 schoenebeck 2 protected:
211     typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap;
212     typedef std::list<Chunk*> ChunkList;
213    
214     uint32_t ListType;
215     ChunkList* pSubChunks;
216     ChunkMap* pSubChunksMap;
217     ChunkList::iterator ChunksIterator;
218     ChunkList::iterator ListIterator;
219    
220 schoenebeck 780 List(File* pFile);
221     List(File* pFile, List* pParent, uint32_t uiListID);
222 schoenebeck 2 void ReadHeader(unsigned long fPos);
223 schoenebeck 780 void WriteHeader(unsigned long fPos);
224 schoenebeck 2 void LoadSubChunks();
225 schoenebeck 780 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 schoenebeck 2 };
228    
229     /** 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 {
231     public:
232 schoenebeck 780 File();
233 schoenebeck 2 File(const String& path);
234 schoenebeck 780 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 schoenebeck 384 virtual ~File();
240 schoenebeck 780 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 schoenebeck 2 private:
255 schoenebeck 780 stream_mode_t Mode;
256     ChunkList ResizedChunks; ///< All chunks which have been resized (enlarged / shortened).
257    
258 schoenebeck 2 unsigned long GetFileSize();
259 schoenebeck 780 void ResizeFile(unsigned long ulNewSize);
260 schoenebeck 2 };
261    
262     /** Will be thrown whenever an error occurs while parsing a RIFF file. */
263     class Exception {
264     public:
265     String Message;
266    
267     Exception(String Message) { Exception::Message = Message; };
268     void PrintMessage();
269     virtual ~Exception() {};
270     };
271    
272 schoenebeck 518 String libraryName();
273     String libraryVersion();
274    
275 schoenebeck 2 } // namespace RIFF
276     #endif // __RIFF_H__

  ViewVC Help
Powered by ViewVC