Some bugs were repaird in XDXF engine:\n- not finding last word from cache\n- not...
authorstranger <dariusz.wiechecki@gmail.com>
Thu, 8 Feb 2007 12:36:06 +0000 (12:36 +0000)
committerstranger <dariusz.wiechecki@gmail.com>
Thu, 8 Feb 2007 12:36:06 +0000 (12:36 +0000)
git-svn-id: file:///svnroot/mdictionary/trunk@68 5bde0345-f819-0410-ac75-e5045f9217cc

src/plugins/xdxf/src/engine_xdxf.c

index 30072af..e609387 100644 (file)
@@ -50,6 +50,7 @@ static gchar* word_translation_cache(XDXFData* data, gchar* word)
                 readed += max_length;
                 buffer = b;
                 already = 0;
+               gchar* tmp = NULL;
                 while(already < max_length) {
                         memcpy(&record_length, buffer, sizeof(guint));
                         memcpy(&trans_offset,
@@ -57,8 +58,9 @@ static gchar* word_translation_cache(XDXFData* data, gchar* word)
                                 sizeof(guint)
                               );
                         buffer[record_length-sizeof(guint)*2] = '\0';
+                       tmp = g_utf8_casefold(buffer+sizeof(guint), -1);
                         if(((record_length - 3*sizeof(guint)) == word_length) &&
-                           (g_utf8_collate(word,buffer+sizeof(guint)) == 0)) {
+                           (g_utf8_collate(word,tmp) == 0)) {
                                 FilePart translation = {0,0};
                                 translation.offset = trans_offset;
                                 memcpy(&(translation.length),
@@ -71,32 +73,34 @@ static gchar* word_translation_cache(XDXFData* data, gchar* word)
                         already += record_length;
                         buffer += record_length;
                 };
+               g_free(tmp);
 
                 if( ( bytes_readed < DICT_SEARCHING_WORD_TRAN_BUFF_SIZE ) || 
-                      ( readed > (file_size - 3) )) {
+                      ( readed > (file_size - 3) ))
+               {
                         break;
-                      };
-                      gnome_vfs_seek(data->cache,
-                                     GNOME_VFS_SEEK_CURRENT,
-                                     ((gint)max_length) - 
-                                          DICT_SEARCHING_WORD_TRAN_BUFF_SIZE
-                                    );
+               };
+               gnome_vfs_seek(data->cache,
+                       GNOME_VFS_SEEK_CURRENT,
+                       ((gint)max_length) -DICT_SEARCHING_WORD_TRAN_BUFF_SIZE
+                       );
         }
         return trans;
 }
 //------------------------------------------------------------------------------
 // searching word translation in xdxf dictionary
 static gchar* word_translation_xdxf(XDXFData* data, gchar* word) {
-        guint word_length = strlen(word);
+       gchar* casefold_word = g_utf8_casefold(word, -1);
+       guint word_length = strlen(casefold_word);
         gchar* trans = NULL;
         gnome_vfs_seek(data->xdxf, GNOME_VFS_SEEK_START, 0);
         GnomeVFSResult vfs_result;
-        GnomeVFSFileSize bytes_readed = DICT_SEARCHING_WORD_TRAN_BUFF_SIZE;                
-        gchar buffer[DICT_SEARCHING_WORD_TRAN_BUFF_SIZE+1];                
+        GnomeVFSFileSize bytes_readed = DICT_SEARCHING_WORD_TRAN_BUFF_SIZE;
+        gchar buffer[DICT_SEARCHING_WORD_TRAN_BUFF_SIZE+1];
         guint64 file_size = get_file_size(data->xdxf);
-                //guint word_len = strlen(word); added by me
-                
-        XML_Parser parser = XML_ParserCreate(NULL);        
+
+
+        XML_Parser parser = XML_ParserCreate(NULL);
         if (!parser) {
                 g_warning("XDXF/%s->%s() Could not open initialize "
                           "XML parser.\n",
@@ -109,7 +113,7 @@ static gchar* word_translation_xdxf(XDXFData* data, gchar* word) {
 
         gchar tmp[DICT_MAX_WORD_LENGTH];
         XDXFWordsTransData search_data = {tmp,
-                                          word,
+                                          casefold_word,
                                           word_length,
                                           0,
                                           NULL,
@@ -290,14 +294,17 @@ static void search_word_trans_end(void *data, const char *el)
 
         if( ('k' == el[0]) && ('\0' == el[1]) ) {
                 loc_data->one_word = 0;
-
-                gint com = g_utf8_collate(loc_data->last_word, loc_data->word);
-                if(com != 0) {
+               gchar* tmp = g_utf8_casefold(loc_data->last_word, -1);
+                gint com = g_utf8_collate(tmp, loc_data->word);
+               g_free(tmp); tmp = NULL;
+                if(com != 0)
+               {
                         //loc_data->cont = FALSE;
                        // dictionaries are not alway properly sorted, so for
                        // searching we should lookd in whole file!
                         return;
-                } else if((loc_data->last_word_length == loc_data->word_len) &&
+                }
+               else if((loc_data->last_word_length == loc_data->word_len) &&
                           (0 == com))
                {
                         loc_data->found = TRUE;
@@ -308,7 +315,6 @@ static void search_word_trans_end(void *data, const char *el)
         }
         else if( (TRUE == loc_data->found) && (0 == g_utf8_collate(el,"ar")) )
        {
-               g_debug("Found!\n");
                 loc_data->found = FALSE;
                 loc_data->cont = FALSE;
                 gulong last_stop = 
@@ -327,7 +333,7 @@ static void search_word_trans_text(void *data, const XML_Char *txt, int len)
         XDXFWordsTransData* loc_data = (XDXFWordsTransData*)data;
         if( (NULL != loc_data->translation ) || (FALSE == loc_data->cont) )  {
                //g_debug("STOP SEARCHING! -> _text\n");
-                return;
+               return;
         };
 
         if(1 == loc_data->one_word) {
@@ -1211,9 +1217,10 @@ static guint get_max_length(gchar* a, guint length)
         guint len = 0;
         guint n = 0;
         memcpy(&n,b,sizeof(guint));
-        while((len + n) <= (length - 4)) {
+        while((len + n) <= length) {
                 len += n;
                 b = b + n;
+               if(len >= (length-sizeof(guint))) break;
                 memcpy(&n,b,sizeof(guint));
         }
         return len;
@@ -1406,8 +1413,11 @@ static void word_list_cache(XDXFData* data, gchar* pattern, GArray* result) {
         guint already = 0;
         guint max_length = 0;
         guint64 file_size = get_file_size(data->cache);
+       gchar* casefold_pattern = g_utf8_casefold(pattern, -1);
+       gchar* casefold_word = NULL;
         guint pattern_len = strlen(pattern);
 
+       guint i = 0;
         while(TRUE)
        {
                 gnome_vfs_read(data->cache,
@@ -1422,10 +1432,12 @@ static void word_list_cache(XDXFData* data, gchar* pattern, GArray* result) {
 
                 guint how_far = 0;
                 while(how_far < max_length) {
+                       ++i;
                         memcpy(&record_length, buf, sizeof(guint));
+                       gchar* tmp = g_utf8_casefold(buf + sizeof(guint), -1);
                         if(((record_length - 3*sizeof(guint)) >= pattern_len) &&
-                           ( g_ascii_strncasecmp(buf + sizeof(guint),
-                                                 pattern,
+                           ( g_ascii_strncasecmp(tmp,
+                                                 casefold_pattern,
                                                  pattern_len
                                                 ) == 0 ) ) {
                                 gchar* new = g_strndup(buf + sizeof(guint),
@@ -1433,14 +1445,15 @@ static void word_list_cache(XDXFData* data, gchar* pattern, GArray* result) {
                                                                3*sizeof(guint));
                                 g_array_append_val(result, new);
                                 g_debug("New Word for pattern \"%s\" found: "
-                                        "%s\n",
+                                        "%s (pos:%d)\n",
                                         pattern,
-                                        new
+                                        new,
+                                       i
                                        );
                         };
                         how_far += record_length;
                         buf = buf + record_length;
-
+                       g_free(tmp); tmp = NULL;
                 }
                 if( (bytes_readed < DICT_SEARCHING_WORD_LIST_BUFF_SIZE) || 
                      (already > (file_size -3)) )
@@ -1453,6 +1466,7 @@ static void word_list_cache(XDXFData* data, gchar* pattern, GArray* result) {
                                     DICT_SEARCHING_WORD_LIST_BUFF_SIZE
                              );
         }
+       g_free(casefold_pattern); casefold_pattern = NULL;
 
         timer(TIMER_STOP,(gchar*)__FUNCTION__);
         timer(TIMER_START,"callback for returning words list START");