2012-07-22 18:02:13 +03:00
|
|
|
/*
|
|
|
|
* CBinaryReader.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
2012-07-22 18:02:13 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2012-07-22 18:02:13 +03:00
|
|
|
class CInputStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads primitive binary values from a underlying stream.
|
|
|
|
*
|
|
|
|
* The integers which are read are supposed to be little-endian values permanently. They will be
|
|
|
|
* converted to big-endian values on big-endian machines.
|
|
|
|
*/
|
|
|
|
class DLL_LINKAGE CBinaryReader : public boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Default c-tor.
|
|
|
|
*/
|
|
|
|
CBinaryReader();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* C-tor.
|
|
|
|
*
|
|
|
|
* @param stream The base stream object which serves as the reading input.
|
|
|
|
*/
|
2012-11-06 19:39:29 +03:00
|
|
|
CBinaryReader(CInputStream * stream);
|
2012-07-22 18:02:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the underlying stream.
|
|
|
|
*
|
|
|
|
* @return the base stream.
|
|
|
|
*/
|
|
|
|
CInputStream * getStream();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the underlying stream.
|
|
|
|
*
|
|
|
|
* @param stream The base stream to set
|
|
|
|
*/
|
2012-11-06 19:39:29 +03:00
|
|
|
void setStream(CInputStream * stream);
|
2012-07-22 18:02:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads n bytes from the stream into the data buffer.
|
|
|
|
*
|
|
|
|
* @param data A pointer to the destination data array.
|
|
|
|
* @param size The number of bytes to read.
|
|
|
|
* @return the number of bytes read actually.
|
|
|
|
*/
|
|
|
|
si64 read(ui8 * data, si64 size);
|
|
|
|
|
|
|
|
/**
|
2012-09-26 16:13:39 +03:00
|
|
|
* Reads integer of various size. Advances the read pointer.
|
2012-07-22 18:02:13 +03:00
|
|
|
*
|
2012-09-26 16:13:39 +03:00
|
|
|
* @return a read integer.
|
2012-07-22 18:02:13 +03:00
|
|
|
*
|
|
|
|
* @throws std::runtime_error if the end of the stream was reached unexpectedly
|
|
|
|
*/
|
|
|
|
ui8 readUInt8();
|
|
|
|
si8 readInt8();
|
|
|
|
ui16 readUInt16();
|
|
|
|
si16 readInt16();
|
|
|
|
ui32 readUInt32();
|
|
|
|
si32 readInt32();
|
|
|
|
ui64 readUInt64();
|
|
|
|
si64 readInt64();
|
|
|
|
|
2023-02-24 16:15:45 +02:00
|
|
|
/// Reads string without any encoding conversions
|
|
|
|
std::string readBaseString();
|
2013-02-07 20:02:15 +03:00
|
|
|
|
|
|
|
inline bool readBool()
|
|
|
|
{
|
|
|
|
return readUInt8() != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void skip(int count);
|
2012-07-22 18:02:13 +03:00
|
|
|
private:
|
2012-11-06 19:39:29 +03:00
|
|
|
/**
|
|
|
|
* Reads any integer. Advances the read pointer by its size.
|
|
|
|
*
|
|
|
|
* @return read integer.
|
|
|
|
*
|
|
|
|
* @throws std::runtime_error if the end of the stream was reached unexpectedly
|
|
|
|
*/
|
|
|
|
template <typename CData>
|
|
|
|
CData readInteger();
|
|
|
|
|
2012-07-22 18:02:13 +03:00
|
|
|
/**
|
|
|
|
* Gets a end of stream exception message.
|
|
|
|
*
|
|
|
|
* @param bytesToRead The number of bytes which should be read.
|
|
|
|
* @return the exception message text
|
|
|
|
*/
|
|
|
|
std::string getEndOfStreamExceptionMsg(long bytesToRead) const;
|
|
|
|
|
|
|
|
/** The underlying base stream */
|
|
|
|
CInputStream * stream;
|
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|