platformFileIO.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _PLATFORM_FILEIO_H_
  23. #define _PLATFORM_FILEIO_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. class File
  28. {
  29. public:
  30. /// What is the status of our file handle?
  31. enum Status
  32. {
  33. Ok = 0, ///< Ok!
  34. IOError, ///< Read or Write error
  35. EOS, ///< End of Stream reached (mostly for reads)
  36. IllegalCall, ///< An unsupported operation used. Always accompanied by AssertWarn
  37. Closed, ///< Tried to operate on a closed stream (or detached filter)
  38. UnknownError ///< Catchall
  39. };
  40. /// How are we accessing the file?
  41. enum AccessMode
  42. {
  43. Read = 0, ///< Open for read only, starting at beginning of file.
  44. Write = 1, ///< Open for write only, starting at beginning of file; will blast old contents of file.
  45. ReadWrite = 2, ///< Open for read-write.
  46. WriteAppend = 3 ///< Write-only, starting at end of file.
  47. };
  48. /// Flags used to indicate what we can do to the file.
  49. enum Capability
  50. {
  51. FileRead = BIT(0),
  52. FileWrite = BIT(1)
  53. };
  54. private:
  55. void *handle; ///< Pointer to the file handle.
  56. Status currentStatus; ///< Current status of the file (Ok, IOError, etc.).
  57. U32 capability; ///< Keeps track of file capabilities.
  58. #ifdef TORQUE_OS_ANDROID
  59. U8* buffer;
  60. U32 size;
  61. U32 filePointer;
  62. #endif
  63. File(const File&); ///< This is here to disable the copy constructor.
  64. File& operator=(const File&); ///< This is here to disable assignment.
  65. public:
  66. File(); ///< Default constructor
  67. virtual ~File(); ///< Destructor
  68. /// Opens a file for access using the specified AccessMode
  69. ///
  70. /// @returns The status of the file
  71. Status open(const char *filename, const AccessMode openMode);
  72. /// Gets the current position in the file
  73. ///
  74. /// This is in bytes from the beginning of the file.
  75. U32 getPosition() const;
  76. /// Sets the current position in the file.
  77. ///
  78. /// You can set either a relative or absolute position to go to in the file.
  79. ///
  80. /// @code
  81. /// File *foo;
  82. ///
  83. /// ... set up file ...
  84. ///
  85. /// // Go to byte 32 in the file...
  86. /// foo->setPosition(32);
  87. ///
  88. /// // Now skip back 20 bytes...
  89. /// foo->setPosition(-20, false);
  90. ///
  91. /// // And forward 17...
  92. /// foo->setPosition(17, false);
  93. /// @endcode
  94. ///
  95. /// @returns The status of the file
  96. Status setPosition(S32 position, bool absolutePos = true);
  97. /// Returns the size of the file
  98. U32 getSize() const;
  99. /// Make sure everything that's supposed to be written to the file gets written.
  100. ///
  101. /// @returns The status of the file.
  102. Status flush();
  103. /// Closes the file
  104. ///
  105. /// @returns The status of the file.
  106. Status close();
  107. /// Gets the status of the file
  108. Status getStatus() const;
  109. /// Reads "size" bytes from the file, and dumps data into "dst".
  110. /// The number of actual bytes read is returned in bytesRead
  111. /// @returns The status of the file
  112. Status read(U32 size, char *dst, U32 *bytesRead = NULL);
  113. /// Writes "size" bytes into the file from the pointer "src".
  114. /// The number of actual bytes written is returned in bytesWritten
  115. /// @returns The status of the file
  116. Status write(U32 size, const char *src, U32 *bytesWritten = NULL);
  117. /// Returns whether or not this file is capable of the given function.
  118. bool hasCapability(Capability cap) const;
  119. protected:
  120. Status setStatus(); ///< Called after error encountered.
  121. Status setStatus(Status status); ///< Setter for the current status.
  122. };
  123. #endif // _FILE_IO_H_