fileio.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 _FILEIO_H_
  23. #define _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 FileStatus
  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. FileStatus currentStatus; ///< Current status of the file (Ok, IOError, etc.).
  57. U32 capability; ///< Keeps track of file capabilities.
  58. File(const File&); ///< This is here to disable the copy constructor.
  59. File& operator=(const File&); ///< This is here to disable assignment.
  60. public:
  61. File(); ///< Default constructor
  62. virtual ~File(); ///< Destructor
  63. /// Opens a file for access using the specified AccessMode
  64. ///
  65. /// @returns The status of the file
  66. FileStatus open(const char *filename, const AccessMode openMode);
  67. /// Gets the current position in the file
  68. ///
  69. /// This is in bytes from the beginning of the file.
  70. U32 getPosition() const;
  71. /// Sets the current position in the file.
  72. ///
  73. /// You can set either a relative or absolute position to go to in the file.
  74. ///
  75. /// @code
  76. /// File *foo;
  77. ///
  78. /// ... set up file ...
  79. ///
  80. /// // Go to byte 32 in the file...
  81. /// foo->setPosition(32);
  82. ///
  83. /// // Now skip back 20 bytes...
  84. /// foo->setPosition(-20, false);
  85. ///
  86. /// // And forward 17...
  87. /// foo->setPosition(17, false);
  88. /// @endcode
  89. ///
  90. /// @returns The status of the file
  91. FileStatus setPosition(S32 position, bool absolutePos = true);
  92. /// Returns the size of the file
  93. U32 getSize() const;
  94. /// Make sure everything that's supposed to be written to the file gets written.
  95. ///
  96. /// @returns The status of the file.
  97. FileStatus flush();
  98. /// Closes the file
  99. ///
  100. /// @returns The status of the file.
  101. FileStatus close();
  102. /// Gets the status of the file
  103. FileStatus getStatus() const;
  104. /// Reads "size" bytes from the file, and dumps data into "dst".
  105. /// The number of actual bytes read is returned in bytesRead
  106. /// @returns The status of the file
  107. FileStatus read(U32 size, char *dst, U32 *bytesRead = NULL);
  108. /// Writes "size" bytes into the file from the pointer "src".
  109. /// The number of actual bytes written is returned in bytesWritten
  110. /// @returns The status of the file
  111. FileStatus write(U32 size, const char *src, U32 *bytesWritten = NULL);
  112. /// Returns whether or not this file is capable of the given function.
  113. bool hasCapability(Capability cap) const;
  114. const void* getHandle() { return handle; }
  115. protected:
  116. FileStatus setStatus(); ///< Called after error encountered.
  117. FileStatus setStatus(FileStatus status); ///< Setter for the current status.
  118. };
  119. #endif // _FILE_IO_H_