FileReader.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * FileReader.h
  3. * ------------
  4. * Purpose: A basic class for transparent reading of memory-based files.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include "mpt/io_read/filecursor.hpp"
  12. #include "mpt/io_read/filecursor_filename_traits.hpp"
  13. #include "mpt/io_read/filecursor_traits_filedata.hpp"
  14. #include "mpt/io_read/filecursor_traits_memory.hpp"
  15. #include "mpt/io_read/filereader.hpp"
  16. #include "openmpt/base/Types.hpp"
  17. #include "mptPathString.h"
  18. #include "mptStringBuffer.h"
  19. #include <algorithm>
  20. #include <array>
  21. #include <limits>
  22. #include <optional>
  23. #include <string>
  24. #include <vector>
  25. #include <cstring>
  26. #include "FileReaderFwd.h"
  27. OPENMPT_NAMESPACE_BEGIN
  28. namespace FileReaderExt
  29. {
  30. // Read a string of length srcSize into fixed-length char array destBuffer using a given read mode.
  31. // The file cursor is advanced by "srcSize" bytes.
  32. // Returns true if at least one byte could be read or 0 bytes were requested.
  33. template<mpt::String::ReadWriteMode mode, size_t destSize, typename TFileCursor>
  34. bool ReadString(TFileCursor &f, char (&destBuffer)[destSize], const typename TFileCursor::pos_type srcSize)
  35. {
  36. typename TFileCursor::PinnedView source = f.ReadPinnedView(srcSize); // Make sure the string is cached properly.
  37. typename TFileCursor::pos_type realSrcSize = source.size(); // In case fewer bytes are available
  38. mpt::String::WriteAutoBuf(destBuffer) = mpt::String::ReadBuf(mode, mpt::byte_cast<const char*>(source.data()), realSrcSize);
  39. return (realSrcSize > 0 || srcSize == 0);
  40. }
  41. // Read a string of length srcSize into a std::string dest using a given read mode.
  42. // The file cursor is advanced by "srcSize" bytes.
  43. // Returns true if at least one character could be read or 0 characters were requested.
  44. template<mpt::String::ReadWriteMode mode, typename TFileCursor>
  45. bool ReadString(TFileCursor &f, std::string &dest, const typename TFileCursor::pos_type srcSize)
  46. {
  47. dest.clear();
  48. typename TFileCursor::PinnedView source = f.ReadPinnedView(srcSize); // Make sure the string is cached properly.
  49. typename TFileCursor::pos_type realSrcSize = source.size(); // In case fewer bytes are available
  50. dest = mpt::String::ReadBuf(mode, mpt::byte_cast<const char*>(source.data()), realSrcSize);
  51. return (realSrcSize > 0 || srcSize == 0);
  52. }
  53. // Read a string of length srcSize into a mpt::charbuf dest using a given read mode.
  54. // The file cursor is advanced by "srcSize" bytes.
  55. // Returns true if at least one character could be read or 0 characters were requested.
  56. template<mpt::String::ReadWriteMode mode, std::size_t len, typename TFileCursor>
  57. bool ReadString(TFileCursor &f, mpt::charbuf<len> &dest, const typename TFileCursor::pos_type srcSize)
  58. {
  59. typename TFileCursor::PinnedView source = f.ReadPinnedView(srcSize); // Make sure the string is cached properly.
  60. typename TFileCursor::pos_type realSrcSize = source.size(); // In case fewer bytes are available
  61. dest = mpt::String::ReadBuf(mode, mpt::byte_cast<const char*>(source.data()), realSrcSize);
  62. return (realSrcSize > 0 || srcSize == 0);
  63. }
  64. // Read a charset encoded string of length srcSize into a mpt::ustring dest using a given read mode.
  65. // The file cursor is advanced by "srcSize" bytes.
  66. // Returns true if at least one character could be read or 0 characters were requested.
  67. template<mpt::String::ReadWriteMode mode, typename TFileCursor>
  68. bool ReadString(TFileCursor &f, mpt::ustring &dest, mpt::Charset charset, const typename TFileCursor::pos_type srcSize)
  69. {
  70. dest.clear();
  71. typename TFileCursor::PinnedView source = f.ReadPinnedView(srcSize); // Make sure the string is cached properly.
  72. typename TFileCursor::pos_type realSrcSize = source.size(); // In case fewer bytes are available
  73. dest = mpt::ToUnicode(charset, mpt::String::ReadBuf(mode, mpt::byte_cast<const char*>(source.data()), realSrcSize));
  74. return (realSrcSize > 0 || srcSize == 0);
  75. }
  76. // Read a string with a preprended length field of type Tsize (must be a packed<*,*> type) into a std::string dest using a given read mode.
  77. // The file cursor is advanced by the string length.
  78. // Returns true if the size field could be read and at least one character could be read or 0 characters were requested.
  79. template<typename Tsize, mpt::String::ReadWriteMode mode, size_t destSize, typename TFileCursor>
  80. bool ReadSizedString(TFileCursor &f, char (&destBuffer)[destSize], const typename TFileCursor::pos_type maxLength = std::numeric_limits<typename TFileCursor::pos_type>::max())
  81. {
  82. mpt::packed<typename Tsize::base_type, typename Tsize::endian_type> srcSize; // Enforce usage of a packed type by ensuring that the passed type has the required typedefs
  83. if(!mpt::IO::FileReader::Read(f, srcSize))
  84. {
  85. return false;
  86. }
  87. return FileReaderExt::ReadString<mode>(f, destBuffer, std::min(static_cast<typename TFileCursor::pos_type>(srcSize), maxLength));
  88. }
  89. // Read a string with a preprended length field of type Tsize (must be a packed<*,*> type) into a std::string dest using a given read mode.
  90. // The file cursor is advanced by the string length.
  91. // Returns true if the size field could be read and at least one character could be read or 0 characters were requested.
  92. template<typename Tsize, mpt::String::ReadWriteMode mode, typename TFileCursor>
  93. bool ReadSizedString(TFileCursor &f, std::string &dest, const typename TFileCursor::pos_type maxLength = std::numeric_limits<typename TFileCursor::pos_type>::max())
  94. {
  95. mpt::packed<typename Tsize::base_type, typename Tsize::endian_type> srcSize; // Enforce usage of a packed type by ensuring that the passed type has the required typedefs
  96. if(!mpt::IO::FileReader::Read(f, srcSize))
  97. {
  98. return false;
  99. }
  100. return FileReaderExt::ReadString<mode>(f, dest, std::min(static_cast<typename TFileCursor::pos_type>(srcSize), maxLength));
  101. }
  102. // Read a string with a preprended length field of type Tsize (must be a packed<*,*> type) into a mpt::charbuf dest using a given read mode.
  103. // The file cursor is advanced by the string length.
  104. // Returns true if the size field could be read and at least one character could be read or 0 characters were requested.
  105. template<typename Tsize, mpt::String::ReadWriteMode mode, std::size_t len, typename TFileCursor>
  106. bool ReadSizedString(TFileCursor &f, mpt::charbuf<len> &dest, const typename TFileCursor::pos_type maxLength = std::numeric_limits<typename TFileCursor::pos_type>::max())
  107. {
  108. mpt::packed<typename Tsize::base_type, typename Tsize::endian_type> srcSize; // Enforce usage of a packed type by ensuring that the passed type has the required typedefs
  109. if(!mpt::IO::FileReader::Read(f, srcSize))
  110. {
  111. return false;
  112. }
  113. return FileReaderExt::ReadString<mode>(f, dest, std::min(static_cast<typename TFileCursor::pos_type>(srcSize), maxLength));
  114. }
  115. } // namespace FileReaderExt
  116. namespace detail {
  117. template <typename Ttraits, typename Tfilenametraits>
  118. using FileCursor = mpt::IO::FileCursor<Ttraits, Tfilenametraits>;
  119. template <typename Ttraits, typename Tfilenametraits>
  120. class FileReader
  121. : public FileCursor<Ttraits, Tfilenametraits>
  122. {
  123. private:
  124. using traits_type = Ttraits;
  125. using filename_traits_type = Tfilenametraits;
  126. public:
  127. using pos_type = typename traits_type::pos_type;
  128. using off_t = pos_type;
  129. using data_type = typename traits_type::data_type;
  130. using ref_data_type = typename traits_type::ref_data_type;
  131. using shared_data_type = typename traits_type::shared_data_type;
  132. using value_data_type = typename traits_type::value_data_type;
  133. using shared_filename_type = typename filename_traits_type::shared_filename_type;
  134. public:
  135. // Initialize invalid file reader object.
  136. FileReader()
  137. {
  138. return;
  139. }
  140. FileReader(const FileCursor<Ttraits, Tfilenametraits> &other)
  141. : FileCursor<Ttraits, Tfilenametraits>(other)
  142. {
  143. return;
  144. }
  145. FileReader(FileCursor<Ttraits, Tfilenametraits> &&other)
  146. : FileCursor<Ttraits, Tfilenametraits>(std::move(other))
  147. {
  148. return;
  149. }
  150. // Initialize file reader object with pointer to data and data length.
  151. template <typename Tbyte>
  152. explicit FileReader(mpt::span<Tbyte> bytedata, shared_filename_type filename = shared_filename_type{})
  153. : FileCursor<Ttraits, Tfilenametraits>(bytedata, std::move(filename))
  154. {
  155. return;
  156. }
  157. // Initialize file reader object based on an existing file reader object window.
  158. explicit FileReader(value_data_type other, shared_filename_type filename = shared_filename_type{})
  159. : FileCursor<Ttraits, Tfilenametraits>(std::move(other), std::move(filename))
  160. {
  161. return;
  162. }
  163. public:
  164. template <typename T>
  165. bool Read(T &target)
  166. {
  167. return mpt::IO::FileReader::Read(*this, target);
  168. }
  169. template <typename T>
  170. T ReadIntLE()
  171. {
  172. return mpt::IO::FileReader::ReadIntLE<T>(*this);
  173. }
  174. template <typename T>
  175. T ReadIntBE()
  176. {
  177. return mpt::IO::FileReader::ReadIntLE<T>(*this);
  178. }
  179. template <typename T>
  180. T ReadTruncatedIntLE(pos_type size)
  181. {
  182. return mpt::IO::FileReader::ReadTruncatedIntLE<T>(*this, size);
  183. }
  184. template <typename T>
  185. T ReadSizedIntLE(pos_type size)
  186. {
  187. return mpt::IO::FileReader::ReadSizedIntLE<T>(*this, size);
  188. }
  189. uint32 ReadUint32LE()
  190. {
  191. return mpt::IO::FileReader::ReadUint32LE(*this);
  192. }
  193. uint32 ReadUint32BE()
  194. {
  195. return mpt::IO::FileReader::ReadUint32BE(*this);
  196. }
  197. int32 ReadInt32LE()
  198. {
  199. return mpt::IO::FileReader::ReadInt32LE(*this);
  200. }
  201. int32 ReadInt32BE()
  202. {
  203. return mpt::IO::FileReader::ReadInt32BE(*this);
  204. }
  205. uint32 ReadUint24LE()
  206. {
  207. return mpt::IO::FileReader::ReadUint24LE(*this);
  208. }
  209. uint32 ReadUint24BE()
  210. {
  211. return mpt::IO::FileReader::ReadUint24BE(*this);
  212. }
  213. uint16 ReadUint16LE()
  214. {
  215. return mpt::IO::FileReader::ReadUint16LE(*this);
  216. }
  217. uint16 ReadUint16BE()
  218. {
  219. return mpt::IO::FileReader::ReadUint16BE(*this);
  220. }
  221. int16 ReadInt16LE()
  222. {
  223. return mpt::IO::FileReader::ReadInt16LE(*this);
  224. }
  225. int16 ReadInt16BE()
  226. {
  227. return mpt::IO::FileReader::ReadInt16BE(*this);
  228. }
  229. char ReadChar()
  230. {
  231. return mpt::IO::FileReader::ReadChar(*this);
  232. }
  233. uint8 ReadUint8()
  234. {
  235. return mpt::IO::FileReader::ReadUint8(*this);
  236. }
  237. int8 ReadInt8()
  238. {
  239. return mpt::IO::FileReader::ReadInt8(*this);
  240. }
  241. float ReadFloatLE()
  242. {
  243. return mpt::IO::FileReader::ReadFloatLE(*this);
  244. }
  245. float ReadFloatBE()
  246. {
  247. return mpt::IO::FileReader::ReadFloatBE(*this);
  248. }
  249. double ReadDoubleLE()
  250. {
  251. return mpt::IO::FileReader::ReadDoubleLE(*this);
  252. }
  253. double ReadDoubleBE()
  254. {
  255. return mpt::IO::FileReader::ReadDoubleBE(*this);
  256. }
  257. template <typename T>
  258. bool ReadStruct(T &target)
  259. {
  260. return mpt::IO::FileReader::ReadStruct(*this, target);
  261. }
  262. template <typename T>
  263. size_t ReadStructPartial(T &target, size_t partialSize = sizeof(T))
  264. {
  265. return mpt::IO::FileReader::ReadStructPartial(*this, target, partialSize);
  266. }
  267. bool ReadNullString(std::string &dest, const pos_type maxLength = std::numeric_limits<pos_type>::max())
  268. {
  269. return mpt::IO::FileReader::ReadNullString(*this, dest, maxLength);
  270. }
  271. bool ReadLine(std::string &dest, const pos_type maxLength = std::numeric_limits<pos_type>::max())
  272. {
  273. return mpt::IO::FileReader::ReadLine(*this, dest, maxLength);
  274. }
  275. template<typename T, std::size_t destSize>
  276. bool ReadArray(T (&destArray)[destSize])
  277. {
  278. return mpt::IO::FileReader::ReadArray(*this, destArray);
  279. }
  280. template<typename T, std::size_t destSize>
  281. bool ReadArray(std::array<T, destSize> &destArray)
  282. {
  283. return mpt::IO::FileReader::ReadArray(*this, destArray);
  284. }
  285. template <typename T, std::size_t destSize>
  286. std::array<T, destSize> ReadArray()
  287. {
  288. return mpt::IO::FileReader::ReadArray<T, destSize>(*this);
  289. }
  290. template<typename T>
  291. bool ReadVector(std::vector<T> &destVector, size_t destSize)
  292. {
  293. return mpt::IO::FileReader::ReadVector(*this, destVector, destSize);
  294. }
  295. template<size_t N>
  296. bool ReadMagic(const char (&magic)[N])
  297. {
  298. return mpt::IO::FileReader::ReadMagic(*this, magic);
  299. }
  300. template<typename T>
  301. bool ReadVarInt(T &target)
  302. {
  303. return mpt::IO::FileReader::ReadVarInt(*this, target);
  304. }
  305. template <typename T>
  306. using Item = mpt::IO::FileReader::Chunk<T, FileReader>;
  307. template <typename T>
  308. using ChunkList = mpt::IO::FileReader::ChunkList<T, FileReader>;
  309. template<typename T>
  310. Item<T> ReadNextChunk(off_t alignment)
  311. {
  312. return mpt::IO::FileReader::ReadNextChunk<T, FileReader>(*this, alignment);
  313. }
  314. template<typename T>
  315. ChunkList<T> ReadChunks(off_t alignment)
  316. {
  317. return mpt::IO::FileReader::ReadChunks<T, FileReader>(*this, alignment);
  318. }
  319. template<typename T>
  320. ChunkList<T> ReadChunksUntil(off_t alignment, decltype(T().GetID()) stopAtID)
  321. {
  322. return mpt::IO::FileReader::ReadChunksUntil<T, FileReader>(*this, alignment, stopAtID);
  323. }
  324. template<mpt::String::ReadWriteMode mode, size_t destSize>
  325. bool ReadString(char (&destBuffer)[destSize], const pos_type srcSize)
  326. {
  327. return FileReaderExt::ReadString<mode>(*this, destBuffer, srcSize);
  328. }
  329. template<mpt::String::ReadWriteMode mode>
  330. bool ReadString(std::string &dest, const pos_type srcSize)
  331. {
  332. return FileReaderExt::ReadString<mode>(*this, dest, srcSize);
  333. }
  334. template<mpt::String::ReadWriteMode mode, std::size_t len>
  335. bool ReadString(mpt::charbuf<len> &dest, const pos_type srcSize)
  336. {
  337. return FileReaderExt::ReadString<mode>(*this, dest, srcSize);
  338. }
  339. template<mpt::String::ReadWriteMode mode>
  340. bool ReadString(mpt::ustring &dest, mpt::Charset charset, const pos_type srcSize)
  341. {
  342. return FileReaderExt::ReadString<mode>(*this, dest, charset, srcSize);
  343. }
  344. template<typename Tsize, mpt::String::ReadWriteMode mode, size_t destSize>
  345. bool ReadSizedString(char (&destBuffer)[destSize], const pos_type maxLength = std::numeric_limits<pos_type>::max())
  346. {
  347. return FileReaderExt::ReadSizedString<Tsize, mode>(*this, destBuffer, maxLength);
  348. }
  349. template<typename Tsize, mpt::String::ReadWriteMode mode>
  350. bool ReadSizedString(std::string &dest, const pos_type maxLength = std::numeric_limits<pos_type>::max())
  351. {
  352. return FileReaderExt::ReadSizedString<Tsize, mode>(*this, dest, maxLength);
  353. }
  354. template<typename Tsize, mpt::String::ReadWriteMode mode, std::size_t len>
  355. bool ReadSizedString(mpt::charbuf<len> &dest, const pos_type maxLength = std::numeric_limits<pos_type>::max())
  356. {
  357. return FileReaderExt::ReadSizedString<Tsize, mode, len>(*this, dest, maxLength);
  358. }
  359. };
  360. } // namespace detail
  361. using FileCursor = detail::FileCursor<mpt::IO::FileCursorTraitsFileData, mpt::IO::FileCursorFilenameTraits<mpt::PathString>>;
  362. using FileReader = detail::FileReader<mpt::IO::FileCursorTraitsFileData, mpt::IO::FileCursorFilenameTraits<mpt::PathString>>;
  363. using ChunkReader = FileReader;
  364. using MemoryFileCursor = detail::FileCursor<mpt::IO::FileCursorTraitsMemory, mpt::IO::FileCursorFilenameTraitsNone>;
  365. using MemoryFileReader = detail::FileReader<mpt::IO::FileCursorTraitsMemory, mpt::IO::FileCursorFilenameTraitsNone>;
  366. OPENMPT_NAMESPACE_END