File.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <vector>
  17. #include <OgreString.h>
  18. #include "common/TypeDefine.h"
  19. /**
  20. * Represents a file.
  21. */
  22. class File{
  23. public:
  24. /**
  25. * Creates an empty file.
  26. */
  27. File();
  28. /**
  29. * Opens a file.
  30. *
  31. * @param[in] file Path to the file.
  32. */
  33. File(const Ogre::String& file);
  34. /**
  35. * Opens a file.
  36. *
  37. * @param[in] file Pointer to the file.
  38. */
  39. explicit File(const File* file);
  40. /**
  41. * Loads a file fragment.
  42. *
  43. * @param[in] file Pointer to the file.
  44. * @param[in] offset Offset to the data to load.
  45. * @param[in] length Length of the data to load.
  46. */
  47. File(const File* file, u32 offset, u32 length);
  48. /**
  49. * Loads a file fragment from a buffer.
  50. *
  51. * @param[in] buffer Pointer to the buffer to load from.
  52. * @param[in] offset Offset to the data to load.
  53. * @param[in] length Length of the data to load.
  54. */
  55. File(const u8* buffer, u32 offset, u32 length);
  56. /**
  57. * Destructor.
  58. */
  59. virtual ~File();
  60. /**
  61. * Copies a file, with its own copy of the data.
  62. */
  63. File(const File& file) = default;
  64. /**
  65. * Moves a file.
  66. */
  67. File(File&& file) = default;
  68. /**
  69. * Copies a file, with its own copy of the data.
  70. */
  71. File& operator=(const File& file) = default;
  72. /**
  73. * Moves a file.
  74. */
  75. File& operator=(File&& file) = default;
  76. /**
  77. * Writes the contents of the buffer to a file.
  78. *
  79. * @param[in] file Path of the file to save
  80. */
  81. void WriteFile(const Ogre::String& file) const;
  82. /**
  83. * Retrieves the file name.
  84. *
  85. * @return The file name, without path.
  86. */
  87. const Ogre::String& GetFileName() const;
  88. /**
  89. * Retrieves the file size.
  90. *
  91. * @return File size, in bytes.
  92. */
  93. u32 GetFileSize() const;
  94. /**
  95. * Loads a buffer with the file data.
  96. *
  97. * @param[out] buffer Pointer to the buffer to load.
  98. * @param[in] start Offset to the data to load.
  99. * @param[in] length Length of the data to load.
  100. */
  101. void GetFileBuffer(
  102. u8* buffer, const u32 &start, const u32 &length
  103. ) const;
  104. /**
  105. * Retrieves a pointer to a byte address in the file.
  106. *
  107. * @param[in] offset The offset to the requested address.
  108. */
  109. u8 GetU8(u32 offset) const;
  110. /**
  111. * Retrieves a pointer to a two-byte address in the file.
  112. *
  113. * The data must b considered to be in little endian.
  114. *
  115. * @param[in] offset The offset to the requested address.
  116. */
  117. u16 GetU16LE(u32 offset) const;
  118. /**
  119. * Retrieves a pointer to a four-byte address in the file.
  120. *
  121. * The data must b considered to be in little endian.
  122. *
  123. * @param[in] offset The offset to the requested address.
  124. */
  125. u32 GetU32LE(u32 offset) const;
  126. /**
  127. * Reads a byte from the file.
  128. *
  129. * Advances the current offset by one byte.
  130. *
  131. * @return The data in the byte in the current offset of the file.
  132. */
  133. u8 readU8();
  134. /**
  135. * Reads two bytes from the file (little endian).
  136. *
  137. * Advances the current offset by two bytes.
  138. *
  139. * @return Two bytes of data from the current offset of the file.
  140. */
  141. u16 readU16LE();
  142. /**
  143. * Reads four bytes from the file (little endian).
  144. *
  145. * Advances the current offset by four byte.
  146. *
  147. * @return Four bytes of data from the current offset of the file.
  148. */
  149. u32 readU32LE();
  150. /**
  151. * Checks the current offset of the file.
  152. *
  153. * Default is 0. It is advanced with {@see readU8}, {@see readU16LE} or {@see readU32LE}.
  154. */
  155. u32 GetCurrentOffset();
  156. /**
  157. * Sets the file offset for reading.
  158. *
  159. * If the offset is larger than the file size, the offset will be set to the end of the
  160. * file
  161. *
  162. * @param[in] offset The new offset.
  163. */
  164. void SetOffset(u32 offset);
  165. protected:
  166. /**
  167. * The file name.
  168. */
  169. Ogre::String file_name_;
  170. /**
  171. * Current offset of the file.
  172. *
  173. * Default is 0. It is advanced with {@see readU8}, {@see readU16LE} or {@see readU32LE}.
  174. */
  175. u32 offset_ = 0;
  176. /**
  177. * The file data. Each file has its own copy, freed with the file.
  178. */
  179. std::vector<u8> buffer_;
  180. };