2010-06-16 12:53:56 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2008 by Oliver Groß *
|
|
|
|
* z.o.gross@gmx.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 "ccrosspackage.h"
|
2012-04-19 15:30:54 +00:00
|
|
|
#include "nonogramproblem.h"
|
2010-06-16 12:53:56 +00:00
|
|
|
#include <QDataStream>
|
|
|
|
#include <QFile>
|
|
|
|
|
2011-12-21 11:49:33 +00:00
|
|
|
namespace libqnono {
|
2010-06-16 12:53:56 +00:00
|
|
|
//public:
|
2012-04-19 15:53:46 +00:00
|
|
|
CCrossPackage::CCrossPackage() : m_headersOnly(false) {}
|
|
|
|
CCrossPackage::~CCrossPackage() {} /* destructor needs more includes than the header has */
|
2010-06-16 12:53:56 +00:00
|
|
|
|
2012-04-19 15:53:46 +00:00
|
|
|
bool CCrossPackage::doReadHeader(QDataStream & in) {
|
2010-06-16 12:53:56 +00:00
|
|
|
QString stringBuffer;
|
|
|
|
QSize sizeBuffer;
|
|
|
|
|
2012-02-28 12:12:48 +00:00
|
|
|
qDebug("reading header");
|
2010-06-16 12:53:56 +00:00
|
|
|
if (in.atEnd()) {
|
|
|
|
qCritical("invalid package file - no header");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
in >> stringBuffer;
|
|
|
|
|
|
|
|
if ((stringBuffer != "QCROSSPACKAGE")) {
|
2012-04-19 15:53:46 +00:00
|
|
|
qCritical("invalid package file - invalid header: %s", stringBuffer.toUtf8().data());
|
2010-06-16 12:53:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in.atEnd()) {
|
|
|
|
qCritical("invalid package file - no package name");
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-28 12:12:48 +00:00
|
|
|
in >> stringBuffer;
|
|
|
|
|
|
|
|
if (stringBuffer.isEmpty()) {
|
|
|
|
qCritical("invalid package file - invalid package name");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_Name = stringBuffer;
|
2010-06-16 12:53:56 +00:00
|
|
|
|
2012-04-09 11:39:44 +00:00
|
|
|
m_headersOnly = true;
|
|
|
|
|
2010-06-16 12:53:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-19 15:53:46 +00:00
|
|
|
bool CCrossPackage::doReadData(QDataStream & in) {
|
2010-06-16 12:53:56 +00:00
|
|
|
m_PictureList.clear();
|
|
|
|
|
|
|
|
qint32 pictureCount = 0;
|
|
|
|
|
|
|
|
if (in.atEnd()) {
|
|
|
|
qCritical("invalid package file - no picture count");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
in >> pictureCount;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < pictureCount && !in.atEnd(); i++) {
|
2012-04-19 15:30:54 +00:00
|
|
|
m_PictureList.append(NonogramProblem());
|
|
|
|
if (!m_PictureList.last().readFromStream(in)) {
|
|
|
|
m_PictureList.pop_back();
|
2012-02-28 12:12:48 +00:00
|
|
|
qCritical("invalid package file - invalid picture");
|
2010-06-16 12:53:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < pictureCount)
|
|
|
|
qWarning("damaged package file - invalid picture count");
|
|
|
|
|
2012-04-09 11:39:44 +00:00
|
|
|
m_headersOnly = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-19 15:30:54 +00:00
|
|
|
const NonogramProblem & CCrossPackage::getPicture(int ndx) {
|
2012-04-09 11:39:44 +00:00
|
|
|
loadPictures();
|
2012-04-19 15:30:54 +00:00
|
|
|
return m_PictureList[ndx];
|
2012-04-09 11:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCrossPackage::loadPictures() {
|
|
|
|
if (!m_headersOnly) return true;
|
2012-04-19 15:53:46 +00:00
|
|
|
|
|
|
|
QFile file(m_FileName);
|
|
|
|
qDebug("opening file: %s", m_FileName.toUtf8().data());
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2012-04-09 11:39:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-19 15:53:46 +00:00
|
|
|
|
|
|
|
QDataStream in(&file);
|
|
|
|
in.setVersion(QDataStream::Qt_4_0);
|
|
|
|
|
|
|
|
return !doReadHeader(in) || !doReadData(in);
|
2010-06-16 12:53:56 +00:00
|
|
|
}
|
|
|
|
|
2012-04-09 11:39:44 +00:00
|
|
|
void CCrossPackage::unloadPictures() {
|
|
|
|
if (!m_PictureList.empty()) {
|
|
|
|
m_PictureList.clear();
|
|
|
|
m_headersOnly = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 12:53:56 +00:00
|
|
|
bool CCrossPackage::save() {
|
2012-04-09 11:39:44 +00:00
|
|
|
Q_ASSERT(!m_headersOnly);
|
2010-06-16 12:53:56 +00:00
|
|
|
QFile file(m_FileName);
|
2012-04-19 15:53:46 +00:00
|
|
|
qDebug("opening file for writing: %s", m_FileName.toUtf8().data());
|
2010-06-16 12:53:56 +00:00
|
|
|
if (!file.open(QIODevice::WriteOnly))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QDataStream out(&file);
|
|
|
|
out.setVersion(QDataStream::Qt_4_0);
|
|
|
|
|
|
|
|
out << QString("QCROSSPACKAGE");
|
|
|
|
out << m_Name;
|
|
|
|
out << (qint32)(m_PictureList.size());
|
|
|
|
|
2012-04-19 15:30:54 +00:00
|
|
|
foreach (const NonogramProblem & i, m_PictureList)
|
|
|
|
i.writeToStream(out);
|
2010-06-16 12:53:56 +00:00
|
|
|
|
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
2012-04-09 11:39:44 +00:00
|
|
|
|
|
|
|
CCrossPackage * CCrossPackage::read(QString fileName) {
|
2012-04-19 15:53:46 +00:00
|
|
|
QFile file(fileName);
|
|
|
|
qDebug("opening file: %s", fileName.toUtf8().data());
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-09 11:39:44 +00:00
|
|
|
CCrossPackage *p = new CCrossPackage();
|
|
|
|
p->setFileName(fileName);
|
2012-04-19 15:53:46 +00:00
|
|
|
|
|
|
|
QDataStream in(&file);
|
|
|
|
in.setVersion(QDataStream::Qt_4_0);
|
|
|
|
if (!p->doReadHeader(in) || !p->doReadData(in)) {
|
2012-04-09 11:39:44 +00:00
|
|
|
delete p;
|
2012-04-19 15:53:46 +00:00
|
|
|
return 0;
|
2012-04-09 11:39:44 +00:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCrossPackage * CCrossPackage::readHeader(QString fileName) {
|
2012-04-19 15:53:46 +00:00
|
|
|
QFile file(fileName);
|
|
|
|
qDebug("opening file: %s", fileName.toUtf8().data());
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-09 11:39:44 +00:00
|
|
|
CCrossPackage *p = new CCrossPackage();
|
|
|
|
p->setFileName(fileName);
|
2012-04-19 15:53:46 +00:00
|
|
|
|
|
|
|
QDataStream in(&file);
|
|
|
|
in.setVersion(QDataStream::Qt_4_0);
|
|
|
|
if (!p->doReadHeader(in)) {
|
2012-04-09 11:39:44 +00:00
|
|
|
delete p;
|
2012-04-19 15:53:46 +00:00
|
|
|
return 0;
|
2012-04-09 11:39:44 +00:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2010-06-16 12:53:56 +00:00
|
|
|
}
|