OVR_SysFile.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**************************************************************************
  2. Filename : OVR_SysFile.cpp
  3. Content : File wrapper class implementation (Win32)
  4. Created : April 5, 1999
  5. Authors : Michael Antonov
  6. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  7. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  8. you may not use the Oculus VR Rift SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. http://www.oculusvr.com/licenses/LICENSE-3.2
  13. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. **************************************************************************/
  19. #define GFILE_CXX
  20. // Standard C library (Captain Obvious guarantees!)
  21. #include <stdio.h>
  22. #include "OVR_SysFile.h"
  23. #include "OVR_File.h"
  24. #include "OVR_Log.h"
  25. namespace OVR {
  26. // This is - a dummy file that fails on all calls.
  27. class UnopenedFile : public File
  28. {
  29. public:
  30. UnopenedFile() { }
  31. ~UnopenedFile() { }
  32. virtual const char* GetFilePath() { return 0; }
  33. // ** File Information
  34. virtual bool IsValid() { return 0; }
  35. virtual bool IsWritable() { return 0; }
  36. // Return position / file size
  37. virtual int Tell() { return 0; }
  38. virtual int64_t LTell() { return 0; }
  39. virtual int GetLength() { return 0; }
  40. virtual int64_t LGetLength() { return 0; }
  41. // virtual bool Stat(FileStats *pfs) { return 0; }
  42. virtual int GetErrorCode() { return Error_FileNotFound; }
  43. // ** Stream implementation & I/O
  44. virtual int Write(const uint8_t * /*pbuffer*/, int /*numBytes*/) { return -1; }
  45. virtual int Read(uint8_t * /*pbuffer*/, int /*numBytes*/) { return -1; }
  46. virtual int SkipBytes(int /*numBytes*/) { return 0; }
  47. virtual int BytesAvailable() { return 0; }
  48. virtual bool Flush() { return 0; }
  49. virtual int Seek(int /*offset*/, int /*origin*/) { return -1; }
  50. virtual int64_t LSeek(int64_t /*offset*/, int /*origin*/) { return -1; }
  51. virtual int CopyFromStream(File * /*pstream*/, int /*byteSize*/) { return -1; }
  52. virtual bool Close() { return 0; }
  53. };
  54. // ***** System File
  55. // System file is created to access objects on file system directly
  56. // This file can refer directly to path
  57. // ** Constructor
  58. SysFile::SysFile() : DelegatedFile(0)
  59. {
  60. pFile = *new UnopenedFile;
  61. }
  62. Ptr<File> FileFILEOpen(const String& path, int flags, int mode);
  63. // Opens a file
  64. SysFile::SysFile(const String& path, int flags, int mode) : DelegatedFile(0)
  65. {
  66. Open(path, flags, mode);
  67. }
  68. // ** Open & management
  69. // Will fail if file's already open
  70. bool SysFile::Open(const String& path, int flags, int mode)
  71. {
  72. pFile = FileFILEOpen(path, flags, mode);
  73. if ((!pFile) || (!pFile->IsValid()))
  74. {
  75. pFile = *new UnopenedFile;
  76. OVR_DEBUG_LOG(("Failed to open file: %s", path.ToCStr()));
  77. return 0;
  78. }
  79. //pFile = *OVR_NEW DelegatedFile(pFile); // MA Testing
  80. if (flags & Open_Buffered)
  81. pFile = *new BufferedFile(pFile);
  82. return 1;
  83. }
  84. // ** Overrides
  85. int SysFile::GetErrorCode()
  86. {
  87. return pFile ? pFile->GetErrorCode() : Error_FileNotFound;
  88. }
  89. // Overrides to provide re-open support
  90. bool SysFile::IsValid()
  91. {
  92. return pFile && pFile->IsValid();
  93. }
  94. bool SysFile::Close()
  95. {
  96. if (IsValid())
  97. {
  98. DelegatedFile::Close();
  99. pFile = *new UnopenedFile;
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. } // OVR