[libqnono] proper reading and writing of package files
This commit is contained in:
parent
efe7150dc7
commit
627f73c31f
@ -24,192 +24,115 @@
|
|||||||
|
|
||||||
namespace libqnono {
|
namespace libqnono {
|
||||||
//public:
|
//public:
|
||||||
CCrossPackage::CCrossPackage() {}
|
CCrossPackage::CCrossPackage() : m_File(NULL) {}
|
||||||
CCrossPackage::CCrossPackage(QString fileName) : m_FileName(fileName) {}
|
CCrossPackage::CCrossPackage(QString fileName) : m_FileName(fileName), m_File(NULL) {}
|
||||||
|
|
||||||
CCrossPackage::~CCrossPackage() {
|
CCrossPackage::~CCrossPackage() {
|
||||||
|
close();
|
||||||
|
|
||||||
foreach (CNonogram * i, m_PictureList) {
|
foreach (CNonogram * i, m_PictureList) {
|
||||||
delete i;
|
delete i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CCrossPackage::open() {
|
bool CCrossPackage::open() {
|
||||||
|
close();
|
||||||
|
|
||||||
if (m_FileName.isEmpty())
|
if (m_FileName.isEmpty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
qDebug("opening file: %s", m_FileName.toAscii().data());
|
qDebug("opening file: %s", m_FileName.toAscii().data());
|
||||||
QFile file(m_FileName);
|
m_File = new QFile(m_FileName);
|
||||||
if (!file.open(QIODevice::ReadOnly))
|
if (!m_File->open(QIODevice::ReadOnly)) {
|
||||||
|
delete m_File;
|
||||||
|
m_File = NULL;
|
||||||
return false;
|
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);
|
in.setVersion(QDataStream::Qt_4_0);
|
||||||
|
|
||||||
QString stringBuffer;
|
QString stringBuffer;
|
||||||
// qint32 intBuffer;
|
|
||||||
QSize sizeBuffer;
|
QSize sizeBuffer;
|
||||||
|
|
||||||
// CNonogram * newPicture = NULL;
|
qDebug("reading header");
|
||||||
|
|
||||||
// qint32 pictureCount = 0;
|
|
||||||
|
|
||||||
if (in.atEnd()) {
|
if (in.atEnd()) {
|
||||||
qCritical("invalid package file - no header");
|
qCritical("invalid package file - no header");
|
||||||
file.close();
|
close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
in >> stringBuffer;
|
in >> stringBuffer;
|
||||||
|
|
||||||
if ((stringBuffer != "QCROSSPACKAGE")) {
|
if ((stringBuffer != "QCROSSPACKAGE")) {
|
||||||
qCritical("invalid package file - invalid header: %s", stringBuffer.toAscii().data());
|
qCritical("invalid package file - invalid header: %s", stringBuffer.toAscii().data());
|
||||||
file.close();
|
close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (in.atEnd()) {
|
if (in.atEnd()) {
|
||||||
qCritical("invalid package file - no package name");
|
qCritical("invalid package file - no package name");
|
||||||
file.close();
|
close();
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CCrossPackage::readAll() {
|
bool CCrossPackage::readData() {
|
||||||
if (m_FileName.isEmpty())
|
if (!m_File)
|
||||||
return false;
|
|
||||||
|
|
||||||
qDebug("reading file: %s", m_FileName.toAscii().data());
|
|
||||||
QFile file(m_FileName);
|
|
||||||
if (!file.open(QIODevice::ReadOnly))
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
m_PictureList.clear();
|
m_PictureList.clear();
|
||||||
|
|
||||||
QDataStream in(&file);
|
QDataStream in(m_File);
|
||||||
in.setVersion(QDataStream::Qt_4_0);
|
in.setVersion(QDataStream::Qt_4_0);
|
||||||
|
|
||||||
QString stringBuffer;
|
|
||||||
qint32 intBuffer;
|
|
||||||
QSize sizeBuffer;
|
|
||||||
|
|
||||||
CNonogram * newPicture = NULL;
|
CNonogram * newPicture = NULL;
|
||||||
|
|
||||||
qint32 pictureCount = 0;
|
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()) {
|
if (in.atEnd()) {
|
||||||
qCritical("invalid package file - no picture count");
|
qCritical("invalid package file - no picture count");
|
||||||
file.close();
|
close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
in >> pictureCount;
|
in >> pictureCount;
|
||||||
|
|
||||||
unsigned char data;
|
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < pictureCount && !in.atEnd(); i++) {
|
for (i = 0; i < pictureCount && !in.atEnd(); i++) {
|
||||||
in >> stringBuffer;
|
newPicture = new CNonogram();
|
||||||
qDebug("reading image: %s", stringBuffer.toAscii().data());
|
if (!newPicture->readFromStream(in)) {
|
||||||
if (in.atEnd()) {
|
qCritical("invalid package file - invalid picture");
|
||||||
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;
|
delete newPicture;
|
||||||
file.close();
|
close();
|
||||||
return false;
|
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;
|
m_PictureList << newPicture;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < pictureCount)
|
if (i < pictureCount)
|
||||||
qWarning("damaged package file - invalid picture count");
|
qWarning("damaged package file - invalid picture count");
|
||||||
|
|
||||||
file.close();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,43 +148,8 @@ namespace libqnono {
|
|||||||
out << m_Name;
|
out << m_Name;
|
||||||
out << (qint32)(m_PictureList.size());
|
out << (qint32)(m_PictureList.size());
|
||||||
|
|
||||||
unsigned char data = 0;
|
foreach (CNonogram * i, m_PictureList)
|
||||||
|
i->writeToStream(out);
|
||||||
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();
|
file.close();
|
||||||
return true;
|
return true;
|
||||||
|
@ -23,6 +23,8 @@
|
|||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
|
|
||||||
|
class QFile;
|
||||||
|
|
||||||
namespace libqnono {
|
namespace libqnono {
|
||||||
class CNonogram;
|
class CNonogram;
|
||||||
|
|
||||||
@ -40,15 +42,20 @@ namespace libqnono {
|
|||||||
void setName(QString value) { m_Name = value; }
|
void setName(QString value) { m_Name = value; }
|
||||||
QString name() const { return m_Name; }
|
QString name() const { return m_Name; }
|
||||||
|
|
||||||
virtual bool open();
|
bool open();
|
||||||
virtual bool readAll();
|
void close();
|
||||||
virtual bool save();
|
|
||||||
|
bool readHeader();
|
||||||
|
bool readData();
|
||||||
|
|
||||||
|
bool save();
|
||||||
|
|
||||||
QMonoPictureList & pictures() { return m_PictureList; }
|
QMonoPictureList & pictures() { return m_PictureList; }
|
||||||
private:
|
private:
|
||||||
QMonoPictureList m_PictureList;
|
QMonoPictureList m_PictureList;
|
||||||
QString m_FileName;
|
QString m_FileName;
|
||||||
QString m_Name;
|
QString m_Name;
|
||||||
|
QFile * m_File;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,10 +46,12 @@ namespace libqnono {
|
|||||||
QStringList fileNames = workDir.entryList(QStringList(m_DataFilter), QDir::Files | QDir::NoDotAndDotDot, QDir::Name);
|
QStringList fileNames = workDir.entryList(QStringList(m_DataFilter), QDir::Files | QDir::NoDotAndDotDot, QDir::Name);
|
||||||
foreach (QString fileName, fileNames) {
|
foreach (QString fileName, fileNames) {
|
||||||
newPackage = new CCrossPackage(workDir.filePath(fileName));
|
newPackage = new CCrossPackage(workDir.filePath(fileName));
|
||||||
if (newPackage->open())
|
newPackage->open();
|
||||||
|
if (newPackage->readHeader())
|
||||||
m_PackageList << newPackage;
|
m_PackageList << newPackage;
|
||||||
else
|
else
|
||||||
delete newPackage;
|
delete newPackage;
|
||||||
|
newPackage->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
reset();
|
reset();
|
||||||
|
@ -73,8 +73,12 @@ namespace libqnono {
|
|||||||
|
|
||||||
m_Package = package;
|
m_Package = package;
|
||||||
|
|
||||||
if (m_Package && m_Package->pictures().isEmpty())
|
if (m_Package && m_Package->pictures().isEmpty()) {
|
||||||
m_Package->readAll();
|
m_Package->open();
|
||||||
|
m_Package->readHeader();
|
||||||
|
m_Package->readData();
|
||||||
|
m_Package->close();
|
||||||
|
}
|
||||||
|
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user