[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    extragear/utils/ktranslator/plugins/sdict
From:       Raul Fernandes <rgfbr () yahoo ! com ! br>
Date:       2006-12-23 10:17:35
Message-ID: 1166869055.798880.32139.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 615924 by rgfbr:



 M  +6 -0      Makefile.am  
 M  +51 -55    sdict.cpp  
 M  +22 -22    sdict.h  
 M  +1 -1      sdictplugin.cpp  
 A             sdicttest.cpp   [License: GPL (v2+)]


--- trunk/extragear/utils/ktranslator/plugins/sdict/Makefile.am #615923:615924
@@ -10,3 +10,9 @@
 
 pluginsdir = $(kde_datadir)/ktranslator_sdict
 kde_services_DATA = ktranslator_sdict.desktop
+
+sdicttest:
+	g++ -o sdicttest.o -c sdicttest.cpp
+	g++ -o sdict.o -c sdict.cpp
+	g++ -o sdicttest sdicttest.o sdict.o -lz
+
--- trunk/extragear/utils/ktranslator/plugins/sdict/sdict.cpp #615923:615924
@@ -19,16 +19,11 @@
  ***************************************************************************/
 #include "sdict.h"
 
-#include <qfile.h>
-
-#include <klocale.h>
-#include <kdebug.h>
-
 #include <zlib.h>
 
 #define CHUNK 0xffffL
 
-Sdict::Sdict( const std::string &arq )
+Sdict::Sdict( const char *filename )
 {
   uint size;
   char block[256];
@@ -36,17 +31,18 @@
   uint copyrightPos;
   uint versionPos;
 
+  m_filename = filename;
+
   // dct file
-  if( !QFile::exists( arq.c_str() ) )
+  file.open( m_filename.c_str() );
+  if( !file.is_open() )
   {
     m_isOk = false;
     return;
   }
-  file = new QFile( arq.c_str() );
 
   // Read the header
-  file->open( IO_ReadOnly );
-  file->readBlock( block, 43 );
+  file.readsome( block, 43 );
   m_inlang[0] = block[4];
   m_inlang[1] = block[5];
   m_inlang[2] = block[6];
@@ -70,71 +66,75 @@
   m_articles = (uchar)block[39] | (uchar)block[40] << 8 | (uchar)block[41] << 16 | \
(uchar)block[42] << 24;  
   // Read title
-  file->at( titlePos );
-  file->readBlock( block, 4 );
+  file.seekg( titlePos );
+  file.readsome( block, 4 );
   size = (uchar)block[0] | (uchar)block[1] << 8 | (uchar)block[2] << 16 | \
(uchar)block[3] << 24;  if( m_compress == 1 )
   {
     size -= 2;
-    file->at( file->at() + 2 );
+    file.get();
+    file.get();
   }
-  file->readBlock( block, size );
+  file.readsome( block, size );
   block[size] = '\0';
-  if( m_compress == 0 ) m_title = QString::fromUtf8( block );
+  if( m_compress == 0 ) m_title = block;
   else{
-    m_title = QString::fromUtf8( Inflate( block ).c_str() );
+    m_title = Inflate( block );
   }
 
   // Read copyright
-  file->at( copyrightPos );
-  file->readBlock( block, 4 );
+  file.seekg( copyrightPos );
+  file.readsome( block, 4 );
   size = (uchar)block[0] | (uchar)block[1] << 8 | (uchar)block[2] << 16 | \
(uchar)block[3] << 24;  if( m_compress == 1 )
   {
     size -= 2;
-    file->at( file->at() + 2 );
+    file.get();
+    file.get();
   }
-  file->readBlock( block, size );
+  file.readsome( block, size );
   block[size] = '\0';
-  if( m_compress == 0 ) m_copyright = QString::fromUtf8( block );
+  if( m_compress == 0 ) m_copyright = block;
   else{
-    m_copyright = QString::fromUtf8( Inflate( block ).c_str() );
+    m_copyright = Inflate( block );
   }
 
   // Read version
-  file->at( versionPos );
-  file->readBlock( block, 4 );
+  file.seekg( versionPos );
+  file.readsome( block, 4 );
   size = (uchar)block[0] | (uchar)block[1] << 8 | (uchar)block[2] << 16 | \
(uchar)block[3] << 24;  if( m_compress == 1 )
   {
     size -= 2;
-    file->at( file->at() + 2 );
+    file.get();
+    file.get();
   }
-  file->readBlock( block, size );
+  file.readsome( block, size );
   block[size] = '\0';
-  if( m_compress == 0 ) m_version = QString::fromUtf8( block );
+  if( m_compress == 0 ) m_version = block;
   else{
-    m_version = QString::fromUtf8( Inflate( block ).c_str() );
+    m_version = Inflate( block );
   }
 
   dic.clear();
   struct entry entry;
 
-  file->at( m_fullidx );
+  file.seekg( m_fullidx );
   dic.reserve( m_size );
   for(uint a = 0;a<m_size;a++)
   {
-    file->readBlock( block, 8 );
+    file.readsome( block, 8 );
     entry.offset = (uchar)block[4] | (uchar)block[5] << 8 | (uchar)block[6] << 16 | \
(uchar)block[7] << 24;  size = (uchar)block[0] | (uchar)block[1] << 8;
     size -= 8;
-    file->readBlock( block, size );
+    file.readsome( block, size );
     block[size] = '\0';
-    entry.headword = QString::fromUtf8( block ).lower();
+    file.tellg();
+    entry.headword = block;
     dic.push_back( entry );
   }
 
-  file->close();
+  file.close();
 
   m_isOk = true;
 }
@@ -142,20 +142,15 @@
 
 Sdict::~Sdict()
 {
-  if( file )
-  {
-    delete file;
-    file = 0;
-  }
 }
 
 
 /*!
-    \fn Sdict::search( const QString &word )
+    \fn Sdict::search( const char *word )
  */
-QString Sdict::search( const QString &word )
+const char *Sdict::search( const char *word )
 {
-  QString result;
+  std::string result;
   uint size;
   char block[256];
 
@@ -168,36 +163,37 @@
 
   if( it != dic.end() )
   {
+    file.open( m_filename.c_str() );
+    if( !file.is_open() ) return "";
 
-    file->open( IO_ReadOnly );
-
-    file->at( m_articles + (*it).offset );
-    file->readBlock( block, 4 );
+    file.seekg( m_articles + (*it).offset );
+    file.readsome( block, 4 );
     size = (uchar)block[0] | (uchar)block[1] << 8 | (uchar)block[2] << 16 | \
(uchar)block[3] << 24;  if( m_compress == 1 )
     {
       size -= 2;
-      file->at( file->at() + 2 );
+      file.get();
+      file.get();
     }
     std::string article;
-    for(uint a=0;a<size;a++) article += file->getch();
-    file->close();
+    article.reserve( size );
+    for(uint a=0;a<size;a++) article += file.get();
+    file.close();
 
-    result = word + '\n';
-    if( m_compress == 0 ) result += QString::fromUtf8( article.c_str() );
+    result = word;
+    result += '\n';
+    if( m_compress == 0 ) result += article;
     else{
-      result += QString::fromUtf8( Inflate( article ).c_str() );
+      result += Inflate( article );
     }
-    return result;
+    return result.c_str();
   }
-  result += "<font color=#000000>" + i18n( "Word not found" ) + "</font>";
-  return result;
+  return "";
 }
 
 
 std::string Sdict::Inflate( const std::string &data )
 {
-    //kdDebug()<< "Inflate()" << endl;
     int ret;
     z_stream strm;
     char out[CHUNK];
--- trunk/extragear/utils/ktranslator/plugins/sdict/sdict.h #615923:615924
@@ -20,53 +20,53 @@
 #ifndef SDICT_H
 #define SDICT_H
 
-#include <qstring.h>
-
 #include <string>
+#include <fstream>
 #include <vector>
 
-class QFile;
+using namespace std;
 
+typedef unsigned char uchar;
+
 /**
 @author Raul Fernandes
 */
 class Sdict{
 public:
-    Sdict( const std::string & );
+    Sdict( const char* );
 
     ~Sdict();
 
-    QString search( const QString & );
-    bool isOk(){ return m_isOk; };
-    uint size() { return m_size; };
-    QString filename() { return m_filename; };
-    QString title() { return m_title; };
-    QString copyright() { return m_copyright; };
-    QString version() { return m_version; };
-    QString inlang() { return m_inlang; };
-    QString outlang() { return m_outlang; };
+    const char *search( const char * );
+    inline bool isOk() const { return m_isOk; };
+    inline uint size() const { return m_size; };
+    inline const char *filename() const { return m_filename.c_str(); };
+    inline const char *title() const { return m_title.c_str(); };
+    inline const char *copyright() const { return m_copyright.c_str(); };
+    inline const char *version() const { return m_version.c_str(); };
+    inline const char *inlang() const { return m_inlang; };
+    inline const char *outlang() const { return m_outlang; };
 
     struct entry {
-      QString headword;
+      string headword;
       ulong offset;
     };
 
 //#ifndef NOPTIMIZE
-    typedef std::vector<entry> Dictionary;
+    typedef vector<entry> Dictionary;
     Dictionary dic;
 //#endif
 
 protected:
-    std::string Inflate( const std::string & );
+    string Inflate( const string & );
 
-    QFile *file;
+    ifstream file;
     bool m_isOk;
-
     uint m_size;
-    QString m_filename;
-    QString m_title;
-    QString m_copyright;
-    QString m_version;
+    string m_filename;
+    string m_title;
+    string m_copyright;
+    string m_version;
     char m_inlang[3];
     char m_outlang[3];
     ushort m_compress;
--- trunk/extragear/utils/ktranslator/plugins/sdict/sdictplugin.cpp #615923:615924
@@ -64,7 +64,7 @@
 
   #endif
 
-  QString result = dic->search( word );
+  QString result = QString::fromUtf8( dic->search( word.utf8().data() ) );
 
   if( result.isEmpty() ) return QString ( "<dicName>%1</dicName><font \
color=#000000>%2</font>" ).arg( dictName ).arg( i18n( "Word not found" ) );  


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic