CBinaryReader.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * CBinaryReader.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. class CInputStream;
  12. /**
  13. * Reads primitive binary values from a underlying stream.
  14. *
  15. * The integers which are read are supposed to be little-endian values permanently. They will be
  16. * converted to big-endian values on big-endian machines.
  17. */
  18. class DLL_LINKAGE CBinaryReader : public boost::noncopyable
  19. {
  20. public:
  21. /**
  22. * Default c-tor.
  23. */
  24. CBinaryReader();
  25. /**
  26. * C-tor.
  27. *
  28. * @param stream The base stream object which serves as the reading input.
  29. */
  30. CBinaryReader(CInputStream * stream);
  31. /**
  32. * Gets the underlying stream.
  33. *
  34. * @return the base stream.
  35. */
  36. CInputStream * getStream();
  37. /**
  38. * Sets the underlying stream.
  39. *
  40. * @param stream The base stream to set
  41. */
  42. void setStream(CInputStream * stream);
  43. /**
  44. * Reads n bytes from the stream into the data buffer.
  45. *
  46. * @param data A pointer to the destination data array.
  47. * @param size The number of bytes to read.
  48. * @return the number of bytes read actually.
  49. */
  50. si64 read(ui8 * data, si64 size);
  51. /**
  52. * Reads integer of various size. Advances the read pointer.
  53. *
  54. * @return a read integer.
  55. *
  56. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  57. */
  58. ui8 readUInt8();
  59. si8 readInt8();
  60. ui16 readUInt16();
  61. si16 readInt16();
  62. ui32 readUInt32();
  63. si32 readInt32();
  64. ui64 readUInt64();
  65. si64 readInt64();
  66. private:
  67. /**
  68. * Reads any integer. Advances the read pointer by its size.
  69. *
  70. * @return read integer.
  71. *
  72. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  73. */
  74. template <typename CData>
  75. CData readInteger();
  76. /**
  77. * Gets a end of stream exception message.
  78. *
  79. * @param bytesToRead The number of bytes which should be read.
  80. * @return the exception message text
  81. */
  82. std::string getEndOfStreamExceptionMsg(long bytesToRead) const;
  83. /** The underlying base stream */
  84. CInputStream * stream;
  85. };