File: CBinaryReader.h

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (108 lines) | stat: -rw-r--r-- 2,361 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * 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
 *
 */
#pragma once

VCMI_LIB_NAMESPACE_BEGIN

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.
	 */
    CBinaryReader(CInputStream * stream);

	/**
	 * Gets the underlying stream.
	 *
	 * @return the base stream.
	 */
	CInputStream * getStream();

	/**
	 * Sets the underlying stream.
	 *
	 * @param stream The base stream to set
	 */
    void setStream(CInputStream * stream);

	/**
	 * 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);

	/**
	 * Reads integer of various size. Advances the read pointer.
	 *
	 * @return a read integer.
	 *
	 * @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();

	/// Reads string without any encoding conversions
	std::string readBaseString();

	inline bool readBool()
	{
		return readUInt8() != 0;
	}

	void skip(int count);
private:
    /**
     * 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();

	/**
	 * 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;
};

VCMI_LIB_NAMESPACE_END