r17982 by jghali - #11280 : saved file will not open
scribus-commit
scribus-commit at lists.scribus.net
Wed Dec 26 11:05:49 UTC 2012
Author: jghali
Date: Wed Dec 26 11:05:49 2012
New Revision: 17982
URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=17982
Log:
#11280 : saved file will not open
Modified:
trunk/Scribus/scribus/plugins/fileloader/scribus134format/scribus134format.cpp
trunk/Scribus/scribus/plugins/fileloader/scribus134format/scribus134format.h
trunk/Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.cpp
trunk/Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.h
trunk/Scribus/scribus/scgzfile.cpp
trunk/Scribus/scribus/scgzfile.h
trunk/Scribus/scribus/scxmlstreamreader.h
Modified: trunk/Scribus/scribus/plugins/fileloader/scribus134format/scribus134format.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17982&path=/trunk/Scribus/scribus/plugins/fileloader/scribus134format/scribus134format.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/fileloader/scribus134format/scribus134format.cpp (original)
+++ trunk/Scribus/scribus/plugins/fileloader/scribus134format/scribus134format.cpp Wed Dec 26 11:05:49 2012
@@ -31,13 +31,15 @@
#include "util_color.h"
#include "util_layer.h"
#include "scgzfile.h"
+
+#include <QApplication>
+#include <QByteArray>
#include <QCursor>
// #include <QDebug>
#include <QFileInfo>
+#include <QIODevice>
#include <QList>
-#include <QByteArray>
-#include <QApplication>
-
+#include <QScopedPointer>
// See scplugin.h and pluginmanager.{cpp,h} for detail on what these methods
// do. That documentatation is not duplicated here.
@@ -138,38 +140,31 @@
return false;
}
-QString Scribus134Format::readSLA(const QString & fileName)
-{
- QByteArray docBytes("");
+QIODevice* Scribus134Format::slaReader(const QString & fileName)
+{
+ if (!fileSupported(0, fileName))
+ return NULL;
+
+ QIODevice* ioDevice = 0;
if(fileName.right(2) == "gz")
{
- if (!ScGzFile::readFromFile(fileName, docBytes))
- {
- // FIXME: Needs better error return
- return QString::null;
+ ioDevice = new ScGzFile(fileName);
+ if (!ioDevice->open(QIODevice::ReadOnly))
+ {
+ delete ioDevice;
+ return NULL;
}
}
else
{
- // Not gzip encoded, just load it
- loadRawText(fileName, docBytes);
- }
- QString docText("");
- int startElemPos = docBytes.left(512).indexOf("<SCRIBUSUTF8NEW ");
- if (startElemPos >= 0)
- {
- QRegExp regExp134("Version=\"1.3.[4-9]");
- QRegExp regExp140("Version=\"1.4.[0-9]");
- bool is134 = ( regExp134.indexIn(docBytes.mid(startElemPos, 64)) >= 0 );
- bool is140 = ( regExp140.indexIn(docBytes.mid(startElemPos, 64)) >= 0 );
- if (is134 || is140)
- docText = QString::fromUtf8(docBytes);
- if (docText.endsWith(QChar(10)) || docText.endsWith(QChar(13)))
- docText.truncate(docText.length()-1);
- }
- if (docText.isEmpty())
- return QString::null;
- return docText;
+ ioDevice = new QFile(fileName);
+ if (!ioDevice->open(QIODevice::ReadOnly))
+ {
+ delete ioDevice;
+ return NULL;
+ }
+ }
+ return ioDevice;
}
void Scribus134Format::getReplacedFontData(bool & getNewReplacement, QMap<QString,QString> &getReplacedFonts, QList<ScFace> &getDummyScFaces)
@@ -204,8 +199,8 @@
QStack< QList<PageItem*> > groupStackP;
QStack<int> groupStack2;
- QString f(readSLA(fileName));
- if (f.isEmpty())
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
{
setFileReadError();
return false;
@@ -216,7 +211,7 @@
if (m_mwProgressBar!=0)
{
- m_mwProgressBar->setMaximum(f.length());
+ m_mwProgressBar->setMaximum(ioDevice->size());
m_mwProgressBar->setValue(0);
}
// Stop autosave timer,it will be restarted only if doc has autosave feature is enabled
@@ -248,7 +243,7 @@
bool hasPageSets = false;
int progress = 0;
- ScXmlStreamReader reader(f);
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -260,7 +255,7 @@
if (m_mwProgressBar != 0)
{
- int newProgress = qRound(reader.characterOffset() / (double) f.length() * 100);
+ int newProgress = qRound(ioDevice->pos() / (double) ioDevice->size() * 100);
if (newProgress != progress)
{
m_mwProgressBar->setValue(reader.characterOffset());
@@ -3097,8 +3092,8 @@
legacyStyleMap[4] = "4";
legacyStyleCount = 5;
- QString f(readSLA(fileName));
- if (f.isEmpty())
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
{
setFileReadError();
return false;
@@ -3109,7 +3104,7 @@
bool firstElement = true;
bool success = true;
- ScXmlStreamReader reader(f);
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -3470,14 +3465,14 @@
bool Scribus134Format::readStyles(const QString& fileName, ScribusDoc* doc, StyleSet<ParagraphStyle> &docParagraphStyles)
{
ParagraphStyle pstyle;
- QString f (readSLA(fileName));
- if (f.isEmpty())
- return false;
-
bool firstElement = true;
bool success = true;
- ScXmlStreamReader reader(f);
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
+ return false;
+
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -3507,13 +3502,14 @@
bool Scribus134Format::readCharStyles(const QString& fileName, ScribusDoc* doc, StyleSet<CharStyle> &docCharStyles)
{
CharStyle cstyle;
- QString f(readSLA(fileName));
- if (f.isEmpty())
- return false;
bool firstElement = true;
bool success = true;
- ScXmlStreamReader reader(f);
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
+ return false;
+
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -3544,13 +3540,14 @@
bool Scribus134Format::readLineStyles(const QString& fileName, QHash<QString,multiLine> *styles)
{
- QString f(readSLA(fileName));
- if (f.isEmpty())
- return false;
bool firstElement = true;
bool success = true;
- ScXmlStreamReader reader(f);
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
+ return false;
+
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -3593,13 +3590,14 @@
bool Scribus134Format::readColors(const QString& fileName, ColorList & colors)
{
- QString f(readSLA(fileName));
- if (f.isEmpty())
- return false;
bool firstElement = true;
bool success = true;
- ScXmlStreamReader reader(f);
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
+ return false;
+
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -3634,13 +3632,14 @@
QString pageName;
int counter = 0;
int counter2 = 0;
- QString f(readSLA(fileName));
- if (f.isEmpty())
- return false;
bool firstElement = true;
bool success = true;
- ScXmlStreamReader reader(f);
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
+ return false;
+
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
Modified: trunk/Scribus/scribus/plugins/fileloader/scribus134format/scribus134format.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17982&path=/trunk/Scribus/scribus/plugins/fileloader/scribus134format/scribus134format.h
==============================================================================
--- trunk/Scribus/scribus/plugins/fileloader/scribus134format/scribus134format.h (original)
+++ trunk/Scribus/scribus/plugins/fileloader/scribus134format/scribus134format.h Wed Dec 26 11:05:49 2012
@@ -13,11 +13,13 @@
#include "scribusstructs.h"
#include "styles/styleset.h"
+#include <QHash>
+#include <QList>
#include <QMap>
-#include <QHash>
+#include <QProgressBar>
#include <QString>
-#include <QList>
-#include <QProgressBar>
+
+class QIODevice;
class ColorList;
class multiLine;
@@ -79,7 +81,7 @@
void registerFormats();
- QString readSLA(const QString & fileName);
+ QIODevice* slaReader(const QString & fileName);
void getStyle(ParagraphStyle& style, ScXmlStreamReader& reader, StyleSet<ParagraphStyle> *docParagraphStyles, ScribusDoc* doc, bool fl);
Modified: trunk/Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17982&path=/trunk/Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.cpp (original)
+++ trunk/Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.cpp Wed Dec 26 11:05:49 2012
@@ -40,13 +40,14 @@
#include "pageitem_arc.h"
#include "pageitem_spiral.h"
#include "pagestructs.h"
+
+#include <QApplication>
+#include <QByteArray>
#include <QCursor>
// #include <QDebug>
#include <QFileInfo>
#include <QList>
-#include <QByteArray>
-#include <QApplication>
-
+#include <QScopedPointer>
// See scplugin.h and pluginmanager.{cpp,h} for detail on what these methods
// do. That documentatation is not duplicated here.
@@ -143,36 +144,31 @@
return false;
}
-QString Scribus150Format::readSLA(const QString & fileName)
+QIODevice* Scribus150Format::slaReader(const QString & fileName)
{
- QByteArray docBytes("");
+ if (!fileSupported(0, fileName))
+ return NULL;
+
+ QIODevice* ioDevice = 0;
if(fileName.right(2) == "gz")
{
- if (!ScGzFile::readFromFile(fileName, docBytes))
- {
- // FIXME: Needs better error return
- return QString::null;
+ ioDevice = new ScGzFile(fileName);
+ if (!ioDevice->open(QIODevice::ReadOnly))
+ {
+ delete ioDevice;
+ return NULL;
}
}
else
{
- // Not gzip encoded, just load it
- loadRawText(fileName, docBytes);
- }
- QString docText("");
- int startElemPos = docBytes.left(512).indexOf("<SCRIBUSUTF8NEW ");
- if (startElemPos >= 0)
- {
- QRegExp regExp150("Version=\"1.5.[0-9]");
- bool is150 = ( regExp150.indexIn(docBytes.mid(startElemPos, 64)) >= 0 );
- if (is150)
- docText = QString::fromUtf8(docBytes);
- if (docText.endsWith(QChar(10)) || docText.endsWith(QChar(13)))
- docText.truncate(docText.length()-1);
- }
- if (docText.isEmpty())
- return QString::null;
- return docText;
+ ioDevice = new QFile(fileName);
+ if (!ioDevice->open(QIODevice::ReadOnly))
+ {
+ delete ioDevice;
+ return NULL;
+ }
+ }
+ return ioDevice;
}
void Scribus150Format::getReplacedFontData(bool & getNewReplacement, QMap<QString,QString> &getReplacedFonts, QList<ScFace> &getDummyScFaces)
@@ -1233,8 +1229,8 @@
notesMasterMarks.clear();
notesNSets.clear();
- QString f(readSLA(fileName));
- if (f.isEmpty())
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
{
setFileReadError();
return false;
@@ -1245,7 +1241,7 @@
if (m_mwProgressBar!=0)
{
- m_mwProgressBar->setMaximum(f.length());
+ m_mwProgressBar->setMaximum(ioDevice->size());
m_mwProgressBar->setValue(0);
}
// Stop autosave timer,it will be restarted only if doc has autosave feature is enabled
@@ -1281,7 +1277,7 @@
bool hasPageSets = false;
int progress = 0;
- ScXmlStreamReader reader(f);
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -1293,7 +1289,7 @@
if (m_mwProgressBar != 0)
{
- int newProgress = qRound(reader.characterOffset() / (double) f.length() * 100);
+ int newProgress = qRound(ioDevice->pos() / (double) ioDevice->size() * 100);
if (newProgress != progress)
{
m_mwProgressBar->setValue(reader.characterOffset());
@@ -5504,8 +5500,8 @@
notesMasterMarks.clear();
notesNSets.clear();
- QString f(readSLA(fileName));
- if (f.isEmpty())
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
{
setFileReadError();
return false;
@@ -5517,7 +5513,7 @@
bool success = true;
isNewFormat = false;
- ScXmlStreamReader reader(f);
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -6097,14 +6093,14 @@
bool Scribus150Format::readStyles(const QString& fileName, ScribusDoc* doc, StyleSet<ParagraphStyle> &docParagraphStyles)
{
ParagraphStyle pstyle;
- QString f (readSLA(fileName));
- if (f.isEmpty())
- return false;
-
bool firstElement = true;
bool success = true;
- ScXmlStreamReader reader(f);
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
+ return false;
+
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -6134,13 +6130,14 @@
bool Scribus150Format::readCharStyles(const QString& fileName, ScribusDoc* doc, StyleSet<CharStyle> &docCharStyles)
{
CharStyle cstyle;
- QString f(readSLA(fileName));
- if (f.isEmpty())
- return false;
bool firstElement = true;
bool success = true;
- ScXmlStreamReader reader(f);
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
+ return false;
+
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -6171,13 +6168,14 @@
bool Scribus150Format::readLineStyles(const QString& fileName, QHash<QString,multiLine> *styles)
{
- QString f(readSLA(fileName));
- if (f.isEmpty())
- return false;
bool firstElement = true;
bool success = true;
- ScXmlStreamReader reader(f);
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
+ return false;
+
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -6220,13 +6218,14 @@
bool Scribus150Format::readColors(const QString& fileName, ColorList & colors)
{
- QString f(readSLA(fileName));
- if (f.isEmpty())
- return false;
bool firstElement = true;
bool success = true;
- ScXmlStreamReader reader(f);
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
+ return false;
+
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
@@ -6261,6 +6260,8 @@
QString pageName;
int counter = 0;
int counter2 = 0;
+ bool firstElement = true;
+ bool success = true;
markeredItemsMap.clear();
markeredMarksMap.clear();
@@ -6269,13 +6270,11 @@
notesMasterMarks.clear();
notesNSets.clear();
- QString f(readSLA(fileName));
- if (f.isEmpty())
+ QScopedPointer<QIODevice> ioDevice(slaReader(fileName));
+ if (ioDevice.isNull())
return false;
- bool firstElement = true;
- bool success = true;
-
- ScXmlStreamReader reader(f);
+
+ ScXmlStreamReader reader(ioDevice.data());
ScXmlStreamAttributes attrs;
while(!reader.atEnd() && !reader.hasError())
{
Modified: trunk/Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17982&path=/trunk/Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.h
==============================================================================
--- trunk/Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.h (original)
+++ trunk/Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.h Wed Dec 26 11:05:49 2012
@@ -15,10 +15,12 @@
#include "styles/styleset.h"
#include "selection.h"
+#include <QList>
#include <QMap>
+#include <QProgressBar>
#include <QString>
-#include <QList>
-#include <QProgressBar>
+
+class QIODevice;
class ColorList;
class multiLine;
@@ -89,7 +91,7 @@
void registerFormats();
- QString readSLA(const QString & fileName);
+ QIODevice* slaReader(const QString & fileName);
void getStyle(ParagraphStyle& style, ScXmlStreamReader& reader, StyleSet<ParagraphStyle> *docParagraphStyles, ScribusDoc* doc, bool fl);
Modified: trunk/Scribus/scribus/scgzfile.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17982&path=/trunk/Scribus/scribus/scgzfile.cpp
==============================================================================
--- trunk/Scribus/scribus/scgzfile.cpp (original)
+++ trunk/Scribus/scribus/scgzfile.cpp Wed Dec 26 11:05:49 2012
@@ -18,8 +18,9 @@
struct ScGzFileDataPrivate
{
- FILE* file;
- gzFile gzfile;
+ FILE* file;
+ gzFile gzfile;
+ quint64 uncSize; // Uncompressed stream size
};
ScGzFile::ScGzFile(const QString& fileName) : QIODevice(), m_fileName(fileName)
@@ -58,8 +59,9 @@
m_data = (ScGzFileDataPrivate*) malloc(sizeof(ScGzFileDataPrivate));
if (m_data)
{
- m_data->file = NULL;
- m_data->gzfile = NULL;
+ m_data->file = NULL;
+ m_data->gzfile = NULL;
+ m_data->uncSize = 0;
}
return m_data;
}
@@ -78,6 +80,7 @@
bool success = false;
FILE* file = NULL;
gzFile gzf = NULL;
+ quint64 fsize = 0;
QString localPath = QDir::toNativeSeparators(fileName);
#if defined(_WIN32)
if (mode == QIODevice::ReadOnly)
@@ -87,7 +90,21 @@
if (file)
{
int fno = _fileno(file);
- gzf = gzdopen(fno,(mode == QIODevice::ReadOnly) ? "rb" : "wb");
+ if (mode == QIODevice::ReadOnly)
+ {
+ fseek(file, 0, SEEK_END);
+ if (ftell(file) > 4)
+ {
+ fseek(file, -4, SEEK_END);
+ int b4 = fgetc(file);
+ int b3 = fgetc(file);
+ int b2 = fgetc(file);
+ int b1 = fgetc(file);
+ fsize = (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
+ fseek(file, 0, SEEK_SET);
+ }
+ }
+ gzf = gzdopen(fno, (mode == QIODevice::ReadOnly) ? "rb" : "wb");
if (!gzf) fclose(file);
}
#else
@@ -98,14 +115,29 @@
if (file)
{
int fno = fileno(file);
+ if (mode == QIODevice::ReadOnly)
+ {
+ fseek(file, 0, SEEK_END);
+ if (ftell(file) > 4)
+ {
+ fseek(file, -4, SEEK_END);
+ int b4 = fgetc(file);
+ int b3 = fgetc(file);
+ int b2 = fgetc(file);
+ int b1 = fgetc(file);
+ fsize = (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
+ fseek(file, 0, SEEK_SET);
+ }
+ }
gzf = gzdopen(fno,(mode == QIODevice::ReadOnly) ? "rb" : "wb");
if (!gzf) fclose(file);
}
#endif
if (gzf)
{
- data->file = file;
- data->gzfile = gzf;
+ data->file = file;
+ data->gzfile = gzf;
+ data->uncSize = fsize;
success = true;
}
return success;
@@ -132,6 +164,13 @@
return success;
}
+qint64 ScGzFile::size () const
+{
+ if (m_data && (openMode() == QIODevice::ReadOnly))
+ return m_data->uncSize;
+ return QIODevice::size();
+}
+
bool ScGzFile::reset(void)
{
if (!m_data || (openMode() != QIODevice::ReadOnly))
@@ -149,6 +188,7 @@
if (openMode() == QIODevice::ReadOnly)
{
int result = gzeof(m_data->gzfile);
+ z_off_t offset = gzoffset(m_data->gzfile);
return result;
}
return QIODevice::atEnd();
Modified: trunk/Scribus/scribus/scgzfile.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17982&path=/trunk/Scribus/scribus/scgzfile.h
==============================================================================
--- trunk/Scribus/scribus/scgzfile.h (original)
+++ trunk/Scribus/scribus/scgzfile.h Wed Dec 26 11:05:49 2012
@@ -39,6 +39,8 @@
virtual bool open(QIODevice::OpenMode mode);
virtual void close();
+ virtual qint64 size () const;
+
bool errorOccurred(void) const;
int error(void) const;
Modified: trunk/Scribus/scribus/scxmlstreamreader.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17982&path=/trunk/Scribus/scribus/scxmlstreamreader.h
==============================================================================
--- trunk/Scribus/scribus/scxmlstreamreader.h (original)
+++ trunk/Scribus/scribus/scxmlstreamreader.h Wed Dec 26 11:05:49 2012
@@ -53,6 +53,7 @@
{
public:
ScXmlStreamReader(const QString& string) : QXmlStreamReader(string) {};
+ ScXmlStreamReader(QIODevice* device) : QXmlStreamReader(device) {};
ScXmlStreamAttributes scAttributes(void) const;
void readToElementEnd(void);
More information about the scribus-commit
mailing list