oculus1
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libovr/Src/Kernel/OVR_File.h Sat Sep 14 16:14:59 2013 +0300 1.3 @@ -0,0 +1,1 @@ 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