src/plugins/stardict/include/engine_stardict.h

00001 /*******************************************************************************
00002 This file is part of mdictionary.
00003 
00004 mdictionary is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 mdictionary is distributed in the hope that it will be useful, 
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License 
00015 along with mdictionary; if not, write to the Free Software
00016 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00017 
00018 Copyright 2006 ComArch S.A.
00019 *******************************************************************************/
00020 #ifndef _DICTIONARY_ENGINE_STARDICT
00021 #define _DICTIONARY_ENGINE_STARDICT
00022 
00023 #ifdef __cplusplus
00024         extern "C" {
00025 #endif
00026 
00027 #ifndef NOLOGS
00028         #define sd_timer timer
00029 #else
00030         #define sd_timer(flag,name) while(FALSE)
00031 #endif
00032 
00033 //______________________________________________________________________________
00034 // *****************************************************************************
00035 //************************************************************* HEADERS SECTION:
00036 //------------------------------------------------------------------------------
00037 // headers with unix types/functions - only for timers and files operations
00038 #include <sys/types.h>
00039 #include <sys/stat.h>
00040 #include <sys/time.h>
00041 #include <fcntl.h>
00042 #include <unistd.h>
00043 //------------------------------------------------------------------------------
00044 // header with GLIB definitions/functions/types
00045 #include <glib.h>
00046 //------------------------------------------------------------------------------
00047 // header with gnome-vfs - recommended I/O API for maemo
00048 #include <libgnomevfs/gnome-vfs.h>
00049 //------------------------------------------------------------------------------
00050 // header with engine API
00051 #include <dictionary_engine.h>
00052 //------------------------------------------------------------------------------
00053 // header with standard gzip support library
00054 #include <zlib.h>
00055 //------------------------------------------------------------------------------
00056 // header with dict zip support library
00057 //#include "dictzip.h"
00058 #include <data.h>
00059 //#include "defs.h"
00060 //------------------------------------------------------------------------------
00061 // headero for ntohl conversion - network byte order to local host order
00062 #include <netinet/in.h>
00063 //------------------------------------------------------------------------------
00064 // header to g_memmove only
00065 #include <string.h>
00066 
00067 //______________________________________________________________________________
00068 // *****************************************************************************
00069 //********************************************************* DEFINITIONS SECTION:
00070 //------------------------------------------------------------------------------
00071 // definitions for timer function - flag telling if we want to start or stop
00072 // timing
00073 #define TIMER_START     TRUE
00074 #define TIMER_STOP      FALSE
00075 
00076 #define WORD_LIST_BUFFER_LENGTH 16*1024
00077 
00078 #define _MIN(a,b)       ((a)<(b))?(a):(b)
00079 //------------------------------------------------------------------------------
00080 // definitions of version and format which engine handles
00081 static const gchar* DIC_ENG_VERSION = "0.1"; 
00082 static const gchar* DIC_ENG_FORMAT = "StarDict";
00083 static const gchar* DIC_ENG_DESCRIPTION = "This module operates on StarDict dictionaries. Version 0.1.";
00084 
00085 //------------------------------------------------------------------------------
00086 // macro for "printing" gboolean statement - "TRUE" or "FALSE"
00087 #define PRINT_STATE(state) ( (state) ? "TRUE" : "FALSE" )
00088 //------------------------------------------------------------------------------
00089 
00090 #define ICON_PATH "/usr/share/pixmaps/stardict_icon.png"
00091 
00092 //______________________________________________________________________________
00093 // *****************************************************************************
00094 //****************************************** DATA STRUCTURE DEFINITIONS SECTION:
00095 //------------------------------------------------------------------------------
00096 //------------------------------------------------------------------------------
00099 struct _FilePart {
00100         guint offset;
00101         guint length;
00102 };
00103 typedef struct _FilePart        FilePart;
00104 //------------------------------------------------------------------------------
00107 struct _SDData {
00108         GnomeVFSHandle*         dictionary;
00109         GnomeVFSHandle*         index;
00110 
00111         gchar*                  dict_path;
00112         gchar*                  ifo_file_name;
00113         gchar*                  idx_file_name;
00114         gboolean                idx_compressed;
00115         gchar*                  dic_file_name;
00116         gboolean                dic_compressed;
00117         gchar*                  lang_from;
00118         gchar*                  lang_to;
00119         gchar*                  title;
00120         gint                    word_count;
00121         gint                    idx_file_length;
00122         gchar*                  types;
00123         gchar*                  icon;
00124 
00125         EngineStatus            last_error;
00126         gboolean                auto_free;
00127 
00128         /* wrapper functions to access idx and dict files without checking
00129          * it they are compressed or not - we decide about this while creating
00130          * engine i function sd_engine_create()*/
00131         gpointer        (*sd_open_idx)(gchar* filename);
00132         //gpointer      (*sd_open_dic)(gchar* filename);
00133         gint            (*sd_read_idx)(gpointer f, gchar* buffer, gint l);
00134         //gint          (*sd_read_dic)(gpointer f, gchar* buffer, gint l);
00135         glong           (*sd_seek_idx)(gpointer f, glong l, gchar from);
00136         //glong         (*sd_seek_dic)(gpointer f, glong l, gchar from);
00137         void            (*sd_close_idx)(gpointer f);
00138         //void          (*sd_close_dic)(gpointer f);
00139         gchar*          (*sd_read_dic_part)(FilePart* part, gchar* file);
00140         gpointer        idx_file;
00141 
00142         cb_progress             cb_progress_caching;
00143         gpointer                cb_progress_caching_data;
00144         gdouble                 cb_progress_caching_seed;
00145 
00146         cb_progress             cb_progress_word_list;
00147         gpointer                cb_progress_word_list_data;
00148         gdouble                 cb_progress_word_list_seed;
00149 
00150         cb_progress             cb_progress_word_trans;
00151         gpointer                cb_progress_word_trans_data;
00152         gdouble                 cb_progress_word_trans_seed;
00153 
00154         cb_word_list            cb_search_word_list;
00155         gpointer                cb_search_word_list_data;
00156 
00157         cb_word_translation     cb_search_word_trans;
00158         gpointer                cb_search_word_trans_data;      
00159 };
00160 typedef struct _SDData        SDData;
00161 //------------------------------------------------------------------------------
00162 
00163 
00164 //______________________________________________________________________________
00165 // *****************************************************************************
00166 //************************************************ ADDITIONAL FUNCTIONS SECTION:
00167 //------------------------------------------------------------------------------
00168 // returning concrete part of file
00169 //static gchar*           read_file_part(FilePart* part, GnomeVFSHandle* file);
00170 //------------------------------------------------------------------------------
00171 // convert string to proper path name (no filename, no "/" at the ned, file
00172 // exist)
00173 static gchar*           string_to_path(gchar** string);
00174 //------------------------------------------------------------------------------
00175 // fill out filenames of dictionary from directory in data->dict_path
00176 gboolean                sd_read_files_names(SDData* data);
00177 //------------------------------------------------------------------------------
00178 // fill out few information from IFO file
00179 gboolean                sd_parse_ifo_file(SDData* data);
00180 //------------------------------------------------------------------------------
00181 // parse concrete record from *.ifo files - used by sd_parse_ifo_file() function
00182 void                    sd_parse_record(SDData* data,gchar* key, gchar* value);
00183 //------------------------------------------------------------------------------
00184 // start/stop timers -  return -1.0 if we start or seconds passed from start 
00185 // if we want to stop timer
00186 static double           timer(gboolean start, gchar* message);
00187 //------------------------------------------------------------------------------
00188 // find proper FilePart structure for concrete word in dictionary
00189 FilePart*               sd_find_file_part(SDData* data, gchar* word);
00190 //------------------------------------------------------------------------------
00191 // parse whole article to proper word translation
00192 gchar*                  sd_parse_stardict_article(gchar* buf,
00193                                                 gchar* type,
00194                                                 guint length
00195                                                 );
00196 //------------------------------------------------------------------------------
00197 // find and return only one, next field from buffer and update length variable
00198 gchar*                  sd_get_buffer_from_article(gchar** buffer,
00199                                                 guint* length
00200                                                 );
00201 //------------------------------------------------------------------------------
00202 // return size of files
00203 //static guint64          get_file_size(GnomeVFSHandle* file);
00204 //------------------------------------------------------------------------------
00205 // these are wrapper functions to acces different type of files: standard or
00206 // compressed with gzip for *.idx or dictzip for *.dict files.
00207 
00208 #define _FILES_WRAPPER_BEG 'b'
00209 #define _FILES_WRAPPER_CUR 'c'
00210 #define _FILES_WRAPPER_END 'e'
00211 
00212 // standard
00213 static gpointer         sd_open(gchar* filename);
00214 static gint             sd_read(gpointer f, gchar* buffer, gint l);
00215 static glong            sd_seek(gpointer f, glong l, gchar from);
00216 static void             sd_close(gpointer f);
00217 // gzipped
00218 static gpointer         sd_open_z(gchar* filename);
00219 static gint             sd_read_z(gpointer f, gchar* buffer, gint l);
00220 static glong            sd_seek_z(gpointer f, glong l, gchar from);
00221 static void             sd_close_z(gpointer f);
00222 // files *.dict[.dz]
00223 //------------------------------------------------------------------------------
00224 // read concrete file part from dictionary - *.dict* file
00225 static gchar*           sd_read_file_part_dz(FilePart* part, gchar* file);
00226 static gchar*           sd_read_file_part(FilePart* part, gchar* file);
00227 //------------------------------------------------------------------------------
00228 
00229 
00230 
00231 
00232 //______________________________________________________________________________
00233 // *****************************************************************************
00234 //****************************************************** MAIN FUNCTIONS SECTION:
00235 //------------------------------------------------------------------------------
00236 gboolean        sd_engine_add_word(Engine* engine,
00237                                  gchar*  word,
00238                                  gchar*  translation);
00239 //------------------------------------------------------------------------------
00240 gboolean        sd_engine_remove_word(Engine* engine,
00241                                      gchar*  word);  
00242 //------------------------------------------------------------------------------      
00243 gchar*          sd_engine_get_lang_from(Engine* engine);
00244 //------------------------------------------------------------------------------
00245 gchar*          sd_engine_get_lang_to(Engine* engine);
00246 //------------------------------------------------------------------------------
00247 gchar*          sd_engine_get_title(Engine* engine);
00248 //------------------------------------------------------------------------------
00249 gchar*          sd_engine_get_icon_path(Engine* engine);
00250 //------------------------------------------------------------------------------
00251 
00252 
00253 
00254 //------------------------------------------------------------------------------
00255 // implementation of dict_eng_module_check(module,location) function
00256 gboolean        sd_engine_check(gchar* location);
00257 //------------------------------------------------------------------------------
00258 // implementation of dict_eng_module_get_description(module) function
00259 gchar*          sd_engine_description();
00260 //------------------------------------------------------------------------------
00261 // implementation of dict_eng_module_get_format(module) function
00262 gchar*          sd_engine_format();
00263 //------------------------------------------------------------------------------
00264 // implementation of dict_eng_module_get_version(module) function
00265 gchar*          sd_engine_version();
00266 //------------------------------------------------------------------------------
00267 // implementation of dict_eng_module_create(module,location,flags) and
00268 // dict_eng_module_create_ext(module,location,flags) functions
00269 Engine*         sd_engine_create(gchar* location, 
00270                               EngineOptimizationFlag flags,
00271                               cb_progress progress_handler,
00272                               gpointer progress_data,
00273                               gdouble seed);
00274 //------------------------------------------------------------------------------
00275 // implementation of dict_eng_destroy(engine) function
00276 void            sd_engine_close(Engine* engine);
00277 //------------------------------------------------------------------------------
00278 // implementation of dict_eng_get_location(engine) function
00279 gchar*          sd_engine_location(Engine* engine);
00280 //------------------------------------------------------------------------------
00281 // implementation of dict_eng_optimize(engine) function
00282 void            sd_engine_optimize(Engine* engine);
00283 //------------------------------------------------------------------------------
00284 // implementation of dict_eng_is_optimized( engine ) function
00285 gboolean        sd_engine_is_optimized(Engine* engine);
00286 //------------------------------------------------------------------------------
00287 // implementation of dict_eng_set_auto_free(engine, state) function
00288 void            sd_engine_set_auto_free(Engine* engine, gboolean state);
00289 //------------------------------------------------------------------------------
00290 // implementation of dict_eng_set_callback(engine,signal,c_handler,data) 
00291 // function
00292 gpointer        sd_engine_set_callback(Engine* engine,
00293                                      gchar* event,
00294                                      gpointer c_handler,
00295                                      gpointer user_data);
00296 //------------------------------------------------------------------------------
00297 // implementation of dict_eng_set_progress_seed(engine, signal, val) function
00298 void            sd_engine_set_progress_seed(Engine* engine,
00299                                          gchar* signal,
00300                                          gdouble seed);
00301 //------------------------------------------------------------------------------
00302 // implementation ofdict_eng_search_word_list(engine,pattern) function
00303 void            sd_engine_search_word_list(Engine* engine,
00304                                            gchar* pattern,
00305                                            gpointer cb_data);
00306 //------------------------------------------------------------------------------
00307 // implementation of dict_eng_search_word_translation(engine,word) function
00308 void            sd_engine_search_word_translation(Engine* engine,
00309                                                   gchar* word,
00310                                                   gpointer cb_data);
00311 //------------------------------------------------------------------------------
00312 // implementation of dict_eng_get_last_state(engine) function
00313 EngineStatus    sd_engine_status(Engine* engine);
00314 //------------------------------------------------------------------------------
00315 // implementation of dict_eng_state_message(error) function
00316 gchar*          sd_engine_status_message(EngineStatus status);
00317 //------------------------------------------------------------------------------
00318 // implementation of engine_global_functions(void) function
00319 EngineModule    engine_global_functions();
00320 
00321 #ifdef __cplusplus
00322 }
00323 #endif
00324 #endif /* #ifndef _DICTIONARY_ENGINE_STARDICT */

Generated on Fri Jan 11 14:30:17 2008 for mDictionary Project by  doxygen 1.5.1