1 | // |
2 | // BinaryReader.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Streams |
6 | // Module: BinaryReaderWriter |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/BinaryReader.h" |
16 | #include "Poco/TextEncoding.h" |
17 | #include "Poco/TextConverter.h" |
18 | #include <algorithm> |
19 | |
20 | |
21 | namespace Poco { |
22 | |
23 | |
24 | BinaryReader::BinaryReader(std::istream& istr, StreamByteOrder order): |
25 | _istr(istr), |
26 | _pTextConverter(0) |
27 | { |
28 | #if defined(POCO_ARCH_BIG_ENDIAN) |
29 | _flipBytes = (order == LITTLE_ENDIAN_BYTE_ORDER); |
30 | #else |
31 | _flipBytes = (order == BIG_ENDIAN_BYTE_ORDER); |
32 | #endif |
33 | } |
34 | |
35 | |
36 | BinaryReader::BinaryReader(std::istream& istr, TextEncoding& encoding, StreamByteOrder order): |
37 | _istr(istr), |
38 | _pTextConverter(new TextConverter(encoding, Poco::TextEncoding::global())) |
39 | { |
40 | #if defined(POCO_ARCH_BIG_ENDIAN) |
41 | _flipBytes = (order == LITTLE_ENDIAN_BYTE_ORDER); |
42 | #else |
43 | _flipBytes = (order == BIG_ENDIAN_BYTE_ORDER); |
44 | #endif |
45 | } |
46 | |
47 | |
48 | BinaryReader::~BinaryReader() |
49 | { |
50 | delete _pTextConverter; |
51 | } |
52 | |
53 | |
54 | BinaryReader& BinaryReader::operator >> (bool& value) |
55 | { |
56 | return read(value, false); |
57 | } |
58 | |
59 | |
60 | BinaryReader& BinaryReader::operator >> (char& value) |
61 | { |
62 | return read(value, false); |
63 | } |
64 | |
65 | |
66 | BinaryReader& BinaryReader::operator >> (unsigned char& value) |
67 | { |
68 | return read(value, false); |
69 | } |
70 | |
71 | |
72 | BinaryReader& BinaryReader::operator >> (signed char& value) |
73 | { |
74 | return read(value, false); |
75 | } |
76 | |
77 | |
78 | BinaryReader& BinaryReader::operator >> (short& value) |
79 | { |
80 | return read(value, _flipBytes); |
81 | } |
82 | |
83 | |
84 | BinaryReader& BinaryReader::operator >> (unsigned short& value) |
85 | { |
86 | return read(value, _flipBytes); |
87 | } |
88 | |
89 | |
90 | BinaryReader& BinaryReader::operator >> (int& value) |
91 | { |
92 | return read(value, _flipBytes); |
93 | } |
94 | |
95 | |
96 | BinaryReader& BinaryReader::operator >> (unsigned int& value) |
97 | { |
98 | return read(value, _flipBytes); |
99 | } |
100 | |
101 | |
102 | #ifndef POCO_LONG_IS_64_BIT |
103 | |
104 | |
105 | BinaryReader& BinaryReader::operator >> (long& value) |
106 | { |
107 | #if defined(POCO_LONG_IS_64_BIT) |
108 | return read((Int64&) value, _flipBytes); |
109 | #else |
110 | return read((Int32&) value, _flipBytes); |
111 | #endif |
112 | } |
113 | |
114 | |
115 | BinaryReader& BinaryReader::operator >> (unsigned long& value) |
116 | { |
117 | #if defined(POCO_LONG_IS_64_BIT) |
118 | return read((UInt64&) value, _flipBytes); |
119 | #else |
120 | return read((UInt32&) value, _flipBytes); |
121 | #endif |
122 | } |
123 | |
124 | |
125 | #endif // POCO_LONG_IS_64_BIT |
126 | |
127 | |
128 | BinaryReader& BinaryReader::operator >> (float& value) |
129 | { |
130 | return read(value, _flipBytes); |
131 | } |
132 | |
133 | |
134 | BinaryReader& BinaryReader::operator >> (double& value) |
135 | { |
136 | return read(value, _flipBytes); |
137 | } |
138 | |
139 | |
140 | BinaryReader& BinaryReader::operator >> (Int64& value) |
141 | { |
142 | return read(value, _flipBytes); |
143 | } |
144 | |
145 | |
146 | BinaryReader& BinaryReader::operator >> (UInt64& value) |
147 | { |
148 | return read(value, _flipBytes); |
149 | } |
150 | |
151 | |
152 | BinaryReader& BinaryReader:: (std::string& value) |
153 | { |
154 | if (!_istr.good()) return *this; |
155 | |
156 | UInt32 size = 0; |
157 | read7BitEncoded(size); |
158 | readRaw(size, value); |
159 | if (_pTextConverter) |
160 | { |
161 | std::string converted; |
162 | _pTextConverter->convert(value, converted); |
163 | std::swap(value, converted); |
164 | } |
165 | return *this; |
166 | } |
167 | |
168 | |
169 | void BinaryReader::read7BitEncoded(UInt32& value) |
170 | { |
171 | read7BitEncoded<UInt32>(value); |
172 | } |
173 | |
174 | |
175 | #if defined(POCO_HAVE_INT64) |
176 | |
177 | |
178 | void BinaryReader::read7BitEncoded(UInt64& value) |
179 | { |
180 | read7BitEncoded<UInt64>(value); |
181 | } |
182 | |
183 | |
184 | #endif |
185 | |
186 | |
187 | void BinaryReader::(std::streamsize length, std::string& value) |
188 | { |
189 | value.clear(); |
190 | value.reserve(static_cast<std::string::size_type>(length)); |
191 | while (length--) |
192 | { |
193 | char c; |
194 | if (!_istr.read(&c, 1).good()) break; |
195 | value += c; |
196 | } |
197 | } |
198 | |
199 | |
200 | void BinaryReader::readRaw(char* buffer, std::streamsize length) |
201 | { |
202 | _istr.read(buffer, length); |
203 | } |
204 | |
205 | |
206 | void BinaryReader::readBOM() |
207 | { |
208 | UInt16 bom; |
209 | _istr.read((char*) &bom, sizeof(bom)); |
210 | _flipBytes = bom != 0xFEFF; |
211 | } |
212 | |
213 | |
214 | } // namespace Poco |
215 | |