/*************************************************************************** * 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" #include "cnonogram.h" #include #include namespace libqcross { //public: CCrossPackage::CCrossPackage() {} CCrossPackage::CCrossPackage(QString fileName) : m_FileName(fileName) {} CCrossPackage::~CCrossPackage() { foreach (CNonogram * i, m_PictureList) { delete i; } } bool CCrossPackage::open() { if (m_FileName.isEmpty()) return false; qDebug("opening file: %s", m_FileName.toAscii().data()); QFile file(m_FileName); if (!file.open(QIODevice::ReadOnly)) return false; QDataStream in(&file); in.setVersion(QDataStream::Qt_4_0); QString stringBuffer; // qint32 intBuffer; QSize sizeBuffer; // CNonogram * newPicture = NULL; // qint32 pictureCount = 0; if (in.atEnd()) { qCritical("invalid package file - no header"); file.close(); return false; } in >> stringBuffer; if ((stringBuffer != "QCROSSPACKAGE")) { qCritical("invalid package file - invalid header: %s", stringBuffer.toAscii().data()); file.close(); return false; } if (in.atEnd()) { qCritical("invalid package file - no package name"); file.close(); return false; } in >> m_Name; file.close(); return true; } bool CCrossPackage::readAll() { if (m_FileName.isEmpty()) return false; qDebug("reading file: %s", m_FileName.toAscii().data()); QFile file(m_FileName); if (!file.open(QIODevice::ReadOnly)) return false; m_PictureList.clear(); QDataStream in(&file); in.setVersion(QDataStream::Qt_4_0); QString stringBuffer; qint32 intBuffer; QSize sizeBuffer; CNonogram * newPicture = NULL; qint32 pictureCount = 0; if (in.atEnd()) { qCritical("invalid package file - no header"); file.close(); return false; } in >> stringBuffer; if ((stringBuffer != "QCROSSPACKAGE")) { qCritical("invalid package file - invalid header: %s", stringBuffer.toAscii().data()); file.close(); return false; } if (in.atEnd()) { qCritical("invalid package file - no package name"); file.close(); return false; } in >> stringBuffer; if (in.atEnd()) { qCritical("invalid package file - no picture count"); file.close(); return false; } in >> pictureCount; unsigned char data; int i; for (i = 0; i < pictureCount && !in.atEnd(); i++) { in >> stringBuffer; qDebug("reading image: %s", stringBuffer.toAscii().data()); if (in.atEnd()) { qCritical("invalid package file - no picture name"); file.close(); return false; } in >> intBuffer; qDebug("width %i", intBuffer); if (in.atEnd()) { qCritical("invalid package file - no picture width"); file.close(); return false; } sizeBuffer.setWidth(intBuffer); in >> intBuffer; qDebug("height %i", intBuffer); if (in.atEnd()) { qCritical("invalid package file - no picture height"); file.close(); return false; } sizeBuffer.setHeight(intBuffer); newPicture = new CNonogram(sizeBuffer); newPicture->setName(stringBuffer); in >> intBuffer; qDebug("timeout %i", intBuffer); if (in.atEnd()) { qCritical("invalid package file - no picture timeout"); delete newPicture; file.close(); return false; } newPicture->setTimeout(intBuffer); for (int x = 0; x < sizeBuffer.width(); x++) { int y = 0; while (y < sizeBuffer.height()) { if (in.atEnd()) { qCritical("invalid package file - data"); delete newPicture; file.close(); return false; } in.readRawData((char *)&data, 1); int b; for (b = 0; b < 8; b++) { if (y < sizeBuffer.height()) newPicture->setPixel(x, y, (bool)(data & 0x80)); data = data << 1; y++; } // out.writeRawData((char *)&data, 1); // newPicture->setPixel(x, y, (bool)(data & 0x80)); // data = data << 1; // y++; // if (!(y % 8)) // in.readRawData((char *)&data, 1); } } m_PictureList << newPicture; } if (i < pictureCount) qWarning("damaged package file - invalid picture count"); file.close(); return true; } bool CCrossPackage::save() { QFile file(m_FileName); 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()); unsigned char data = 0; foreach (CNonogram * i, m_PictureList) { out << i->name(); out << (qint32)i->width(); out << (qint32)i->height(); out << (qint32)i->timeout(); for (int x = 0; x < i->width(); x++) { data = 0x00; int y = 0; while (y < i->height()) { int b; for (b = 0; b < 8; b++) { data = data << 1; if (y < i->height() && i->pixel(x, y)) data |= 0x01; y++; } out.writeRawData((char *)&data, 1); // if (i->pixel(x, y)) // data |= 0x01; // // y++; // if (!(y % 8)) { // out.writeRawData((char *)&data, 1); // data = 0x00; // } // // data = data << 1; } // if (y % 8) // out.writeRawData((char *)&data, 1); } } file.close(); return true; } }