rev |
line source |
nuclear@3
|
1 /************************************************************************************
|
nuclear@3
|
2
|
nuclear@3
|
3 PublicHeader: Kernel
|
nuclear@3
|
4 Filename : OVR_File.h
|
nuclear@3
|
5 Content : Header for all internal file management - functions and structures
|
nuclear@3
|
6 to be inherited by OS specific subclasses.
|
nuclear@3
|
7 Created : September 19, 2012
|
nuclear@3
|
8 Notes :
|
nuclear@3
|
9
|
nuclear@3
|
10 Notes : errno may not be preserved across use of BaseFile member functions
|
nuclear@3
|
11 : Directories cannot be deleted while files opened from them are in use
|
nuclear@3
|
12 (For the GetFullName function)
|
nuclear@3
|
13
|
nuclear@3
|
14 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
|
nuclear@3
|
15
|
nuclear@3
|
16 Use of this software is subject to the terms of the Oculus license
|
nuclear@3
|
17 agreement provided at the time of installation or download, or which
|
nuclear@3
|
18 otherwise accompanies this software in either electronic or hard copy form.
|
nuclear@3
|
19
|
nuclear@3
|
20 ************************************************************************************/
|
nuclear@3
|
21
|
nuclear@3
|
22 #ifndef OVR_File_h
|
nuclear@3
|
23 #define OVR_File_h
|
nuclear@3
|
24
|
nuclear@3
|
25 #include "OVR_RefCount.h"
|
nuclear@3
|
26 #include "OVR_Std.h"
|
nuclear@3
|
27 #include "OVR_Alg.h"
|
nuclear@3
|
28
|
nuclear@3
|
29 #include <stdio.h>
|
nuclear@3
|
30 #include "OVR_String.h"
|
nuclear@3
|
31
|
nuclear@3
|
32 namespace OVR {
|
nuclear@3
|
33
|
nuclear@3
|
34 // ***** Declared classes
|
nuclear@3
|
35 class FileConstants;
|
nuclear@3
|
36 class File;
|
nuclear@3
|
37 class DelegatedFile;
|
nuclear@3
|
38 class BufferedFile;
|
nuclear@3
|
39
|
nuclear@3
|
40
|
nuclear@3
|
41 // ***** Flags for File & Directory accesses
|
nuclear@3
|
42
|
nuclear@3
|
43 class FileConstants
|
nuclear@3
|
44 {
|
nuclear@3
|
45 public:
|
nuclear@3
|
46
|
nuclear@3
|
47 // *** File open flags
|
nuclear@3
|
48 enum OpenFlags
|
nuclear@3
|
49 {
|
nuclear@3
|
50 Open_Read = 1,
|
nuclear@3
|
51 Open_Write = 2,
|
nuclear@3
|
52 Open_ReadWrite = 3,
|
nuclear@3
|
53
|
nuclear@3
|
54 // Opens file and truncates it to zero length
|
nuclear@3
|
55 // - file must have write permission
|
nuclear@3
|
56 // - when used with Create, it opens an existing
|
nuclear@3
|
57 // file and empties it or creates a new file
|
nuclear@3
|
58 Open_Truncate = 4,
|
nuclear@3
|
59
|
nuclear@3
|
60 // Creates and opens new file
|
nuclear@3
|
61 // - does not erase contents if file already
|
nuclear@3
|
62 // exists unless combined with Truncate
|
nuclear@3
|
63 Open_Create = 8,
|
nuclear@3
|
64
|
nuclear@3
|
65 // Returns an error value if the file already exists
|
nuclear@3
|
66 Open_CreateOnly = 24,
|
nuclear@3
|
67
|
nuclear@3
|
68 // Open file with buffering
|
nuclear@3
|
69 Open_Buffered = 32
|
nuclear@3
|
70 };
|
nuclear@3
|
71
|
nuclear@3
|
72 // *** File Mode flags
|
nuclear@3
|
73 enum Modes
|
nuclear@3
|
74 {
|
nuclear@3
|
75 Mode_Read = 0444,
|
nuclear@3
|
76 Mode_Write = 0222,
|
nuclear@3
|
77 Mode_Execute = 0111,
|
nuclear@3
|
78
|
nuclear@3
|
79 Mode_ReadWrite = 0666
|
nuclear@3
|
80 };
|
nuclear@3
|
81
|
nuclear@3
|
82 // *** Seek operations
|
nuclear@3
|
83 enum SeekOps
|
nuclear@3
|
84 {
|
nuclear@3
|
85 Seek_Set = 0,
|
nuclear@3
|
86 Seek_Cur = 1,
|
nuclear@3
|
87 Seek_End = 2
|
nuclear@3
|
88 };
|
nuclear@3
|
89
|
nuclear@3
|
90 // *** Errors
|
nuclear@3
|
91 enum Errors
|
nuclear@3
|
92 {
|
nuclear@3
|
93 Error_FileNotFound = 0x1001,
|
nuclear@3
|
94 Error_Access = 0x1002,
|
nuclear@3
|
95 Error_IOError = 0x1003,
|
nuclear@3
|
96 Error_DiskFull = 0x1004
|
nuclear@3
|
97 };
|
nuclear@3
|
98 };
|
nuclear@3
|
99
|
nuclear@3
|
100
|
nuclear@3
|
101 //-----------------------------------------------------------------------------------
|
nuclear@3
|
102 // ***** File Class
|
nuclear@3
|
103
|
nuclear@3
|
104 // The pure virtual base random-access file
|
nuclear@3
|
105 // This is a base class to all files
|
nuclear@3
|
106
|
nuclear@3
|
107 class File : public RefCountBase<File>, public FileConstants
|
nuclear@3
|
108 {
|
nuclear@3
|
109 public:
|
nuclear@3
|
110 File() { }
|
nuclear@3
|
111 // ** Location Information
|
nuclear@3
|
112
|
nuclear@3
|
113 // Returns a file name path relative to the 'reference' directory
|
nuclear@3
|
114 // This is often a path that was used to create a file
|
nuclear@3
|
115 // (this is not a global path, global path can be obtained with help of directory)
|
nuclear@3
|
116 virtual const char* GetFilePath() = 0;
|
nuclear@3
|
117
|
nuclear@3
|
118
|
nuclear@3
|
119 // ** File Information
|
nuclear@3
|
120
|
nuclear@3
|
121 // Return 1 if file's usable (open)
|
nuclear@3
|
122 virtual bool IsValid() = 0;
|
nuclear@3
|
123 // Return 1 if file's writable, otherwise 0
|
nuclear@3
|
124 virtual bool IsWritable() = 0;
|
nuclear@3
|
125
|
nuclear@3
|
126 // Return position
|
nuclear@3
|
127 virtual int Tell() = 0;
|
nuclear@3
|
128 virtual SInt64 LTell() = 0;
|
nuclear@3
|
129
|
nuclear@3
|
130 // File size
|
nuclear@3
|
131 virtual int GetLength() = 0;
|
nuclear@3
|
132 virtual SInt64 LGetLength() = 0;
|
nuclear@3
|
133
|
nuclear@3
|
134 // Returns file stats
|
nuclear@3
|
135 // 0 for failure
|
nuclear@3
|
136 //virtual bool Stat(FileStats *pfs) = 0;
|
nuclear@3
|
137
|
nuclear@3
|
138 // Return errno-based error code
|
nuclear@3
|
139 // Useful if any other function failed
|
nuclear@3
|
140 virtual int GetErrorCode() = 0;
|
nuclear@3
|
141
|
nuclear@3
|
142
|
nuclear@3
|
143 // ** Stream implementation & I/O
|
nuclear@3
|
144
|
nuclear@3
|
145 // Blocking write, will write in the given number of bytes to the stream
|
nuclear@3
|
146 // Returns : -1 for error
|
nuclear@3
|
147 // Otherwise number of bytes read
|
nuclear@3
|
148 virtual int Write(const UByte *pbufer, int numBytes) = 0;
|
nuclear@3
|
149 // Blocking read, will read in the given number of bytes or less from the stream
|
nuclear@3
|
150 // Returns : -1 for error
|
nuclear@3
|
151 // Otherwise number of bytes read,
|
nuclear@3
|
152 // if 0 or < numBytes, no more bytes available; end of file or the other side of stream is closed
|
nuclear@3
|
153 virtual int Read(UByte *pbufer, int numBytes) = 0;
|
nuclear@3
|
154
|
nuclear@3
|
155 // Skips (ignores) a given # of bytes
|
nuclear@3
|
156 // Same return values as Read
|
nuclear@3
|
157 virtual int SkipBytes(int numBytes) = 0;
|
nuclear@3
|
158
|
nuclear@3
|
159 // Returns the number of bytes available to read from a stream without blocking
|
nuclear@3
|
160 // For a file, this should generally be number of bytes to the end
|
nuclear@3
|
161 virtual int BytesAvailable() = 0;
|
nuclear@3
|
162
|
nuclear@3
|
163 // Causes any implementation's buffered data to be delivered to destination
|
nuclear@3
|
164 // Return 0 for error
|
nuclear@3
|
165 virtual bool Flush() = 0;
|
nuclear@3
|
166
|
nuclear@3
|
167
|
nuclear@3
|
168 // Need to provide a more optimized implementation that doe snot necessarily involve a lot of seeking
|
nuclear@3
|
169 inline bool IsEOF() { return !BytesAvailable(); }
|
nuclear@3
|
170
|
nuclear@3
|
171
|
nuclear@3
|
172 // Seeking
|
nuclear@3
|
173 // Returns new position, -1 for error
|
nuclear@3
|
174 virtual int Seek(int offset, int origin=Seek_Set) = 0;
|
nuclear@3
|
175 virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set) = 0;
|
nuclear@3
|
176 // Seek simplification
|
nuclear@3
|
177 int SeekToBegin() {return Seek(0); }
|
nuclear@3
|
178 int SeekToEnd() {return Seek(0,Seek_End); }
|
nuclear@3
|
179 int Skip(int numBytes) {return Seek(numBytes,Seek_Cur); }
|
nuclear@3
|
180
|
nuclear@3
|
181
|
nuclear@3
|
182 // Appends other file data from a stream
|
nuclear@3
|
183 // Return -1 for error, else # of bytes written
|
nuclear@3
|
184 virtual int CopyFromStream(File *pstream, int byteSize) = 0;
|
nuclear@3
|
185
|
nuclear@3
|
186 // Closes the file
|
nuclear@3
|
187 // After close, file cannot be accessed
|
nuclear@3
|
188 virtual bool Close() = 0;
|
nuclear@3
|
189
|
nuclear@3
|
190
|
nuclear@3
|
191 // ***** Inlines for convenient primitive type serialization
|
nuclear@3
|
192
|
nuclear@3
|
193 // Read/Write helpers
|
nuclear@3
|
194 private:
|
nuclear@3
|
195 UInt64 PRead64() { UInt64 v = 0; Read((UByte*)&v, 8); return v; }
|
nuclear@3
|
196 UInt32 PRead32() { UInt32 v = 0; Read((UByte*)&v, 4); return v; }
|
nuclear@3
|
197 UInt16 PRead16() { UInt16 v = 0; Read((UByte*)&v, 2); return v; }
|
nuclear@3
|
198 UByte PRead8() { UByte v = 0; Read((UByte*)&v, 1); return v; }
|
nuclear@3
|
199 void PWrite64(UInt64 v) { Write((UByte*)&v, 8); }
|
nuclear@3
|
200 void PWrite32(UInt32 v) { Write((UByte*)&v, 4); }
|
nuclear@3
|
201 void PWrite16(UInt16 v) { Write((UByte*)&v, 2); }
|
nuclear@3
|
202 void PWrite8(UByte v) { Write((UByte*)&v, 1); }
|
nuclear@3
|
203
|
nuclear@3
|
204 public:
|
nuclear@3
|
205
|
nuclear@3
|
206 // Writing primitive types - Little Endian
|
nuclear@3
|
207 inline void WriteUByte(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); }
|
nuclear@3
|
208 inline void WriteSByte(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); }
|
nuclear@3
|
209 inline void WriteUInt8(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); }
|
nuclear@3
|
210 inline void WriteSInt8(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); }
|
nuclear@3
|
211 inline void WriteUInt16(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToLE(v)); }
|
nuclear@3
|
212 inline void WriteSInt16(SInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToLE(v)); }
|
nuclear@3
|
213 inline void WriteUInt32(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToLE(v)); }
|
nuclear@3
|
214 inline void WriteSInt32(SInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToLE(v)); }
|
nuclear@3
|
215 inline void WriteUInt64(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToLE(v)); }
|
nuclear@3
|
216 inline void WriteSInt64(SInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToLE(v)); }
|
nuclear@3
|
217 inline void WriteFloat(float v) { v = Alg::ByteUtil::SystemToLE(v); Write((UByte*)&v, 4); }
|
nuclear@3
|
218 inline void WriteDouble(double v) { v = Alg::ByteUtil::SystemToLE(v); Write((UByte*)&v, 8); }
|
nuclear@3
|
219 // Writing primitive types - Big Endian
|
nuclear@3
|
220 inline void WriteUByteBE(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); }
|
nuclear@3
|
221 inline void WriteSByteBE(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); }
|
nuclear@3
|
222 inline void WriteUInt8BE(UInt16 v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); }
|
nuclear@3
|
223 inline void WriteSInt8BE(SInt16 v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); }
|
nuclear@3
|
224 inline void WriteUInt16BE(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToBE(v)); }
|
nuclear@3
|
225 inline void WriteSInt16BE(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToBE(v)); }
|
nuclear@3
|
226 inline void WriteUInt32BE(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToBE(v)); }
|
nuclear@3
|
227 inline void WriteSInt32BE(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToBE(v)); }
|
nuclear@3
|
228 inline void WriteUInt64BE(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToBE(v)); }
|
nuclear@3
|
229 inline void WriteSInt64BE(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToBE(v)); }
|
nuclear@3
|
230 inline void WriteFloatBE(float v) { v = Alg::ByteUtil::SystemToBE(v); Write((UByte*)&v, 4); }
|
nuclear@3
|
231 inline void WriteDoubleBE(double v) { v = Alg::ByteUtil::SystemToBE(v); Write((UByte*)&v, 8); }
|
nuclear@3
|
232
|
nuclear@3
|
233 // Reading primitive types - Little Endian
|
nuclear@3
|
234 inline UByte ReadUByte() { return (UByte)Alg::ByteUtil::LEToSystem(PRead8()); }
|
nuclear@3
|
235 inline SByte ReadSByte() { return (SByte)Alg::ByteUtil::LEToSystem(PRead8()); }
|
nuclear@3
|
236 inline UByte ReadUInt8() { return (UByte)Alg::ByteUtil::LEToSystem(PRead8()); }
|
nuclear@3
|
237 inline SByte ReadSInt8() { return (SByte)Alg::ByteUtil::LEToSystem(PRead8()); }
|
nuclear@3
|
238 inline UInt16 ReadUInt16() { return (UInt16)Alg::ByteUtil::LEToSystem(PRead16()); }
|
nuclear@3
|
239 inline SInt16 ReadSInt16() { return (SInt16)Alg::ByteUtil::LEToSystem(PRead16()); }
|
nuclear@3
|
240 inline UInt32 ReadUInt32() { return (UInt32)Alg::ByteUtil::LEToSystem(PRead32()); }
|
nuclear@3
|
241 inline SInt32 ReadSInt32() { return (SInt32)Alg::ByteUtil::LEToSystem(PRead32()); }
|
nuclear@3
|
242 inline UInt64 ReadUInt64() { return (UInt64)Alg::ByteUtil::LEToSystem(PRead64()); }
|
nuclear@3
|
243 inline SInt64 ReadSInt64() { return (SInt64)Alg::ByteUtil::LEToSystem(PRead64()); }
|
nuclear@3
|
244 inline float ReadFloat() { float v = 0.0f; Read((UByte*)&v, 4); return Alg::ByteUtil::LEToSystem(v); }
|
nuclear@3
|
245 inline double ReadDouble() { double v = 0.0; Read((UByte*)&v, 8); return Alg::ByteUtil::LEToSystem(v); }
|
nuclear@3
|
246 // Reading primitive types - Big Endian
|
nuclear@3
|
247 inline UByte ReadUByteBE() { return (UByte)Alg::ByteUtil::BEToSystem(PRead8()); }
|
nuclear@3
|
248 inline SByte ReadSByteBE() { return (SByte)Alg::ByteUtil::BEToSystem(PRead8()); }
|
nuclear@3
|
249 inline UByte ReadUInt8BE() { return (UByte)Alg::ByteUtil::BEToSystem(PRead8()); }
|
nuclear@3
|
250 inline SByte ReadSInt8BE() { return (SByte)Alg::ByteUtil::BEToSystem(PRead8()); }
|
nuclear@3
|
251 inline UInt16 ReadUInt16BE() { return (UInt16)Alg::ByteUtil::BEToSystem(PRead16()); }
|
nuclear@3
|
252 inline SInt16 ReadSInt16BE() { return (SInt16)Alg::ByteUtil::BEToSystem(PRead16()); }
|
nuclear@3
|
253 inline UInt32 ReadUInt32BE() { return (UInt32)Alg::ByteUtil::BEToSystem(PRead32()); }
|
nuclear@3
|
254 inline SInt32 ReadSInt32BE() { return (SInt32)Alg::ByteUtil::BEToSystem(PRead32()); }
|
nuclear@3
|
255 inline UInt64 ReadUInt64BE() { return (UInt64)Alg::ByteUtil::BEToSystem(PRead64()); }
|
nuclear@3
|
256 inline SInt64 ReadSInt64BE() { return (SInt64)Alg::ByteUtil::BEToSystem(PRead64()); }
|
nuclear@3
|
257 inline float ReadFloatBE() { float v = 0.0f; Read((UByte*)&v, 4); return Alg::ByteUtil::BEToSystem(v); }
|
nuclear@3
|
258 inline double ReadDoubleBE() { double v = 0.0; Read((UByte*)&v, 8); return Alg::ByteUtil::BEToSystem(v); }
|
nuclear@3
|
259 };
|
nuclear@3
|
260
|
nuclear@3
|
261
|
nuclear@3
|
262 // *** Delegated File
|
nuclear@3
|
263
|
nuclear@3
|
264 class DelegatedFile : public File
|
nuclear@3
|
265 {
|
nuclear@3
|
266 protected:
|
nuclear@3
|
267 // Delegating file pointer
|
nuclear@3
|
268 Ptr<File> pFile;
|
nuclear@3
|
269
|
nuclear@3
|
270 // Hidden default constructor
|
nuclear@3
|
271 DelegatedFile() : pFile(0) { }
|
nuclear@3
|
272 DelegatedFile(const DelegatedFile &source) : File() { OVR_UNUSED(source); }
|
nuclear@3
|
273 public:
|
nuclear@3
|
274 // Constructors
|
nuclear@3
|
275 DelegatedFile(File *pfile) : pFile(pfile) { }
|
nuclear@3
|
276
|
nuclear@3
|
277 // ** Location Information
|
nuclear@3
|
278 virtual const char* GetFilePath() { return pFile->GetFilePath(); }
|
nuclear@3
|
279
|
nuclear@3
|
280 // ** File Information
|
nuclear@3
|
281 virtual bool IsValid() { return pFile && pFile->IsValid(); }
|
nuclear@3
|
282 virtual bool IsWritable() { return pFile->IsWritable(); }
|
nuclear@3
|
283 // virtual bool IsRecoverable() { return pFile->IsRecoverable(); }
|
nuclear@3
|
284
|
nuclear@3
|
285 virtual int Tell() { return pFile->Tell(); }
|
nuclear@3
|
286 virtual SInt64 LTell() { return pFile->LTell(); }
|
nuclear@3
|
287
|
nuclear@3
|
288 virtual int GetLength() { return pFile->GetLength(); }
|
nuclear@3
|
289 virtual SInt64 LGetLength() { return pFile->LGetLength(); }
|
nuclear@3
|
290
|
nuclear@3
|
291 //virtual bool Stat(FileStats *pfs) { return pFile->Stat(pfs); }
|
nuclear@3
|
292
|
nuclear@3
|
293 virtual int GetErrorCode() { return pFile->GetErrorCode(); }
|
nuclear@3
|
294
|
nuclear@3
|
295 // ** Stream implementation & I/O
|
nuclear@3
|
296 virtual int Write(const UByte *pbuffer, int numBytes) { return pFile->Write(pbuffer,numBytes); }
|
nuclear@3
|
297 virtual int Read(UByte *pbuffer, int numBytes) { return pFile->Read(pbuffer,numBytes); }
|
nuclear@3
|
298
|
nuclear@3
|
299 virtual int SkipBytes(int numBytes) { return pFile->SkipBytes(numBytes); }
|
nuclear@3
|
300
|
nuclear@3
|
301 virtual int BytesAvailable() { return pFile->BytesAvailable(); }
|
nuclear@3
|
302
|
nuclear@3
|
303 virtual bool Flush() { return pFile->Flush(); }
|
nuclear@3
|
304
|
nuclear@3
|
305 // Seeking
|
nuclear@3
|
306 virtual int Seek(int offset, int origin=Seek_Set) { return pFile->Seek(offset,origin); }
|
nuclear@3
|
307 virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set) { return pFile->LSeek(offset,origin); }
|
nuclear@3
|
308
|
nuclear@3
|
309 virtual int CopyFromStream(File *pstream, int byteSize) { return pFile->CopyFromStream(pstream,byteSize); }
|
nuclear@3
|
310
|
nuclear@3
|
311 // Closing the file
|
nuclear@3
|
312 virtual bool Close() { return pFile->Close(); }
|
nuclear@3
|
313 };
|
nuclear@3
|
314
|
nuclear@3
|
315
|
nuclear@3
|
316 //-----------------------------------------------------------------------------------
|
nuclear@3
|
317 // ***** Buffered File
|
nuclear@3
|
318
|
nuclear@3
|
319 // This file class adds buffering to an existing file
|
nuclear@3
|
320 // Buffered file never fails by itself; if there's not
|
nuclear@3
|
321 // enough memory for buffer, no buffer's used
|
nuclear@3
|
322
|
nuclear@3
|
323 class BufferedFile : public DelegatedFile
|
nuclear@3
|
324 {
|
nuclear@3
|
325 protected:
|
nuclear@3
|
326 enum BufferModeType
|
nuclear@3
|
327 {
|
nuclear@3
|
328 NoBuffer,
|
nuclear@3
|
329 ReadBuffer,
|
nuclear@3
|
330 WriteBuffer
|
nuclear@3
|
331 };
|
nuclear@3
|
332
|
nuclear@3
|
333 // Buffer & the mode it's in
|
nuclear@3
|
334 UByte* pBuffer;
|
nuclear@3
|
335 BufferModeType BufferMode;
|
nuclear@3
|
336 // Position in buffer
|
nuclear@3
|
337 unsigned Pos;
|
nuclear@3
|
338 // Data in buffer if reading
|
nuclear@3
|
339 unsigned DataSize;
|
nuclear@3
|
340 // Underlying file position
|
nuclear@3
|
341 UInt64 FilePos;
|
nuclear@3
|
342
|
nuclear@3
|
343 // Initializes buffering to a certain mode
|
nuclear@3
|
344 bool SetBufferMode(BufferModeType mode);
|
nuclear@3
|
345 // Flushes buffer
|
nuclear@3
|
346 // WriteBuffer - write data to disk, ReadBuffer - reset buffer & fix file position
|
nuclear@3
|
347 void FlushBuffer();
|
nuclear@3
|
348 // Loads data into ReadBuffer
|
nuclear@3
|
349 // WARNING: Right now LoadBuffer() assumes the buffer's empty
|
nuclear@3
|
350 void LoadBuffer();
|
nuclear@3
|
351
|
nuclear@3
|
352 // Hidden constructor
|
nuclear@3
|
353 BufferedFile();
|
nuclear@3
|
354 inline BufferedFile(const BufferedFile &source) : DelegatedFile() { OVR_UNUSED(source); }
|
nuclear@3
|
355 public:
|
nuclear@3
|
356
|
nuclear@3
|
357 // Constructor
|
nuclear@3
|
358 // - takes another file as source
|
nuclear@3
|
359 BufferedFile(File *pfile);
|
nuclear@3
|
360 ~BufferedFile();
|
nuclear@3
|
361
|
nuclear@3
|
362
|
nuclear@3
|
363 // ** Overridden functions
|
nuclear@3
|
364
|
nuclear@3
|
365 // We override all the functions that can possibly
|
nuclear@3
|
366 // require buffer mode switch, flush, or extra calculations
|
nuclear@3
|
367 virtual int Tell();
|
nuclear@3
|
368 virtual SInt64 LTell();
|
nuclear@3
|
369
|
nuclear@3
|
370 virtual int GetLength();
|
nuclear@3
|
371 virtual SInt64 LGetLength();
|
nuclear@3
|
372
|
nuclear@3
|
373 // virtual bool Stat(GFileStats *pfs);
|
nuclear@3
|
374
|
nuclear@3
|
375 virtual int Write(const UByte *pbufer, int numBytes);
|
nuclear@3
|
376 virtual int Read(UByte *pbufer, int numBytes);
|
nuclear@3
|
377
|
nuclear@3
|
378 virtual int SkipBytes(int numBytes);
|
nuclear@3
|
379
|
nuclear@3
|
380 virtual int BytesAvailable();
|
nuclear@3
|
381
|
nuclear@3
|
382 virtual bool Flush();
|
nuclear@3
|
383
|
nuclear@3
|
384 virtual int Seek(int offset, int origin=Seek_Set);
|
nuclear@3
|
385 virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set);
|
nuclear@3
|
386
|
nuclear@3
|
387 virtual int CopyFromStream(File *pstream, int byteSize);
|
nuclear@3
|
388
|
nuclear@3
|
389 virtual bool Close();
|
nuclear@3
|
390 };
|
nuclear@3
|
391
|
nuclear@3
|
392
|
nuclear@3
|
393 //-----------------------------------------------------------------------------------
|
nuclear@3
|
394 // ***** Memory File
|
nuclear@3
|
395
|
nuclear@3
|
396 class MemoryFile : public File
|
nuclear@3
|
397 {
|
nuclear@3
|
398 public:
|
nuclear@3
|
399
|
nuclear@3
|
400 const char* GetFilePath() { return FilePath.ToCStr(); }
|
nuclear@3
|
401
|
nuclear@3
|
402 bool IsValid() { return Valid; }
|
nuclear@3
|
403 bool IsWritable() { return false; }
|
nuclear@3
|
404
|
nuclear@3
|
405 bool Flush() { return true; }
|
nuclear@3
|
406 int GetErrorCode() { return 0; }
|
nuclear@3
|
407
|
nuclear@3
|
408 int Tell() { return FileIndex; }
|
nuclear@3
|
409 SInt64 LTell() { return (SInt64) FileIndex; }
|
nuclear@3
|
410
|
nuclear@3
|
411 int GetLength() { return FileSize; }
|
nuclear@3
|
412 SInt64 LGetLength() { return (SInt64) FileSize; }
|
nuclear@3
|
413
|
nuclear@3
|
414 bool Close()
|
nuclear@3
|
415 {
|
nuclear@3
|
416 Valid = false;
|
nuclear@3
|
417 return false;
|
nuclear@3
|
418 }
|
nuclear@3
|
419
|
nuclear@3
|
420 int CopyFromStream(File *pstream, int byteSize)
|
nuclear@3
|
421 { OVR_UNUSED2(pstream, byteSize);
|
nuclear@3
|
422 return 0;
|
nuclear@3
|
423 }
|
nuclear@3
|
424
|
nuclear@3
|
425 int Write(const UByte *pbuffer, int numBytes)
|
nuclear@3
|
426 { OVR_UNUSED2(pbuffer, numBytes);
|
nuclear@3
|
427 return 0;
|
nuclear@3
|
428 }
|
nuclear@3
|
429
|
nuclear@3
|
430 int Read(UByte *pbufer, int numBytes)
|
nuclear@3
|
431 {
|
nuclear@3
|
432 if (FileIndex + numBytes > FileSize)
|
nuclear@3
|
433 {
|
nuclear@3
|
434 numBytes = FileSize - FileIndex;
|
nuclear@3
|
435 }
|
nuclear@3
|
436
|
nuclear@3
|
437 if (numBytes > 0)
|
nuclear@3
|
438 {
|
nuclear@3
|
439 ::memcpy (pbufer, &FileData [FileIndex], numBytes);
|
nuclear@3
|
440
|
nuclear@3
|
441 FileIndex += numBytes;
|
nuclear@3
|
442 }
|
nuclear@3
|
443
|
nuclear@3
|
444 return numBytes;
|
nuclear@3
|
445 }
|
nuclear@3
|
446
|
nuclear@3
|
447 int SkipBytes(int numBytes)
|
nuclear@3
|
448 {
|
nuclear@3
|
449 if (FileIndex + numBytes > FileSize)
|
nuclear@3
|
450 {
|
nuclear@3
|
451 numBytes = FileSize - FileIndex;
|
nuclear@3
|
452 }
|
nuclear@3
|
453
|
nuclear@3
|
454 FileIndex += numBytes;
|
nuclear@3
|
455
|
nuclear@3
|
456 return numBytes;
|
nuclear@3
|
457 }
|
nuclear@3
|
458
|
nuclear@3
|
459 int BytesAvailable()
|
nuclear@3
|
460 {
|
nuclear@3
|
461 return (FileSize - FileIndex);
|
nuclear@3
|
462 }
|
nuclear@3
|
463
|
nuclear@3
|
464 int Seek(int offset, int origin = Seek_Set)
|
nuclear@3
|
465 {
|
nuclear@3
|
466 switch (origin)
|
nuclear@3
|
467 {
|
nuclear@3
|
468 case Seek_Set : FileIndex = offset; break;
|
nuclear@3
|
469 case Seek_Cur : FileIndex += offset; break;
|
nuclear@3
|
470 case Seek_End : FileIndex = FileSize - offset; break;
|
nuclear@3
|
471 }
|
nuclear@3
|
472
|
nuclear@3
|
473 return FileIndex;
|
nuclear@3
|
474 }
|
nuclear@3
|
475
|
nuclear@3
|
476 SInt64 LSeek(SInt64 offset, int origin = Seek_Set)
|
nuclear@3
|
477 {
|
nuclear@3
|
478 return (SInt64) Seek((int) offset, origin);
|
nuclear@3
|
479 }
|
nuclear@3
|
480
|
nuclear@3
|
481 public:
|
nuclear@3
|
482
|
nuclear@3
|
483 MemoryFile (const String& fileName, const UByte *pBuffer, int buffSize)
|
nuclear@3
|
484 : FilePath(fileName)
|
nuclear@3
|
485 {
|
nuclear@3
|
486 FileData = pBuffer;
|
nuclear@3
|
487 FileSize = buffSize;
|
nuclear@3
|
488 FileIndex = 0;
|
nuclear@3
|
489 Valid = (!fileName.IsEmpty() && pBuffer && buffSize > 0) ? true : false;
|
nuclear@3
|
490 }
|
nuclear@3
|
491
|
nuclear@3
|
492 // pfileName should be encoded as UTF-8 to support international file names.
|
nuclear@3
|
493 MemoryFile (const char* pfileName, const UByte *pBuffer, int buffSize)
|
nuclear@3
|
494 : FilePath(pfileName)
|
nuclear@3
|
495 {
|
nuclear@3
|
496 FileData = pBuffer;
|
nuclear@3
|
497 FileSize = buffSize;
|
nuclear@3
|
498 FileIndex = 0;
|
nuclear@3
|
499 Valid = (pfileName && pBuffer && buffSize > 0) ? true : false;
|
nuclear@3
|
500 }
|
nuclear@3
|
501 private:
|
nuclear@3
|
502
|
nuclear@3
|
503 String FilePath;
|
nuclear@3
|
504 const UByte *FileData;
|
nuclear@3
|
505 int FileSize;
|
nuclear@3
|
506 int FileIndex;
|
nuclear@3
|
507 bool Valid;
|
nuclear@3
|
508 };
|
nuclear@3
|
509
|
nuclear@3
|
510
|
nuclear@3
|
511 // ***** Global path helpers
|
nuclear@3
|
512
|
nuclear@3
|
513 // Find trailing short filename in a path.
|
nuclear@3
|
514 const char* OVR_CDECL GetShortFilename(const char* purl);
|
nuclear@3
|
515
|
nuclear@3
|
516 } // OVR
|
nuclear@3
|
517
|
nuclear@3
|
518 #endif
|