1 | // |
---|
2 | // BinaryReader.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/BinaryReader.cpp#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Streams |
---|
8 | // Module: BinaryReaderWriter |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/BinaryReader.h" |
---|
38 | #include "Poco/ByteOrder.h" |
---|
39 | |
---|
40 | |
---|
41 | namespace Poco { |
---|
42 | |
---|
43 | |
---|
44 | BinaryReader::BinaryReader(std::istream& istr, StreamByteOrder byteOrder): |
---|
45 | _istr(istr) |
---|
46 | { |
---|
47 | #if defined(POCO_ARCH_BIG_ENDIAN) |
---|
48 | _flipBytes = (byteOrder == LITTLE_ENDIAN_BYTE_ORDER); |
---|
49 | #else |
---|
50 | _flipBytes = (byteOrder == BIG_ENDIAN_BYTE_ORDER); |
---|
51 | #endif |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | BinaryReader::~BinaryReader() |
---|
56 | { |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | BinaryReader& BinaryReader::operator >> (bool& value) |
---|
61 | { |
---|
62 | _istr.read((char*) &value, sizeof(value)); |
---|
63 | return *this; |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | BinaryReader& BinaryReader::operator >> (char& value) |
---|
68 | { |
---|
69 | _istr.read((char*) &value, sizeof(value)); |
---|
70 | return *this; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | BinaryReader& BinaryReader::operator >> (unsigned char& value) |
---|
75 | { |
---|
76 | _istr.read((char*) &value, sizeof(value)); |
---|
77 | return *this; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | BinaryReader& BinaryReader::operator >> (signed char& value) |
---|
82 | { |
---|
83 | _istr.read((char*) &value, sizeof(value)); |
---|
84 | return *this; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | BinaryReader& BinaryReader::operator >> (short& value) |
---|
89 | { |
---|
90 | _istr.read((char*) &value, sizeof(value)); |
---|
91 | if (_flipBytes) value = ByteOrder::flipBytes(value); |
---|
92 | return *this; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | BinaryReader& BinaryReader::operator >> (unsigned short& value) |
---|
97 | { |
---|
98 | _istr.read((char*) &value, sizeof(value)); |
---|
99 | if (_flipBytes) value = ByteOrder::flipBytes(value); |
---|
100 | return *this; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | BinaryReader& BinaryReader::operator >> (int& value) |
---|
105 | { |
---|
106 | _istr.read((char*) &value, sizeof(value)); |
---|
107 | if (_flipBytes) value = ByteOrder::flipBytes(value); |
---|
108 | return *this; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | BinaryReader& BinaryReader::operator >> (unsigned int& value) |
---|
113 | { |
---|
114 | _istr.read((char*) &value, sizeof(value)); |
---|
115 | if (_flipBytes) value = ByteOrder::flipBytes(value); |
---|
116 | return *this; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | BinaryReader& BinaryReader::operator >> (long& value) |
---|
121 | { |
---|
122 | _istr.read((char*) &value, sizeof(value)); |
---|
123 | #if defined(POCO_LONG_IS_64_BIT) |
---|
124 | if (_flipBytes) value = ByteOrder::flipBytes((Int64) value); |
---|
125 | #else |
---|
126 | if (_flipBytes) value = ByteOrder::flipBytes((Int32) value); |
---|
127 | #endif |
---|
128 | return *this; |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | BinaryReader& BinaryReader::operator >> (unsigned long& value) |
---|
133 | { |
---|
134 | _istr.read((char*) &value, sizeof(value)); |
---|
135 | #if defined(POCO_LONG_IS_64_BIT) |
---|
136 | if (_flipBytes) value = ByteOrder::flipBytes((UInt64) value); |
---|
137 | #else |
---|
138 | if (_flipBytes) value = ByteOrder::flipBytes((UInt32) value); |
---|
139 | #endif |
---|
140 | return *this; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | BinaryReader& BinaryReader::operator >> (float& value) |
---|
145 | { |
---|
146 | if (_flipBytes) |
---|
147 | { |
---|
148 | char* ptr = (char*) &value; |
---|
149 | ptr += sizeof(value); |
---|
150 | for (unsigned i = 0; i < sizeof(value); ++i) |
---|
151 | _istr.read(--ptr, 1); |
---|
152 | } |
---|
153 | else |
---|
154 | { |
---|
155 | _istr.read((char*) &value, sizeof(value)); |
---|
156 | } |
---|
157 | return *this; |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | BinaryReader& BinaryReader::operator >> (double& value) |
---|
162 | { |
---|
163 | if (_flipBytes) |
---|
164 | { |
---|
165 | char* ptr = (char*) &value; |
---|
166 | ptr += sizeof(value); |
---|
167 | for (unsigned i = 0; i < sizeof(value); ++i) |
---|
168 | _istr.read(--ptr, 1); |
---|
169 | } |
---|
170 | else |
---|
171 | { |
---|
172 | _istr.read((char*) &value, sizeof(value)); |
---|
173 | } |
---|
174 | return *this; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT) |
---|
179 | |
---|
180 | |
---|
181 | BinaryReader& BinaryReader::operator >> (Int64& value) |
---|
182 | { |
---|
183 | _istr.read((char*) &value, sizeof(value)); |
---|
184 | if (_flipBytes) value = ByteOrder::flipBytes(value); |
---|
185 | return *this; |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | BinaryReader& BinaryReader::operator >> (UInt64& value) |
---|
190 | { |
---|
191 | _istr.read((char*) &value, sizeof(value)); |
---|
192 | if (_flipBytes) value = ByteOrder::flipBytes(value); |
---|
193 | return *this; |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | #endif |
---|
198 | |
---|
199 | |
---|
200 | BinaryReader& BinaryReader::operator >> (std::string& value) |
---|
201 | { |
---|
202 | UInt32 size = 0; |
---|
203 | read7BitEncoded(size); |
---|
204 | value.clear(); |
---|
205 | value.reserve(size); |
---|
206 | while (size--) |
---|
207 | { |
---|
208 | char c; |
---|
209 | _istr.read(&c, 1); |
---|
210 | value += c; |
---|
211 | } |
---|
212 | return *this; |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | void BinaryReader::read7BitEncoded(UInt32& value) |
---|
217 | { |
---|
218 | char c; |
---|
219 | value = 0; |
---|
220 | int s = 0; |
---|
221 | do |
---|
222 | { |
---|
223 | c = 0; |
---|
224 | _istr.read(&c, 1); |
---|
225 | UInt32 x = (c & 0x7F); |
---|
226 | x <<= s; |
---|
227 | value += x; |
---|
228 | s += 7; |
---|
229 | } |
---|
230 | while (c & 0x80); |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | #if defined(POCO_HAVE_INT64) |
---|
235 | |
---|
236 | |
---|
237 | void BinaryReader::read7BitEncoded(UInt64& value) |
---|
238 | { |
---|
239 | char c; |
---|
240 | value = 0; |
---|
241 | int s = 0; |
---|
242 | do |
---|
243 | { |
---|
244 | c = 0; |
---|
245 | _istr.read(&c, 1); |
---|
246 | UInt64 x = (c & 0x7F); |
---|
247 | x <<= s; |
---|
248 | value += x; |
---|
249 | s += 7; |
---|
250 | } |
---|
251 | while (c & 0x80); |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | #endif |
---|
256 | |
---|
257 | |
---|
258 | void BinaryReader::readRaw(std::streamsize length, std::string& value) |
---|
259 | { |
---|
260 | value.clear(); |
---|
261 | value.reserve(length); |
---|
262 | while (length--) |
---|
263 | { |
---|
264 | char c; |
---|
265 | _istr.read(&c, 1); |
---|
266 | value += c; |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | void BinaryReader::readRaw(char* buffer, std::streamsize length) |
---|
272 | { |
---|
273 | _istr.read(buffer, length); |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | void BinaryReader::readBOM() |
---|
278 | { |
---|
279 | UInt16 bom; |
---|
280 | _istr.read((char*) &bom, sizeof(bom)); |
---|
281 | _flipBytes = bom != 0xFEFF; |
---|
282 | } |
---|
283 | |
---|
284 | |
---|
285 | } // namespace Poco |
---|