File.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * Copyright (c) 2006-2017 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_FILESYSTEM_FILE_H
  21. #define LOVE_FILESYSTEM_FILE_H
  22. // STD
  23. #include <string>
  24. // LOVE
  25. #include "common/Data.h"
  26. #include "common/Object.h"
  27. #include "common/StringMap.h"
  28. #include "common/int.h"
  29. #include "FileData.h"
  30. namespace love
  31. {
  32. namespace filesystem
  33. {
  34. /**
  35. * A File interface, providing generic means of reading from and
  36. * writing to files.
  37. **/
  38. class File : public Object
  39. {
  40. public:
  41. static love::Type type;
  42. /**
  43. * File open mode.
  44. **/
  45. enum Mode
  46. {
  47. MODE_CLOSED,
  48. MODE_READ,
  49. MODE_WRITE,
  50. MODE_APPEND,
  51. MODE_MAX_ENUM
  52. };
  53. enum BufferMode
  54. {
  55. BUFFER_NONE,
  56. BUFFER_LINE,
  57. BUFFER_FULL,
  58. BUFFER_MAX_ENUM
  59. };
  60. /**
  61. * Used to indicate ALL data in a file.
  62. **/
  63. static const int64 ALL = -1;
  64. /**
  65. * Destructor.
  66. **/
  67. virtual ~File();
  68. /**
  69. * Opens the file in a certain mode.
  70. *
  71. * @param mode MODE_READ, MODE_WRITE, MODE_APPEND.
  72. * @return True if successful, false otherwise.
  73. **/
  74. virtual bool open(Mode mode) = 0;
  75. /**
  76. * Closes the file.
  77. *
  78. * @return True if successful, false otherwise.
  79. **/
  80. virtual bool close() = 0;
  81. /**
  82. * Gets whether the file is open.
  83. **/
  84. virtual bool isOpen() const = 0;
  85. /**
  86. * Gets the size of the file.
  87. *
  88. * @return The size of the file.
  89. **/
  90. virtual int64 getSize() = 0;
  91. /**
  92. * Reads data from the file and allocates a Data object.
  93. *
  94. * @param size The number of bytes to attempt reading, or -1 for EOF.
  95. * @return A newly allocated Data object.
  96. **/
  97. virtual FileData *read(int64 size = ALL);
  98. /**
  99. * Reads data into the destination buffer.
  100. *
  101. * @param dst The destination buffer.
  102. * @param size The number of bytes to attempt reading.
  103. * @return The number of bytes actually read.
  104. **/
  105. virtual int64 read(void *dst, int64 size) = 0;
  106. /**
  107. * Writes data into the File.
  108. *
  109. * @param data The source buffer.
  110. * @param size The size of the buffer.
  111. * @return True of success, false otherwise.
  112. **/
  113. virtual bool write(const void *data, int64 size) = 0;
  114. /**
  115. * Writes a Data object into the File.
  116. *
  117. * @param data The data object to write into the file.
  118. * @param size The number of bytes to attempt writing, or -1 for everything.
  119. * @return True of success, false otherwise.
  120. **/
  121. virtual bool write(const Data *data, int64 size = ALL);
  122. /**
  123. * Flushes the currently buffered file data to disk. Only applicable in
  124. * write mode.
  125. **/
  126. virtual bool flush() = 0;
  127. /**
  128. * Checks whether we are currently at end-of-file.
  129. *
  130. * @return True if EOF, false otherwise.
  131. **/
  132. virtual bool isEOF() = 0;
  133. /**
  134. * Gets the current position in the File.
  135. *
  136. * @return The current byte position in the File.
  137. **/
  138. virtual int64 tell() = 0;
  139. /**
  140. * Seeks to a certain position in the File.
  141. *
  142. * @param pos The byte position in the file.
  143. * @return True on success, false otherwise.
  144. **/
  145. virtual bool seek(uint64 pos) = 0;
  146. /**
  147. * Sets the buffering mode for the file. When buffering is enabled, the file
  148. * will not write to disk (or will pre-load data if in read mode) until the
  149. * buffer's capacity is reached.
  150. * In the BUFFER_LINE mode, the file will also write to disk if a newline is
  151. * written.
  152. *
  153. * @param bufmode The buffer mode.
  154. * @param size The size in bytes of the buffer.
  155. **/
  156. virtual bool setBuffer(BufferMode bufmode, int64 size) = 0;
  157. /**
  158. * @param[out] size The size in bytes of the buffer.
  159. * @return The current buffer mode.
  160. **/
  161. virtual BufferMode getBuffer(int64 &size) const = 0;
  162. /**
  163. * Gets the current mode of the File.
  164. * @return The current mode of the File; CLOSED, READ, WRITE or APPEND.
  165. **/
  166. virtual Mode getMode() const = 0;
  167. /**
  168. * Gets the filename for this File, or empty string if none.
  169. * @return The filename for this File.
  170. **/
  171. virtual const std::string &getFilename() const = 0;
  172. /**
  173. * Gets the file extension for this File, or empty string if none.
  174. * @return The file extension for this File (without the dot).
  175. **/
  176. virtual std::string getExtension() const;
  177. static bool getConstant(const char *in, Mode &out);
  178. static bool getConstant(Mode in, const char *&out);
  179. static bool getConstant(const char *in, BufferMode &out);
  180. static bool getConstant(BufferMode in, const char *&out);
  181. private:
  182. static StringMap<Mode, MODE_MAX_ENUM>::Entry modeEntries[];
  183. static StringMap<Mode, MODE_MAX_ENUM> modes;
  184. static StringMap<BufferMode, BUFFER_MAX_ENUM>::Entry bufferModeEntries[];
  185. static StringMap<BufferMode, BUFFER_MAX_ENUM> bufferModes;
  186. }; // File
  187. } // filesystem
  188. } // love
  189. #endif // LOVE_FILESYSTEM_FILE_H