qcross/libqnono/nonogrammarker.cpp

116 lines
3.9 KiB
C++
Raw Normal View History

/***************************************************************************
* Copyright (C) 2012 Stefan Bühler <stbuehler@web.de> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "nonogrammarker.h"
#include <QDataStream>
namespace libqnono {
static const quint64 NonogramMarker_DataStream_MAGIC = Q_UINT64_C(0xe40d3ea829a828a7);
NonogramMarker::NonogramMarker() : m_size(0,0), m_data(0) {
}
NonogramMarker::NonogramMarker(QSize size)
: m_size(size) {
int n = m_size.width() * m_size.height();
m_data = new Mark[n];
for (int i = 0; i < n; ++i) m_data[i] = NONE;
}
NonogramMarker::NonogramMarker(const NonogramMarker& other)
: m_size(other.size()), m_data(0) {
int n = m_size.width() * m_size.height();
m_data = new Mark[n];
for (int i = 0; i < n; ++i) m_data[i] = other.m_data[i];
}
NonogramMarker::~NonogramMarker() {
delete [] m_data; m_data = 0;
}
NonogramMarker& NonogramMarker::operator=(const NonogramMarker& other) {
delete [] m_data; m_data = 0;
m_size = other.m_size;
int n = m_size.width() * m_size.height();
m_data = new Mark[n];
for (int i = 0; i < n; ++i) m_data[i] = other.m_data[i];
return *this;
}
bool NonogramMarker::operator==(const NonogramMarker& other) const {
if (m_size != other.m_size) return false;
int n = m_size.width() * m_size.height();
for (int i = 0; i < n; ++i) if (m_data[i] != other.m_data[i]) return false;
return true;
}
bool NonogramMarker::readFromStream(QDataStream & stream) {
quint64 magic;
stream >> magic;
if (NonogramMarker_DataStream_MAGIC != magic || QDataStream::Ok != stream.status()) {
if (QDataStream::ReadPastEnd != stream.status()) stream.setStatus(QDataStream::ReadCorruptData);
return false;
}
QSize size;
stream >> size;
if (QDataStream::Ok != stream.status()) return false;
int n = size.width() * size.height();
Mark *data = new Mark[n];
for (int i = 0; i < n; ++i) {
if (stream.atEnd()) {
stream.setStatus(QDataStream::ReadPastEnd);
delete[] data;
return false;
}
quint8 val;
stream >> val;
data[i] = static_cast<Mark>(val);
}
if (QDataStream::Ok != stream.status()) return false;
m_size = size;
delete[] m_data;
m_data = data;
return true;
}
void NonogramMarker::writeToStream(QDataStream & stream) const {
stream << NonogramMarker_DataStream_MAGIC << m_size;
int n = m_size.width() * m_size.height();
for (int i = 0; i < n; ++i) {
stream << static_cast<quint8>(m_data[i]);
}
}
QDataStream & operator<<(QDataStream & stream, const NonogramMarker & marker) {
marker.writeToStream(stream);
return stream;
}
QDataStream & operator>>(QDataStream & stream, NonogramMarker & marker) {
marker.readFromStream(stream);
return stream;
}
}