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. /**
  21. * Reads any integer. Advances the read pointer by its size.
  22. *
  23. * @return read integer.
  24. *
  25. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  26. */
  27. template <typename CData>
  28. CData readInteger();
  29. public:
  30. /**
  31. * Default c-tor.
  32. */
  33. CBinaryReader();
  34. /**
  35. * C-tor.
  36. *
  37. * @param stream The base stream object which serves as the reading input.
  38. */
  39. CBinaryReader(CInputStream & stream);
  40. /**
  41. * Gets the underlying stream.
  42. *
  43. * @return the base stream.
  44. */
  45. CInputStream * getStream();
  46. /**
  47. * Sets the underlying stream.
  48. *
  49. * @param stream The base stream to set
  50. */
  51. void setStream(CInputStream & stream);
  52. /**
  53. * Reads n bytes from the stream into the data buffer.
  54. *
  55. * @param data A pointer to the destination data array.
  56. * @param size The number of bytes to read.
  57. * @return the number of bytes read actually.
  58. */
  59. si64 read(ui8 * data, si64 size);
  60. /**
  61. * Reads integer of various size. Advances the read pointer.
  62. *
  63. * @return a read integer.
  64. *
  65. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  66. */
  67. ui8 readUInt8();
  68. si8 readInt8();
  69. ui16 readUInt16();
  70. si16 readInt16();
  71. ui32 readUInt32();
  72. si32 readInt32();
  73. ui64 readUInt64();
  74. si64 readInt64();
  75. private:
  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. };