qcrosspackage: remove QFile* member, leads to trivial destructor (fixing copy/assign)

This commit is contained in:
Stefan Bühler 2012-04-19 17:53:46 +02:00
parent e9c726b27e
commit 05bccab309
2 changed files with 39 additions and 64 deletions

View File

@ -24,68 +24,33 @@
namespace libqnono { namespace libqnono {
//public: //public:
CCrossPackage::CCrossPackage() : m_File(NULL), m_headersOnly(false) {} CCrossPackage::CCrossPackage() : m_headersOnly(false) {}
CCrossPackage::~CCrossPackage() {} /* destructor needs more includes than the header has */
CCrossPackage::~CCrossPackage() {
close();
}
bool CCrossPackage::open() {
close();
if (m_FileName.isEmpty())
return false;
qDebug("opening file: %s", m_FileName.toAscii().data());
m_File = new QFile(m_FileName);
if (!m_File->open(QIODevice::ReadOnly)) {
delete m_File;
m_File = NULL;
return false;
}
return true;
}
void CCrossPackage::close() {
if (m_File) {
m_File->close();
delete m_File;
m_File = NULL;
}
}
bool CCrossPackage::doReadHeader() {
QDataStream in(m_File);
in.setVersion(QDataStream::Qt_4_0);
bool CCrossPackage::doReadHeader(QDataStream & in) {
QString stringBuffer; QString stringBuffer;
QSize sizeBuffer; QSize sizeBuffer;
qDebug("reading header"); qDebug("reading header");
if (in.atEnd()) { if (in.atEnd()) {
qCritical("invalid package file - no header"); qCritical("invalid package file - no header");
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.toUtf8().data());
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");
close();
return false; return false;
} }
in >> stringBuffer; in >> stringBuffer;
if (stringBuffer.isEmpty()) { if (stringBuffer.isEmpty()) {
qCritical("invalid package file - invalid package name"); qCritical("invalid package file - invalid package name");
close();
return false; return false;
} }
m_Name = stringBuffer; m_Name = stringBuffer;
@ -95,20 +60,13 @@ namespace libqnono {
return true; return true;
} }
bool CCrossPackage::doReadData() { bool CCrossPackage::doReadData(QDataStream & in) {
if (!m_File)
return false;
m_PictureList.clear(); m_PictureList.clear();
QDataStream in(m_File);
in.setVersion(QDataStream::Qt_4_0);
qint32 pictureCount = 0; qint32 pictureCount = 0;
if (in.atEnd()) { if (in.atEnd()) {
qCritical("invalid package file - no picture count"); qCritical("invalid package file - no picture count");
close();
return false; return false;
} }
in >> pictureCount; in >> pictureCount;
@ -119,7 +77,6 @@ namespace libqnono {
if (!m_PictureList.last().readFromStream(in)) { if (!m_PictureList.last().readFromStream(in)) {
m_PictureList.pop_back(); m_PictureList.pop_back();
qCritical("invalid package file - invalid picture"); qCritical("invalid package file - invalid picture");
close();
return false; return false;
} }
} }
@ -139,12 +96,17 @@ namespace libqnono {
bool CCrossPackage::loadPictures() { bool CCrossPackage::loadPictures() {
if (!m_headersOnly) return true; if (!m_headersOnly) return true;
if (!open() || !doReadHeader() || !doReadData()) {
close(); QFile file(m_FileName);
qDebug("opening file: %s", m_FileName.toUtf8().data());
if (!file.open(QIODevice::ReadOnly)) {
return false; return false;
} }
close();
return true; QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_0);
return !doReadHeader(in) || !doReadData(in);
} }
void CCrossPackage::unloadPictures() { void CCrossPackage::unloadPictures() {
@ -157,6 +119,7 @@ namespace libqnono {
bool CCrossPackage::save() { bool CCrossPackage::save() {
Q_ASSERT(!m_headersOnly); Q_ASSERT(!m_headersOnly);
QFile file(m_FileName); QFile file(m_FileName);
qDebug("opening file for writing: %s", m_FileName.toUtf8().data());
if (!file.open(QIODevice::WriteOnly)) if (!file.open(QIODevice::WriteOnly))
return false; return false;
@ -175,24 +138,40 @@ namespace libqnono {
} }
CCrossPackage * CCrossPackage::read(QString fileName) { CCrossPackage * CCrossPackage::read(QString fileName) {
QFile file(fileName);
qDebug("opening file: %s", fileName.toUtf8().data());
if (!file.open(QIODevice::ReadOnly)) {
return 0;
}
CCrossPackage *p = new CCrossPackage(); CCrossPackage *p = new CCrossPackage();
p->setFileName(fileName); p->setFileName(fileName);
if (!p->open() || !p->doReadHeader() || !p->doReadData()) {
QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_0);
if (!p->doReadHeader(in) || !p->doReadData(in)) {
delete p; delete p;
return NULL; return 0;
} }
p->close();
return p; return p;
} }
CCrossPackage * CCrossPackage::readHeader(QString fileName) { CCrossPackage * CCrossPackage::readHeader(QString fileName) {
QFile file(fileName);
qDebug("opening file: %s", fileName.toUtf8().data());
if (!file.open(QIODevice::ReadOnly)) {
return 0;
}
CCrossPackage *p = new CCrossPackage(); CCrossPackage *p = new CCrossPackage();
p->setFileName(fileName); p->setFileName(fileName);
if (!p->open() || !p->doReadHeader()) {
QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_0);
if (!p->doReadHeader(in)) {
delete p; delete p;
return NULL; return 0;
} }
p->close();
return p; return p;
} }
} }

View File

@ -53,17 +53,13 @@ namespace libqnono {
static CCrossPackage * readHeader(QString fileName); static CCrossPackage * readHeader(QString fileName);
protected: protected:
bool open(); bool doReadHeader(QDataStream & in);
void close(); bool doReadData(QDataStream & in);
bool doReadHeader();
bool doReadData();
private: private:
QMonoPictureList m_PictureList; QMonoPictureList m_PictureList;
QString m_FileName; QString m_FileName;
QString m_Name; QString m_Name;
QFile * m_File;
bool m_headersOnly; /** valid headers but no pictures */ bool m_headersOnly; /** valid headers but no pictures */
}; };
} }