platformFileIO.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #ifdef TORQUE_OS_LINUX
  28. // Need to remove this once Xlib stops leaking
  29. #undef Status
  30. #endif
  31. class File
  32. {
  33. public:
  34. /// What is the status of our file handle?
  35. enum Status
  36. {
  37. Ok = 0, ///< Ok!
  38. IOError, ///< Read or Write error
  39. EOS, ///< End of Stream reached (mostly for reads)
  40. IllegalCall, ///< An unsupported operation used. Always accompanied by AssertWarn
  41. Closed, ///< Tried to operate on a closed stream (or detached filter)
  42. UnknownError ///< Catchall
  43. };
  44. /// How are we accessing the file?
  45. enum AccessMode
  46. {
  47. Read = 0, ///< Open for read only, starting at beginning of file.
  48. Write = 1, ///< Open for write only, starting at beginning of file; will blast old contents of file.
  49. ReadWrite = 2, ///< Open for read-write.
  50. WriteAppend = 3 ///< Write-only, starting at end of file.
  51. };
  52. /// Flags used to indicate what we can do to the file.
  53. enum Capability
  54. {
  55. FileRead = BIT(0),
  56. FileWrite = BIT(1)
  57. };
  58. private:
  59. void *handle; ///< Pointer to the file handle.
  60. Status currentStatus; ///< Current status of the file (Ok, IOError, etc.).
  61. U32 capability; ///< Keeps track of file capabilities.
  62. #ifdef TORQUE_OS_ANDROID
  63. U8* buffer;
  64. U32 size;
  65. U32 filePointer;
  66. #endif
  67. File(const File&); ///< This is here to disable the copy constructor.
  68. File& operator=(const File&); ///< This is here to disable assignment.
  69. public:
  70. File(); ///< Default constructor
  71. virtual ~File(); ///< Destructor
  72. /// Opens a file for access using the specified AccessMode
  73. ///
  74. /// @returns The status of the file
  75. Status open(const char *filename, const AccessMode openMode);
  76. /// Gets the current position in the file
  77. ///
  78. /// This is in bytes from the beginning of the file.
  79. U32 getPosition() const;
  80. /// Sets the current position in the file.
  81. ///
  82. /// You can set either a relative or absolute position to go to in the file.
  83. ///
  84. /// @code
  85. /// File *foo;
  86. ///
  87. /// ... set up file ...
  88. ///
  89. /// // Go to byte 32 in the file...
  90. /// foo->setPosition(32);
  91. ///
  92. /// // Now skip back 20 bytes...
  93. /// foo->setPosition(-20, false);
  94. ///
  95. /// // And forward 17...
  96. /// foo->setPosition(17, false);
  97. /// @endcode
  98. ///
  99. /// @returns The status of the file
  100. Status setPosition(S32 position, bool absolutePos = true);
  101. /// Returns the size of the file
  102. U32 getSize() const;
  103. /// Make sure everything that's supposed to be written to the file gets written.
  104. ///
  105. /// @returns The status of the file.
  106. Status flush();
  107. /// Closes the file
  108. ///
  109. /// @returns The status of the file.
  110. Status close();
  111. /// Gets the status of the file
  112. Status getStatus() const;
  113. /// Reads "size" bytes from the file, and dumps data into "dst".
  114. /// The number of actual bytes read is returned in bytesRead
  115. /// @returns The status of the file
  116. Status read(U32 size, char *dst, U32 *bytesRead = NULL);
  117. /// Writes "size" bytes into the file from the pointer "src".
  118. /// The number of actual bytes written is returned in bytesWritten
  119. /// @returns The status of the file
  120. Status write(U32 size, const char *src, U32 *bytesWritten = NULL);
  121. /// Returns whether or not this file is capable of the given function.
  122. bool hasCapability(Capability cap) const;
  123. protected:
  124. Status setStatus(); ///< Called after error encountered.
  125. Status setStatus(Status status); ///< Setter for the current status.
  126. };
  127. #endif // _FILE_IO_H_