oculus1

diff libovr/Src/Kernel/OVR_FileFILE.cpp @ 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_FileFILE.cpp	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,1 @@
     1.4 +/**************************************************************************
     1.5 
     1.6 Filename    :   OVR_FileFILE.cpp
     1.7 Content     :   File wrapper class implementation (Win32)
     1.8 
     1.9 Created     :   April 5, 1999
    1.10 Authors     :   Michael Antonov
    1.11 
    1.12 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
    1.13 
    1.14 Use of this software is subject to the terms of the Oculus license
    1.15 agreement provided at the time of installation or download, or which
    1.16 otherwise accompanies this software in either electronic or hard copy form.
    1.17 
    1.18 **************************************************************************/
    1.19 
    1.20 #define  GFILE_CXX
    1.21 
    1.22 #include "OVR_Types.h"
    1.23 #include "OVR_Log.h"
    1.24 
    1.25 // Standard C library (Captain Obvious guarantees!)
    1.26 #include <stdio.h>
    1.27 #ifndef OVR_OS_WINCE
    1.28 #include <sys/stat.h>
    1.29 #endif
    1.30 
    1.31 #include "OVR_SysFile.h"
    1.32 
    1.33 #ifndef OVR_OS_WINCE
    1.34 #include <errno.h>
    1.35 #endif
    1.36 
    1.37 namespace OVR {
    1.38 
    1.39 // ***** File interface
    1.40 
    1.41 // ***** FILEFile - C streams file
    1.42 
    1.43 static int SFerror ()
    1.44 {
    1.45     if (errno == ENOENT)
    1.46         return FileConstants::Error_FileNotFound;
    1.47     else if (errno == EACCES || errno == EPERM)
    1.48         return FileConstants::Error_Access;
    1.49     else if (errno == ENOSPC)
    1.50         return FileConstants::Error_DiskFull;
    1.51     else
    1.52         return FileConstants::Error_IOError;
    1.53 };
    1.54 
    1.55 #ifdef OVR_OS_WIN32
    1.56 #include "windows.h"
    1.57 // A simple helper class to disable/enable system error mode, if necessary
    1.58 // Disabling happens conditionally only if a drive name is involved
    1.59 class SysErrorModeDisabler
    1.60 {
    1.61     BOOL    Disabled;
    1.62     UINT    OldMode;
    1.63 public:
    1.64     SysErrorModeDisabler(const char* pfileName)
    1.65     {
    1.66         if (pfileName && (pfileName[0]!=0) && pfileName[1]==':')
    1.67         {
    1.68             Disabled = 1;
    1.69             OldMode = ::SetErrorMode(SEM_FAILCRITICALERRORS);
    1.70         }
    1.71         else
    1.72             Disabled = 0;
    1.73     }
    1.74 
    1.75     ~SysErrorModeDisabler()
    1.76     {
    1.77         if (Disabled) ::SetErrorMode(OldMode);
    1.78     }
    1.79 };
    1.80 #else
    1.81 class SysErrorModeDisabler
    1.82 {
    1.83 public:
    1.84     SysErrorModeDisabler(const char* pfileName) { }
    1.85 };
    1.86 #endif // OVR_OS_WIN32
    1.87 
    1.88 
    1.89 // This macro enables verification of I/O results after seeks against a pre-loaded
    1.90 // full file buffer copy. This is generally not necessary, but can been used to debug
    1.91 // memory corruptions; we've seen this fail due to EAX2/DirectSound corrupting memory
    1.92 // under FMOD with XP64 (32-bit) and Realtek HA Audio driver.
    1.93 //#define GFILE_VERIFY_SEEK_ERRORS
    1.94 
    1.95 
    1.96 // This is the simplest possible file implementation, it wraps around the descriptor
    1.97 // This file is delegated to by SysFile.
    1.98 
    1.99 class FILEFile : public File
   1.100 {
   1.101 protected:
   1.102 
   1.103     // Allocated filename
   1.104     String      FileName;
   1.105 
   1.106     // File handle & open mode
   1.107     bool        Opened;
   1.108     FILE*       fs;
   1.109     int         OpenFlags;
   1.110     // Error code for last request
   1.111     int         ErrorCode;
   1.112 
   1.113     int         LastOp;
   1.114 
   1.115 #ifdef OVR_FILE_VERIFY_SEEK_ERRORS
   1.116     UByte*      pFileTestBuffer;
   1.117     unsigned    FileTestLength;
   1.118     unsigned    TestPos; // File pointer position during tests.
   1.119 #endif
   1.120 
   1.121 public:
   1.122 
   1.123     FILEFile()
   1.124     {
   1.125         Opened = 0; FileName = "";
   1.126 
   1.127 #ifdef OVR_FILE_VERIFY_SEEK_ERRORS
   1.128         pFileTestBuffer =0;
   1.129         FileTestLength  =0;
   1.130         TestPos         =0;
   1.131 #endif
   1.132     }
   1.133     // Initialize file by opening it
   1.134     FILEFile(const String& fileName, int flags, int Mode);
   1.135     // The 'pfileName' should be encoded as UTF-8 to support international file names.
   1.136     FILEFile(const char* pfileName, int flags, int Mode);
   1.137 
   1.138     ~FILEFile()
   1.139     {
   1.140         if (Opened)
   1.141             Close();
   1.142     }
   1.143 
   1.144     virtual const char* GetFilePath();
   1.145 
   1.146     // ** File Information
   1.147     virtual bool        IsValid();
   1.148     virtual bool        IsWritable();
   1.149 
   1.150     // Return position / file size
   1.151     virtual int         Tell();
   1.152     virtual SInt64      LTell();
   1.153     virtual int         GetLength();
   1.154     virtual SInt64      LGetLength();
   1.155 
   1.156 //  virtual bool        Stat(FileStats *pfs);
   1.157     virtual int         GetErrorCode();
   1.158 
   1.159     // ** Stream implementation & I/O
   1.160     virtual int         Write(const UByte *pbuffer, int numBytes);
   1.161     virtual int         Read(UByte *pbuffer, int numBytes);
   1.162     virtual int         SkipBytes(int numBytes);
   1.163     virtual int         BytesAvailable();
   1.164     virtual bool        Flush();
   1.165     virtual int         Seek(int offset, int origin);
   1.166     virtual SInt64      LSeek(SInt64 offset, int origin);
   1.167     
   1.168     virtual int         CopyFromStream(File *pStream, int byteSize);
   1.169     virtual bool        Close();    
   1.170 private:
   1.171     void                init();
   1.172 };
   1.173 
   1.174 
   1.175 // Initialize file by opening it
   1.176 FILEFile::FILEFile(const String& fileName, int flags, int mode)
   1.177   : FileName(fileName), OpenFlags(flags)
   1.178 {
   1.179     OVR_UNUSED(mode);
   1.180     init();
   1.181 }
   1.182 
   1.183 // The 'pfileName' should be encoded as UTF-8 to support international file names.
   1.184 FILEFile::FILEFile(const char* pfileName, int flags, int mode)
   1.185   : FileName(pfileName), OpenFlags(flags)
   1.186 {
   1.187     OVR_UNUSED(mode);
   1.188     init();
   1.189 }
   1.190 
   1.191 void FILEFile::init()
   1.192 {
   1.193     // Open mode for file's open
   1.194     const char *omode = "rb";
   1.195 
   1.196     if (OpenFlags & Open_Truncate)
   1.197     {
   1.198         if (OpenFlags & Open_Read)
   1.199             omode = "w+b";
   1.200         else
   1.201             omode = "wb";
   1.202     }
   1.203     else if (OpenFlags & Open_Create)
   1.204     {
   1.205         if (OpenFlags & Open_Read)
   1.206             omode = "a+b";
   1.207         else
   1.208             omode = "ab";
   1.209     }
   1.210     else if (OpenFlags & Open_Write)
   1.211         omode = "r+b";
   1.212 
   1.213 #ifdef OVR_OS_WIN32
   1.214     SysErrorModeDisabler disabler(FileName.ToCStr());
   1.215 #endif
   1.216 
   1.217 #if defined(OVR_CC_MSVC) && (OVR_CC_MSVC >= 1400)
   1.218     wchar_t womode[16];
   1.219     wchar_t *pwFileName = (wchar_t*)OVR_ALLOC((UTF8Util::GetLength(FileName.ToCStr())+1) * sizeof(wchar_t));
   1.220     UTF8Util::DecodeString(pwFileName, FileName.ToCStr());
   1.221     OVR_ASSERT(strlen(omode) < sizeof(womode)/sizeof(womode[0]));
   1.222     UTF8Util::DecodeString(womode, omode);
   1.223     _wfopen_s(&fs, pwFileName, womode);
   1.224     OVR_FREE(pwFileName);
   1.225 #else
   1.226     fs = fopen(FileName.ToCStr(), omode);
   1.227 #endif
   1.228     if (fs)
   1.229         rewind (fs);
   1.230     Opened = (fs != NULL);
   1.231     // Set error code
   1.232     if (!Opened)
   1.233         ErrorCode = SFerror();
   1.234     else
   1.235     {
   1.236         // If we are testing file seek correctness, pre-load the entire file so
   1.237         // that we can do comparison tests later.
   1.238 #ifdef OVR_FILE_VERIFY_SEEK_ERRORS        
   1.239         TestPos         = 0;
   1.240         fseek(fs, 0, SEEK_END);
   1.241         FileTestLength  = ftell(fs);
   1.242         fseek(fs, 0, SEEK_SET);
   1.243         pFileTestBuffer = (UByte*)OVR_ALLOC(FileTestLength);
   1.244         if (pFileTestBuffer)
   1.245         {
   1.246             OVR_ASSERT(FileTestLength == (unsigned)Read(pFileTestBuffer, FileTestLength));
   1.247             Seek(0, Seek_Set);
   1.248         }        
   1.249 #endif
   1.250 
   1.251         ErrorCode = 0;
   1.252     }
   1.253     LastOp = 0;
   1.254 }
   1.255 
   1.256 
   1.257 const char* FILEFile::GetFilePath()
   1.258 {
   1.259     return FileName.ToCStr();
   1.260 }
   1.261 
   1.262 
   1.263 // ** File Information
   1.264 bool    FILEFile::IsValid()
   1.265 {
   1.266     return Opened;
   1.267 }
   1.268 bool    FILEFile::IsWritable()
   1.269 {
   1.270     return IsValid() && (OpenFlags&Open_Write);
   1.271 }
   1.272 /*
   1.273 bool    FILEFile::IsRecoverable()
   1.274 {
   1.275     return IsValid() && ((OpenFlags&OVR_FO_SAFETRUNC) == OVR_FO_SAFETRUNC);
   1.276 }
   1.277 */
   1.278 
   1.279 // Return position / file size
   1.280 int     FILEFile::Tell()
   1.281 {
   1.282     int pos = (int)ftell (fs);
   1.283     if (pos < 0)
   1.284         ErrorCode = SFerror();
   1.285     return pos;
   1.286 }
   1.287 
   1.288 SInt64  FILEFile::LTell()
   1.289 {
   1.290     SInt64 pos = ftell(fs);
   1.291     if (pos < 0)
   1.292         ErrorCode = SFerror();
   1.293     return pos;
   1.294 }
   1.295 
   1.296 int     FILEFile::GetLength()
   1.297 {
   1.298     int pos = Tell();
   1.299     if (pos >= 0)
   1.300     {
   1.301         Seek (0, Seek_End);
   1.302         int size = Tell();
   1.303         Seek (pos, Seek_Set);
   1.304         return size;
   1.305     }
   1.306     return -1;
   1.307 }
   1.308 SInt64  FILEFile::LGetLength()
   1.309 {
   1.310     SInt64 pos = LTell();
   1.311     if (pos >= 0)
   1.312     {
   1.313         LSeek (0, Seek_End);
   1.314         SInt64 size = LTell();
   1.315         LSeek (pos, Seek_Set);
   1.316         return size;
   1.317     }
   1.318     return -1;
   1.319 }
   1.320 
   1.321 int     FILEFile::GetErrorCode()
   1.322 {
   1.323     return ErrorCode;
   1.324 }
   1.325 
   1.326 // ** Stream implementation & I/O
   1.327 int     FILEFile::Write(const UByte *pbuffer, int numBytes)
   1.328 {
   1.329     if (LastOp && LastOp != Open_Write)
   1.330         fflush(fs);
   1.331     LastOp = Open_Write;
   1.332     int written = (int) fwrite(pbuffer, 1, numBytes, fs);
   1.333     if (written < numBytes)
   1.334         ErrorCode = SFerror();
   1.335 
   1.336 #ifdef OVR_FILE_VERIFY_SEEK_ERRORS
   1.337     if (written > 0)
   1.338         TestPos += written;
   1.339 #endif
   1.340 
   1.341     return written;
   1.342 }
   1.343 
   1.344 int     FILEFile::Read(UByte *pbuffer, int numBytes)
   1.345 {
   1.346     if (LastOp && LastOp != Open_Read)
   1.347         fflush(fs);
   1.348     LastOp = Open_Read;
   1.349     int read = (int) fread(pbuffer, 1, numBytes, fs);
   1.350     if (read < numBytes)
   1.351         ErrorCode = SFerror();
   1.352 
   1.353 #ifdef OVR_FILE_VERIFY_SEEK_ERRORS
   1.354     if (read > 0)
   1.355     {
   1.356         // Read-in data must match our pre-loaded buffer data!
   1.357         UByte* pcompareBuffer = pFileTestBuffer + TestPos;
   1.358         for (int i=0; i< read; i++)
   1.359         {
   1.360             OVR_ASSERT(pcompareBuffer[i] == pbuffer[i]);
   1.361         }
   1.362 
   1.363         //OVR_ASSERT(!memcmp(pFileTestBuffer + TestPos, pbuffer, read));
   1.364         TestPos += read;
   1.365         OVR_ASSERT(ftell(fs) == (int)TestPos);
   1.366     }
   1.367 #endif
   1.368 
   1.369     return read;
   1.370 }
   1.371 
   1.372 // Seeks ahead to skip bytes
   1.373 int     FILEFile::SkipBytes(int numBytes)
   1.374 {
   1.375     SInt64 pos    = LTell();
   1.376     SInt64 newPos = LSeek(numBytes, Seek_Cur);
   1.377 
   1.378     // Return -1 for major error
   1.379     if ((pos==-1) || (newPos==-1))
   1.380     {
   1.381         return -1;
   1.382     }
   1.383     //ErrorCode = ((NewPos-Pos)<numBytes) ? errno : 0;
   1.384 
   1.385     return int (newPos-(int)pos);
   1.386 }
   1.387 
   1.388 // Return # of bytes till EOF
   1.389 int     FILEFile::BytesAvailable()
   1.390 {
   1.391     SInt64 pos    = LTell();
   1.392     SInt64 endPos = LGetLength();
   1.393 
   1.394     // Return -1 for major error
   1.395     if ((pos==-1) || (endPos==-1))
   1.396     {
   1.397         ErrorCode = SFerror();
   1.398         return 0;
   1.399     }
   1.400     else
   1.401         ErrorCode = 0;
   1.402 
   1.403     return int (endPos-(int)pos);
   1.404 }
   1.405 
   1.406 // Flush file contents
   1.407 bool    FILEFile::Flush()
   1.408 {
   1.409     return !fflush(fs);
   1.410 }
   1.411 
   1.412 int     FILEFile::Seek(int offset, int origin)
   1.413 {
   1.414     int newOrigin = 0;
   1.415     switch(origin)
   1.416     {
   1.417     case Seek_Set: newOrigin = SEEK_SET; break;
   1.418     case Seek_Cur: newOrigin = SEEK_CUR; break;
   1.419     case Seek_End: newOrigin = SEEK_END; break;
   1.420     }
   1.421 
   1.422     if (newOrigin == SEEK_SET && offset == Tell())
   1.423         return Tell();
   1.424 
   1.425     if (fseek (fs, offset, newOrigin))
   1.426     {
   1.427 #ifdef OVR_FILE_VERIFY_SEEK_ERRORS
   1.428         OVR_ASSERT(0);
   1.429 #endif
   1.430         return -1;
   1.431     }
   1.432     
   1.433 #ifdef OVR_FILE_VERIFY_SEEK_ERRORS
   1.434     // Track file position after seeks for read verification later.
   1.435     switch(origin)
   1.436     {
   1.437     case Seek_Set:  TestPos = offset;       break;
   1.438     case Seek_Cur:  TestPos += offset;      break;    
   1.439     case Seek_End:  TestPos = FileTestLength + offset; break;
   1.440     }
   1.441     OVR_ASSERT((int)TestPos == Tell());
   1.442 #endif
   1.443 
   1.444     return (int)Tell();
   1.445 }
   1.446 
   1.447 SInt64  FILEFile::LSeek(SInt64 offset, int origin)
   1.448 {
   1.449     return Seek((int)offset,origin);
   1.450 }
   1.451 
   1.452 int FILEFile::CopyFromStream(File *pstream, int byteSize)
   1.453 {
   1.454     UByte   buff[0x4000];
   1.455     int     count = 0;
   1.456     int     szRequest, szRead, szWritten;
   1.457 
   1.458     while (byteSize)
   1.459     {
   1.460         szRequest = (byteSize > int(sizeof(buff))) ? int(sizeof(buff)) : byteSize;
   1.461 
   1.462         szRead    = pstream->Read(buff, szRequest);
   1.463         szWritten = 0;
   1.464         if (szRead > 0)
   1.465             szWritten = Write(buff, szRead);
   1.466 
   1.467         count    += szWritten;
   1.468         byteSize -= szWritten;
   1.469         if (szWritten < szRequest)
   1.470             break;
   1.471     }
   1.472     return count;
   1.473 }
   1.474 
   1.475 
   1.476 bool FILEFile::Close()
   1.477 {
   1.478 #ifdef OVR_FILE_VERIFY_SEEK_ERRORS
   1.479     if (pFileTestBuffer)
   1.480     {
   1.481         OVR_FREE(pFileTestBuffer);
   1.482         pFileTestBuffer = 0;
   1.483         FileTestLength  = 0;
   1.484     }
   1.485 #endif
   1.486 
   1.487     bool closeRet = !fclose(fs);
   1.488 
   1.489     if (!closeRet)
   1.490     {
   1.491         ErrorCode = SFerror();
   1.492         return 0;
   1.493     }
   1.494     else
   1.495     {
   1.496         Opened    = 0;
   1.497         fs        = 0;
   1.498         ErrorCode = 0;
   1.499     }
   1.500 
   1.501     // Handle safe truncate
   1.502     /*
   1.503     if ((OpenFlags & OVR_FO_SAFETRUNC) == OVR_FO_SAFETRUNC)
   1.504     {
   1.505         // Delete original file (if it existed)
   1.506         DWORD oldAttributes = FileUtilWin32::GetFileAttributes(FileName);
   1.507         if (oldAttributes!=0xFFFFFFFF)
   1.508             if (!FileUtilWin32::DeleteFile(FileName))
   1.509             {
   1.510                 // Try to remove the readonly attribute
   1.511                 FileUtilWin32::SetFileAttributes(FileName, oldAttributes & (~FILE_ATTRIBUTE_READONLY) );
   1.512                 // And delete the file again
   1.513                 if (!FileUtilWin32::DeleteFile(FileName))
   1.514                     return 0;
   1.515             }
   1.516 
   1.517         // Rename temp file to real filename
   1.518         if (!FileUtilWin32::MoveFile(TempName, FileName))
   1.519         {
   1.520             //ErrorCode = errno;
   1.521             return 0;
   1.522         }
   1.523     }
   1.524     */
   1.525     return 1;
   1.526 }
   1.527 
   1.528 /*
   1.529 bool    FILEFile::CloseCancel()
   1.530 {
   1.531     bool closeRet = (bool)::CloseHandle(fd);
   1.532 
   1.533     if (!closeRet)
   1.534     {
   1.535         //ErrorCode = errno;
   1.536         return 0;
   1.537     }
   1.538     else
   1.539     {
   1.540         Opened    = 0;
   1.541         fd        = INVALID_HANDLE_VALUE;
   1.542         ErrorCode = 0;
   1.543     }
   1.544 
   1.545     // Handle safe truncate (delete tmp file, leave original unchanged)
   1.546     if ((OpenFlags&OVR_FO_SAFETRUNC) == OVR_FO_SAFETRUNC)
   1.547         if (!FileUtilWin32::DeleteFile(TempName))
   1.548         {
   1.549             //ErrorCode = errno;
   1.550             return 0;
   1.551         }
   1.552     return 1;
   1.553 }
   1.554 */
   1.555 
   1.556 File *FileFILEOpen(const String& path, int flags, int mode)
   1.557 {
   1.558     return new FILEFile(path, flags, mode);
   1.559 }
   1.560 
   1.561 // Helper function: obtain file information time.
   1.562 bool    SysFile::GetFileStat(FileStat* pfileStat, const String& path)
   1.563 {
   1.564 #if defined(OVR_OS_WIN32)
   1.565     // 64-bit implementation on Windows.
   1.566     struct __stat64 fileStat;
   1.567     // Stat returns 0 for success.
   1.568     wchar_t *pwpath = (wchar_t*)OVR_ALLOC((UTF8Util::GetLength(path.ToCStr())+1)*sizeof(wchar_t));
   1.569     UTF8Util::DecodeString(pwpath, path.ToCStr());
   1.570 
   1.571     int ret = _wstat64(pwpath, &fileStat);
   1.572     OVR_FREE(pwpath);
   1.573     if (ret) return false;
   1.574 #else
   1.575     struct stat fileStat;
   1.576     // Stat returns 0 for success.
   1.577     if (stat(path, &fileStat) != 0)
   1.578         return false;
   1.579 #endif
   1.580     pfileStat->AccessTime = fileStat.st_atime;
   1.581     pfileStat->ModifyTime = fileStat.st_mtime;
   1.582     pfileStat->FileSize   = fileStat.st_size;
   1.583     return true;
   1.584 }
   1.585 
   1.586 } // Scaleform
   1.587 \ No newline at end of file