1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
10 * KWorship 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 *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
21 * @file KwPascalStream.cpp
22 * @brief Provides Pascal serialization of binary data.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwPascalStream.h"
31 #include <QStringList>
34 * Constructors + destructor
37 /// Use a byte array as a buffer.
38 KwPascalStream::KwPascalStream(QByteArray
* a
, QIODevice::OpenMode mode
)
44 /// Use a constant byte array as a buffer.
45 KwPascalStream::KwPascalStream(const QByteArray
* a
)
46 : m_d(new QBuffer((QByteArray
*)a
))
48 m_d
->open(QIODevice::ReadOnly
);
52 KwPascalStream::~KwPascalStream()
61 /// Find whether we're at the end of the file.
62 bool KwPascalStream::atEnd() const
67 /// Read a single byte.
68 uint8_t KwPascalStream::read8()
71 m_d
->read((char*)&result
, 1);
76 uint16_t KwPascalStream::read16()
79 m_d
->read((char*)&result
, 2);
80 return qFromLittleEndian(result
);
84 uint32_t KwPascalStream::read32()
87 m_d
->read((char*)&result
, 4);
88 return qFromLittleEndian(result
);
91 /// Read short string.
92 QByteArray
KwPascalStream::readString()
94 uint8_t len
= read8();
95 return m_d
->read(len
);
99 QByteArray
KwPascalStream::readLString()
101 uint32_t len
= read32();
102 return m_d
->read(len
);
105 /// Read wide string.
106 QString
KwPascalStream::readWString()
108 uint32_t len
= read32();
109 QByteArray data
= m_d
->read(len
>>1);
110 return QString::fromUtf8(data
, data
.size());
113 /// Read UTF8 string.
114 QString
KwPascalStream::readUtf8String()
116 return QString::fromUtf8(readLString());
123 /// Write a single byte.
124 void KwPascalStream::write8(uint8_t byte
)
126 m_d
->write((char*)&byte
,1);
130 void KwPascalStream::write16(uint16_t word
)
132 word
= qToLittleEndian(word
);
133 m_d
->write((char*)&word
, 2);
137 void KwPascalStream::write32(uint32_t dword
)
139 dword
= qToLittleEndian(dword
);
140 m_d
->write((char*)&dword
, 4);
143 /// Write short string (length < 2^8).
144 void KwPascalStream::writeString(const QByteArray
& str
)
146 int len
= str
.size();
152 m_d
->write(str
, len
);
155 /// Write long string.
156 void KwPascalStream::writeLString(const QByteArray
& str
)
158 unsigned int len
= str
.size();
160 m_d
->write(str
, len
);
163 /// Write UTF8 string.
164 void KwPascalStream::writeUtf8String(const QString
& str
)
166 writeLString(str
.toUtf8());
173 /// Write a property.
174 void KwPascalStream::writeProperty(const QByteArray
& name
, const QVariant
& value
)
181 void KwPascalStream::readProperty(QByteArray
& name
, QVariant
& value
)
188 void KwPascalStream::writeStartList()
194 void KwPascalStream::writeEndList()
199 /// Write a string in an appropriate format.
200 KwPascalStream
& KwPascalStream::operator << (const QString
& str
)
202 if (str
.size() == str
.toUtf8().size())
204 if (str
.size() < 0x100)
207 writeString(str
.toLatin1());
212 writeLString(str
.toLatin1());
217 write8(vaUTF8String
);
218 writeUtf8String(str
);
224 KwPascalStream
& KwPascalStream::operator << (const QVariant
& var
)
229 *this << var
.toList();
231 case QVariant::StringList
:
232 *this << var
.toStringList();
234 case QVariant::String
:
235 *this << var
.toString();
238 Q_ASSERT(!"Unimplemented variant type");
245 KwPascalStream
& KwPascalStream::operator >> (QVariant
& var
)
247 // Read the type byte
248 int8_t type
= read8();
271 var
= (int8_t)read8();
274 var
= (int16_t)read16();
277 var
= (int32_t)read32();
280 /// @todo Implement missing readers
284 var
= QString::fromLatin1(readString());
287 /// @todo Implement missing readers
298 /// @todo Implement missing readers
302 var
= QString::fromLatin1(readLString());
309 /// @todo Implement missing readers
316 /// @todo Implement missing readers
320 var
= readUtf8String();