1 | // |
---|
2 | // BinaryReader.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/BinaryReader.h#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Streams |
---|
8 | // Module: BinaryReaderWriter |
---|
9 | // |
---|
10 | // Definition of the BinaryReader class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_BinaryReader_INCLUDED |
---|
40 | #define Foundation_BinaryReader_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include <istream> |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | |
---|
49 | |
---|
50 | class Foundation_API BinaryReader |
---|
51 | /// This class reads basic types in binary form into an input stream. |
---|
52 | /// It provides an extractor-based interface similar to istream. |
---|
53 | /// The reader also supports automatic conversion from big-endian |
---|
54 | /// (network byte order) to little-endian and vice-versa. |
---|
55 | /// Use a BinaryWriter to create a stream suitable for a BinaryReader. |
---|
56 | { |
---|
57 | public: |
---|
58 | enum StreamByteOrder |
---|
59 | { |
---|
60 | NATIVE_BYTE_ORDER = 1, /// the host's native byte-order |
---|
61 | BIG_ENDIAN_BYTE_ORDER = 2, /// big-endian (network) byte-order |
---|
62 | NETWORK_BYTE_ORDER = 2, /// big-endian (network) byte-order |
---|
63 | LITTLE_ENDIAN_BYTE_ORDER = 3, /// little-endian byte-order |
---|
64 | UNSPECIFIED_BYTE_ORDER = 4 /// unknown, byte-order will be determined by reading the byte-order mark |
---|
65 | }; |
---|
66 | |
---|
67 | BinaryReader(std::istream& istr, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER); |
---|
68 | /// Creates the BinaryReader. |
---|
69 | |
---|
70 | ~BinaryReader(); |
---|
71 | /// Destroys the BinaryReader. |
---|
72 | |
---|
73 | BinaryReader& operator >> (bool& value); |
---|
74 | BinaryReader& operator >> (char& value); |
---|
75 | BinaryReader& operator >> (unsigned char& value); |
---|
76 | BinaryReader& operator >> (signed char& value); |
---|
77 | BinaryReader& operator >> (short& value); |
---|
78 | BinaryReader& operator >> (unsigned short& value); |
---|
79 | BinaryReader& operator >> (int& value); |
---|
80 | BinaryReader& operator >> (unsigned int& value); |
---|
81 | BinaryReader& operator >> (long& value); |
---|
82 | BinaryReader& operator >> (unsigned long& value); |
---|
83 | BinaryReader& operator >> (float& value); |
---|
84 | BinaryReader& operator >> (double& value); |
---|
85 | |
---|
86 | #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT) |
---|
87 | BinaryReader& operator >> (Int64& value); |
---|
88 | BinaryReader& operator >> (UInt64& value); |
---|
89 | #endif |
---|
90 | |
---|
91 | BinaryReader& operator >> (std::string& value); |
---|
92 | |
---|
93 | void read7BitEncoded(UInt32& value); |
---|
94 | /// Reads a 32-bit unsigned integer in compressed format. |
---|
95 | /// See BinaryWriter::write7BitEncoded() for a description |
---|
96 | /// of the compression algorithm. |
---|
97 | |
---|
98 | #if defined(POCO_HAVE_INT64) |
---|
99 | void read7BitEncoded(UInt64& value); |
---|
100 | /// Reads a 64-bit unsigned integer in compressed format. |
---|
101 | /// See BinaryWriter::write7BitEncoded() for a description |
---|
102 | /// of the compression algorithm. |
---|
103 | #endif |
---|
104 | |
---|
105 | void readRaw(std::streamsize length, std::string& value); |
---|
106 | /// Reads length bytes of raw data into value. |
---|
107 | |
---|
108 | void readRaw(char* buffer, std::streamsize length); |
---|
109 | /// Reads length bytes of raw data into buffer. |
---|
110 | |
---|
111 | void readBOM(); |
---|
112 | /// Reads a byte-order mark from the stream and configures |
---|
113 | /// the reader for the encountered byte order. |
---|
114 | /// A byte-order mark is a 16-bit integer with a value of 0xFEFF, |
---|
115 | /// written in host byte order. |
---|
116 | |
---|
117 | bool good(); |
---|
118 | /// Returns _istr.good(); |
---|
119 | |
---|
120 | bool fail(); |
---|
121 | /// Returns _istr.fail(); |
---|
122 | |
---|
123 | bool bad(); |
---|
124 | /// Returns _istr.bad(); |
---|
125 | |
---|
126 | bool eof(); |
---|
127 | /// Returns _istr.eof(); |
---|
128 | |
---|
129 | std::istream& stream() const; |
---|
130 | /// Returns the underlying stream. |
---|
131 | |
---|
132 | StreamByteOrder byteOrder() const; |
---|
133 | /// Returns the byte-order used by the reader, which is |
---|
134 | /// either BIG_ENDIAN_BYTE_ORDER or LITTLE_ENDIAN_BYTE_ORDER. |
---|
135 | |
---|
136 | private: |
---|
137 | std::istream& _istr; |
---|
138 | bool _flipBytes; |
---|
139 | }; |
---|
140 | |
---|
141 | |
---|
142 | // |
---|
143 | // inlines |
---|
144 | // |
---|
145 | |
---|
146 | |
---|
147 | inline bool BinaryReader::good() |
---|
148 | { |
---|
149 | return _istr.good(); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | inline bool BinaryReader::fail() |
---|
154 | { |
---|
155 | return _istr.fail(); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | inline bool BinaryReader::bad() |
---|
160 | { |
---|
161 | return _istr.bad(); |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | inline bool BinaryReader::eof() |
---|
166 | { |
---|
167 | return _istr.eof(); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | inline std::istream& BinaryReader::stream() const |
---|
172 | { |
---|
173 | return _istr; |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | inline BinaryReader::StreamByteOrder BinaryReader::byteOrder() const |
---|
178 | { |
---|
179 | #if defined(POCO_ARCH_BIG_ENDIAN) |
---|
180 | return _flipBytes ? LITTLE_ENDIAN_BYTE_ORDER : BIG_ENDIAN_BYTE_ORDER; |
---|
181 | #else |
---|
182 | return _flipBytes ? BIG_ENDIAN_BYTE_ORDER : LITTLE_ENDIAN_BYTE_ORDER; |
---|
183 | #endif |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | } // namespace Poco |
---|
188 | |
---|
189 | |
---|
190 | #endif // Foundation_BinaryReader_INCLUDED |
---|