CBinaryReader.h 3.3 KB

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