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 #include <QIODevice>
28 
29 #define RETTYPE(type) \
30  type tmp; \
31  (*d->s) >> tmp; \
32  return tmp;
33 
34 DClass<DataStreamOperatorWrapper>
35 {
36 public:
37  QDataStream *s;
38 };
39 
40 DPointered(DataStreamOperatorWrapper)
41 
43 {
44  assert(stream != nullptr);
45  d->s = stream;
46 }
47 
48 DataStreamOperatorWrapper::~DataStreamOperatorWrapper()
49 {
50 }
51 
53 {
54  return d->s;
55 }
56 
57 const QDataStream *DataStreamOperatorWrapper::dataStream() const
58 {
59  return d->s;
60 }
61 
62 qint8 DataStreamOperatorWrapper::readQInt8()
63 {
64  RETTYPE(qint8);
65 }
66 
67 bool DataStreamOperatorWrapper::readBool()
68 {
69  RETTYPE(bool);
70 }
71 
72 quint8 DataStreamOperatorWrapper::readQUInt8()
73 {
74  RETTYPE(quint8);
75 }
76 
77 quint16 DataStreamOperatorWrapper::readQUInt16()
78 {
79  RETTYPE(quint16);
80 }
81 
82 qint16 DataStreamOperatorWrapper::readQInt16()
83 {
84  RETTYPE(qint16);
85 }
86 
87 quint32 DataStreamOperatorWrapper::readQUInt32()
88 {
89  RETTYPE(quint32);
90 }
91 
92 qint32 DataStreamOperatorWrapper::readQInt32()
93 {
94  RETTYPE(qint32);
95 }
96 
97 quint64 DataStreamOperatorWrapper::readQUInt64()
98 {
99  RETTYPE(quint64);
100 }
101 
102 qint64 DataStreamOperatorWrapper::readQInt64()
103 {
104  RETTYPE(qint64);
105 }
106 
107 float DataStreamOperatorWrapper::readFloat()
108 {
109  RETTYPE(float);
110 }
111 
112 double DataStreamOperatorWrapper::readDouble()
113 {
114  RETTYPE(double);
115 }
116 
117 QByteArray DataStreamOperatorWrapper::readRaw(qint64 length)
118 {
119  return d->s->device()->read(length);
120 }
121 
123 {
124  return d->s->device()->readAll();
125 }
126 
128 {
129  return readRawMaxUntilByte(stopByte, -1);
130 }
131 
132 QByteArray DataStreamOperatorWrapper::readRawMaxUntilByte(char stopByte, qint64 length)
133 {
134  QByteArray result;
135  qint64 counter = 0;
136  while (!d->s->atEnd() && (length < 0 || counter < length))
137  {
138  quint8 rByte;
139  *d->s >> rByte;
140  result += rByte;
141  ++counter;
142 
143  if (rByte == stopByte)
144  break;
145  }
146  return result;
147 }
148 
150 {
151  return d->s->device()->size() - d->s->device()->pos();
152 }
153 
155 {
156  return d->s->skipRawData(len);
157 }