Added readers for compressed and uncompressed files
authorMateusz Półrola <mateusz.polrola@comarch.pl>
Tue, 5 Oct 2010 12:20:53 +0000 (14:20 +0200)
committerMateusz Półrola <mateusz.polrola@comarch.pl>
Tue, 5 Oct 2010 12:20:53 +0000 (14:20 +0200)
src/plugins/stardict/CompressedReader.cpp [new file with mode: 0644]
src/plugins/stardict/CompressedReader.h [new file with mode: 0644]
src/plugins/stardict/StarDictReader.h [new file with mode: 0644]
src/plugins/stardict/StarDictReaderFactory.cpp [new file with mode: 0644]
src/plugins/stardict/StarDictReaderFactory.h [new file with mode: 0644]
src/plugins/stardict/UncompressedReader.cpp [new file with mode: 0644]
src/plugins/stardict/UncompressedReader.h [new file with mode: 0644]
src/plugins/stardict/stardict.pro

diff --git a/src/plugins/stardict/CompressedReader.cpp b/src/plugins/stardict/CompressedReader.cpp
new file mode 100644 (file)
index 0000000..c10c2fc
--- /dev/null
@@ -0,0 +1,110 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#include "CompressedReader.h"
+#include <QtEndian>
+
+CompressedReader::CompressedReader(QObject *parent) :
+    StarDictReader(parent) {
+}
+
+CompressedReader::CompressedReader(QString filename, QObject *parent) :
+    StarDictReader(parent) {
+    open(filename);
+}
+
+CompressedReader::~CompressedReader() {
+    if(_file != NULL)
+        gzclose(_file);
+}
+
+bool CompressedReader::open(QString file) {
+    _file = gzopen(file.toStdString().c_str(), "rb");
+    if(_file == NULL)
+        return false;
+    return true;
+}
+
+void CompressedReader::close() {
+    gzclose(_file);
+    _file = NULL;
+}
+
+
+QChar CompressedReader::readChar() {
+    char c[1];
+    gzread(_file, c, 1);
+    return QChar(c[0]);
+}
+
+qint32 CompressedReader::readInt32BigEndian() {
+    qint32 value;
+    gzread(_file, (void*)(&value), 4);
+
+    return qFromBigEndian(value);
+}
+
+qint64 CompressedReader::readInt64BigEndian() {
+    qint64 value;
+    gzread(_file, (void*)(&value), 8);
+
+    return value;
+}
+
+QString CompressedReader::readKeyword() {
+    QString result;
+    QChar c;
+    c = readChar();
+
+    while(c != '\0') {
+        result += c;
+        c = readChar();
+    }
+
+    return result;
+}
+
+QString CompressedReader::readString(qint32 offset, qint32 len) {
+    char* buf;
+    buf = new char[len];
+
+    gzseek(_file, offset, SEEK_SET);
+    gzread(_file, buf, len);
+
+    QString result(buf);
+    delete [] buf;
+    return result;
+}
+
+QString CompressedReader::readString(qint64 offset, qint32 len) {
+    char* buf;
+    buf = new char[len];
+
+    gzseek(_file, offset, SEEK_SET);
+    gzread(_file, buf, len);
+
+    QString result(buf);
+    delete [] buf;
+    return result;
+}
+
diff --git a/src/plugins/stardict/CompressedReader.h b/src/plugins/stardict/CompressedReader.h
new file mode 100644 (file)
index 0000000..d4fe10b
--- /dev/null
@@ -0,0 +1,53 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#ifndef COMPRESSEDREADER_H
+#define COMPRESSEDREADER_H
+
+#include <QObject>
+#include <zconf.h>
+#include <zlibdefs.h>
+#include <zlib.h>
+#include "StarDictReader.h"
+
+class CompressedReader : public StarDictReader
+{
+    Q_OBJECT
+public:
+    CompressedReader(QObject *parent = 0);
+    CompressedReader(QString filename, QObject *parent = 0);
+    ~CompressedReader();
+
+    QString readString(qint32 offset, qint32 len);
+    QString readString(qint64 offset, qint32 len);
+    QChar readChar();
+    qint32 readInt32BigEndian();
+    qint64 readInt64BigEndian();
+    QString readKeyword();
+    bool open(QString file);
+    void close();
+private:
+    gzFile _file;
+};
+
+#endif // COMPRESSEDREADER_H
diff --git a/src/plugins/stardict/StarDictReader.h b/src/plugins/stardict/StarDictReader.h
new file mode 100644 (file)
index 0000000..9e1ddf6
--- /dev/null
@@ -0,0 +1,45 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#ifndef STARDICTREADER_H
+#define STARDICTREADER_H
+
+#include <QObject>
+
+class StarDictReader : public QObject {
+    Q_OBJECT
+public:
+    StarDictReader(QObject *parent = 0) : QObject(parent) {}
+    virtual ~StarDictReader() {}
+
+    virtual QString readString(qint32 offset, qint32 len)=0;
+    virtual QString readString(qint64 offset, qint32 len)=0;
+    virtual QChar readChar()=0;
+    virtual qint32 readInt32BigEndian()=0;
+    virtual qint64 readInt64BigEndian()=0;
+    virtual QString readKeyword()=0;
+    virtual bool open(QString file)=0;
+    virtual void close()=0;
+};
+
+#endif // STARDICTREADER_H
diff --git a/src/plugins/stardict/StarDictReaderFactory.cpp b/src/plugins/stardict/StarDictReaderFactory.cpp
new file mode 100644 (file)
index 0000000..757517f
--- /dev/null
@@ -0,0 +1,42 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#include "StarDictReaderFactory.h"
+#include "CompressedReader.h"
+#include "UncompressedReader.h"
+
+
+StarDictReader* StarDictReaderFactory::createReader(QString filename) {
+    StarDictReader* reader;
+    if(filename.endsWith(".gz", Qt::CaseInsensitive) ||
+       filename.endsWith(".dz", Qt::CaseInsensitive)) {
+        reader = new CompressedReader();
+    }
+    else {
+        reader = new UncompressedReader();
+    }
+
+    reader->open(filename);
+    return reader;
+}
+
diff --git a/src/plugins/stardict/StarDictReaderFactory.h b/src/plugins/stardict/StarDictReaderFactory.h
new file mode 100644 (file)
index 0000000..e5f5e93
--- /dev/null
@@ -0,0 +1,34 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#ifndef STARDICTREADERFACTORY_H
+#define STARDICTREADERFACTORY_H
+#include "StarDictReader.h"
+
+class StarDictReaderFactory
+{
+public:
+    static StarDictReader* createReader(QString file);
+};
+
+#endif // STARDICTREADERFACTORY_H
diff --git a/src/plugins/stardict/UncompressedReader.cpp b/src/plugins/stardict/UncompressedReader.cpp
new file mode 100644 (file)
index 0000000..15102ef
--- /dev/null
@@ -0,0 +1,89 @@
+#include "UncompressedReader.h"
+
+
+UncompressedReader::UncompressedReader(QObject *parent) :
+        StarDictReader(parent) {
+
+}
+
+UncompressedReader::UncompressedReader(QString filename, QObject *parent) :
+StarDictReader(parent) {
+    open(filename);
+}
+
+UncompressedReader::~UncompressedReader() {
+    if(_file.isOpen())
+        _file.close();
+}
+
+bool UncompressedReader::open(QString file) {
+    _file.setFileName(file);
+    _stream.setDevice(&_file);
+    return _file.open(QFile::ReadOnly);
+}
+
+void UncompressedReader::close() {
+    _file.close();
+}
+
+QChar UncompressedReader::readChar() {
+    char c[1];
+
+    _stream.readRawData(c, 1);
+    return QChar(c[0]);
+}
+
+QString UncompressedReader::readKeyword() {
+    QString result;
+    QChar c;
+    c = readChar();
+
+    while(c != '\0') {
+        result += c;
+        c = readChar();
+    }
+
+    return result;
+}
+
+QString UncompressedReader::readString(qint32 offset, qint32 len) {
+    char* buf;
+    buf = new char[len];
+
+    _file.seek(offset);
+    _stream.readRawData(buf, len);
+
+    QString result(buf);
+    delete [] buf;
+    return result;
+}
+
+QString UncompressedReader::readString(qint64 offset, qint32 len) {
+    char* buf;
+    buf = new char[len];
+
+    _file.seek(offset);
+    _stream.readRawData(buf, len);
+
+    QString result(buf);
+    delete [] buf;
+    return result;
+}
+
+qint32 UncompressedReader::readInt32BigEndian() {
+    _stream.setByteOrder(QDataStream::BigEndian);
+    qint32 value;
+    _stream>>value;
+    _stream.setByteOrder(QDataStream::LittleEndian);
+
+    return value;
+}
+
+qint64 UncompressedReader::readInt64BigEndian() {
+    _stream.setByteOrder(QDataStream::BigEndian);
+    qint64 value;
+    _stream>>value;
+    _stream.setByteOrder(QDataStream::LittleEndian);
+
+    return value;
+}
diff --git a/src/plugins/stardict/UncompressedReader.h b/src/plugins/stardict/UncompressedReader.h
new file mode 100644 (file)
index 0000000..eeb8582
--- /dev/null
@@ -0,0 +1,55 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#ifndef UNCOMPRESSEDREADER_H
+#define UNCOMPRESSEDREADER_H
+
+#include <QObject>
+#include <QFile>
+#include <QDataStream>
+#include <QString>
+#include "StarDictReader.h"
+
+class UncompressedReader : public StarDictReader
+{
+    Q_OBJECT
+public:
+    UncompressedReader(QObject *parent = 0);
+    UncompressedReader(QString filename, QObject *parent = 0);
+    ~UncompressedReader();
+
+    QString readString(qint32 offset, qint32 len);
+    QString readString(qint64 offset, qint32 len);
+    QChar readChar();
+    qint32 readInt32BigEndian();
+    qint64 readInt64BigEndian();
+    QString readKeyword();
+    bool open(QString file);
+    void close();
+
+private:
+    QFile _file;
+    QDataStream _stream;
+};
+
+#endif // UNCOMPRESSEDREADER_H
index e12c77b..7401341 100644 (file)
@@ -9,11 +9,16 @@ QT = core \
 
 maemo5:QT += maemo5
 
+LIBS += -lz
+
 SOURCES +=  \
     StarDictPlugin.cpp \
     TranslationStarDict.cpp \
     StarDictDialog.cpp \
-    StarDialog.cpp
+    StarDialog.cpp \
+    CompressedReader.cpp \
+    UncompressedReader.cpp \
+    StarDictReaderFactory.cpp
 
 
 HEADERS += \
@@ -24,7 +29,11 @@ HEADERS += \
     ../../include/translation.h \
     ../../include/settings.h \
     ../../include/CommonDictInterface.h \
-    StarDialog.h
+    StarDialog.h \
+    CompressedReader.h \
+    UncompressedReader.h \
+    StarDictReaderFactory.h \
+    StarDictReader.h
 
 RESOURCES += \
     StarDict.qrc