File.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 file[in] Path to the file.
  27. */
  28. File(const Ogre::String& file);
  29. /**
  30. * Opens a file.
  31. *
  32. * @param file[in] Pointer to the file.
  33. */
  34. File(const File* file);
  35. /**
  36. * Loads a file fragment.
  37. *
  38. * @param file[in] Pointer to the file.
  39. * @param offset[in] Offset to the data to load.
  40. * @param length[in] 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 buffer[in] Pointer to the buffer to load from.
  47. * @param offset[in] Offset to the data to load.
  48. * @param length[in] 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 file Path of the file to save
  59. */
  60. void WriteFile(const Ogre::String& file) const;
  61. /**
  62. * Retrieves the file name.
  63. *
  64. * @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 buffer[out] Pointer to the buffer to load.
  77. * @param start[in] Offset to the data to load.
  78. * @param length[in] 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 offset[in] 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 offset[in] 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 offset[in] 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. protected:
  130. /**
  131. * The file name.
  132. */
  133. Ogre::String file_name_;
  134. /**
  135. * Current offset of the file.
  136. *
  137. * Default is 0. Can be set on instantiation with {@see
  138. * File(const File* file, u32 offset, u32 length)} or {@see
  139. * File(const u8* buffer, u32 offset, u32 length)} and advanced with
  140. * {@see readU8}, {@see readU16LE} or {@see readU32LE}
  141. */
  142. u32 offset_;
  143. /**
  144. * The file buffer.
  145. *
  146. * It contains the file data.
  147. */
  148. u8* buffer_;
  149. /**
  150. * The allocated size of {@see buffer_}.
  151. */
  152. u32 buffer_size_;
  153. };