Reading entire file before parsing

This commit is contained in:
Rafael 2016-07-22 16:06:36 +02:00
parent 6fe55a3402
commit b4c5710724
2 changed files with 66 additions and 41 deletions

View file

@ -24,6 +24,8 @@
#include <QVariantMap> #include <QVariantMap>
#include <QJsonArray> #include <QJsonArray>
#include <QString> #include <QString>
#include <QBuffer>
#include <QDebug>
#include <QImage> #include <QImage>
#include <QFile> #include <QFile>
@ -32,12 +34,19 @@ SnapmaticPicture::SnapmaticPicture(const QString &fileName, QObject *parent) : Q
// PARSE INT INIT - DO NOT CHANGE THIS VALUES // PARSE INT INIT - DO NOT CHANGE THIS VALUES
snapmaticHeaderLength = 278; snapmaticHeaderLength = 278;
snapmaticUsefulLength = 260; snapmaticUsefulLength = 260;
snapmaticFileMaxSize = 528192;
jpegHeaderLineDifStr = 2; jpegHeaderLineDifStr = 2;
jpegPreHeaderLength = 14; jpegPreHeaderLength = 14;
jpegPicStreamLength = 524288; jpegPicStreamLength = 524288;
jsonStreamLength = 3076; jsonStreamLength = 3076;
tideStreamLength = 260; tideStreamLength = 260;
// PARSE EDITOR INIT
jpegStreamEditorBegin = 292;
jsonStreamEditorBegin = 524588;
jsonStreamEditorLength = 3072;
rawPicContent = "";
// INIT PIC // INIT PIC
cachePicture = QImage(0, 0, QImage::Format_RGB32); cachePicture = QImage(0, 0, QImage::Format_RGB32);
picExportFileName = ""; picExportFileName = "";
@ -60,12 +69,14 @@ SnapmaticPicture::SnapmaticPicture(const QString &fileName, QObject *parent) : Q
jsonPlyrsList = QStringList(); jsonPlyrsList = QStringList();
} }
bool SnapmaticPicture::readingPicture() bool SnapmaticPicture::readingPicture(bool writeEnabled)
{ {
// Start opening file // Start opening file
// lastStep is like currentStep // lastStep is like currentStep
QFile *picFile = new QFile(picFileName); QFile *picFile = new QFile(picFileName);
QIODevice *picStream;
if (!picFile->open(QFile::ReadOnly)) if (!picFile->open(QFile::ReadOnly))
{ {
lastStep = "1;/1,OpenFile," + StringParser::convertDrawStringForLog(picFileName); lastStep = "1;/1,OpenFile," + StringParser::convertDrawStringForLog(picFileName);
@ -73,117 +84,123 @@ bool SnapmaticPicture::readingPicture()
delete picFile; delete picFile;
return false; return false;
} }
rawPicContent = picFile->read(snapmaticFileMaxSize);
picStream = new QBuffer(&rawPicContent);
picStream->open(QIODevice::ReadWrite);
// Reading Snapmatic Header // Reading Snapmatic Header
if (!picFile->isReadable()) if (!picStream->isReadable())
{ {
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",1,NOHEADER"; lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",1,NOHEADER";
picFile->close(); picStream->close();
picFile->deleteLater(); picStream->deleteLater();
delete picFile; delete picFile;
return false; return false;
} }
QByteArray snapmaticHeaderLine = picFile->read(snapmaticHeaderLength); QByteArray snapmaticHeaderLine = picStream->read(snapmaticHeaderLength);
pictureStr = getSnapmaticPictureString(snapmaticHeaderLine); pictureStr = getSnapmaticPictureString(snapmaticHeaderLine);
// Reading JPEG Header Line // Reading JPEG Header Line
if (!picFile->isReadable()) if (!picStream->isReadable())
{ {
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",2,NOHEADER"; lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",2,NOHEADER";
picFile->close(); picStream->close();
picFile->deleteLater(); picStream->deleteLater();
delete picFile; delete picFile;
return false; return false;
} }
QByteArray jpegHeaderLine = picFile->read(jpegPreHeaderLength); QByteArray jpegHeaderLine = picStream->read(jpegPreHeaderLength);
// Checking for JPEG // Checking for JPEG
jpegHeaderLine.remove(0, jpegHeaderLineDifStr); jpegHeaderLine.remove(0, jpegHeaderLineDifStr);
if (jpegHeaderLine.left(4) != "JPEG") if (jpegHeaderLine.left(4) != "JPEG")
{ {
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",2,NOJPEG"; lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",2,NOJPEG";
picFile->close(); picStream->close();
picFile->deleteLater(); picStream->deleteLater();
delete picFile; delete picFile;
return false; return false;
} }
// Read JPEG Stream // Read JPEG Stream
if (!picFile->isReadable()) if (!picStream->isReadable())
{ {
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",2,NOPIC"; lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",2,NOPIC";
picFile->close(); picStream->close();
picFile->deleteLater(); picStream->deleteLater();
delete picFile; delete picFile;
return false; return false;
} }
QByteArray jpegRawContent = picFile->read(jpegPicStreamLength); QByteArray jpegRawContent = picStream->read(jpegPicStreamLength);
picOk = cachePicture.loadFromData(jpegRawContent, "JPEG"); picOk = cachePicture.loadFromData(jpegRawContent, "JPEG");
// Read JSON Stream // Read JSON Stream
if (!picFile->isReadable()) if (!picStream->isReadable())
{ {
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",3,NOJSON"; lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",3,NOJSON";
picFile->close(); picStream->close();
picFile->deleteLater(); picStream->deleteLater();
delete picFile; delete picFile;
return picOk; return picOk;
} }
else if (picFile->read(4) != "JSON") else if (picStream->read(4) != "JSON")
{ {
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",3,CTJSON"; lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",3,CTJSON";
picFile->close(); picStream->close();
picFile->deleteLater(); picStream->deleteLater();
delete picFile; delete picFile;
return picOk; return picOk;
} }
QByteArray jsonRawContent = picFile->read(jsonStreamLength); QByteArray jsonRawContent = picStream->read(jsonStreamLength);
jsonStr = getSnapmaticJSONString(jsonRawContent); jsonStr = getSnapmaticJSONString(jsonRawContent);
parseJsonContent(); // JSON parsing is own function parseJsonContent(); // JSON parsing is own function
if (!picFile->isReadable()) if (!picStream->isReadable())
{ {
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",4,NOTITL"; lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",4,NOTITL";
picFile->close(); picStream->close();
picFile->deleteLater(); picStream->deleteLater();
delete picFile; delete picFile;
return picOk; return picOk;
} }
else if (picFile->read(4) != "TITL") else if (picStream->read(4) != "TITL")
{ {
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",4,CTTITL"; lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",4,CTTITL";
picFile->close(); picStream->close();
picFile->deleteLater(); picStream->deleteLater();
delete picFile; delete picFile;
return picOk; return picOk;
} }
QByteArray titlRawContent = picFile->read(tideStreamLength); QByteArray titlRawContent = picStream->read(tideStreamLength);
titlStr = getSnapmaticTIDEString(titlRawContent); titlStr = getSnapmaticTIDEString(titlRawContent);
if (!picFile->isReadable()) if (!picStream->isReadable())
{ {
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",5,NODESC"; lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",5,NODESC";
picFile->close(); picStream->close();
picFile->deleteLater(); picStream->deleteLater();
delete picFile; delete picFile;
return picOk; return picOk;
} }
else if (picFile->read(4) != "DESC") else if (picStream->read(4) != "DESC")
{ {
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",5,CTDESC"; lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",5,CTDESC";
picFile->close(); picStream->close();
picFile->deleteLater(); picStream->deleteLater();
delete picFile; delete picFile;
return picOk; return picOk;
} }
QByteArray descRawContent = picFile->read(tideStreamLength); QByteArray descRawContent = picStream->read(tideStreamLength);
descStr = getSnapmaticTIDEString(descRawContent); descStr = getSnapmaticTIDEString(descRawContent);
parseSnapmaticExportAndSortString(); parseSnapmaticExportAndSortString();
picFile->close(); picStream->close();
picStream->deleteLater();
picFile->deleteLater(); picFile->deleteLater();
delete picStream;
delete picFile; delete picFile;
if (!writeEnabled) { rawPicContent.clear(); }
return picOk; return picOk;
} }
@ -251,12 +268,12 @@ void SnapmaticPicture::parseSnapmaticExportAndSortString()
} }
} }
bool SnapmaticPicture::readingPictureFromFile(const QString &fileName) bool SnapmaticPicture::readingPictureFromFile(const QString &fileName, bool writeEnabled)
{ {
if (fileName != "") if (fileName != "")
{ {
picFileName = fileName; picFileName = fileName;
return readingPicture(); return readingPicture(writeEnabled);
} }
else else
{ {

View file

@ -31,8 +31,8 @@ class SnapmaticPicture : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit SnapmaticPicture(const QString &fileName = "", QObject *parent = 0); explicit SnapmaticPicture(const QString &fileName = "", QObject *parent = 0);
bool readingPictureFromFile(const QString &fileName); bool readingPictureFromFile(const QString &fileName, bool writeEnabled = false);
bool readingPicture(); bool readingPicture(bool writeEnabled = false);
bool isPicOk(); bool isPicOk();
QImage getPicture(); QImage getPicture();
QString getLastStep(); QString getLastStep();
@ -70,16 +70,24 @@ private:
QString titlStr; QString titlStr;
QString descStr; QString descStr;
bool picOk; bool picOk;
bool editMode;
// PARSE INT // PARSE INT
int snapmaticHeaderLength; int snapmaticHeaderLength;
int snapmaticUsefulLength; int snapmaticUsefulLength;
int snapmaticFileMaxSize;
int jpegHeaderLineDifStr; int jpegHeaderLineDifStr;
int jpegPreHeaderLength; int jpegPreHeaderLength;
int jpegPicStreamLength; int jpegPicStreamLength;
int jsonStreamLength; int jsonStreamLength;
int tideStreamLength; int tideStreamLength;
// PARSE EDITOR
int jpegStreamEditorBegin;
int jsonStreamEditorBegin;
int jsonStreamEditorLength;
QByteArray rawPicContent;
// JSON // JSON
void parseJsonContent(); void parseJsonContent();
bool jsonOk; bool jsonOk;