1 | // |
---|
2 | // BinaryWriter.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/BinaryWriter.h#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Streams |
---|
8 | // Module: BinaryReaderWriter |
---|
9 | // |
---|
10 | // Definition of the BinaryWriter 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_BinaryWriter_INCLUDED |
---|
40 | #define Foundation_BinaryWriter_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include <ostream> |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | |
---|
49 | |
---|
50 | class Foundation_API BinaryWriter |
---|
51 | /// This class writes basic types in binary form into an output stream. |
---|
52 | /// It provides an inserter-based interface similar to ostream. |
---|
53 | /// The writer also supports automatic conversion from big-endian |
---|
54 | /// (network byte order) to little-endian and vice-versa. |
---|
55 | /// Use a BinaryReader to read from a stream created by a BinaryWriter. |
---|
56 | /// Be careful when exchanging data between systems with different |
---|
57 | /// data type sizes (e.g., 32-bit and 64-bit architectures), as the sizes |
---|
58 | /// of some of the basic types may be different. For example, writing a |
---|
59 | /// long integer on a 64-bit system and reading it on a 32-bit system |
---|
60 | /// may yield an incorrent result. Use fixed-size types (Int32, Int64, etc.) |
---|
61 | /// in such a case. |
---|
62 | { |
---|
63 | public: |
---|
64 | enum StreamByteOrder |
---|
65 | { |
---|
66 | NATIVE_BYTE_ORDER = 1, /// the host's native byte-order |
---|
67 | BIG_ENDIAN_BYTE_ORDER = 2, /// big-endian (network) byte-order |
---|
68 | NETWORK_BYTE_ORDER = 2, /// big-endian (network) byte-order |
---|
69 | LITTLE_ENDIAN_BYTE_ORDER = 3 /// little-endian byte-order |
---|
70 | }; |
---|
71 | |
---|
72 | BinaryWriter(std::ostream& ostr, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER); |
---|
73 | /// Creates the BinaryWriter. |
---|
74 | |
---|
75 | ~BinaryWriter(); |
---|
76 | /// Destroys the BinaryWriter. |
---|
77 | |
---|
78 | BinaryWriter& operator << (bool value); |
---|
79 | BinaryWriter& operator << (char value); |
---|
80 | BinaryWriter& operator << (unsigned char value); |
---|
81 | BinaryWriter& operator << (signed char value); |
---|
82 | BinaryWriter& operator << (short value); |
---|
83 | BinaryWriter& operator << (unsigned short value); |
---|
84 | BinaryWriter& operator << (int value); |
---|
85 | BinaryWriter& operator << (unsigned int value); |
---|
86 | BinaryWriter& operator << (long value); |
---|
87 | BinaryWriter& operator << (unsigned long value); |
---|
88 | BinaryWriter& operator << (float value); |
---|
89 | BinaryWriter& operator << (double value); |
---|
90 | |
---|
91 | #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT) |
---|
92 | BinaryWriter& operator << (Int64 value); |
---|
93 | BinaryWriter& operator << (UInt64 value); |
---|
94 | #endif |
---|
95 | |
---|
96 | BinaryWriter& operator << (const std::string& value); |
---|
97 | BinaryWriter& operator << (const char* value); |
---|
98 | |
---|
99 | void write7BitEncoded(UInt32 value); |
---|
100 | /// Writes a 32-bit unsigned integer in a compressed format. |
---|
101 | /// The value is written out seven bits at a time, starting |
---|
102 | /// with the seven least-significant bits. |
---|
103 | /// The high bit of a byte indicates whether there are more bytes to be |
---|
104 | /// written after this one. |
---|
105 | /// If value will fit in seven bits, it takes only one byte of space. |
---|
106 | /// If value will not fit in seven bits, the high bit is set on the first byte and |
---|
107 | /// written out. value is then shifted by seven bits and the next byte is written. |
---|
108 | /// This process is repeated until the entire integer has been written. |
---|
109 | |
---|
110 | #if defined(POCO_HAVE_INT64) |
---|
111 | void write7BitEncoded(UInt64 value); |
---|
112 | /// Writes a 64-bit unsigned integer in a compressed format. |
---|
113 | /// The value written out seven bits at a time, starting |
---|
114 | /// with the seven least-significant bits. |
---|
115 | /// The high bit of a byte indicates whether there are more bytes to be |
---|
116 | /// written after this one. |
---|
117 | /// If value will fit in seven bits, it takes only one byte of space. |
---|
118 | /// If value will not fit in seven bits, the high bit is set on the first byte and |
---|
119 | /// written out. value is then shifted by seven bits and the next byte is written. |
---|
120 | /// This process is repeated until the entire integer has been written. |
---|
121 | #endif |
---|
122 | |
---|
123 | void writeRaw(const std::string& rawData); |
---|
124 | /// Writes the string as-is to the stream. |
---|
125 | |
---|
126 | void writeRaw(const char* buffer, std::streamsize length); |
---|
127 | /// Writes length raw bytes from the given buffer to the stream. |
---|
128 | |
---|
129 | void writeBOM(); |
---|
130 | /// Writes a byte-order mark to the stream. A byte order mark is |
---|
131 | /// a 16-bit integer with a value of 0xFEFF, written in host byte-order. |
---|
132 | /// A BinaryReader uses the byte-order mark to determine the byte-order |
---|
133 | /// of the stream. |
---|
134 | |
---|
135 | void flush(); |
---|
136 | /// Flushes the underlying stream. |
---|
137 | |
---|
138 | bool good(); |
---|
139 | /// Returns _ostr.good(); |
---|
140 | |
---|
141 | bool fail(); |
---|
142 | /// Returns _ostr.fail(); |
---|
143 | |
---|
144 | bool bad(); |
---|
145 | /// Returns _ostr.bad(); |
---|
146 | |
---|
147 | std::ostream& stream() const; |
---|
148 | /// Returns the underlying stream. |
---|
149 | |
---|
150 | StreamByteOrder byteOrder() const; |
---|
151 | /// Returns the byte ordering used by the writer, which is |
---|
152 | /// either BIG_ENDIAN_BYTE_ORDER or LITTLE_ENDIAN_BYTE_ORDER. |
---|
153 | |
---|
154 | private: |
---|
155 | std::ostream& _ostr; |
---|
156 | bool _flipBytes; |
---|
157 | }; |
---|
158 | |
---|
159 | |
---|
160 | // |
---|
161 | // inlines |
---|
162 | // |
---|
163 | |
---|
164 | |
---|
165 | inline std::ostream& BinaryWriter::stream() const |
---|
166 | { |
---|
167 | return _ostr; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | inline bool BinaryWriter::good() |
---|
172 | { |
---|
173 | return _ostr.good(); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | inline bool BinaryWriter::fail() |
---|
178 | { |
---|
179 | return _ostr.fail(); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | inline bool BinaryWriter::bad() |
---|
184 | { |
---|
185 | return _ostr.bad(); |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | inline BinaryWriter::StreamByteOrder BinaryWriter::byteOrder() const |
---|
190 | { |
---|
191 | #if defined(POCO_ARCH_BIG_ENDIAN) |
---|
192 | return _flipBytes ? LITTLE_ENDIAN_BYTE_ORDER : BIG_ENDIAN_BYTE_ORDER; |
---|
193 | #else |
---|
194 | return _flipBytes ? BIG_ENDIAN_BYTE_ORDER : LITTLE_ENDIAN_BYTE_ORDER; |
---|
195 | #endif |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | } // namespace Poco |
---|
200 | |
---|
201 | |
---|
202 | #endif // Foundation_BinaryWriter_INCLUDED |
---|