oculus1

annotate libovr/Src/Kernel/OVR_File.h @ 1:e2f9e4603129

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