]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/framework/File.h
Various Mac OS X tweaks to get this to build. Probably breaking things.
[icculus/iodoom3.git] / neo / framework / File.h
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #ifndef __FILE_H__
30 #define __FILE_H__
31
32 /*
33 ==============================================================
34
35   File Streams.
36
37 ==============================================================
38 */
39
40 // mode parm for Seek
41 typedef enum {
42         FS_SEEK_CUR,
43         FS_SEEK_END,
44         FS_SEEK_SET
45 } fsOrigin_t;
46
47 class idFileSystemLocal;
48
49
50 class idFile {
51 public:
52         virtual                                 ~idFile( void ) {};
53                                                         // Get the name of the file.
54         virtual const char *    GetName( void );
55                                                         // Get the full file path.
56         virtual const char *    GetFullPath( void );
57                                                         // Read data from the file to the buffer.
58         virtual int                             Read( void *buffer, int len );
59                                                         // Write data from the buffer to the file.
60         virtual int                             Write( const void *buffer, int len );
61                                                         // Returns the length of the file.
62         virtual int                             Length( void );
63                                                         // Return a time value for reload operations.
64         virtual ID_TIME_T                       Timestamp( void );
65                                                         // Returns offset in file.
66         virtual int                             Tell( void );
67                                                         // Forces flush on files being writting to.
68         virtual void                    ForceFlush( void );
69                                                         // Causes any buffered data to be written to the file.
70         virtual void                    Flush( void );
71                                                         // Seek on a file.
72         virtual int                             Seek( long offset, fsOrigin_t origin );
73                                                         // Go back to the beginning of the file.
74         virtual void                    Rewind( void );
75                                                         // Like fprintf.
76         virtual int                             Printf( const char *fmt, ... ) id_attribute((format(printf,2,3)));
77                                                         // Like fprintf but with argument pointer
78         virtual int                             VPrintf( const char *fmt, va_list arg );
79                                                         // Write a string with high precision floating point numbers to the file.
80         virtual int                             WriteFloatString( const char *fmt, ... ) id_attribute((format(printf,2,3)));
81         
82         // Endian portable alternatives to Read(...)
83         virtual int                             ReadInt( int &value );
84         virtual int                             ReadUnsignedInt( unsigned int &value );
85         virtual int                             ReadShort( short &value );
86         virtual int                             ReadUnsignedShort( unsigned short &value );
87         virtual int                             ReadChar( char &value );
88         virtual int                             ReadUnsignedChar( unsigned char &value );
89         virtual int                             ReadFloat( float &value );
90         virtual int                             ReadBool( bool &value );
91         virtual int                             ReadString( idStr &string );
92         virtual int                             ReadVec2( idVec2 &vec );
93         virtual int                             ReadVec3( idVec3 &vec );
94         virtual int                             ReadVec4( idVec4 &vec );
95         virtual int                             ReadVec6( idVec6 &vec );
96         virtual int                             ReadMat3( idMat3 &mat );
97         
98         // Endian portable alternatives to Write(...)
99         virtual int                             WriteInt( const int value );
100         virtual int                             WriteUnsignedInt( const unsigned int value );
101         virtual int                             WriteShort( const short value );
102         virtual int                             WriteUnsignedShort( unsigned short value );
103         virtual int                             WriteChar( const char value );
104         virtual int                             WriteUnsignedChar( const unsigned char value );
105         virtual int                             WriteFloat( const float value );
106         virtual int                             WriteBool( const bool value );
107         virtual int                             WriteString( const char *string );
108         virtual int                             WriteVec2( const idVec2 &vec );
109         virtual int                             WriteVec3( const idVec3 &vec );
110         virtual int                             WriteVec4( const idVec4 &vec );
111         virtual int                             WriteVec6( const idVec6 &vec );
112         virtual int                             WriteMat3( const idMat3 &mat );
113 };
114
115
116 class idFile_Memory : public idFile {
117         friend class                    idFileSystemLocal;
118
119 public:
120                                                         idFile_Memory( void );  // file for writing without name
121                                                         idFile_Memory( const char *name );      // file for writing
122                                                         idFile_Memory( const char *name, char *data, int length );      // file for writing
123                                                         idFile_Memory( const char *name, const char *data, int length );        // file for reading
124         virtual                                 ~idFile_Memory( void );
125
126         virtual const char *    GetName( void ) { return name.c_str(); }
127         virtual const char *    GetFullPath( void ) { return name.c_str(); }
128         virtual int                             Read( void *buffer, int len );
129         virtual int                             Write( const void *buffer, int len );
130         virtual int                             Length( void );
131         virtual ID_TIME_T                       Timestamp( void );
132         virtual int                             Tell( void );
133         virtual void                    ForceFlush( void );
134         virtual void                    Flush( void );
135         virtual int                             Seek( long offset, fsOrigin_t origin );
136
137                                                         // changes memory file to read only
138         virtual void                    MakeReadOnly( void );
139                                                         // clear the file
140         virtual void                    Clear( bool freeMemory = true );
141                                                         // set data for reading
142         void                                    SetData( const char *data, int length );
143                                                         // returns const pointer to the memory buffer
144         const char *                    GetDataPtr( void ) const { return filePtr; }
145                                                         // set the file granularity
146         void                                    SetGranularity( int g ) { assert( g > 0 ); granularity = g; }
147
148 private:
149         idStr                                   name;                   // name of the file
150         int                                             mode;                   // open mode
151         int                                             maxSize;                // maximum size of file
152         int                                             fileSize;               // size of the file
153         int                                             allocated;              // allocated size
154         int                                             granularity;    // file granularity
155         char *                                  filePtr;                // buffer holding the file data
156         char *                                  curPtr;                 // current read/write pointer
157 };
158
159
160 class idFile_BitMsg : public idFile {
161         friend class                    idFileSystemLocal;
162
163 public:
164                                                         idFile_BitMsg( idBitMsg &msg );
165                                                         idFile_BitMsg( const idBitMsg &msg );
166         virtual                                 ~idFile_BitMsg( void );
167
168         virtual const char *    GetName( void ) { return name.c_str(); }
169         virtual const char *    GetFullPath( void ) { return name.c_str(); }
170         virtual int                             Read( void *buffer, int len );
171         virtual int                             Write( const void *buffer, int len );
172         virtual int                             Length( void );
173         virtual ID_TIME_T                       Timestamp( void );
174         virtual int                             Tell( void );
175         virtual void                    ForceFlush( void );
176         virtual void                    Flush( void );
177         virtual int                             Seek( long offset, fsOrigin_t origin );
178
179 private:
180         idStr                                   name;                   // name of the file
181         int                                             mode;                   // open mode
182         idBitMsg *                              msg;
183 };
184
185
186 class idFile_Permanent : public idFile {
187         friend class                    idFileSystemLocal;
188
189 public:
190                                                         idFile_Permanent( void );
191         virtual                                 ~idFile_Permanent( void );
192
193         virtual const char *    GetName( void ) { return name.c_str(); }
194         virtual const char *    GetFullPath( void ) { return fullPath.c_str(); }
195         virtual int                             Read( void *buffer, int len );
196         virtual int                             Write( const void *buffer, int len );
197         virtual int                             Length( void );
198         virtual ID_TIME_T                       Timestamp( void );
199         virtual int                             Tell( void );
200         virtual void                    ForceFlush( void );
201         virtual void                    Flush( void );
202         virtual int                             Seek( long offset, fsOrigin_t origin );
203
204         // returns file pointer
205         FILE *                                  GetFilePtr( void ) { return o; }
206
207 private:
208         idStr                                   name;                   // relative path of the file - relative path
209         idStr                                   fullPath;               // full file path - OS path
210         int                                             mode;                   // open mode
211         int                                             fileSize;               // size of the file
212         FILE *                                  o;                              // file handle
213         bool                                    handleSync;             // true if written data is immediately flushed
214 };
215
216
217 class idFile_InZip : public idFile {
218         friend class                    idFileSystemLocal;
219
220 public:
221                                                         idFile_InZip( void );
222         virtual                                 ~idFile_InZip( void );
223
224         virtual const char *    GetName( void ) { return name.c_str(); }
225         virtual const char *    GetFullPath( void ) { return fullPath.c_str(); }
226         virtual int                             Read( void *buffer, int len );
227         virtual int                             Write( const void *buffer, int len );
228         virtual int                             Length( void );
229         virtual ID_TIME_T                       Timestamp( void );
230         virtual int                             Tell( void );
231         virtual void                    ForceFlush( void );
232         virtual void                    Flush( void );
233         virtual int                             Seek( long offset, fsOrigin_t origin );
234
235 private:
236         idStr                                   name;                   // name of the file in the pak
237         idStr                                   fullPath;               // full file path including pak file name
238         int                                             zipFilePos;             // zip file info position in pak
239         int                                             fileSize;               // size of the file
240         void *                                  z;                              // unzip info
241 };
242
243 #endif /* !__FILE_H__ */