[libqnono] proper reading and writing of package files

This commit is contained in:
Oliver Groß 2012-02-28 13:12:48 +01:00
parent efe7150dc7
commit 627f73c31f
4 changed files with 65 additions and 164 deletions

View File

@ -24,192 +24,115 @@
namespace libqnono {
//public:
CCrossPackage::CCrossPackage() {}
CCrossPackage::CCrossPackage(QString fileName) : m_FileName(fileName) {}
CCrossPackage::CCrossPackage() : m_File(NULL) {}
CCrossPackage::CCrossPackage(QString fileName) : m_FileName(fileName), m_File(NULL) {}
CCrossPackage::~CCrossPackage() {
close();
foreach (CNonogram * i, m_PictureList) {
delete i;
}
}
bool CCrossPackage::open() {
close();
if (m_FileName.isEmpty())
return false;
qDebug("opening file: %s", m_FileName.toAscii().data());
QFile file(m_FileName);
if (!file.open(QIODevice::ReadOnly))
m_File = new QFile(m_FileName);
if (!m_File->open(QIODevice::ReadOnly)) {
delete m_File;
m_File = NULL;
return false;
}
QDataStream in(&file);
return true;
}
void CCrossPackage::close() {
if (m_File) {
m_File->close();
delete m_File;
m_File = NULL;
}
}
bool CCrossPackage::readHeader() {
QDataStream in(m_File);
in.setVersion(QDataStream::Qt_4_0);
QString stringBuffer;
// qint32 intBuffer;
QSize sizeBuffer;
// CNonogram * newPicture = NULL;
// qint32 pictureCount = 0;
qDebug("reading header");
if (in.atEnd()) {
qCritical("invalid package file - no header");
file.close();
close();
return false;
}
in >> stringBuffer;
if ((stringBuffer != "QCROSSPACKAGE")) {
qCritical("invalid package file - invalid header: %s", stringBuffer.toAscii().data());
file.close();
close();
return false;
}
if (in.atEnd()) {
qCritical("invalid package file - no package name");
file.close();
close();
return false;
}
in >> m_Name;
in >> stringBuffer;
if (stringBuffer.isEmpty()) {
qCritical("invalid package file - invalid package name");
close();
return false;
}
m_Name = stringBuffer;
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))
bool CCrossPackage::readData() {
if (!m_File)
return false;
m_PictureList.clear();
QDataStream in(&file);
QDataStream in(m_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();
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");
newPicture = new CNonogram();
if (!newPicture->readFromStream(in)) {
qCritical("invalid package file - invalid picture");
delete newPicture;
file.close();
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;
}
@ -225,43 +148,8 @@ namespace libqnono {
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);
}
}
foreach (CNonogram * i, m_PictureList)
i->writeToStream(out);
file.close();
return true;

View File

@ -23,6 +23,8 @@
#include <QString>
#include <QList>
class QFile;
namespace libqnono {
class CNonogram;
@ -40,15 +42,20 @@ namespace libqnono {
void setName(QString value) { m_Name = value; }
QString name() const { return m_Name; }
virtual bool open();
virtual bool readAll();
virtual bool save();
bool open();
void close();
bool readHeader();
bool readData();
bool save();
QMonoPictureList & pictures() { return m_PictureList; }
private:
QMonoPictureList m_PictureList;
QString m_FileName;
QString m_Name;
QFile * m_File;
};
}

View File

@ -46,10 +46,12 @@ namespace libqnono {
QStringList fileNames = workDir.entryList(QStringList(m_DataFilter), QDir::Files | QDir::NoDotAndDotDot, QDir::Name);
foreach (QString fileName, fileNames) {
newPackage = new CCrossPackage(workDir.filePath(fileName));
if (newPackage->open())
newPackage->open();
if (newPackage->readHeader())
m_PackageList << newPackage;
else
delete newPackage;
newPackage->close();
}
reset();

View File

@ -73,8 +73,12 @@ namespace libqnono {
m_Package = package;
if (m_Package && m_Package->pictures().isEmpty())
m_Package->readAll();
if (m_Package && m_Package->pictures().isEmpty()) {
m_Package->open();
m_Package->readHeader();
m_Package->readData();
m_Package->close();
}
reset();
}