1 /*******************************************************************************
2 This file is part of mdictionary.
4 mdictionary is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 mdictionary is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with mdictionary; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 Copyright 2006-2008 ComArch S.A.
19 *******************************************************************************/
20 /** \defgroup XDXFEngine Dictionary Engine - XDXF format
21 * \brief XDXF-based dictionary engine.
23 * This is library with dictionary engine supporting XDXF dictionaries. XDXF is
24 * based on XML language. For more information, please go to:
25 * \li http://xdxf.sourceforge.net/
27 * TODO: currently XDXF engine does not support all function from engine API!
28 * It need implementation of API version 0.2 function, like getting icon path
32 /** \file engine_xdxf.h
33 * \brief Header for XDXF-based dictionary engine.
36 #ifndef _DICTIONARY_ENGINE_XDXF
37 #define _DICTIONARY_ENGINE_XDXF
43 /* headers with unix types/functions - onl for timers */
44 #include <sys/types.h>
50 #include <glib.h> /* header with GLIB definitions/functions/types */
51 #include <libgnomevfs/gnome-vfs.h> /* header with gnome-vfs - recommended I/O
53 #include <expat.h> /* header with expat - XML Parser API */
54 #include <string.h> /* manipulating strings */
55 #include <dictionary_engine.h> /* header wit engine API */
60 /** \brief Flags telling that we want to start timer. */
61 #define TIMER_START TRUE
62 /** \brief Flags telling that we want to stop timer. */
63 #define TIMER_STOP FALSE
65 /** \brief Start/stop timers.
67 * \param start do we want start new timer or end the last one
68 * \param message string which shoul be printed by function while debbuging
69 * \return -1.0 if we start or seconds passed from start if we want to
72 /* in final releases timer capapilities should be removed for increase
74 /* static double timer(gboolean start, gchar* message); */
79 /** \brief Version of XDXF engine. */
80 #define DIC_ENG_VERSION "0.1b"
82 /** \brief Short description of format supported by the current engine. */
83 #define DIC_ENG_FORMAT "XDXF"
85 /** \brief Buffer's length used while generating cache for dictionary. */
86 #define DICT_CACHEING_BUFF_SIZE 16*1024
88 /** \brief Buffer's length used while searching for words list. */
89 #define DICT_SEARCHING_WORD_LIST_BUFF_SIZE 16*1024
91 /** \brief Buffer's length used while searching for translation. */
92 #define DICT_SEARCHING_WORD_TRAN_BUFF_SIZE 16*1024
94 /** \brief Maximum length of word in dictionary.
96 * Engine use this value while searching in cache file. If this would be too
97 * low value engine would work incorrectly, but too big value will decrease
98 * performance of searching. 512 is optimal in most cases.
100 #define DICT_MAX_WORD_LENGTH 512
102 /** \brief Translate boolean value into string. */
103 #define PRINT_STATE(state) ( (state) ? "TRUE" : "FALSE" )
106 /** \brief Structure used while dict_eng_module_check() is working.
108 struct _XDXFCheckingData {
110 /**< \brief do we need to continue checking */
112 /**< \brief returned value telling if given file was proper XDXF
115 /**< \brief keep depth of XDXF structure while parsing file */
117 typedef struct _XDXFCheckingData XDXFCheckingData;
119 /** \brief Structure to help parse xdxf file for searching words list.
121 struct _XDXFWordsListData {
123 /**< \brief last found word */
125 /**< \brief pattern for words to search */
127 /**< \brief length of pattern */
128 guint last_word_length;
129 /**< \brief length of last found word */
131 /**< \brief result of searching - GArray with words matching pattern */
133 /**< \brief 1 while engine is parsing whole particular word (k tag), 0
136 /**< \brief do we need to continue searching */
138 typedef struct _XDXFWordsListData XDXFWordsListData;
140 /** \brief Structure to help parse xdxf file for searching word's translation.
142 struct _XDXFWordsTransData {
144 /**< \brief last found word in dictionary */
146 /**< \brief word to search translation for */
148 /**< \brief length of word */
149 guint last_word_length;
150 /**< \brief length of last found word */
152 /**< \brief found translation or NULL if such was not found */
154 /**< \brief 1 while engine is parsing whole particular word (k tag), 0
157 /**< \brief do we need to continue searching */
159 /**< \brief keeps offset in file of last found article
160 * (word plus translation) */
162 /**< \brief pointer to expat XML parser */
164 /**< \brief telling if translation was found */
165 GnomeVFSHandle* xdxf;
166 /**< \brief pointer to dictionary file */
168 typedef struct _XDXFWordsTransData XDXFWordsTransData;
170 /** \brief Structure to help make optimization possible
172 struct _XDXFCacheData {
174 /**< \brief buffer with output part of cache file */
176 /**< \brief keeps offset in file of the beggining of last found article
179 /**< \brief keeps offset in file of the end of last found article */
181 /**< \brief keeps length of last found article */
182 GnomeVFSHandle* cache;
183 /**< \brief pointer to cache file */
185 /**< \brief pointer to expat XML parser */
187 /**< \brief 1 if parser is parsing article key (word), 2 if parser is
188 * parsing article value (translation), 0 otherwise. */
190 /**< \brief buffer's length */
192 typedef struct _XDXFCacheData XDXFCacheData;
194 /** \brief Internal data structure for representing part of file.
198 /**< \brief offset in file of file part */
200 /**< \brief length of file part */
202 typedef struct _FilePart FilePart;
204 /** \brief Internal data structure of XDXF Engine.
207 GnomeVFSHandle* xdxf;
208 /**< \brief pointer to *.xdxf file */
209 GnomeVFSHandle* cache;
210 /**< \brief pointer to cache file */
212 /**< \brief path to dictionary */
213 EngineStatus last_error;
214 /**< \brief status of last taken action */
216 /**< \brief auto free mechanism status: FALSE - off, TRUE - on */
218 cb_progress cb_progress_caching;
219 /**< \brief pointer to callback function called while informing about
220 * caching progress */
221 gpointer cb_progress_caching_data;
222 /**< \brief pointer to data passed to callback function called while
223 * informing about caching progress */
224 gdouble cb_progress_caching_seed;
225 /**< \brief how often progress callback should be called. 0.01 mean
226 * that after each 1% of work callback shoul be called */
228 cb_progress cb_progress_word_list;
229 /**< \brief pointer to callback function called while informing about
230 * words list searching progress */
231 gpointer cb_progress_word_list_data;
232 /**< \brief pointer to data passed to callback function called while
233 * informing about words list searching progress */
234 gdouble cb_progress_word_list_seed;
235 /**< \brief how often progress callback should be called. 0.01 mean
236 * that after each 1% of work callback shoul be called */
238 cb_progress cb_progress_word_trans;
239 /**< \brief pointer to callback function called while informing about
240 * word's translation searching progress */
241 gpointer cb_progress_word_trans_data;
242 /**< \brief pointer to data passed to callback function called while
243 * informing about word's translation searching progress */
244 gdouble cb_progress_word_trans_seed;
245 /**< \brief how often progress callback should be called. 0.01 mean
246 * that after each 1% of work callback shoul be called */
248 cb_word_list cb_search_word_list;
249 /**< \brief pointer to callback function called after words list is
251 gpointer cb_search_word_list_data;
252 /**< \brief pointer to data passed to callback function called after
253 * words list is found */
255 cb_word_translation cb_search_word_trans;
256 /**< \brief pointer to callback function called after word's translation
258 gpointer cb_search_word_trans_data;
259 /**< \brief pointer to data passed to callback function called after
260 * word's translation is found */
262 typedef struct _XDXFData XDXFData;
265 /** \name Parsing Expat's callbacks */
268 /** \brief Checking XML file is proper XDXF file - tag starts.
270 * See Expat documentation for more information about parser callbacks.
272 static void is_xdxf_file_start(void *data,
276 /** \brief Checking XML file is proper XDXF file - tag ends.
278 * See Expat documentation for more information about parser callbacks.
280 static void is_xdxf_file_end(void *data, const char *el);
282 /** \brief Searching for words list - tag start.
284 * See Expat documentation for more information about parser callbacks.
286 static void search_word_list_start(void *data,
289 /** \brief Searching for words list - tag ends.
291 * See Expat documentation for more information about parser callbacks.
293 static void search_word_list_end(void *data, const char *el);
295 /** \brief Searching for words list - text node.
297 * See Expat documentation for more information about parser callbacks.
299 static void search_word_list_text(void *data, const XML_Char *txt, int len);
301 /** \brief Searching for word's translation - tag start.
303 * See Expat documentation for more information about parser callbacks.
305 static void search_word_trans_start(void *data,
309 /** \brief Searching for word's translation - tag ends.
311 * See Expat documentation for more information about parser callbacks.
313 static void search_word_trans_end(void *data, const char *el);
315 /** \brief Searching for word's translation - text node.
317 * See Expat documentation for more information about parser callbacks.
319 static void search_word_trans_text(void *data,
325 /** \brief Return particular part of file. */
326 static gchar* read_file_part(FilePart* part, GnomeVFSHandle* file);
328 /** \brief Convert string to proper path name. */
329 static gchar* string_to_path(gchar** string);
331 /** \brief Tells if file is in XDXF format (file should exist). */
332 static gboolean is_xdxf_file(gchar* file);
334 /** \brief Get file's lenght. */
335 static guint64 get_file_size(GnomeVFSHandle* file);
337 /** \brief Return how many records (from cache file) are in the current buffer.
339 static guint get_max_length(gchar* a, guint length);
341 /** \brief Searching for word's translation in cache file. */
342 static gchar* word_translation_cache(XDXFData* data, gchar* word);
344 /** \brief Searching for word's translation in XDXF file. */
345 static gchar* word_translation_xdxf(XDXFData* data, gchar* word);
347 /** \brief Searching for words list in cache file. */
348 static void word_list_cache(XDXFData* data,
353 /** \brief Searching for words list in XDXF file. */
354 static void word_list_xdxf(XDXFData* data,
359 /** \name Module functions */
362 /** \brief dict_eng_module_check() function implementation. */
363 gboolean xdxf_engine_check(gchar* location);
365 /** \brief dict_eng_module_get_description() function implementation. */
366 gchar* xdxf_engine_description();
368 /** \brief dict_eng_module_get_format() function implementation. */
369 gchar* xdxf_engine_format();
371 /** \brief dict_eng_module_get_version() function implementation. */
372 gchar* xdxf_engine_version();
374 /** \brief dict_eng_module_create() function implementation. */
375 Engine* xdxf_engine_create(gchar* location,
376 EngineOptimizationFlag flags,
377 cb_progress progress_handler,
378 gpointer progress_data,
383 /** \name Particular dictionary function */
386 /** \brief dict_eng_destroy() function implementation. */
387 void xdxf_engine_close(Engine* engine);
389 /** \brief dict_eng_get_location() function implementation. */
390 gchar* xdxf_engine_location(Engine* engine);
392 /** \brief dict_eng_optimize() function implementation. */
393 void xdxf_engine_optimize(Engine* engine);
395 /** \brief dict_eng_is_optimized() function implementation. */
396 gboolean xdxf_engine_is_optimized(Engine* engine);
398 /** \brief dict_eng_set_auto_free() function implementation. */
399 void xdxf_engine_set_auto_free(Engine* engine, gboolean state);
401 /** \brief dict_eng_set_callback() function implementation. */
402 gpointer xdxf_engine_set_callbacks(Engine* engine,
407 /** \brief dict_eng_set_progress_seed() function implementation. */
408 void xdxf_engine_set_progress_seed(Engine* engine,
412 /** \brief dict_eng_search_word_list() function implementation. */
413 void xdxf_engine_search_word_list(Engine* engine,
417 /** \brief dict_eng_search_word_translation() function implementation. */
418 void xdxf_engine_search_word_translation(Engine* engine,
422 /*** \brief dict_eng_search_word_translation_extended() function implementation.
424 /* this function was removed from engine API */
425 /*void xdxf_engine_search_word_translation_extended(Engine* engine,
429 /** \brief dict_eng_get_last_status() function implementation. */
430 EngineStatus xdxf_engine_error(Engine* engine);
432 /** \brief dict_eng_status_message() function implementation. */
433 gchar* xdxf_engine_error_message(EngineStatus error);
435 /** \brief dict_eng_add_word() function implementation. */
436 gboolean xdxf_engine_add_word(Engine* engine,
440 /** \brief dict_eng_remove_word() function implementation. */
441 gboolean xdxf_engine_remove_word(Engine* engine, gchar* word);
444 /** \brief implementation of engine_global_functions(void) function. */
445 EngineModule engine_global_functions();