File.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * Copyright (c) 2006-2013 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. /**
  42. * File open mode.
  43. **/
  44. enum Mode
  45. {
  46. CLOSED,
  47. READ,
  48. WRITE,
  49. APPEND,
  50. MODE_MAX_ENUM
  51. };
  52. /**
  53. * Used to indicate ALL data in a file.
  54. **/
  55. static const int64 ALL = -1;
  56. /**
  57. * Destructor.
  58. **/
  59. virtual ~File();
  60. /**
  61. * Opens the file in a certain mode.
  62. *
  63. * @param mode READ, WRITE, APPEND.
  64. * @return True if successful, false otherwise.
  65. **/
  66. virtual bool open(Mode mode) = 0;
  67. /**
  68. * Closes the file.
  69. *
  70. * @return True if successful, false otherwise.
  71. **/
  72. virtual bool close() = 0;
  73. /**
  74. * Gets whether the file is open.
  75. **/
  76. virtual bool isOpen() const = 0;
  77. /**
  78. * Gets the size of the file.
  79. *
  80. * @return The size of the file.
  81. **/
  82. virtual int64 getSize() = 0;
  83. /**
  84. * Reads data from the file and allocates a Data object.
  85. *
  86. * @param size The number of bytes to attempt reading, or -1 for EOF.
  87. * @return A newly allocated Data object.
  88. **/
  89. virtual FileData *read(int64 size = ALL) = 0;
  90. /**
  91. * Reads data into the destination buffer.
  92. *
  93. * @param dst The destination buffer.
  94. * @param size The number of bytes to attempt reading.
  95. * @return The number of bytes actually read.
  96. **/
  97. virtual int64 read(void *dst, int64 size) = 0;
  98. /**
  99. * Writes data into the File.
  100. *
  101. * @param data The source buffer.
  102. * @param size The size of the buffer.
  103. * @return True of success, false otherwise.
  104. **/
  105. virtual bool write(const void *data, int64 size) = 0;
  106. /**
  107. * Writes a Data object into the File.
  108. *
  109. * @param data The data object to write into the file.
  110. * @param size The number of bytes to attempt writing, or -1 for everything.
  111. * @return True of success, false otherwise.
  112. **/
  113. virtual bool write(const Data *data, int64 size = ALL) = 0;
  114. /**
  115. * Checks whether we are currently at end-of-file.
  116. *
  117. * @return True if EOF, false otherwise.
  118. **/
  119. virtual bool eof() = 0;
  120. /**
  121. * Gets the current position in the File.
  122. *
  123. * @return The current byte position in the File.
  124. **/
  125. virtual int64 tell() = 0;
  126. /**
  127. * Seeks to a certain position in the File.
  128. *
  129. * @param pos The byte position in the file.
  130. * @return True on success, false otherwise.
  131. **/
  132. virtual bool seek(uint64 pos) = 0;
  133. /**
  134. * Gets the current mode of the File.
  135. * @return The current mode of the File; CLOSED, READ, WRITE or APPEND.
  136. **/
  137. virtual Mode getMode() const = 0;
  138. /**
  139. * Gets the filename for this File, or empty string if none.
  140. * @return The filename for this File.
  141. **/
  142. virtual std::string getFilename() const = 0;
  143. /**
  144. * Gets the file extension for this File, or empty string if none.
  145. * @return The file extension for this File (without the dot).
  146. **/
  147. virtual std::string getExtension() const = 0;
  148. static bool getConstant(const char *in, Mode &out);
  149. static bool getConstant(Mode in, const char *&out);
  150. private:
  151. static StringMap<Mode, MODE_MAX_ENUM>::Entry modeEntries[];
  152. static StringMap<Mode, MODE_MAX_ENUM> modes;
  153. }; // File
  154. } // filesystem
  155. } // love
  156. #endif // LOVE_FILESYSTEM_FILE_H