CBinaryReader.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. VCMI_LIB_NAMESPACE_BEGIN
  12. class CInputStream;
  13. /**
  14. * Reads primitive binary values from a underlying stream.
  15. *
  16. * The integers which are read are supposed to be little-endian values permanently. They will be
  17. * converted to big-endian values on big-endian machines.
  18. */
  19. class DLL_LINKAGE CBinaryReader : public boost::noncopyable
  20. {
  21. public:
  22. /**
  23. * Default c-tor.
  24. */
  25. CBinaryReader();
  26. /**
  27. * C-tor.
  28. *
  29. * @param stream The base stream object which serves as the reading input.
  30. */
  31. CBinaryReader(CInputStream * stream);
  32. /**
  33. * Gets the underlying stream.
  34. *
  35. * @return the base stream.
  36. */
  37. CInputStream * getStream();
  38. /**
  39. * Sets the underlying stream.
  40. *
  41. * @param stream The base stream to set
  42. */
  43. void setStream(CInputStream * stream);
  44. /**
  45. * Reads n bytes from the stream into the data buffer.
  46. *
  47. * @param data A pointer to the destination data array.
  48. * @param size The number of bytes to read.
  49. * @return the number of bytes read actually.
  50. */
  51. si64 read(ui8 * data, si64 size);
  52. /**
  53. * Reads integer of various size. Advances the read pointer.
  54. *
  55. * @return a read integer.
  56. *
  57. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  58. */
  59. ui8 readUInt8();
  60. si8 readInt8();
  61. ui16 readUInt16();
  62. si16 readInt16();
  63. ui32 readUInt32();
  64. si32 readInt32();
  65. ui64 readUInt64();
  66. si64 readInt64();
  67. /// Reads string without any encoding conversions
  68. std::string readBaseString();
  69. inline bool readBool()
  70. {
  71. return readUInt8() != 0;
  72. }
  73. void skip(int count);
  74. private:
  75. /**
  76. * Reads any integer. Advances the read pointer by its size.
  77. *
  78. * @return read integer.
  79. *
  80. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  81. */
  82. template <typename CData>
  83. CData readInteger();
  84. /**
  85. * Gets a end of stream exception message.
  86. *
  87. * @param bytesToRead The number of bytes which should be read.
  88. * @return the exception message text
  89. */
  90. std::string getEndOfStreamExceptionMsg(long bytesToRead) const;
  91. /** The underlying base stream */
  92. CInputStream * stream;
  93. };
  94. VCMI_LIB_NAMESPACE_END