Extended implementing StarDict *.dict file
authorBartosz Szatkowski <bulislaw@linux.com>
Wed, 6 Oct 2010 12:32:45 +0000 (14:32 +0200)
committerBartosz Szatkowski <bulislaw@linux.com>
Wed, 6 Oct 2010 12:32:45 +0000 (14:32 +0200)
src/plugins/stardict/CompressedReader.cpp
src/plugins/stardict/CompressedReader.h
src/plugins/stardict/StarDictPlugin.cpp
src/plugins/stardict/StarDictPlugin.h
src/plugins/stardict/StarDictReader.h
src/plugins/stardict/UncompressedReader.cpp
src/plugins/stardict/UncompressedReader.h

index c10c2fc..1e11c82 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "CompressedReader.h"
 #include <QtEndian>
+#include <QDebug>
 
 CompressedReader::CompressedReader(QObject *parent) :
     StarDictReader(parent) {
@@ -84,27 +85,15 @@ QString CompressedReader::readKeyword() {
     return result;
 }
 
-QString CompressedReader::readString(qint32 offset, qint32 len) {
+QByteArray CompressedReader::readString(qint64 offset, qint32 len) {
     char* buf;
     buf = new char[len];
 
     gzseek(_file, offset, SEEK_SET);
     gzread(_file, buf, len);
 
-    QString result(buf);
+    QByteArray res(buf, len);
     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;
+    return res;
 }
 
index ab5caba..1e7c14a 100644 (file)
@@ -50,19 +50,11 @@ public:
 
     /*!
       Reads translations text from compressed dict file.
-      \param offset 32-bit offset of translation in uncompressed file, readed
-             from idx file
-      \param len length of uncompressed translation, readed from idx file too
-      */
-    QString readString(qint32 offset, qint32 len);
-
-    /*!
-      Reads translations text from compressed dict file.
       \param offset 64-bit offset of translation in uncompressed file, readed
              from idx file
       \param len length of uncompressed translation, readed from idx file too
       */
-    QString readString(qint64 offset, qint32 len);
+    QByteArray readString(qint64 offset, qint32 len);
 
     /*!
       Reads 32-bits integer value from compressed file and convert it from
index 8e27f51..bdd5218 100644 (file)
@@ -140,13 +140,95 @@ QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
 }
 
 
+QByteArray StarDictPlugin::read(QByteArray::iterator it,
+        QByteArray::iterator end, int bytes) {
+    QByteArray ret;
+    if(bytes == 0 && it != end)
+        while(*it != '\0')
+            ret.append(*it++);
+    else
+        for(int i = 0; i < bytes && it != end; i++)
+            ret.append(*it++);
+    return ret;
+}
+
+
+QString StarDictPlugin::interpret(QByteArray::iterator it,
+        QByteArray::iterator end, QChar mode, bool last) {
+    QString result;
+   if(mode == 'm')
+       result += QString::fromUtf8(read(it++, end));
+   else if(mode == 'l')
+       result += QString::fromUtf8(read(it++, end));
+   else if(mode ==  'g')
+       result += QString::fromUtf8(read(it++, end));
+   else if(mode == 't')
+       result += QString::fromUtf8(read(it++, end));
+   else if(mode == 'x')
+       result += QString::fromUtf8(read(it++, end));
+   else if(mode == 'y')
+       result += QString::fromUtf8(read(it++, end));
+   else if(mode == 'k')
+       result += QString::fromUtf8(read(it++, end));
+   else if(mode == 'w')
+       result += QString::fromUtf8(read(it++, end));
+   else if(mode == 'h')
+       result += QString::fromUtf8(read(it++, end));
+   else if(mode == 'r')
+       result += QString::fromUtf8(read(it++, end));
+
+   else if(mode == 'W') {
+       if(!last) {
+           QByteArray tmp ;
+           tmp.append(*(it++));
+           tmp.append(*(it++));
+           tmp.append(*(it++));
+           tmp.append(*(it));
+           result += read(it++, end, (qint32)qFromBigEndian(tmp.data()));
+       } else
+           result += read(it++, end);
+   } else if(mode == 'P') {
+       if(!last) {
+           QByteArray tmp ;
+           tmp.append(*(it++));
+           tmp.append(*(it++));
+           tmp.append(*(it++));
+           tmp.append(*(it));
+           result += read(it++, end, (qint32)qFromBigEndian(tmp.data()));
+       } else
+           result += read(it++, end);
+   }
+   return result;
+}
+
+
+QString StarDictPlugin::format(QByteArray raw, QString mode) {
+    QString result;
+    if(mode == "") {
+        for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
+            char tmp = *(++it);
+            result += interpret(--it, raw.end(), tmp);
+        }
+    } else {
+        QByteArray::iterator it = raw.begin();
+        foreach(QChar tmp, mode) {
+            result += interpret(it, raw.end(), tmp);
+        }
+    }
+
+    return result;
+
+}
+
+
 
 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
     if(!dictReader)
         return "";
 
-    qDebug() << dictReader->readString(offset, len);
-    return dictReader->readString(offset, len);
+    QByteArray raw = dictReader->readString(offset, len);
+
+    return format(raw, settings()->value("sametypesequence"));
 }
 
 
index 89e38eb..eba7ad0 100644 (file)
@@ -38,6 +38,7 @@
 #include <QtPlugin>
 #include <QHash>
 #include <QIcon>
+#include <QtEndian>
 
 #include "../../include/CommonDictInterface.h"
 #include "../../include/settings.h"
@@ -183,6 +184,11 @@ private:
     StarDictDialog* _dictDialog;
     Settings* _ifoFileSettings;
     StarDictReader * dictReader;
+    QString format(QByteArray, QString mode);
+    QByteArray read(QByteArray::iterator, QByteArray::iterator end,
+           int bytes = 0);
+    QString interpret(QByteArray::iterator, QByteArray::iterator,
+            QChar, bool last = false);
 };
 
 #endif // XDXFPLUGIN_H
index 035d5e5..b1bbbdc 100644 (file)
@@ -40,17 +40,10 @@ public:
 
     /*!
       Reads translations text from dict file.
-      \param offset 32-bit offset of translation in file, readed from idx file
-      \param len length of translation, readed from idx file too
-      */
-    virtual QString readString(qint32 offset, qint32 len)=0;
-
-    /*!
-      Reads translations text from dict file.
       \param offset 64-bit offset of translation in file, readed from idx file
       \param len length of translation, readed from idx file too
       */
-    virtual QString readString(qint64 offset, qint32 len)=0;
+    virtual QByteArray readString(qint64 offset, qint32 len)=0;
 
     /*!
       Reads 32-bits integer value from file and convert it from BigEndian
index 15102ef..6129e6a 100644 (file)
@@ -46,26 +46,15 @@ QString UncompressedReader::readKeyword() {
     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) {
+QByteArray UncompressedReader::readString(qint64 offset, qint32 len) {
     char* buf;
     buf = new char[len];
 
     _file.seek(offset);
     _stream.readRawData(buf, len);
 
-    QString result(buf);
+    QByteArray result(buf, len);
     delete [] buf;
     return result;
 }
index 462e08d..30e4809 100644 (file)
@@ -52,19 +52,11 @@ public:
 
     /*!
       Reads translations text from file
-      \param offset 32-bit offset of translation in file, readed
-             from idx file
-      \param len length of translation, readed from idx file too
-      */
-    QString readString(qint32 offset, qint32 len);
-
-    /*!
-      Reads translations text from file
       \param offset 64-bit offset of translation in file, readed
              from idx file
       \param len length of translation, readed from idx file too
       */
-    QString readString(qint64 offset, qint32 len);
+    QByteArray readString(qint64 offset, qint32 len);
 
     /*!
       Reads 32-bits integer value from file and convert it from