From 8ba3eb46791abb7ed5f1367ed1ec39293d7465d0 Mon Sep 17 00:00:00 2001 From: Arto Jonsson Date: Wed, 15 Dec 2010 17:06:26 +0200 Subject: [PATCH] Add QDataStream functions to PgnGameEntry --- projects/lib/src/pgngameentry.cpp | 56 +++++++++++++++++++++++++++++++++++++++ projects/lib/src/pgngameentry.h | 19 +++++++++++++ 2 files changed, 75 insertions(+) diff --git a/projects/lib/src/pgngameentry.cpp b/projects/lib/src/pgngameentry.cpp index ccc9e7e..55a217c 100644 --- a/projects/lib/src/pgngameentry.cpp +++ b/projects/lib/src/pgngameentry.cpp @@ -18,6 +18,7 @@ #include "pgngameentry.h" #include #include "pgnstream.h" +#include PgnStream& operator>>(PgnStream& in, PgnGameEntry& entry) @@ -26,6 +27,17 @@ PgnStream& operator>>(PgnStream& in, PgnGameEntry& entry) return in; } +QDataStream& operator>>(QDataStream& in, PgnGameEntry& entry) +{ + entry.read(in); + return in; +} + +QDataStream& operator<<(QDataStream& out, const PgnGameEntry& entry) +{ + entry.write(out); + return out; +} PgnGameEntry::PgnGameEntry(const QByteArray& variant) : m_round(0), @@ -128,6 +140,50 @@ bool PgnGameEntry::read(PgnStream& in) return foundTag; } +bool PgnGameEntry::read(QDataStream& in) +{ + // modifying this function can cause backward compatibility issues + // in other programs. + + qint32 round; + in >> round; + m_round = round; + + in >> m_pos; + in >> m_lineNumber; + in >> m_event; + in >> m_site; + in >> m_white; + in >> m_black; + in >> m_variant; + + qint32 resultType; + qint32 resultWinner; + + in >> resultType; + in >> resultWinner; + m_result = Chess::Result(Chess::Result::Type(resultType), Chess::Side::Type(resultWinner)); + + return in.status() == QDataStream::Ok; +} + +void PgnGameEntry::write(QDataStream& out) const +{ + // modifying this function can cause backward compatibility issues + // in other programs. + + out << (qint32)m_round; + out << m_pos; + out << m_lineNumber; + out << m_event; + out << m_site; + out << m_white; + out << m_black; + out << m_variant; + out << (qint32)m_result.type(); + out << (qint32)m_result.winner(); +} + qint64 PgnGameEntry::pos() const { return m_pos; diff --git a/projects/lib/src/pgngameentry.h b/projects/lib/src/pgngameentry.h index f8f6054..ddbff21 100644 --- a/projects/lib/src/pgngameentry.h +++ b/projects/lib/src/pgngameentry.h @@ -20,6 +20,7 @@ #include "board/result.h" class PgnStream; +class QDataStream; /*! @@ -47,6 +48,17 @@ class LIB_EXPORT PgnGameEntry */ bool read(PgnStream& in); + /*! + * Reads an entry from data stream. + * Returns true if successfull. + */ + bool read(QDataStream& in); + + /*! + * Writes an entry to data stream. + */ + void write(QDataStream& out) const; + /*! Returns the stream position where the game begins. */ qint64 pos() const; /*! Returns the line number where the game begins. */ @@ -84,4 +96,11 @@ class LIB_EXPORT PgnGameEntry /*! Reads a PGN game entry from a PGN stream. */ extern LIB_EXPORT PgnStream& operator>>(PgnStream& in, PgnGameEntry& entry); +/*! Reads a PGN game entry from a data stream. */ +extern LIB_EXPORT QDataStream& operator>>(QDataStream& in, PgnGameEntry& entry); + +/*! Writes a PGN game entry to a data stream. */ +extern LIB_EXPORT QDataStream& operator<<(QDataStream& out, + const PgnGameEntry& entry); + #endif // PGNGAMEENTRY_H -- 2.11.4.GIT