minor build fix
[qstardict] / plugins / stardict / lib.h
1 #ifndef __SD_LIB_H__
2 #define __SD_LIB_H__
3
4 #include <cstdio>
5 #include <list>
6 #include <memory>
7 #include <string>
8 #include <vector>
9
10 #include "dictziplib.hpp"
11
12 const int MAX_MATCH_ITEM_PER_LIB = 100;
13 const int MAX_FUZZY_DISTANCE = 3; // at most MAX_FUZZY_DISTANCE-1 differences allowed when find similar words
14
15 struct cacheItem
16 {
17     guint32 offset;
18     gchar *data;
19     //write code here to make it inline
20     cacheItem()
21     {
22         data = NULL;
23     }
24     ~cacheItem()
25     {
26         g_free(data);
27     }
28 };
29
30 const int WORDDATA_CACHE_NUM = 10;
31 const int INVALID_INDEX = -100;
32
33 class DictBase
34 {
35     public:
36         DictBase();
37         ~DictBase();
38         gchar * GetWordData(guint32 idxitem_offset, guint32 idxitem_size);
39         bool containSearchData();
40         bool SearchData(std::vector<std::string> &SearchWords, guint32 idxitem_offset, guint32 idxitem_size, gchar *origin_data);
41     protected:
42         std::string sametypesequence;
43         FILE *dictfile;
44         std::auto_ptr<dictData> dictdzfile;
45     private:
46         cacheItem cache[WORDDATA_CACHE_NUM];
47         gint cache_cur;
48 };
49
50 //this structure contain all information about dictionary
51 struct DictInfo
52 {
53     std::string ifo_file_name;
54     guint32 wordcount;
55     std::string bookname;
56     std::string author;
57     std::string email;
58     std::string website;
59     std::string date;
60     std::string description;
61     guint32 index_file_size;
62     std::string sametypesequence;
63     bool load_from_ifo_file(const std::string& ifofilename, bool istreedict);
64 };
65
66 class index_file
67 {
68     public:
69         guint32 wordentry_offset;
70         guint32 wordentry_size;
71
72         virtual ~index_file()
73         {}
74         virtual bool load(const std::string& url, gulong wc, gulong fsize) = 0;
75         virtual const gchar *get_key(glong idx) = 0;
76         virtual void get_data(glong idx) = 0;
77         virtual const gchar *get_key_and_data(glong idx) = 0;
78         virtual bool lookup(const char *str, glong &idx) = 0;
79 };
80
81 class Dict : public DictBase
82 {
83     private:
84         std::string ifo_file_name;
85         gulong wordcount;
86         std::string bookname;
87
88         std::auto_ptr<index_file> idx_file;
89
90         bool load_ifofile(const std::string& ifofilename, gulong &idxfilesize);
91     public:
92         Dict()
93         {}
94         bool load(const std::string& ifofilename);
95
96         gulong narticles()
97         {
98             return wordcount;
99         }
100         const std::string& dict_name()
101         {
102             return bookname;
103         }
104         const std::string& ifofilename()
105         {
106             return ifo_file_name;
107         }
108
109         const gchar *get_key(glong index)
110         {
111             return idx_file->get_key(index);
112         }
113         gchar *get_data(glong index)
114         {
115             idx_file->get_data(index);
116             return DictBase::GetWordData(idx_file->wordentry_offset, idx_file->wordentry_size);
117         }
118         void get_key_and_data(glong index, const gchar **key, guint32 *offset, guint32 *size)
119         {
120             *key = idx_file->get_key_and_data(index);
121             *offset = idx_file->wordentry_offset;
122             *size = idx_file->wordentry_size;
123         }
124         bool Lookup(const char *str, glong &idx)
125         {
126             return idx_file->lookup(str, idx);
127         }
128
129         bool LookupWithRule(GPatternSpec *pspec, glong *aIndex, int iBuffLen);
130 };
131
132 typedef std::list<std::string> strlist_t;
133
134 class Libs
135 {
136     public:
137         typedef void (*progress_func_t)(void);
138
139         Libs(progress_func_t f = NULL);
140         ~Libs();
141         void load_dict(const std::string& url);
142         void load(const strlist_t& dicts_dirs,
143                   const strlist_t& order_list,
144                   const strlist_t& disable_list);
145         void reload(const strlist_t& dicts_dirs,
146                     const strlist_t& order_list,
147                     const strlist_t& disable_list);
148
149         glong narticles(int idict)
150         {
151             return oLib[idict]->narticles();
152         }
153         const std::string& dict_name(int idict)
154         {
155             return oLib[idict]->dict_name();
156         }
157         gint ndicts()
158         {
159             return oLib.size();
160         }
161
162         const gchar * poGetWord(glong iIndex, int iLib)
163         {
164             return oLib[iLib]->get_key(iIndex);
165         }
166         gchar * poGetWordData(glong iIndex, int iLib)
167         {
168             if (iIndex == INVALID_INDEX)
169                 return NULL;
170             return oLib[iLib]->get_data(iIndex);
171         }
172         const gchar *poGetCurrentWord(glong *iCurrent);
173         const gchar *poGetNextWord(const gchar *word, glong *iCurrent);
174         const gchar *poGetPreWord(glong *iCurrent);
175         bool LookupWord(const gchar* sWord, glong& iWordIndex, int iLib)
176         {
177             return oLib[iLib]->Lookup(sWord, iWordIndex);
178         }
179         bool LookupSimilarWord(const gchar* sWord, glong & iWordIndex, int iLib);
180         bool SimpleLookupWord(const gchar* sWord, glong & iWordIndex, int iLib);
181
182
183         bool LookupWithFuzzy(const gchar *sWord, gchar *reslist[], gint reslist_size, gint iLib);
184         gint LookupWithRule(const gchar *sWord, gchar *reslist[]);
185         bool LookupData(const gchar *sWord, std::vector<gchar *> *reslist);
186     private:
187         std::vector<Dict *> oLib; // word Libs.
188         int iMaxFuzzyDistance;
189         progress_func_t progress_func;
190 };
191
192
193 typedef enum {
194     qtSIMPLE, qtREGEXP, qtFUZZY, qtDATA
195 } query_t;
196
197 extern query_t analyze_query(const char *s, std::string& res);
198
199 #endif//!__SD_LIB_H__