Very Simple Cross-Platform Library
DataStream.h
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 
43 namespace hu { namespace varadiistvan { namespace scpl { namespace io {
44 
45 //------------------------------------------------------------------------------
46 
65 class DataStream : public BlockingStream
66 {
67 public:
72 
76  template <typename T> T readT(T defaultValue = 0);
77 
81  int8_t readS8();
82 
86  uint8_t readU8();
87 
91  int16_t readS16();
92 
96  uint16_t readU16();
97 
101  int32_t readS32();
102 
106  uint32_t readU32();
107 
111  int64_t readS64();
112 
116  uint64_t readU64();
117 
121  float readFloat();
122 
126  double readDouble();
127 
131  long double readLongDouble();
132 
139  size_t readLength();
140 
144  std::string readString();
145 
153  size_t readString(char* dest, size_t capacity);
154 
158  bool writeS8(int8_t x);
159 
163  bool writeU8(uint8_t x);
164 
168  bool writeS16(int16_t x);
169 
173  bool writeU16(uint16_t x);
174 
178  bool writeS32(int32_t x);
179 
183  bool writeU32(uint32_t x);
184 
188  bool writeS64(int64_t x);
189 
193  bool writeU64(uint64_t x);
194 
198  bool writeFloat(float x);
199 
203  bool writeDouble(double x);
204 
208  bool writeLongDouble(long double x);
209 
215  bool writeLength(size_t length);
216 
222  bool writeString(const char* s, size_t length);
223 
229  bool writeString(const std::string& s);
230 
236  bool writeString(const char* s);
237 };
238 
239 //------------------------------------------------------------------------------
240 // Inline definitions
241 //------------------------------------------------------------------------------
242 
244  BlockingStream(stream)
245 {
246 }
247 
248 //------------------------------------------------------------------------------
249 
250 template <typename T> T DataStream::readT(T defaultValue)
251 {
252  T value = defaultValue;
253  read(&value, sizeof(value));
254  return value;
255 }
256 
257 //------------------------------------------------------------------------------
258 
259 inline int8_t DataStream::readS8()
260 {
261  return readT<int8_t>(0);
262 }
263 
264 //------------------------------------------------------------------------------
265 
266 inline uint8_t DataStream::readU8()
267 {
268  return readT<uint8_t>(0);
269 }
270 
271 //------------------------------------------------------------------------------
272 
273 inline int16_t DataStream::readS16()
274 {
275  return readT<int16_t>(0);
276 }
277 
278 //------------------------------------------------------------------------------
279 
280 inline uint16_t DataStream::readU16()
281 {
282  return readT<uint16_t>(0);
283 }
284 
285 //------------------------------------------------------------------------------
286 
287 inline int32_t DataStream::readS32()
288 {
289  return readT<int32_t>(0);
290 }
291 
292 //------------------------------------------------------------------------------
293 
294 inline uint32_t DataStream::readU32()
295 {
296  return readT<uint32_t>(0);
297 }
298 
299 //------------------------------------------------------------------------------
300 
301 inline int64_t DataStream::readS64()
302 {
303  return readT<int64_t>(0);
304 }
305 
306 //------------------------------------------------------------------------------
307 
308 inline uint64_t DataStream::readU64()
309 {
310  return readT<uint64_t>(0);
311 }
312 
313 //------------------------------------------------------------------------------
314 
315 inline float DataStream::readFloat()
316 {
317  return readT<float>(0.0);
318 }
319 
320 //------------------------------------------------------------------------------
321 
322 inline double DataStream::readDouble()
323 {
324  return readT<double>(0.0);
325 }
326 
327 //------------------------------------------------------------------------------
328 
329 inline long double DataStream::readLongDouble()
330 {
331  return readT<long double>(0.0);
332 }
333 
334 //------------------------------------------------------------------------------
335 
336 inline bool DataStream::writeS8(int8_t x)
337 {
338  return write(&x, sizeof(x));
339 }
340 
341 //------------------------------------------------------------------------------
342 
343 inline bool DataStream::writeU8(uint8_t x)
344 {
345  return write(&x, sizeof(x));
346 }
347 
348 //------------------------------------------------------------------------------
349 
350 inline bool DataStream::writeS16(int16_t x)
351 {
352  return write(&x, sizeof(x));
353 }
354 
355 //------------------------------------------------------------------------------
356 
357 inline bool DataStream::writeU16(uint16_t x)
358 {
359  return write(&x, sizeof(x));
360 }
361 
362 //------------------------------------------------------------------------------
363 
364 inline bool DataStream::writeS32(int32_t x)
365 {
366  return write(&x, sizeof(x));
367 }
368 
369 //------------------------------------------------------------------------------
370 
371 inline bool DataStream::writeU32(uint32_t x)
372 {
373  return write(&x, sizeof(x));
374 }
375 
376 //------------------------------------------------------------------------------
377 
378 inline bool DataStream::writeS64(int64_t x)
379 {
380  return write(&x, sizeof(x));
381 }
382 
383 //------------------------------------------------------------------------------
384 
385 inline bool DataStream::writeU64(uint64_t x)
386 {
387  return write(&x, sizeof(x));
388 }
389 
390 //------------------------------------------------------------------------------
391 
392 inline bool DataStream::writeFloat(float x)
393 {
394  return write(&x, sizeof(x));
395 }
396 
397 //------------------------------------------------------------------------------
398 
399 inline bool DataStream::writeDouble(double x)
400 {
401  return write(&x, sizeof(x));
402 }
403 
404 //------------------------------------------------------------------------------
405 
406 inline bool DataStream::writeLongDouble(long double x)
407 {
408  return write(&x, sizeof(x));
409 }
410 
411 //------------------------------------------------------------------------------
412 
413 inline 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 
421 inline bool DataStream::writeString(const std::string& s)
422 {
423  return writeString(s.c_str(), s.length());
424 }
425 
426 //------------------------------------------------------------------------------
427 
428 inline 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:
bool write(const void *src, size_t length)
bool read(void *dest, size_t length)
DataStream(BufferedStream &stream)
Definition: DataStream.h:243
bool writeString(const char *s, size_t length)
Definition: DataStream.h:413