BinaryFile.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include "common/File.h"
  19. /**
  20. * Represents a file.
  21. *
  22. * This class is for files that need a lot of bit-reading for parsing. It's slower than it's
  23. * counterpart {@see File}, which is better used in the rest of cases.
  24. */
  25. class BinaryFile{
  26. public:
  27. /**
  28. * Opens a file.
  29. *
  30. * @param[in] file Pointer to the file.
  31. */
  32. BinaryFile(const File* file);
  33. /**
  34. * Destructor.
  35. */
  36. virtual ~BinaryFile();
  37. /**
  38. * Retrieves the file name.
  39. *
  40. * @return The file name, without path.
  41. */
  42. const Ogre::String& GetFileName() const;
  43. /**
  44. * Retrieves the file size.
  45. *
  46. * @return File size, in bytes.
  47. */
  48. u32 GetFileSize() const;
  49. /**
  50. * Loads a buffer with the file data.
  51. *
  52. * @param[out] buffer Pointer to the buffer to load.
  53. * @param[in] start Offset to the data to load.
  54. * @param[in] length Length of the data to load.
  55. */
  56. void GetFileBuffer(u8* buffer, const u32 &start, const u32 &length) const;
  57. /**
  58. * Retrieves bits from the file.
  59. *
  60. * @param[in] offset_bits The offset, in bytes, to the bits to read.
  61. * @param[in] offset_bits The offset, in bits, of the byte at {@see offset_byte} to read
  62. * from.
  63. * @param[in] bits Number of bits to read.
  64. * @return Decimal valud of the read bits.
  65. */
  66. int GetBits(u32 offset_bytes, u8 offset_bits, u8 bits) const;
  67. /**
  68. * Retrieves bits from the file at the current offset.
  69. *
  70. * @param[in] bits Number of bits to read.
  71. * @return Decimal valud of the read bits.
  72. */
  73. int ReadBits(u8 bits);
  74. /**
  75. * Retrieves a byte.
  76. *
  77. * @param[in] offset The offset of the byte.
  78. * @return The byte at the specified offset.
  79. */
  80. u8 GetU8(u32 offset) const;
  81. /**
  82. * Retrieves two bytes (little endian) from the file.
  83. *
  84. * @param[in] offset The offset to the bytes.
  85. * @return The bytes at the specified offset.
  86. */
  87. u16 GetU16LE(u32 offset) const;
  88. /**
  89. * Retrieves four bytes (little endian) from the file.
  90. *
  91. * @return The bytes at the specified offset.
  92. */
  93. u32 GetU32LE(u32 offset) const;
  94. /**
  95. * Reads a byte from the file at the current offset.
  96. *
  97. * Before reading, it aligns the current offset to the next byte.Advances the current
  98. * offset by one byte when done.
  99. *
  100. * @return The data in the byte in the current offset of the file.
  101. */
  102. u8 ReadU8();
  103. /**
  104. * Reads two bytes from the file (little endian) at the current offset.
  105. *
  106. * Before reading, it aligns the current offset to the next byte.Advances the current
  107. * offset by two bytes when done.
  108. *
  109. * @return Two bytes of data from the current offset of the file.
  110. */
  111. u16 ReadU16LE();
  112. /**
  113. * Reads four bytes from the file (little endian) at the current offset.
  114. *
  115. * Before reading, it aligns the current offset to the next byte.Advances the current
  116. * offset by four bytes when done.
  117. *
  118. * @return Four bytes of data from the current offset of the file.
  119. */
  120. u32 ReadU32LE();
  121. /**
  122. * Checks the current offset, in bits, of the file.
  123. *
  124. * Default is 0. Can be set on instantiation with {@see
  125. * BinaryFile(const File* file, u32 offset, u32 length)} or {@see
  126. * BinaryFile(const u8* buffer, u32 offset, u32 length)} and advanced with
  127. * {@see GeadBitsLE}, or {@see ReadBitsLE}
  128. */
  129. unsigned int GetCurrentOffset();
  130. /**
  131. * Sets the file offset for reading.
  132. *
  133. * If the offset is larger than the file size, the offset will be set to the end of the
  134. * file
  135. *
  136. * @param[in] offset The new offset, in bits.
  137. */
  138. void SetOffset(unsigned int offset);
  139. /**
  140. * Aligns the current offset to the next byte.
  141. */
  142. void Align();
  143. private:
  144. /**
  145. * Offset, to the bit.
  146. */
  147. struct Offset{
  148. /**
  149. * File offset, in bytes.
  150. */
  151. u32 bytes;
  152. /**
  153. * Ofset of the current byte, in bits
  154. */
  155. u8 bits;
  156. /**
  157. * Constructor, initializes to 0.
  158. */
  159. Offset(): bytes(0), bits(0){};
  160. };
  161. /**
  162. * The file name.
  163. */
  164. Ogre::String file_name_;
  165. /**
  166. * Current offset of the file.
  167. *
  168. * Default is 0. Can be set on instantiation with {@see
  169. * File(const File* file, u32 offset, u32 length)} or {@see
  170. * File(const u8* buffer, u32 offset, u32 length)} and advanced with
  171. * {@see readU8}, {@see readU16LE} or {@see readU32LE}
  172. */
  173. Offset offset_;
  174. /**
  175. * The file buffer, in bytes.
  176. *
  177. * It contains the file data, in bytes.
  178. */
  179. u8* buffer_bytes_;
  180. /**
  181. * The allocated size of {@see buffer_bytes_}, in bytes.
  182. */
  183. u32 buffer_size_bytes_;
  184. /**
  185. * The file buffer, bit by bit.
  186. *
  187. * It contains the file data, in bits.
  188. */
  189. u8* buffer_bits_;
  190. /**
  191. * The allocated size of {@see buffer_bits_}, in bits.
  192. */
  193. u32 buffer_size_bits_;
  194. };