From 850f2dc8d8ce16cdf3c0af0bec52687d6cd3d794 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mateusz=20P=C3=B3=C5=82rola?= Date: Tue, 5 Oct 2010 14:20:53 +0200 Subject: [PATCH] Added readers for compressed and uncompressed files --- src/plugins/stardict/CompressedReader.cpp | 110 ++++++++++++++++++++++++ src/plugins/stardict/CompressedReader.h | 53 ++++++++++++ src/plugins/stardict/StarDictReader.h | 45 ++++++++++ src/plugins/stardict/StarDictReaderFactory.cpp | 42 +++++++++ src/plugins/stardict/StarDictReaderFactory.h | 34 ++++++++ src/plugins/stardict/UncompressedReader.cpp | 89 +++++++++++++++++++ src/plugins/stardict/UncompressedReader.h | 55 ++++++++++++ src/plugins/stardict/stardict.pro | 13 ++- 8 files changed, 439 insertions(+), 2 deletions(-) create mode 100644 src/plugins/stardict/CompressedReader.cpp create mode 100644 src/plugins/stardict/CompressedReader.h create mode 100644 src/plugins/stardict/StarDictReader.h create mode 100644 src/plugins/stardict/StarDictReaderFactory.cpp create mode 100644 src/plugins/stardict/StarDictReaderFactory.h create mode 100644 src/plugins/stardict/UncompressedReader.cpp create mode 100644 src/plugins/stardict/UncompressedReader.h diff --git a/src/plugins/stardict/CompressedReader.cpp b/src/plugins/stardict/CompressedReader.cpp new file mode 100644 index 0000000..c10c2fc --- /dev/null +++ b/src/plugins/stardict/CompressedReader.cpp @@ -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 . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +//Created by Mateusz Półrola + +#include "CompressedReader.h" +#include + +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 index 0000000..d4fe10b --- /dev/null +++ b/src/plugins/stardict/CompressedReader.h @@ -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 . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +//Created by Mateusz Półrola + +#ifndef COMPRESSEDREADER_H +#define COMPRESSEDREADER_H + +#include +#include +#include +#include +#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 index 0000000..9e1ddf6 --- /dev/null +++ b/src/plugins/stardict/StarDictReader.h @@ -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 . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +//Created by Mateusz Półrola + +#ifndef STARDICTREADER_H +#define STARDICTREADER_H + +#include + +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 index 0000000..757517f --- /dev/null +++ b/src/plugins/stardict/StarDictReaderFactory.cpp @@ -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 . + + 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 index 0000000..e5f5e93 --- /dev/null +++ b/src/plugins/stardict/StarDictReaderFactory.h @@ -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 . + + 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 index 0000000..15102ef --- /dev/null +++ b/src/plugins/stardict/UncompressedReader.cpp @@ -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 index 0000000..eeb8582 --- /dev/null +++ b/src/plugins/stardict/UncompressedReader.h @@ -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 . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +//Created by Mateusz Półrola + +#ifndef UNCOMPRESSEDREADER_H +#define UNCOMPRESSEDREADER_H + +#include +#include +#include +#include +#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 diff --git a/src/plugins/stardict/stardict.pro b/src/plugins/stardict/stardict.pro index e12c77b..7401341 100644 --- a/src/plugins/stardict/stardict.pro +++ b/src/plugins/stardict/stardict.pro @@ -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 -- 1.7.9.5