1 /*
2  * Copyright 2011 kubtek <kubtek@mail.com>
3  *
4  * This file is part of StarDict.
5  *
6  * StarDict 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 3 of the License, or
9  * (at your option) any later version.
10  *
11  * StarDict 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with StarDict.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef _DICTBASE_H_
21 #define _DICTBASE_H_
22 
23 #include <glib.h>
24 #include <vector>
25 #include <string>
26 #include <memory>
27 #include <stdio.h>
28 
29 #include "dictziplib.h"
30 
31 enum InstantDictType {
32 	InstantDictType_UNKNOWN = 0,
33 	InstantDictType_LOCAL,
34 	InstantDictType_VIRTUAL,
35 	InstantDictType_NET,
36 };
37 
38 struct InstantDictIndex {
39 	InstantDictType type;
40 	size_t index;
41 };
42 
43 struct cacheItem {
44   guint32 offset;
45 	gchar *data;
cacheItemcacheItem46   cacheItem() {data= NULL;}
~cacheItemcacheItem47   ~cacheItem() {g_free(data);}
48 };
49 
50 const int WORDDATA_CACHE_NUM = 10;
51 const int UNSET_INDEX = -1;
52 const int INVALID_INDEX=-100;
53 extern const gchar* const DICT_DATA_TYPE_SEARCH_DATA_STR;
54 
55 
56 class DictBase {
57 public:
58 	DictBase();
59 	~DictBase();
60 	bool load(const std::string& filebasename, const char* mainext);
61 	gchar * GetWordData(guint32 idxitem_offset, guint32 idxitem_size);
containSearchData()62 	bool containSearchData() {
63 		if (sametypesequence.empty())
64 			return true;
65 
66 		return sametypesequence.find_first_of(DICT_DATA_TYPE_SEARCH_DATA_STR) !=
67 			std::string::npos;
68 	}
69 	bool SearchData(std::vector<std::string> &SearchWords, guint32 idxitem_offset, guint32 idxitem_size, gchar *origin_data);
70 protected:
71 	std::string sametypesequence;
72 private:
73 	FILE *dictfile;
74 	std::auto_ptr<dictData> dictdzfile;
75 	cacheItem cache[WORDDATA_CACHE_NUM];
76 	gint cache_cur;
77 };
78 
79 #endif//!_DICTBASE_H_
80