source: vscpl/src/hu/varadiistvan/scpl/io/DataStream.h@ 10:1ee8d0a23099

Last change on this file since 10:1ee8d0a23099 was 10:1ee8d0a23099, checked in by István Váradi <ivaradi@…>, 11 years ago

Imported the code of the blocking and data streams

File size: 11.3 KB
Line 
1// Copyright (c) 2013 by István Váradi
2
3// This file is part of VSCPL, a simple cross-platform utility library
4
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7
8// 1. Redistributions of source code must retain the above copyright notice, this
9// list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25// The views and conclusions contained in the software and documentation are those
26// of the authors and should not be interpreted as representing official policies,
27// either expressed or implied, of the FreeBSD Project.
28
29#ifndef HU_VARADIISTVAN_SCPL_IO_DATASTREAM_H
30#define HU_VARADIISTVAN_SCPL_IO_DATASTREAM_H
31//------------------------------------------------------------------------------
32
33#include "BlockingStream.h"
34
35#include <string>
36
37#include <cstring>
38
39#include <inttypes.h>
40
41//------------------------------------------------------------------------------
42
43namespace hu { namespace varadiistvan { namespace scpl { namespace io {
44
45//------------------------------------------------------------------------------
46
47/**
48 * A blocking stream with extra functions to read and write values of
49 * basic data types. The values are read and written in host
50 * byte-order, so this stream does not work properly between machines
51 * of different endianness.
52 *
53 * The reading functions return the value read and you can check of
54 * error or interruption only by calling the appropriate
55 * functions. The purpose of this behaviour is that the data could be
56 * read item-by-item without having to check for the stream's
57 * health all the time. Such checks are needed only, if some decision
58 * is made, based on the data. In case of an error, the read functions
59 * return some well-define default value, typically 0.
60 *
61 * The writing functions return a boolean value indicating success or
62 * failure, but writing can be handled the same way as reading from
63 * the point of view of error checking.
64 */
65class DataStream : public BlockingStream
66{
67public:
68 /**
69 * Construct the stream.
70 */
71 DataStream(BufferedStream& stream);
72
73 /**
74 * Template for simple, type-specific reading.
75 */
76 template <typename T> T readT(T defaultValue = 0);
77
78 /**
79 * Read a signed, 8-bit value
80 */
81 int8_t readS8();
82
83 /**
84 * Read an unsigned, 8-bit value
85 */
86 uint8_t readU8();
87
88 /**
89 * Read a signed, 16-bit value
90 */
91 int16_t readS16();
92
93 /**
94 * Read an unsigned, 16-bit value
95 */
96 uint16_t readU16();
97
98 /**
99 * Read a signed, 32-bit value
100 */
101 int32_t readS32();
102
103 /**
104 * Read an unsigned, 32-bit value
105 */
106 uint32_t readU32();
107
108 /**
109 * Read a signed, 64-bit value
110 */
111 int64_t readS64();
112
113 /**
114 * Read an unsigned, 64-bit value
115 */
116 uint64_t readU64();
117
118 /**
119 * Read a float value.
120 */
121 float readFloat();
122
123 /**
124 * Read a double value.
125 */
126 double readDouble();
127
128 /**
129 * Read a long double value.
130 */
131 long double readLongDouble();
132
133 /**
134 * Read a length value. This is a of at most 32 bits, represented
135 * by a variable number of bytes for space efficiency.
136 *
137 * @see writeLength()
138 */
139 size_t readLength();
140
141 /**
142 * Read a string.
143 */
144 std::string readString();
145
146 /**
147 * Read a string into the given buffer. If the string is too large
148 * for the buffer, its end will be discarded. A trailing 0 is
149 * added in cases!
150 *
151 * @return the actual length of the string.
152 */
153 size_t readString(char* dest, size_t capacity);
154
155 /**
156 * Write a signed, 8-bit value
157 */
158 bool writeS8(int8_t x);
159
160 /**
161 * Write an unsigned, 8-bit value
162 */
163 bool writeU8(uint8_t x);
164
165 /**
166 * Write a signed, 16-bit value
167 */
168 bool writeS16(int16_t x);
169
170 /**
171 * Write an unsigned, 16-bit value
172 */
173 bool writeU16(uint16_t x);
174
175 /**
176 * Write a signed, 32-bit value
177 */
178 bool writeS32(int32_t x);
179
180 /**
181 * Write an unsigned, 32-bit value
182 */
183 bool writeU32(uint32_t x);
184
185 /**
186 * Write a signed, 64-bit value
187 */
188 bool writeS64(int64_t x);
189
190 /**
191 * Write an unsigned, 64-bit value
192 */
193 bool writeU64(uint64_t x);
194
195 /**
196 * Write a float value.
197 */
198 bool writeFloat(float x);
199
200 /**
201 * Write a double value.
202 */
203 bool writeDouble(double x);
204
205 /**
206 * Write a long double value.
207 */
208 bool writeLongDouble(long double x);
209
210 /**
211 * Write a length value. This a 32-bit value written as a sequence
212 * of bytes of variable length. A bytes's lower 7 bits contain
213 * parts of the value. The MSB indicates if another byte is left.
214 */
215 bool writeLength(size_t length);
216
217 /**
218 * Write a string. First the string's length is written as a
219 * length (see writeLength()), then the characters of the string
220 * without the trailing 0.
221 */
222 bool writeString(const char* s, size_t length);
223
224 /**
225 * Write a string. First the string's length is written as a
226 * length (see writeLength()), then the characters of the string
227 * without the trailing 0.
228 */
229 bool writeString(const std::string& s);
230
231 /**
232 * Write a string. First the string's length is written as a
233 * length (see writeLength()), then the characters of the string
234 * without the trailing 0.
235 */
236 bool writeString(const char* s);
237};
238
239//------------------------------------------------------------------------------
240// Inline definitions
241//------------------------------------------------------------------------------
242
243inline DataStream::DataStream(BufferedStream& stream) :
244 BlockingStream(stream)
245{
246}
247
248//------------------------------------------------------------------------------
249
250template <typename T> T DataStream::readT(T defaultValue)
251{
252 T value = defaultValue;
253 read(&value, sizeof(value));
254 return value;
255}
256
257//------------------------------------------------------------------------------
258
259inline int8_t DataStream::readS8()
260{
261 return readT<int8_t>(0);
262}
263
264//------------------------------------------------------------------------------
265
266inline uint8_t DataStream::readU8()
267{
268 return readT<uint8_t>(0);
269}
270
271//------------------------------------------------------------------------------
272
273inline int16_t DataStream::readS16()
274{
275 return readT<int16_t>(0);
276}
277
278//------------------------------------------------------------------------------
279
280inline uint16_t DataStream::readU16()
281{
282 return readT<uint16_t>(0);
283}
284
285//------------------------------------------------------------------------------
286
287inline int32_t DataStream::readS32()
288{
289 return readT<int32_t>(0);
290}
291
292//------------------------------------------------------------------------------
293
294inline uint32_t DataStream::readU32()
295{
296 return readT<uint32_t>(0);
297}
298
299//------------------------------------------------------------------------------
300
301inline int64_t DataStream::readS64()
302{
303 return readT<int64_t>(0);
304}
305
306//------------------------------------------------------------------------------
307
308inline uint64_t DataStream::readU64()
309{
310 return readT<uint64_t>(0);
311}
312
313//------------------------------------------------------------------------------
314
315inline float DataStream::readFloat()
316{
317 return readT<float>(0.0);
318}
319
320//------------------------------------------------------------------------------
321
322inline double DataStream::readDouble()
323{
324 return readT<double>(0.0);
325}
326
327//------------------------------------------------------------------------------
328
329inline long double DataStream::readLongDouble()
330{
331 return readT<long double>(0.0);
332}
333
334//------------------------------------------------------------------------------
335
336inline bool DataStream::writeS8(int8_t x)
337{
338 return write(&x, sizeof(x));
339}
340
341//------------------------------------------------------------------------------
342
343inline bool DataStream::writeU8(uint8_t x)
344{
345 return write(&x, sizeof(x));
346}
347
348//------------------------------------------------------------------------------
349
350inline bool DataStream::writeS16(int16_t x)
351{
352 return write(&x, sizeof(x));
353}
354
355//------------------------------------------------------------------------------
356
357inline bool DataStream::writeU16(uint16_t x)
358{
359 return write(&x, sizeof(x));
360}
361
362//------------------------------------------------------------------------------
363
364inline bool DataStream::writeS32(int32_t x)
365{
366 return write(&x, sizeof(x));
367}
368
369//------------------------------------------------------------------------------
370
371inline bool DataStream::writeU32(uint32_t x)
372{
373 return write(&x, sizeof(x));
374}
375
376//------------------------------------------------------------------------------
377
378inline bool DataStream::writeS64(int64_t x)
379{
380 return write(&x, sizeof(x));
381}
382
383//------------------------------------------------------------------------------
384
385inline bool DataStream::writeU64(uint64_t x)
386{
387 return write(&x, sizeof(x));
388}
389
390//------------------------------------------------------------------------------
391
392inline bool DataStream::writeFloat(float x)
393{
394 return write(&x, sizeof(x));
395}
396
397//------------------------------------------------------------------------------
398
399inline bool DataStream::writeDouble(double x)
400{
401 return write(&x, sizeof(x));
402}
403
404//------------------------------------------------------------------------------
405
406inline bool DataStream::writeLongDouble(long double x)
407{
408 return write(&x, sizeof(x));
409}
410
411//------------------------------------------------------------------------------
412
413inline bool DataStream::writeString(const char* s, size_t length)
414{
415 if (!writeLength(length)) return false;
416 else return write(s, length);
417}
418
419//------------------------------------------------------------------------------
420
421inline bool DataStream::writeString(const std::string& s)
422{
423 return writeString(s.c_str(), s.length());
424}
425
426//------------------------------------------------------------------------------
427
428inline bool DataStream::writeString(const char* s)
429{
430 return writeString(s, strlen(s));
431}
432
433//------------------------------------------------------------------------------
434
435} /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
436
437//------------------------------------------------------------------------------
438#endif // HU_VARADIISTVAN_SCPL_IO_DATASTREAM_H
439
440// Local Variables:
441// mode: C++
442// c-basic-offset: 4
443// indent-tabs-mode: nil
444// End:
Note: See TracBrowser for help on using the repository browser.