File.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <OgreString.h>
  17. #include "common/TypeDefine.h"
  18. /**
  19. * Represents a file.
  20. */
  21. class File{
  22. public:
  23. /**
  24. * Opens a file.
  25. *
  26. * @param[in] file Path to the file.
  27. */
  28. File(const Ogre::String& file);
  29. /**
  30. * Opens a file.
  31. *
  32. * @param[in] file Pointer to the file.
  33. */
  34. File(const File* file);
  35. /**
  36. * Loads a file fragment.
  37. *
  38. * @param[in] file Pointer to the file.
  39. * @param[in] offset Offset to the data to load.
  40. * @param[in] length Length of the data to load.
  41. */
  42. File(const File* file, u32 offset, u32 length);
  43. /**
  44. * Loads a file fragment from a buffer.
  45. *
  46. * @param[in] buffer Pointer to the buffer to load from.
  47. * @param[in] offset Offset to the data to load.
  48. * @param[in] length Length of the data to load.
  49. */
  50. File(const u8* buffer, u32 offset, u32 length);
  51. /**
  52. * Destructor.
  53. */
  54. virtual ~File();
  55. /**
  56. * Writes the contents of the buffer to a file.
  57. *
  58. * @param[in] file Path of the file to save
  59. */
  60. void WriteFile(const Ogre::String& file) const;
  61. /**
  62. * Retrieves the file name.
  63. *
  64. * @return The file name, without path.
  65. */
  66. const Ogre::String& GetFileName() const;
  67. /**
  68. * Retrieves the file size.
  69. *
  70. * @return File size, in bytes.
  71. */
  72. u32 GetFileSize() const;
  73. /**
  74. * Loads a buffer with the file data.
  75. *
  76. * @param[out] buffer Pointer to the buffer to load.
  77. * @param[in] start Offset to the data to load.
  78. * @param[in] length Length of the data to load.
  79. */
  80. void GetFileBuffer(
  81. u8* buffer, const u32 &start, const u32 &length
  82. ) const;
  83. /**
  84. * Retrieves a pointer to a byte address in the file.
  85. *
  86. * @param[in] offset The offset to the requested address.
  87. */
  88. u8 GetU8(u32 offset) const;
  89. /**
  90. * Retrieves a pointer to a two-byte address in the file.
  91. *
  92. * The data must b considered to be in little endian.
  93. *
  94. * @param[in] offset The offset to the requested address.
  95. */
  96. u16 GetU16LE(u32 offset) const;
  97. /**
  98. * Retrieves a pointer to a four-byte address in the file.
  99. *
  100. * The data must b considered to be in little endian.
  101. *
  102. * @param[in] offset The offset to the requested address.
  103. */
  104. u32 GetU32LE(u32 offset) const;
  105. /**
  106. * Reads a byte from the file.
  107. *
  108. * Advances the current offset by one byte.
  109. *
  110. * @return The data in the byte in the current offset of the file.
  111. */
  112. u8 readU8();
  113. /**
  114. * Reads two bytes from the file (little endian).
  115. *
  116. * Advances the current offset by two bytes.
  117. *
  118. * @return Two bytes of data from the current offset of the file.
  119. */
  120. u16 readU16LE();
  121. /**
  122. * Reads four bytes from the file (little endian).
  123. *
  124. * Advances the current offset by four byte.
  125. *
  126. * @return Four bytes of data from the current offset of the file.
  127. */
  128. u32 readU32LE();
  129. /**
  130. * Checks the current offset of the file.
  131. *
  132. * Default is 0. Can be set on instantiation with {@see
  133. * File(const File* file, u32 offset, u32 length)} or {@see
  134. * File(const u8* buffer, u32 offset, u32 length)} and advanced with
  135. * {@see readU8}, {@see readU16LE} or {@see readU32LE}
  136. */
  137. u32 GetCurrentOffset();
  138. /**
  139. * Sets the file offset for reading.
  140. *
  141. * If the offset is larger than the file size, the offset will be set to the end of the
  142. * file
  143. *
  144. * @param[in] offset The new offset.
  145. */
  146. void SetOffset(u32 offset);
  147. protected:
  148. /**
  149. * The file name.
  150. */
  151. Ogre::String file_name_;
  152. /**
  153. * Current offset of the file.
  154. *
  155. * Default is 0. Can be set on instantiation with {@see
  156. * File(const File* file, u32 offset, u32 length)} or {@see
  157. * File(const u8* buffer, u32 offset, u32 length)} and advanced with
  158. * {@see readU8}, {@see readU16LE} or {@see readU32LE}
  159. */
  160. u32 offset_ = 0;
  161. /**
  162. * The file buffer.
  163. *
  164. * It contains the file data.
  165. */
  166. u8* buffer_;
  167. /**
  168. * The allocated size of {@see buffer_}.
  169. */
  170. u32 buffer_size_;
  171. };