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

View File

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