source: xplcommon/src/xplcommon/DataStream.h@ 31:bbd688924703

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

Implemented the data stream

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