CBinaryReader.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 a unsigned 8 bit integer. Advances the read pointer by one byte.
  62. *
  63. * @return a unsigned 8 bit integer.
  64. *
  65. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  66. */
  67. ui8 readUInt8();
  68. /**
  69. * Reads a signed 8 bit integer. Advances the read pointer by one byte.
  70. *
  71. * @return a signed 8 bit integer.
  72. *
  73. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  74. */
  75. si8 readInt8();
  76. /**
  77. * Reads a unsigned 16 bit integer. Advances the read pointer by two bytes.
  78. *
  79. * @return a unsigned 16 bit integer.
  80. *
  81. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  82. */
  83. ui16 readUInt16();
  84. /**
  85. * Reads a signed 16 bit integer. Advances the read pointer by two bytes.
  86. *
  87. * @return a signed 16 bit integer.
  88. *
  89. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  90. */
  91. si16 readInt16();
  92. /**
  93. * Reads a unsigned 32 bit integer. Advances the read pointer by four bytes.
  94. *
  95. * @return a unsigned 32 bit integer.
  96. *
  97. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  98. */
  99. ui32 readUInt32();
  100. /**
  101. * Reads a signed 32 bit integer. Advances the read pointer by four bytes.
  102. *
  103. * @return a signed 32 bit integer.
  104. *
  105. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  106. */
  107. si32 readInt32();
  108. /**
  109. * Reads a unsigned 64 bit integer. Advances the read pointer by eight bytes.
  110. *
  111. * @return a unsigned 64 bit integer.
  112. *
  113. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  114. */
  115. ui64 readUInt64();
  116. /**
  117. * Reads a signed 64 bit integer. Advances the read pointer by eight bytes.
  118. *
  119. * @return a signed 64 bit integer.
  120. *
  121. * @throws std::runtime_error if the end of the stream was reached unexpectedly
  122. */
  123. si64 readInt64();
  124. private:
  125. /**
  126. * Gets a end of stream exception message.
  127. *
  128. * @param bytesToRead The number of bytes which should be read.
  129. * @return the exception message text
  130. */
  131. std::string getEndOfStreamExceptionMsg(long bytesToRead) const;
  132. /** The underlying base stream */
  133. CInputStream * stream;
  134. };