"Fossies" - the Fresh Open Source Software Archive

Member "kydpdict-0.9.5/src/engine_dict.cpp" (14 Aug 2005, 7165 Bytes) of package /linux/privat/old/kydpdict-0.9.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "engine_dict.cpp" see the Fossies "Dox" file reference documentation.

    1 /***************************************************************************
    2  *                                                                         *
    3  *   This program is free software; you can redistribute it and/or modify  *
    4  *   it under the terms of the GNU General Public License as published by  *
    5  *   the Free Software Foundation; either version 2 of the License, or     *
    6  *   (at your option) any later version.                                   *
    7  *                                                                         *
    8  ***************************************************************************/
    9 
   10 #include <qlistbox.h>
   11 #include <qmessagebox.h>
   12 #include <qtextcodec.h>
   13 #include <qregexp.h>
   14 
   15 // for memset
   16 #include <string.h>
   17 // for gzfile
   18 #include <zlib.h>
   19 
   20 #include "kydpconfig.h"
   21 #include "engine_dict.h"
   22 #include "engine_dict.moc"
   23 
   24 EngineDICT::EngineDICT(kydpConfig *config, QListBox *listBox, ydpConverter *converter) : ydpDictionary(config, listBox, converter)
   25 {
   26     for (int i=0; i<2; i++) {
   27         dictCache_LL[i].offsets = NULL;
   28     dictCache_LL[i].lengths = NULL;
   29     }
   30 }
   31 
   32 EngineDICT::~EngineDICT()
   33 {
   34     for (int i=0; i<2; i++) {
   35     if (this->dictCache[i].wordCount>0) {
   36         if (this->dictCache[i].words) {
   37         delete [] dictCache_LL[i].offsets;
   38         delete [] dictCache_LL[i].lengths;
   39         }
   40     }
   41     }
   42 }
   43 
   44 int EngineDICT::OpenDictionary(void)
   45 {
   46     int i;
   47     QFile fData;
   48 
   49     /* open index and definition files */
   50     UpdateFName();
   51     fIndex.setName( cnf->topPath + "/" + cnf->indexFName );
   52     if (!(fIndex.open(IO_ReadOnly))) {
   53     fIndex.setName( cnf->topPath + "/" + cnf->indexFName.upper());
   54     if (!(fIndex.open(IO_ReadOnly))) {
   55         QMessageBox::critical(0, "Kydpdict", tr( "Can't open index file!\n" ) + fIndex.name() );
   56         return 1;
   57     }
   58     }
   59 
   60     fData.setName( cnf->topPath + "/" + cnf->dataFName );
   61     if (!(fData.open(IO_ReadOnly))) {
   62     fData.setName( cnf->topPath + "/" + cnf->dataFName.upper());
   63     if (!(fData.open(IO_ReadOnly))) {
   64         QMessageBox::critical(0, "Kydpdict", tr( "Can't open data file!\n" ) + fData.name() );
   65         return 1;
   66     }
   67     }
   68     fData.close();
   69 
   70     zData = gzopen(fData.name(), "r");
   71     if (zData == NULL)
   72     return 1;
   73 
   74     i = 0;
   75 //    if (!(this->cnf->toPolish)) i++;
   76     if (this->dictCache[i].wordCount>0) {
   77     offsets = dictCache_LL[i].offsets;
   78     lengths = dictCache_LL[i].lengths;
   79     } else {
   80         FillWordList();
   81     dictCache_LL[i].offsets = offsets;
   82     dictCache_LL[i].lengths = lengths;
   83     }
   84     return ydpDictionary::OpenDictionary(); // required call
   85 }
   86 
   87 int EngineDICT::CheckDictionary(void)
   88 {
   89     QFile f;
   90 
   91     if (cnf->language != LANG_ENGLISH)
   92     return 0;
   93     if (! cnf->toPolish)
   94     return 0;
   95 
   96     UpdateFName();
   97     f.setName( cnf->topPath + "/" + cnf->indexFName );
   98     if ( !(f.exists()) ) {
   99         f.setName( cnf->topPath + "/" + cnf->indexFName.upper() );
  100     if ( !(f.exists()) )
  101             return 0;
  102     }
  103     f.setName( cnf->topPath + "/" + cnf->dataFName );
  104     if ( !(f.exists()) ) {
  105     f.setName( cnf->topPath + "/" + cnf->dataFName.upper() );
  106     if ( !(f.exists()) )
  107             return 0;
  108     }
  109     return 1;
  110 }
  111 
  112 void EngineDICT::CloseDictionary()
  113 {
  114     fIndex.close();
  115     gzclose(zData);
  116     ydpDictionary::CloseDictionary();   // required call
  117 }
  118 
  119 void EngineDICT::UpdateFName()
  120 {
  121 //  cnf->indexFName = cnf->toPolish ? "dvp_1.dic" : "dvp_2.dic";
  122     cnf->topPath = "/usr/share/dictd/";
  123     cnf->indexFName = "sap_en-pl.index";
  124     cnf->dataFName = "sap_en-pl.dict.dz";
  125 }
  126 
  127 int EngineDICT::Base64Decode(const char *input) {
  128 
  129 const static char base64tab[] = {
  130     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,             /* 00-0f */
  131     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,         /* 10-1f */
  132     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,           /* 20-2f */
  133     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,       /* 30-3f */
  134     0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,        /* 40-4f */
  135     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,      /* 50-5f */
  136     0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,  /* 60-6f */
  137     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0 };    /* 70-7f */
  138 
  139     int i;
  140     int result = 0;
  141     int c;
  142     for (i=0;(input[i]) && (input[i]!='\n');i++) {
  143     result *= 64;
  144     c = input[i];
  145     if ( (c<0) || (c>0x7F) )
  146         c = 0;
  147     result += base64tab[c];
  148     }
  149 
  150     return result;
  151 }
  152 
  153 void EngineDICT::FillWordList()
  154 {
  155     int i = 0;
  156     int j, l;
  157     char buf[256];      // XXX arbitrary
  158     char *start;
  159     char *len;
  160 
  161     wordCount = 40000;      // XXX arbitrary
  162     words = new char* [wordCount];
  163     offsets = new unsigned int [wordCount];
  164     lengths = new unsigned int [wordCount];
  165     fIndex.at(0);
  166     while ( fIndex.readLine(buf,255) > 0 ) {
  167     // analyze this
  168     l = strlen(buf);
  169     j = 0;
  170     while ( (j<l) && (buf[j]!='\t') ) { j++; }; // skip word part
  171     buf[j] = '\0'; j++; start = &buf[j];        // get offset b64
  172     words[i] = new char [j];            // copy word
  173     strcpy(words[i], buf);
  174     while ( (j<l) && (buf[j]!='\t') ) { j++; }; // skip start part
  175     buf[j] = '\0'; j++; len = &buf[j];      // get length b64
  176     offsets[i] = Base64Decode(start);
  177     lengths[i] = Base64Decode(len);
  178     i++;
  179     }
  180     wordCount = i;
  181 }
  182 
  183 int EngineDICT::ReadDefinition(int index)
  184 {
  185     char *buf = new char [lengths[index]+10];       // safeguard at the end
  186 
  187     if (gzseek(zData, offsets[index], SEEK_SET) < 0)
  188     return 1;
  189     if (gzread(zData, buf, lengths[index]) < 0)
  190     return 1;
  191 //    gzrewind(zData);  // unneeded I presume
  192 
  193     buf[lengths[index]] = '\0'; // sometimes junk at the end
  194     curDefinition = buf;
  195     delete buf;
  196 
  197     return 0;
  198 }
  199 
  200 QString EngineDICT::rtf2html(QString definition) {
  201     QString tmp = definition;
  202     tmp.replace("\n","<br>");
  203     tmp.replace(QRegExp("^(\\w+)"),"<b>\\1</b>");
  204     tmp.replace(" ","&nbsp;");
  205     tmp = "<qt type=\"page\"><font color=" + color4 + ">" + tmp + "</font></qt>";
  206     return tmp;
  207 }
  208 
  209 ////////////
  210 
  211 ConvertDICT::ConvertDICT(void) {
  212     codec = QTextCodec::codecForName("ISO8859-2");
  213 }
  214 
  215 ConvertDICT::~ConvertDICT() {
  216 
  217 }
  218 
  219 char ConvertDICT::toLower(const char c) {
  220     const static char upper_cp[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  221     const static char lower_cp[] = "abcdefghijklmnopqrstuvwxyz";
  222 
  223     unsigned int i;
  224     for (i=0;i<sizeof(upper_cp);i++)
  225     if (c == upper_cp[i])
  226         return lower_cp[i];
  227     return c;
  228 }
  229 
  230 int ConvertDICT::charIndex(const char c) {
  231     const static char lower_cp[] = "abcdefghijklmnopqrstuvwxyz";
  232     unsigned int i;
  233     for (i=0;i<sizeof(lower_cp);i++)
  234     if (c == lower_cp[i])
  235         return i;
  236     return sizeof(lower_cp)+1;
  237 }
  238 
  239 char *ConvertDICT::toLocal(const char *input) {
  240     int i, j = 0;
  241     int len = strlen(input);
  242     static char buffer[256];        // ugly? maybe
  243 
  244     memset(buffer,0,sizeof(buffer));
  245 
  246     for (i=0;i!=len;) {
  247     if ((input[i] != ' ') && (input[i] != '\t') && (input[i] != '-') && (input[i] !=','))
  248         buffer[j++] = toLower(input[i++]);
  249     else
  250         ++i;
  251     }
  252     return buffer;
  253 }
  254 
  255 QString ConvertDICT::toUnicode(const char *input) {
  256     return codec->toUnicode(input);
  257 }
  258 
  259 QCString ConvertDICT::fromUnicode(QString input) {
  260     return codec->fromUnicode(input);
  261 }
  262 
  263 QString ConvertDICT::convertChunk(const char *inp, int size, bool unicodeFont) {
  264     return codec->toUnicode(inp);
  265 }