oculus1
diff libovr/Src/Kernel/OVR_File.h @ 3:b069a5c27388
added a couple more stuff, fixed all the LibOVR line endings
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 15 Sep 2013 04:10:05 +0300 |
parents | e2f9e4603129 |
children |
line diff
1.1 --- a/libovr/Src/Kernel/OVR_File.h Sat Sep 14 17:51:03 2013 +0300 1.2 +++ b/libovr/Src/Kernel/OVR_File.h Sun Sep 15 04:10:05 2013 +0300 1.3 @@ -1,1 +1,518 @@ 1.4 -/************************************************************************************ 1.5 1.6 PublicHeader: Kernel 1.7 Filename : OVR_File.h 1.8 Content : Header for all internal file management - functions and structures 1.9 to be inherited by OS specific subclasses. 1.10 Created : September 19, 2012 1.11 Notes : 1.12 1.13 Notes : errno may not be preserved across use of BaseFile member functions 1.14 : Directories cannot be deleted while files opened from them are in use 1.15 (For the GetFullName function) 1.16 1.17 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. 1.18 1.19 Use of this software is subject to the terms of the Oculus license 1.20 agreement provided at the time of installation or download, or which 1.21 otherwise accompanies this software in either electronic or hard copy form. 1.22 1.23 ************************************************************************************/ 1.24 1.25 #ifndef OVR_File_h 1.26 #define OVR_File_h 1.27 1.28 #include "OVR_RefCount.h" 1.29 #include "OVR_Std.h" 1.30 #include "OVR_Alg.h" 1.31 1.32 #include <stdio.h> 1.33 #include "OVR_String.h" 1.34 1.35 namespace OVR { 1.36 1.37 // ***** Declared classes 1.38 class FileConstants; 1.39 class File; 1.40 class DelegatedFile; 1.41 class BufferedFile; 1.42 1.43 1.44 // ***** Flags for File & Directory accesses 1.45 1.46 class FileConstants 1.47 { 1.48 public: 1.49 1.50 // *** File open flags 1.51 enum OpenFlags 1.52 { 1.53 Open_Read = 1, 1.54 Open_Write = 2, 1.55 Open_ReadWrite = 3, 1.56 1.57 // Opens file and truncates it to zero length 1.58 // - file must have write permission 1.59 // - when used with Create, it opens an existing 1.60 // file and empties it or creates a new file 1.61 Open_Truncate = 4, 1.62 1.63 // Creates and opens new file 1.64 // - does not erase contents if file already 1.65 // exists unless combined with Truncate 1.66 Open_Create = 8, 1.67 1.68 // Returns an error value if the file already exists 1.69 Open_CreateOnly = 24, 1.70 1.71 // Open file with buffering 1.72 Open_Buffered = 32 1.73 }; 1.74 1.75 // *** File Mode flags 1.76 enum Modes 1.77 { 1.78 Mode_Read = 0444, 1.79 Mode_Write = 0222, 1.80 Mode_Execute = 0111, 1.81 1.82 Mode_ReadWrite = 0666 1.83 }; 1.84 1.85 // *** Seek operations 1.86 enum SeekOps 1.87 { 1.88 Seek_Set = 0, 1.89 Seek_Cur = 1, 1.90 Seek_End = 2 1.91 }; 1.92 1.93 // *** Errors 1.94 enum Errors 1.95 { 1.96 Error_FileNotFound = 0x1001, 1.97 Error_Access = 0x1002, 1.98 Error_IOError = 0x1003, 1.99 Error_DiskFull = 0x1004 1.100 }; 1.101 }; 1.102 1.103 1.104 //----------------------------------------------------------------------------------- 1.105 // ***** File Class 1.106 1.107 // The pure virtual base random-access file 1.108 // This is a base class to all files 1.109 1.110 class File : public RefCountBase<File>, public FileConstants 1.111 { 1.112 public: 1.113 File() { } 1.114 // ** Location Information 1.115 1.116 // Returns a file name path relative to the 'reference' directory 1.117 // This is often a path that was used to create a file 1.118 // (this is not a global path, global path can be obtained with help of directory) 1.119 virtual const char* GetFilePath() = 0; 1.120 1.121 1.122 // ** File Information 1.123 1.124 // Return 1 if file's usable (open) 1.125 virtual bool IsValid() = 0; 1.126 // Return 1 if file's writable, otherwise 0 1.127 virtual bool IsWritable() = 0; 1.128 1.129 // Return position 1.130 virtual int Tell() = 0; 1.131 virtual SInt64 LTell() = 0; 1.132 1.133 // File size 1.134 virtual int GetLength() = 0; 1.135 virtual SInt64 LGetLength() = 0; 1.136 1.137 // Returns file stats 1.138 // 0 for failure 1.139 //virtual bool Stat(FileStats *pfs) = 0; 1.140 1.141 // Return errno-based error code 1.142 // Useful if any other function failed 1.143 virtual int GetErrorCode() = 0; 1.144 1.145 1.146 // ** Stream implementation & I/O 1.147 1.148 // Blocking write, will write in the given number of bytes to the stream 1.149 // Returns : -1 for error 1.150 // Otherwise number of bytes read 1.151 virtual int Write(const UByte *pbufer, int numBytes) = 0; 1.152 // Blocking read, will read in the given number of bytes or less from the stream 1.153 // Returns : -1 for error 1.154 // Otherwise number of bytes read, 1.155 // if 0 or < numBytes, no more bytes available; end of file or the other side of stream is closed 1.156 virtual int Read(UByte *pbufer, int numBytes) = 0; 1.157 1.158 // Skips (ignores) a given # of bytes 1.159 // Same return values as Read 1.160 virtual int SkipBytes(int numBytes) = 0; 1.161 1.162 // Returns the number of bytes available to read from a stream without blocking 1.163 // For a file, this should generally be number of bytes to the end 1.164 virtual int BytesAvailable() = 0; 1.165 1.166 // Causes any implementation's buffered data to be delivered to destination 1.167 // Return 0 for error 1.168 virtual bool Flush() = 0; 1.169 1.170 1.171 // Need to provide a more optimized implementation that doe snot necessarily involve a lot of seeking 1.172 inline bool IsEOF() { return !BytesAvailable(); } 1.173 1.174 1.175 // Seeking 1.176 // Returns new position, -1 for error 1.177 virtual int Seek(int offset, int origin=Seek_Set) = 0; 1.178 virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set) = 0; 1.179 // Seek simplification 1.180 int SeekToBegin() {return Seek(0); } 1.181 int SeekToEnd() {return Seek(0,Seek_End); } 1.182 int Skip(int numBytes) {return Seek(numBytes,Seek_Cur); } 1.183 1.184 1.185 // Appends other file data from a stream 1.186 // Return -1 for error, else # of bytes written 1.187 virtual int CopyFromStream(File *pstream, int byteSize) = 0; 1.188 1.189 // Closes the file 1.190 // After close, file cannot be accessed 1.191 virtual bool Close() = 0; 1.192 1.193 1.194 // ***** Inlines for convenient primitive type serialization 1.195 1.196 // Read/Write helpers 1.197 private: 1.198 UInt64 PRead64() { UInt64 v = 0; Read((UByte*)&v, 8); return v; } 1.199 UInt32 PRead32() { UInt32 v = 0; Read((UByte*)&v, 4); return v; } 1.200 UInt16 PRead16() { UInt16 v = 0; Read((UByte*)&v, 2); return v; } 1.201 UByte PRead8() { UByte v = 0; Read((UByte*)&v, 1); return v; } 1.202 void PWrite64(UInt64 v) { Write((UByte*)&v, 8); } 1.203 void PWrite32(UInt32 v) { Write((UByte*)&v, 4); } 1.204 void PWrite16(UInt16 v) { Write((UByte*)&v, 2); } 1.205 void PWrite8(UByte v) { Write((UByte*)&v, 1); } 1.206 1.207 public: 1.208 1.209 // Writing primitive types - Little Endian 1.210 inline void WriteUByte(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); } 1.211 inline void WriteSByte(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); } 1.212 inline void WriteUInt8(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); } 1.213 inline void WriteSInt8(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); } 1.214 inline void WriteUInt16(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToLE(v)); } 1.215 inline void WriteSInt16(SInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToLE(v)); } 1.216 inline void WriteUInt32(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToLE(v)); } 1.217 inline void WriteSInt32(SInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToLE(v)); } 1.218 inline void WriteUInt64(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToLE(v)); } 1.219 inline void WriteSInt64(SInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToLE(v)); } 1.220 inline void WriteFloat(float v) { v = Alg::ByteUtil::SystemToLE(v); Write((UByte*)&v, 4); } 1.221 inline void WriteDouble(double v) { v = Alg::ByteUtil::SystemToLE(v); Write((UByte*)&v, 8); } 1.222 // Writing primitive types - Big Endian 1.223 inline void WriteUByteBE(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); } 1.224 inline void WriteSByteBE(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); } 1.225 inline void WriteUInt8BE(UInt16 v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); } 1.226 inline void WriteSInt8BE(SInt16 v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); } 1.227 inline void WriteUInt16BE(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToBE(v)); } 1.228 inline void WriteSInt16BE(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToBE(v)); } 1.229 inline void WriteUInt32BE(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToBE(v)); } 1.230 inline void WriteSInt32BE(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToBE(v)); } 1.231 inline void WriteUInt64BE(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToBE(v)); } 1.232 inline void WriteSInt64BE(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToBE(v)); } 1.233 inline void WriteFloatBE(float v) { v = Alg::ByteUtil::SystemToBE(v); Write((UByte*)&v, 4); } 1.234 inline void WriteDoubleBE(double v) { v = Alg::ByteUtil::SystemToBE(v); Write((UByte*)&v, 8); } 1.235 1.236 // Reading primitive types - Little Endian 1.237 inline UByte ReadUByte() { return (UByte)Alg::ByteUtil::LEToSystem(PRead8()); } 1.238 inline SByte ReadSByte() { return (SByte)Alg::ByteUtil::LEToSystem(PRead8()); } 1.239 inline UByte ReadUInt8() { return (UByte)Alg::ByteUtil::LEToSystem(PRead8()); } 1.240 inline SByte ReadSInt8() { return (SByte)Alg::ByteUtil::LEToSystem(PRead8()); } 1.241 inline UInt16 ReadUInt16() { return (UInt16)Alg::ByteUtil::LEToSystem(PRead16()); } 1.242 inline SInt16 ReadSInt16() { return (SInt16)Alg::ByteUtil::LEToSystem(PRead16()); } 1.243 inline UInt32 ReadUInt32() { return (UInt32)Alg::ByteUtil::LEToSystem(PRead32()); } 1.244 inline SInt32 ReadSInt32() { return (SInt32)Alg::ByteUtil::LEToSystem(PRead32()); } 1.245 inline UInt64 ReadUInt64() { return (UInt64)Alg::ByteUtil::LEToSystem(PRead64()); } 1.246 inline SInt64 ReadSInt64() { return (SInt64)Alg::ByteUtil::LEToSystem(PRead64()); } 1.247 inline float ReadFloat() { float v = 0.0f; Read((UByte*)&v, 4); return Alg::ByteUtil::LEToSystem(v); } 1.248 inline double ReadDouble() { double v = 0.0; Read((UByte*)&v, 8); return Alg::ByteUtil::LEToSystem(v); } 1.249 // Reading primitive types - Big Endian 1.250 inline UByte ReadUByteBE() { return (UByte)Alg::ByteUtil::BEToSystem(PRead8()); } 1.251 inline SByte ReadSByteBE() { return (SByte)Alg::ByteUtil::BEToSystem(PRead8()); } 1.252 inline UByte ReadUInt8BE() { return (UByte)Alg::ByteUtil::BEToSystem(PRead8()); } 1.253 inline SByte ReadSInt8BE() { return (SByte)Alg::ByteUtil::BEToSystem(PRead8()); } 1.254 inline UInt16 ReadUInt16BE() { return (UInt16)Alg::ByteUtil::BEToSystem(PRead16()); } 1.255 inline SInt16 ReadSInt16BE() { return (SInt16)Alg::ByteUtil::BEToSystem(PRead16()); } 1.256 inline UInt32 ReadUInt32BE() { return (UInt32)Alg::ByteUtil::BEToSystem(PRead32()); } 1.257 inline SInt32 ReadSInt32BE() { return (SInt32)Alg::ByteUtil::BEToSystem(PRead32()); } 1.258 inline UInt64 ReadUInt64BE() { return (UInt64)Alg::ByteUtil::BEToSystem(PRead64()); } 1.259 inline SInt64 ReadSInt64BE() { return (SInt64)Alg::ByteUtil::BEToSystem(PRead64()); } 1.260 inline float ReadFloatBE() { float v = 0.0f; Read((UByte*)&v, 4); return Alg::ByteUtil::BEToSystem(v); } 1.261 inline double ReadDoubleBE() { double v = 0.0; Read((UByte*)&v, 8); return Alg::ByteUtil::BEToSystem(v); } 1.262 }; 1.263 1.264 1.265 // *** Delegated File 1.266 1.267 class DelegatedFile : public File 1.268 { 1.269 protected: 1.270 // Delegating file pointer 1.271 Ptr<File> pFile; 1.272 1.273 // Hidden default constructor 1.274 DelegatedFile() : pFile(0) { } 1.275 DelegatedFile(const DelegatedFile &source) : File() { OVR_UNUSED(source); } 1.276 public: 1.277 // Constructors 1.278 DelegatedFile(File *pfile) : pFile(pfile) { } 1.279 1.280 // ** Location Information 1.281 virtual const char* GetFilePath() { return pFile->GetFilePath(); } 1.282 1.283 // ** File Information 1.284 virtual bool IsValid() { return pFile && pFile->IsValid(); } 1.285 virtual bool IsWritable() { return pFile->IsWritable(); } 1.286 // virtual bool IsRecoverable() { return pFile->IsRecoverable(); } 1.287 1.288 virtual int Tell() { return pFile->Tell(); } 1.289 virtual SInt64 LTell() { return pFile->LTell(); } 1.290 1.291 virtual int GetLength() { return pFile->GetLength(); } 1.292 virtual SInt64 LGetLength() { return pFile->LGetLength(); } 1.293 1.294 //virtual bool Stat(FileStats *pfs) { return pFile->Stat(pfs); } 1.295 1.296 virtual int GetErrorCode() { return pFile->GetErrorCode(); } 1.297 1.298 // ** Stream implementation & I/O 1.299 virtual int Write(const UByte *pbuffer, int numBytes) { return pFile->Write(pbuffer,numBytes); } 1.300 virtual int Read(UByte *pbuffer, int numBytes) { return pFile->Read(pbuffer,numBytes); } 1.301 1.302 virtual int SkipBytes(int numBytes) { return pFile->SkipBytes(numBytes); } 1.303 1.304 virtual int BytesAvailable() { return pFile->BytesAvailable(); } 1.305 1.306 virtual bool Flush() { return pFile->Flush(); } 1.307 1.308 // Seeking 1.309 virtual int Seek(int offset, int origin=Seek_Set) { return pFile->Seek(offset,origin); } 1.310 virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set) { return pFile->LSeek(offset,origin); } 1.311 1.312 virtual int CopyFromStream(File *pstream, int byteSize) { return pFile->CopyFromStream(pstream,byteSize); } 1.313 1.314 // Closing the file 1.315 virtual bool Close() { return pFile->Close(); } 1.316 }; 1.317 1.318 1.319 //----------------------------------------------------------------------------------- 1.320 // ***** Buffered File 1.321 1.322 // This file class adds buffering to an existing file 1.323 // Buffered file never fails by itself; if there's not 1.324 // enough memory for buffer, no buffer's used 1.325 1.326 class BufferedFile : public DelegatedFile 1.327 { 1.328 protected: 1.329 enum BufferModeType 1.330 { 1.331 NoBuffer, 1.332 ReadBuffer, 1.333 WriteBuffer 1.334 }; 1.335 1.336 // Buffer & the mode it's in 1.337 UByte* pBuffer; 1.338 BufferModeType BufferMode; 1.339 // Position in buffer 1.340 unsigned Pos; 1.341 // Data in buffer if reading 1.342 unsigned DataSize; 1.343 // Underlying file position 1.344 UInt64 FilePos; 1.345 1.346 // Initializes buffering to a certain mode 1.347 bool SetBufferMode(BufferModeType mode); 1.348 // Flushes buffer 1.349 // WriteBuffer - write data to disk, ReadBuffer - reset buffer & fix file position 1.350 void FlushBuffer(); 1.351 // Loads data into ReadBuffer 1.352 // WARNING: Right now LoadBuffer() assumes the buffer's empty 1.353 void LoadBuffer(); 1.354 1.355 // Hidden constructor 1.356 BufferedFile(); 1.357 inline BufferedFile(const BufferedFile &source) : DelegatedFile() { OVR_UNUSED(source); } 1.358 public: 1.359 1.360 // Constructor 1.361 // - takes another file as source 1.362 BufferedFile(File *pfile); 1.363 ~BufferedFile(); 1.364 1.365 1.366 // ** Overridden functions 1.367 1.368 // We override all the functions that can possibly 1.369 // require buffer mode switch, flush, or extra calculations 1.370 virtual int Tell(); 1.371 virtual SInt64 LTell(); 1.372 1.373 virtual int GetLength(); 1.374 virtual SInt64 LGetLength(); 1.375 1.376 // virtual bool Stat(GFileStats *pfs); 1.377 1.378 virtual int Write(const UByte *pbufer, int numBytes); 1.379 virtual int Read(UByte *pbufer, int numBytes); 1.380 1.381 virtual int SkipBytes(int numBytes); 1.382 1.383 virtual int BytesAvailable(); 1.384 1.385 virtual bool Flush(); 1.386 1.387 virtual int Seek(int offset, int origin=Seek_Set); 1.388 virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set); 1.389 1.390 virtual int CopyFromStream(File *pstream, int byteSize); 1.391 1.392 virtual bool Close(); 1.393 }; 1.394 1.395 1.396 //----------------------------------------------------------------------------------- 1.397 // ***** Memory File 1.398 1.399 class MemoryFile : public File 1.400 { 1.401 public: 1.402 1.403 const char* GetFilePath() { return FilePath.ToCStr(); } 1.404 1.405 bool IsValid() { return Valid; } 1.406 bool IsWritable() { return false; } 1.407 1.408 bool Flush() { return true; } 1.409 int GetErrorCode() { return 0; } 1.410 1.411 int Tell() { return FileIndex; } 1.412 SInt64 LTell() { return (SInt64) FileIndex; } 1.413 1.414 int GetLength() { return FileSize; } 1.415 SInt64 LGetLength() { return (SInt64) FileSize; } 1.416 1.417 bool Close() 1.418 { 1.419 Valid = false; 1.420 return false; 1.421 } 1.422 1.423 int CopyFromStream(File *pstream, int byteSize) 1.424 { OVR_UNUSED2(pstream, byteSize); 1.425 return 0; 1.426 } 1.427 1.428 int Write(const UByte *pbuffer, int numBytes) 1.429 { OVR_UNUSED2(pbuffer, numBytes); 1.430 return 0; 1.431 } 1.432 1.433 int Read(UByte *pbufer, int numBytes) 1.434 { 1.435 if (FileIndex + numBytes > FileSize) 1.436 { 1.437 numBytes = FileSize - FileIndex; 1.438 } 1.439 1.440 if (numBytes > 0) 1.441 { 1.442 ::memcpy (pbufer, &FileData [FileIndex], numBytes); 1.443 1.444 FileIndex += numBytes; 1.445 } 1.446 1.447 return numBytes; 1.448 } 1.449 1.450 int SkipBytes(int numBytes) 1.451 { 1.452 if (FileIndex + numBytes > FileSize) 1.453 { 1.454 numBytes = FileSize - FileIndex; 1.455 } 1.456 1.457 FileIndex += numBytes; 1.458 1.459 return numBytes; 1.460 } 1.461 1.462 int BytesAvailable() 1.463 { 1.464 return (FileSize - FileIndex); 1.465 } 1.466 1.467 int Seek(int offset, int origin = Seek_Set) 1.468 { 1.469 switch (origin) 1.470 { 1.471 case Seek_Set : FileIndex = offset; break; 1.472 case Seek_Cur : FileIndex += offset; break; 1.473 case Seek_End : FileIndex = FileSize - offset; break; 1.474 } 1.475 1.476 return FileIndex; 1.477 } 1.478 1.479 SInt64 LSeek(SInt64 offset, int origin = Seek_Set) 1.480 { 1.481 return (SInt64) Seek((int) offset, origin); 1.482 } 1.483 1.484 public: 1.485 1.486 MemoryFile (const String& fileName, const UByte *pBuffer, int buffSize) 1.487 : FilePath(fileName) 1.488 { 1.489 FileData = pBuffer; 1.490 FileSize = buffSize; 1.491 FileIndex = 0; 1.492 Valid = (!fileName.IsEmpty() && pBuffer && buffSize > 0) ? true : false; 1.493 } 1.494 1.495 // pfileName should be encoded as UTF-8 to support international file names. 1.496 MemoryFile (const char* pfileName, const UByte *pBuffer, int buffSize) 1.497 : FilePath(pfileName) 1.498 { 1.499 FileData = pBuffer; 1.500 FileSize = buffSize; 1.501 FileIndex = 0; 1.502 Valid = (pfileName && pBuffer && buffSize > 0) ? true : false; 1.503 } 1.504 private: 1.505 1.506 String FilePath; 1.507 const UByte *FileData; 1.508 int FileSize; 1.509 int FileIndex; 1.510 bool Valid; 1.511 }; 1.512 1.513 1.514 // ***** Global path helpers 1.515 1.516 // Find trailing short filename in a path. 1.517 const char* OVR_CDECL GetShortFilename(const char* purl); 1.518 1.519 } // OVR 1.520 1.521 #endif 1.522 \ No newline at end of file 1.523 +/************************************************************************************ 1.524 + 1.525 +PublicHeader: Kernel 1.526 +Filename : OVR_File.h 1.527 +Content : Header for all internal file management - functions and structures 1.528 + to be inherited by OS specific subclasses. 1.529 +Created : September 19, 2012 1.530 +Notes : 1.531 + 1.532 +Notes : errno may not be preserved across use of BaseFile member functions 1.533 + : Directories cannot be deleted while files opened from them are in use 1.534 + (For the GetFullName function) 1.535 + 1.536 +Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. 1.537 + 1.538 +Use of this software is subject to the terms of the Oculus license 1.539 +agreement provided at the time of installation or download, or which 1.540 +otherwise accompanies this software in either electronic or hard copy form. 1.541 + 1.542 +************************************************************************************/ 1.543 + 1.544 +#ifndef OVR_File_h 1.545 +#define OVR_File_h 1.546 + 1.547 +#include "OVR_RefCount.h" 1.548 +#include "OVR_Std.h" 1.549 +#include "OVR_Alg.h" 1.550 + 1.551 +#include <stdio.h> 1.552 +#include "OVR_String.h" 1.553 + 1.554 +namespace OVR { 1.555 + 1.556 +// ***** Declared classes 1.557 +class FileConstants; 1.558 +class File; 1.559 +class DelegatedFile; 1.560 +class BufferedFile; 1.561 + 1.562 + 1.563 +// ***** Flags for File & Directory accesses 1.564 + 1.565 +class FileConstants 1.566 +{ 1.567 +public: 1.568 + 1.569 + // *** File open flags 1.570 + enum OpenFlags 1.571 + { 1.572 + Open_Read = 1, 1.573 + Open_Write = 2, 1.574 + Open_ReadWrite = 3, 1.575 + 1.576 + // Opens file and truncates it to zero length 1.577 + // - file must have write permission 1.578 + // - when used with Create, it opens an existing 1.579 + // file and empties it or creates a new file 1.580 + Open_Truncate = 4, 1.581 + 1.582 + // Creates and opens new file 1.583 + // - does not erase contents if file already 1.584 + // exists unless combined with Truncate 1.585 + Open_Create = 8, 1.586 + 1.587 + // Returns an error value if the file already exists 1.588 + Open_CreateOnly = 24, 1.589 + 1.590 + // Open file with buffering 1.591 + Open_Buffered = 32 1.592 + }; 1.593 + 1.594 + // *** File Mode flags 1.595 + enum Modes 1.596 + { 1.597 + Mode_Read = 0444, 1.598 + Mode_Write = 0222, 1.599 + Mode_Execute = 0111, 1.600 + 1.601 + Mode_ReadWrite = 0666 1.602 + }; 1.603 + 1.604 + // *** Seek operations 1.605 + enum SeekOps 1.606 + { 1.607 + Seek_Set = 0, 1.608 + Seek_Cur = 1, 1.609 + Seek_End = 2 1.610 + }; 1.611 + 1.612 + // *** Errors 1.613 + enum Errors 1.614 + { 1.615 + Error_FileNotFound = 0x1001, 1.616 + Error_Access = 0x1002, 1.617 + Error_IOError = 0x1003, 1.618 + Error_DiskFull = 0x1004 1.619 + }; 1.620 +}; 1.621 + 1.622 + 1.623 +//----------------------------------------------------------------------------------- 1.624 +// ***** File Class 1.625 + 1.626 +// The pure virtual base random-access file 1.627 +// This is a base class to all files 1.628 + 1.629 +class File : public RefCountBase<File>, public FileConstants 1.630 +{ 1.631 +public: 1.632 + File() { } 1.633 + // ** Location Information 1.634 + 1.635 + // Returns a file name path relative to the 'reference' directory 1.636 + // This is often a path that was used to create a file 1.637 + // (this is not a global path, global path can be obtained with help of directory) 1.638 + virtual const char* GetFilePath() = 0; 1.639 + 1.640 + 1.641 + // ** File Information 1.642 + 1.643 + // Return 1 if file's usable (open) 1.644 + virtual bool IsValid() = 0; 1.645 + // Return 1 if file's writable, otherwise 0 1.646 + virtual bool IsWritable() = 0; 1.647 + 1.648 + // Return position 1.649 + virtual int Tell() = 0; 1.650 + virtual SInt64 LTell() = 0; 1.651 + 1.652 + // File size 1.653 + virtual int GetLength() = 0; 1.654 + virtual SInt64 LGetLength() = 0; 1.655 + 1.656 + // Returns file stats 1.657 + // 0 for failure 1.658 + //virtual bool Stat(FileStats *pfs) = 0; 1.659 + 1.660 + // Return errno-based error code 1.661 + // Useful if any other function failed 1.662 + virtual int GetErrorCode() = 0; 1.663 + 1.664 + 1.665 + // ** Stream implementation & I/O 1.666 + 1.667 + // Blocking write, will write in the given number of bytes to the stream 1.668 + // Returns : -1 for error 1.669 + // Otherwise number of bytes read 1.670 + virtual int Write(const UByte *pbufer, int numBytes) = 0; 1.671 + // Blocking read, will read in the given number of bytes or less from the stream 1.672 + // Returns : -1 for error 1.673 + // Otherwise number of bytes read, 1.674 + // if 0 or < numBytes, no more bytes available; end of file or the other side of stream is closed 1.675 + virtual int Read(UByte *pbufer, int numBytes) = 0; 1.676 + 1.677 + // Skips (ignores) a given # of bytes 1.678 + // Same return values as Read 1.679 + virtual int SkipBytes(int numBytes) = 0; 1.680 + 1.681 + // Returns the number of bytes available to read from a stream without blocking 1.682 + // For a file, this should generally be number of bytes to the end 1.683 + virtual int BytesAvailable() = 0; 1.684 + 1.685 + // Causes any implementation's buffered data to be delivered to destination 1.686 + // Return 0 for error 1.687 + virtual bool Flush() = 0; 1.688 + 1.689 + 1.690 + // Need to provide a more optimized implementation that doe snot necessarily involve a lot of seeking 1.691 + inline bool IsEOF() { return !BytesAvailable(); } 1.692 + 1.693 + 1.694 + // Seeking 1.695 + // Returns new position, -1 for error 1.696 + virtual int Seek(int offset, int origin=Seek_Set) = 0; 1.697 + virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set) = 0; 1.698 + // Seek simplification 1.699 + int SeekToBegin() {return Seek(0); } 1.700 + int SeekToEnd() {return Seek(0,Seek_End); } 1.701 + int Skip(int numBytes) {return Seek(numBytes,Seek_Cur); } 1.702 + 1.703 + 1.704 + // Appends other file data from a stream 1.705 + // Return -1 for error, else # of bytes written 1.706 + virtual int CopyFromStream(File *pstream, int byteSize) = 0; 1.707 + 1.708 + // Closes the file 1.709 + // After close, file cannot be accessed 1.710 + virtual bool Close() = 0; 1.711 + 1.712 + 1.713 + // ***** Inlines for convenient primitive type serialization 1.714 + 1.715 + // Read/Write helpers 1.716 +private: 1.717 + UInt64 PRead64() { UInt64 v = 0; Read((UByte*)&v, 8); return v; } 1.718 + UInt32 PRead32() { UInt32 v = 0; Read((UByte*)&v, 4); return v; } 1.719 + UInt16 PRead16() { UInt16 v = 0; Read((UByte*)&v, 2); return v; } 1.720 + UByte PRead8() { UByte v = 0; Read((UByte*)&v, 1); return v; } 1.721 + void PWrite64(UInt64 v) { Write((UByte*)&v, 8); } 1.722 + void PWrite32(UInt32 v) { Write((UByte*)&v, 4); } 1.723 + void PWrite16(UInt16 v) { Write((UByte*)&v, 2); } 1.724 + void PWrite8(UByte v) { Write((UByte*)&v, 1); } 1.725 + 1.726 +public: 1.727 + 1.728 + // Writing primitive types - Little Endian 1.729 + inline void WriteUByte(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); } 1.730 + inline void WriteSByte(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); } 1.731 + inline void WriteUInt8(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); } 1.732 + inline void WriteSInt8(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); } 1.733 + inline void WriteUInt16(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToLE(v)); } 1.734 + inline void WriteSInt16(SInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToLE(v)); } 1.735 + inline void WriteUInt32(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToLE(v)); } 1.736 + inline void WriteSInt32(SInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToLE(v)); } 1.737 + inline void WriteUInt64(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToLE(v)); } 1.738 + inline void WriteSInt64(SInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToLE(v)); } 1.739 + inline void WriteFloat(float v) { v = Alg::ByteUtil::SystemToLE(v); Write((UByte*)&v, 4); } 1.740 + inline void WriteDouble(double v) { v = Alg::ByteUtil::SystemToLE(v); Write((UByte*)&v, 8); } 1.741 + // Writing primitive types - Big Endian 1.742 + inline void WriteUByteBE(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); } 1.743 + inline void WriteSByteBE(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); } 1.744 + inline void WriteUInt8BE(UInt16 v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); } 1.745 + inline void WriteSInt8BE(SInt16 v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); } 1.746 + inline void WriteUInt16BE(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToBE(v)); } 1.747 + inline void WriteSInt16BE(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToBE(v)); } 1.748 + inline void WriteUInt32BE(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToBE(v)); } 1.749 + inline void WriteSInt32BE(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToBE(v)); } 1.750 + inline void WriteUInt64BE(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToBE(v)); } 1.751 + inline void WriteSInt64BE(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToBE(v)); } 1.752 + inline void WriteFloatBE(float v) { v = Alg::ByteUtil::SystemToBE(v); Write((UByte*)&v, 4); } 1.753 + inline void WriteDoubleBE(double v) { v = Alg::ByteUtil::SystemToBE(v); Write((UByte*)&v, 8); } 1.754 + 1.755 + // Reading primitive types - Little Endian 1.756 + inline UByte ReadUByte() { return (UByte)Alg::ByteUtil::LEToSystem(PRead8()); } 1.757 + inline SByte ReadSByte() { return (SByte)Alg::ByteUtil::LEToSystem(PRead8()); } 1.758 + inline UByte ReadUInt8() { return (UByte)Alg::ByteUtil::LEToSystem(PRead8()); } 1.759 + inline SByte ReadSInt8() { return (SByte)Alg::ByteUtil::LEToSystem(PRead8()); } 1.760 + inline UInt16 ReadUInt16() { return (UInt16)Alg::ByteUtil::LEToSystem(PRead16()); } 1.761 + inline SInt16 ReadSInt16() { return (SInt16)Alg::ByteUtil::LEToSystem(PRead16()); } 1.762 + inline UInt32 ReadUInt32() { return (UInt32)Alg::ByteUtil::LEToSystem(PRead32()); } 1.763 + inline SInt32 ReadSInt32() { return (SInt32)Alg::ByteUtil::LEToSystem(PRead32()); } 1.764 + inline UInt64 ReadUInt64() { return (UInt64)Alg::ByteUtil::LEToSystem(PRead64()); } 1.765 + inline SInt64 ReadSInt64() { return (SInt64)Alg::ByteUtil::LEToSystem(PRead64()); } 1.766 + inline float ReadFloat() { float v = 0.0f; Read((UByte*)&v, 4); return Alg::ByteUtil::LEToSystem(v); } 1.767 + inline double ReadDouble() { double v = 0.0; Read((UByte*)&v, 8); return Alg::ByteUtil::LEToSystem(v); } 1.768 + // Reading primitive types - Big Endian 1.769 + inline UByte ReadUByteBE() { return (UByte)Alg::ByteUtil::BEToSystem(PRead8()); } 1.770 + inline SByte ReadSByteBE() { return (SByte)Alg::ByteUtil::BEToSystem(PRead8()); } 1.771 + inline UByte ReadUInt8BE() { return (UByte)Alg::ByteUtil::BEToSystem(PRead8()); } 1.772 + inline SByte ReadSInt8BE() { return (SByte)Alg::ByteUtil::BEToSystem(PRead8()); } 1.773 + inline UInt16 ReadUInt16BE() { return (UInt16)Alg::ByteUtil::BEToSystem(PRead16()); } 1.774 + inline SInt16 ReadSInt16BE() { return (SInt16)Alg::ByteUtil::BEToSystem(PRead16()); } 1.775 + inline UInt32 ReadUInt32BE() { return (UInt32)Alg::ByteUtil::BEToSystem(PRead32()); } 1.776 + inline SInt32 ReadSInt32BE() { return (SInt32)Alg::ByteUtil::BEToSystem(PRead32()); } 1.777 + inline UInt64 ReadUInt64BE() { return (UInt64)Alg::ByteUtil::BEToSystem(PRead64()); } 1.778 + inline SInt64 ReadSInt64BE() { return (SInt64)Alg::ByteUtil::BEToSystem(PRead64()); } 1.779 + inline float ReadFloatBE() { float v = 0.0f; Read((UByte*)&v, 4); return Alg::ByteUtil::BEToSystem(v); } 1.780 + inline double ReadDoubleBE() { double v = 0.0; Read((UByte*)&v, 8); return Alg::ByteUtil::BEToSystem(v); } 1.781 +}; 1.782 + 1.783 + 1.784 +// *** Delegated File 1.785 + 1.786 +class DelegatedFile : public File 1.787 +{ 1.788 +protected: 1.789 + // Delegating file pointer 1.790 + Ptr<File> pFile; 1.791 + 1.792 + // Hidden default constructor 1.793 + DelegatedFile() : pFile(0) { } 1.794 + DelegatedFile(const DelegatedFile &source) : File() { OVR_UNUSED(source); } 1.795 +public: 1.796 + // Constructors 1.797 + DelegatedFile(File *pfile) : pFile(pfile) { } 1.798 + 1.799 + // ** Location Information 1.800 + virtual const char* GetFilePath() { return pFile->GetFilePath(); } 1.801 + 1.802 + // ** File Information 1.803 + virtual bool IsValid() { return pFile && pFile->IsValid(); } 1.804 + virtual bool IsWritable() { return pFile->IsWritable(); } 1.805 +// virtual bool IsRecoverable() { return pFile->IsRecoverable(); } 1.806 + 1.807 + virtual int Tell() { return pFile->Tell(); } 1.808 + virtual SInt64 LTell() { return pFile->LTell(); } 1.809 + 1.810 + virtual int GetLength() { return pFile->GetLength(); } 1.811 + virtual SInt64 LGetLength() { return pFile->LGetLength(); } 1.812 + 1.813 + //virtual bool Stat(FileStats *pfs) { return pFile->Stat(pfs); } 1.814 + 1.815 + virtual int GetErrorCode() { return pFile->GetErrorCode(); } 1.816 + 1.817 + // ** Stream implementation & I/O 1.818 + virtual int Write(const UByte *pbuffer, int numBytes) { return pFile->Write(pbuffer,numBytes); } 1.819 + virtual int Read(UByte *pbuffer, int numBytes) { return pFile->Read(pbuffer,numBytes); } 1.820 + 1.821 + virtual int SkipBytes(int numBytes) { return pFile->SkipBytes(numBytes); } 1.822 + 1.823 + virtual int BytesAvailable() { return pFile->BytesAvailable(); } 1.824 + 1.825 + virtual bool Flush() { return pFile->Flush(); } 1.826 + 1.827 + // Seeking 1.828 + virtual int Seek(int offset, int origin=Seek_Set) { return pFile->Seek(offset,origin); } 1.829 + virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set) { return pFile->LSeek(offset,origin); } 1.830 + 1.831 + virtual int CopyFromStream(File *pstream, int byteSize) { return pFile->CopyFromStream(pstream,byteSize); } 1.832 + 1.833 + // Closing the file 1.834 + virtual bool Close() { return pFile->Close(); } 1.835 +}; 1.836 + 1.837 + 1.838 +//----------------------------------------------------------------------------------- 1.839 +// ***** Buffered File 1.840 + 1.841 +// This file class adds buffering to an existing file 1.842 +// Buffered file never fails by itself; if there's not 1.843 +// enough memory for buffer, no buffer's used 1.844 + 1.845 +class BufferedFile : public DelegatedFile 1.846 +{ 1.847 +protected: 1.848 + enum BufferModeType 1.849 + { 1.850 + NoBuffer, 1.851 + ReadBuffer, 1.852 + WriteBuffer 1.853 + }; 1.854 + 1.855 + // Buffer & the mode it's in 1.856 + UByte* pBuffer; 1.857 + BufferModeType BufferMode; 1.858 + // Position in buffer 1.859 + unsigned Pos; 1.860 + // Data in buffer if reading 1.861 + unsigned DataSize; 1.862 + // Underlying file position 1.863 + UInt64 FilePos; 1.864 + 1.865 + // Initializes buffering to a certain mode 1.866 + bool SetBufferMode(BufferModeType mode); 1.867 + // Flushes buffer 1.868 + // WriteBuffer - write data to disk, ReadBuffer - reset buffer & fix file position 1.869 + void FlushBuffer(); 1.870 + // Loads data into ReadBuffer 1.871 + // WARNING: Right now LoadBuffer() assumes the buffer's empty 1.872 + void LoadBuffer(); 1.873 + 1.874 + // Hidden constructor 1.875 + BufferedFile(); 1.876 + inline BufferedFile(const BufferedFile &source) : DelegatedFile() { OVR_UNUSED(source); } 1.877 +public: 1.878 + 1.879 + // Constructor 1.880 + // - takes another file as source 1.881 + BufferedFile(File *pfile); 1.882 + ~BufferedFile(); 1.883 + 1.884 + 1.885 + // ** Overridden functions 1.886 + 1.887 + // We override all the functions that can possibly 1.888 + // require buffer mode switch, flush, or extra calculations 1.889 + virtual int Tell(); 1.890 + virtual SInt64 LTell(); 1.891 + 1.892 + virtual int GetLength(); 1.893 + virtual SInt64 LGetLength(); 1.894 + 1.895 +// virtual bool Stat(GFileStats *pfs); 1.896 + 1.897 + virtual int Write(const UByte *pbufer, int numBytes); 1.898 + virtual int Read(UByte *pbufer, int numBytes); 1.899 + 1.900 + virtual int SkipBytes(int numBytes); 1.901 + 1.902 + virtual int BytesAvailable(); 1.903 + 1.904 + virtual bool Flush(); 1.905 + 1.906 + virtual int Seek(int offset, int origin=Seek_Set); 1.907 + virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set); 1.908 + 1.909 + virtual int CopyFromStream(File *pstream, int byteSize); 1.910 + 1.911 + virtual bool Close(); 1.912 +}; 1.913 + 1.914 + 1.915 +//----------------------------------------------------------------------------------- 1.916 +// ***** Memory File 1.917 + 1.918 +class MemoryFile : public File 1.919 +{ 1.920 +public: 1.921 + 1.922 + const char* GetFilePath() { return FilePath.ToCStr(); } 1.923 + 1.924 + bool IsValid() { return Valid; } 1.925 + bool IsWritable() { return false; } 1.926 + 1.927 + bool Flush() { return true; } 1.928 + int GetErrorCode() { return 0; } 1.929 + 1.930 + int Tell() { return FileIndex; } 1.931 + SInt64 LTell() { return (SInt64) FileIndex; } 1.932 + 1.933 + int GetLength() { return FileSize; } 1.934 + SInt64 LGetLength() { return (SInt64) FileSize; } 1.935 + 1.936 + bool Close() 1.937 + { 1.938 + Valid = false; 1.939 + return false; 1.940 + } 1.941 + 1.942 + int CopyFromStream(File *pstream, int byteSize) 1.943 + { OVR_UNUSED2(pstream, byteSize); 1.944 + return 0; 1.945 + } 1.946 + 1.947 + int Write(const UByte *pbuffer, int numBytes) 1.948 + { OVR_UNUSED2(pbuffer, numBytes); 1.949 + return 0; 1.950 + } 1.951 + 1.952 + int Read(UByte *pbufer, int numBytes) 1.953 + { 1.954 + if (FileIndex + numBytes > FileSize) 1.955 + { 1.956 + numBytes = FileSize - FileIndex; 1.957 + } 1.958 + 1.959 + if (numBytes > 0) 1.960 + { 1.961 + ::memcpy (pbufer, &FileData [FileIndex], numBytes); 1.962 + 1.963 + FileIndex += numBytes; 1.964 + } 1.965 + 1.966 + return numBytes; 1.967 + } 1.968 + 1.969 + int SkipBytes(int numBytes) 1.970 + { 1.971 + if (FileIndex + numBytes > FileSize) 1.972 + { 1.973 + numBytes = FileSize - FileIndex; 1.974 + } 1.975 + 1.976 + FileIndex += numBytes; 1.977 + 1.978 + return numBytes; 1.979 + } 1.980 + 1.981 + int BytesAvailable() 1.982 + { 1.983 + return (FileSize - FileIndex); 1.984 + } 1.985 + 1.986 + int Seek(int offset, int origin = Seek_Set) 1.987 + { 1.988 + switch (origin) 1.989 + { 1.990 + case Seek_Set : FileIndex = offset; break; 1.991 + case Seek_Cur : FileIndex += offset; break; 1.992 + case Seek_End : FileIndex = FileSize - offset; break; 1.993 + } 1.994 + 1.995 + return FileIndex; 1.996 + } 1.997 + 1.998 + SInt64 LSeek(SInt64 offset, int origin = Seek_Set) 1.999 + { 1.1000 + return (SInt64) Seek((int) offset, origin); 1.1001 + } 1.1002 + 1.1003 +public: 1.1004 + 1.1005 + MemoryFile (const String& fileName, const UByte *pBuffer, int buffSize) 1.1006 + : FilePath(fileName) 1.1007 + { 1.1008 + FileData = pBuffer; 1.1009 + FileSize = buffSize; 1.1010 + FileIndex = 0; 1.1011 + Valid = (!fileName.IsEmpty() && pBuffer && buffSize > 0) ? true : false; 1.1012 + } 1.1013 + 1.1014 + // pfileName should be encoded as UTF-8 to support international file names. 1.1015 + MemoryFile (const char* pfileName, const UByte *pBuffer, int buffSize) 1.1016 + : FilePath(pfileName) 1.1017 + { 1.1018 + FileData = pBuffer; 1.1019 + FileSize = buffSize; 1.1020 + FileIndex = 0; 1.1021 + Valid = (pfileName && pBuffer && buffSize > 0) ? true : false; 1.1022 + } 1.1023 +private: 1.1024 + 1.1025 + String FilePath; 1.1026 + const UByte *FileData; 1.1027 + int FileSize; 1.1028 + int FileIndex; 1.1029 + bool Valid; 1.1030 +}; 1.1031 + 1.1032 + 1.1033 +// ***** Global path helpers 1.1034 + 1.1035 +// Find trailing short filename in a path. 1.1036 +const char* OVR_CDECL GetShortFilename(const char* purl); 1.1037 + 1.1038 +} // OVR 1.1039 + 1.1040 +#endif