2
0

CBinaryReader.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. std::string readString();
  67. inline bool readBool()
  68. {
  69. return readUInt8() != 0;
  70. }
  71. void skip(int count);
  72. private:
  73. /**
  74. * Reads any integer. Advances the read pointer by its size.
  75. *
  76. * @return read integer.
  77. *
  78. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  79. */
  80. template <typename CData>
  81. CData readInteger();
  82. /**
  83. * Gets a end of stream exception message.
  84. *
  85. * @param bytesToRead The number of bytes which should be read.
  86. * @return the exception message text
  87. */
  88. std::string getEndOfStreamExceptionMsg(long bytesToRead) const;
  89. /** The underlying base stream */
  90. CInputStream * stream;
  91. };