datastreamoperatorwrapper.cpp
1 //------------------------------------------------------------------------------
2 // datastreamoperatorwrapper.h
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2012 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "datastreamoperatorwrapper.h"
24 
25 #include "strings.hpp"
26 #include <cassert>
27 
28 #define RETTYPE(type) \
29  type tmp; \
30  (*d->s) >> tmp; \
31  return tmp;
32 
33 DClass<DataStreamOperatorWrapper>
34 {
35 public:
36  QDataStream *s;
37 };
38 
39 DPointered(DataStreamOperatorWrapper)
40 
42 {
43  assert(stream != nullptr);
44  d->s = stream;
45 }
46 
47 DataStreamOperatorWrapper::~DataStreamOperatorWrapper()
48 {
49 }
50 
52 {
53  return d->s;
54 }
55 
56 const QDataStream *DataStreamOperatorWrapper::dataStream() const
57 {
58  return d->s;
59 }
60 
61 qint8 DataStreamOperatorWrapper::readQInt8()
62 {
63  RETTYPE(qint8);
64 }
65 
66 bool DataStreamOperatorWrapper::readBool()
67 {
68  RETTYPE(bool);
69 }
70 
71 quint8 DataStreamOperatorWrapper::readQUInt8()
72 {
73  RETTYPE(quint8);
74 }
75 
76 quint16 DataStreamOperatorWrapper::readQUInt16()
77 {
78  RETTYPE(quint16);
79 }
80 
81 qint16 DataStreamOperatorWrapper::readQInt16()
82 {
83  RETTYPE(qint16);
84 }
85 
86 quint32 DataStreamOperatorWrapper::readQUInt32()
87 {
88  RETTYPE(quint32);
89 }
90 
91 qint32 DataStreamOperatorWrapper::readQInt32()
92 {
93  RETTYPE(qint32);
94 }
95 
96 quint64 DataStreamOperatorWrapper::readQUInt64()
97 {
98  RETTYPE(quint64);
99 }
100 
101 qint64 DataStreamOperatorWrapper::readQInt64()
102 {
103  RETTYPE(qint64);
104 }
105 
106 float DataStreamOperatorWrapper::readFloat()
107 {
108  RETTYPE(float);
109 }
110 
111 double DataStreamOperatorWrapper::readDouble()
112 {
113  RETTYPE(double);
114 }
115 
116 QByteArray DataStreamOperatorWrapper::readRaw(qint64 length)
117 {
118  return d->s->device()->read(length);
119 }
120 
122 {
123  return d->s->device()->readAll();
124 }
125 
127 {
128  return readRawMaxUntilByte(stopByte, -1);
129 }
130 
131 QByteArray DataStreamOperatorWrapper::readRawMaxUntilByte(char stopByte, qint64 length)
132 {
133  QByteArray result;
134  qint64 counter = 0;
135  while (!d->s->atEnd() && (length < 0 || counter < length))
136  {
137  quint8 rByte;
138  *d->s >> rByte;
139  result += rByte;
140  ++counter;
141 
142  if (rByte == stopByte)
143  break;
144  }
145  return result;
146 }
147 
149 {
150  return d->s->device()->size() - d->s->device()->pos();
151 }
152 
154 {
155  return d->s->skipRawData(len);
156 }
qint64 remaining() const
Returns a remaining amount of bytes from the underlying QIODevice.
QByteArray readRawUntilByte(char stopByte)
Reads raw data from the current position of passed QDataStream until a specified byte is encountered...
Wraps around QDataStream stream operators to provide cleaner reading interface.
QDataStream * dataStream()
Gets underlying QDataStream.
int skipRawData(int len)
Reads and discards specified amount of data.
QByteArray readRaw(qint64 length)
Reads specified amount of raw bytes.
QByteArray readRawAll()
Reads all remaining raw bytes.
QByteArray readRawMaxUntilByte(char stopByte, qint64 length)
Reads up to &#39;length&#39; bytes; can stop early on stopByte.