2 * This file part of sdcv - console version of Stardict program
3 * http://sdcv.sourceforge.net
4 * Copyright (C) 2005-2006 Evgeniy <dushistov@mail.ru>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Library General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <glib/gi18n.h>
28 #include "libwrapper.hpp"
29 #include "mstardict.hpp"
31 static std::string xdxf2text(const char *p)
36 if (g_str_has_prefix(p, ">")) {
39 } else if (g_str_has_prefix(p, "<")) {
42 } else if (g_str_has_prefix(p, "&")) {
45 } else if (g_str_has_prefix(p, """)) {
53 const char *next=strchr(p, '>');
57 std::string name(p+1, next-p-1);
61 else if (name=="/abr")
64 const char *begin=next;
65 if ((next=strstr(begin, "</k>"))!=NULL)
66 next+=sizeof("</k>")-1-1;
85 else if (!name.empty() && name[0]=='c' && name!="co") {
86 std::string::size_type pos=name.find("code");
87 if (pos!=std::string::size_type(-1)) {
88 pos+=sizeof("code=\"")-1;
89 std::string::size_type end_pos=name.find("\"");
90 std::string color(name, pos, end_pos-pos);
95 } else if (name=="/c")
103 static string parse_data(const gchar *data)
109 guint32 data_size, sec_size=0;
112 data_size=*((guint32 *)p);
114 while (guint32(p - data)<data_size) {
118 case 'l': //need more work...
119 sec_size = strlen(p);
122 m_str = g_strndup(p, sec_size);
129 sec_size = strlen(p);
132 m_str = g_strndup(p, sec_size);
133 res += xdxf2text(m_str);
139 sec_size = strlen(p);
142 m_str = g_strndup(p, sec_size);
143 res += "["+string(m_str)+"]";
149 sec_size = strlen(p);
154 sec_size=*((guint32 *)p);
155 sec_size+=sizeof(guint32);
164 void Library::ListWords(CurrentIndex* iIndex)
166 CurrentIndex *iCurrent = (CurrentIndex*)g_memdup(iIndex, sizeof(CurrentIndex)*query_dictmask.size());
168 pMStarDict->ResultsListClear();
171 const gchar * poCurrentWord = poGetCurrentWord(iIndex, query_dictmask, 0);
173 pMStarDict->ResultsListInsertLast(poCurrentWord);
176 while (iWordCount < 30 && (poCurrentWord = poGetNextWord(NULL, iIndex, query_dictmask, 0))) {
177 pMStarDict->ResultsListInsertLast(poCurrentWord);
181 pMStarDict->ReScroll();
187 bool Library::BuildResultData(std::vector<InstantDictIndex> &dictmask, const char* sWord, CurrentIndex *iIndex, int iLib, TSearchResultList& res_list)
190 bool bFound = false, bLookupWord = false, bLookupSynonymWord = false;
191 gint nWord=0, count=0, i=0, j=0;
193 iRealLib = dictmask[iLib].index;
195 bLookupWord = LookupWord(sWord, iIndex[iLib].idx, iIndex[iLib].idx_suggest, iRealLib, 0);
197 bLookupWord = LookupSimilarWord(sWord, iIndex[iLib].idx, iIndex[iLib].idx_suggest, iRealLib, 0);
199 bLookupWord = SimpleLookupWord(sWord, iIndex[iLib].idx, iIndex[iLib].idx_suggest, iRealLib, 0);
201 bLookupSynonymWord = LookupSynonymWord(sWord, iIndex[iLib].synidx, iIndex[iLib].synidx_suggest, iRealLib, 0);
202 if (!bLookupSynonymWord)
203 bLookupSynonymWord = LookupSynonymSimilarWord(sWord, iIndex[iLib].synidx, iIndex[iLib].synidx_suggest, iRealLib, 0);
204 if (!bLookupSynonymWord)
205 bLookupSynonymWord = SimpleLookupSynonymWord(sWord, iIndex[iLib].synidx, iIndex[iLib].synidx_suggest, iRealLib, 0);
207 g_debug ("bookname: %s, iLib: %d, iRealLib: %d, str: %s", dict_name(iLib).c_str(), iLib, iRealLib, sWord);
209 if (bLookupWord || bLookupSynonymWord) {
213 if (bLookupSynonymWord)
214 nWord+=GetOrigWordCount(iIndex[iLib].synidx, iRealLib, false);
217 count = GetOrigWordCount(iIndex[iLib].idx, iRealLib, true);
218 for (i=0;i<count;i++) {
219 res_list.push_back(TSearchResult(dict_name(iLib),
220 poGetWord(iIndex[iLib].idx, iRealLib, 0),
221 parse_data(poGetOrigWordData(iIndex[iLib].idx+i, iRealLib))));
227 for (j = 0; i < nWord; i++, j++) {
228 res_list.push_back(TSearchResult(dict_name(iLib),
229 poGetWord(iIndex[iLib].synidx+j, iRealLib, 0),
230 parse_data(poGetOrigWordData(iIndex[iLib].synidx+j, iRealLib))));
239 bool Library::SimpleLookup(const gchar* sWord, CurrentIndex* piIndex)
241 CurrentIndex *iIndex;
242 TSearchResultList results;
246 iIndex = (CurrentIndex *)g_malloc(sizeof(CurrentIndex) * query_dictmask.size());
250 for (size_t iLib=0; iLib<query_dictmask.size(); iLib++) {
251 bFound = BuildResultData(query_dictmask, sWord, iIndex, iLib, results);
260 void Library::LookupWithFuzzy(const string &str, TSearchResultList& res_list)
262 static const int MAXFUZZY=10;
264 gchar *fuzzy_res[MAXFUZZY];
265 if (!Libs::LookupWithFuzzy(str.c_str(), fuzzy_res, MAXFUZZY, query_dictmask))
268 for (gchar **p=fuzzy_res, **end=fuzzy_res+MAXFUZZY;
270 // SimpleLookup(*p, res_list);
275 void Library::LookupWithRule(const string &str, TSearchResultList& res_list)
277 std::vector<gchar *> match_res((MAX_MATCH_ITEM_PER_LIB) * query_dictmask.size());
279 gint nfound=Libs::LookupWithRule(str.c_str(), &match_res[0], query_dictmask);
283 for (gint i=0; i<nfound; ++i) {
284 // SimpleLookup(match_res[i], res_list);
285 g_free(match_res[i]);
289 void Library::LookupData(const string &str, TSearchResultList& res_list)
292 std::vector<gchar *> drl[query_dictmask.size()];
293 if (!Libs::LookupData(str.c_str(), drl, NULL, NULL, &cancel, query_dictmask))
295 for (size_t iLib=0; iLib<query_dictmask.size(); iLib++)
296 for (std::vector<gchar *>::size_type j=0; j<drl[iLib].size(); ++j) {
297 // SimpleLookup(drl[iLib][j], res_list);
298 g_free(drl[iLib][j]);
302 Library::Library () : Libs (NULL, FALSE, 0, 0)
304 iCurrentIndex = NULL;
310 g_free (iCurrentIndex);