2 * This file part of StarDict - A international dictionary for GNOME.
3 * http://stardict.sourceforge.net
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Implementation of class to work with GtkTree
22 * based StarDict's dictionaries
29 #include "getuint32.h"
31 #include "treedict.hpp"
33 GtkTreeStore *TreeDict::model=NULL;
40 // It is said G_TYPE_UINT will always be 32 bit.
41 // see http://bugzilla.gnome.org/show_bug.cgi?id=337966
42 model = gtk_tree_store_new (3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT); //word, offset, size
45 bool TreeDict::load(const std::string& ifofilename)
48 if (!load_ifofile(ifofilename, &tdxfilesize))
51 std::string fullfilename(ifofilename);
52 fullfilename.replace(fullfilename.length()-sizeof("ifo")+1, sizeof("ifo")-1, "dict.dz");
54 if (g_file_test(fullfilename.c_str(), G_FILE_TEST_EXISTS)) {
55 dictdzfile.reset(new dictData);
56 if (!dictdzfile->open(fullfilename, 0)) {
57 //g_print("open file %s failed!\n",fullfilename);
61 fullfilename.erase(fullfilename.length()-sizeof(".dz")+1, sizeof(".dz")-1);
62 dictfile = fopen(fullfilename.c_str(),"rb");
64 //g_print("open file %s failed!\n",fullfilename);
69 fullfilename=ifofilename;
70 fullfilename.replace(fullfilename.length()-sizeof("ifo")+1, sizeof("ifo")-1, "tdx.gz");
73 if (g_file_test(fullfilename.c_str(), G_FILE_TEST_EXISTS)) {
75 in = gzopen(fullfilename.c_str(),"rb");
77 //g_print("Open file %s failed!\n",idxfilename);
81 buffer = (gchar *)g_malloc(tdxfilesize);
84 len = gzread(in, buffer, tdxfilesize);
90 if (len != tdxfilesize) {
95 fullfilename.erase(fullfilename.length()-sizeof(".gz")+1, sizeof(".gz")-1);
97 if (!(file = fopen (fullfilename.c_str(), "rb"))) {
98 //g_print("Open file %s failed!\n",fullfilename);
101 buffer = (gchar *)g_malloc(tdxfilesize);
103 read_len = fread(buffer, 1, tdxfilesize, file);
105 if (read_len!=tdxfilesize) {
111 gchar *tmp_buffer = buffer;
112 load_model(&tmp_buffer, NULL, 1); // tmp_buffer will be changed.
117 bool TreeDict::load_ifofile(const std::string& ifofilename, gulong *tdxfilesize)
120 if (!dict_info.load_from_ifo_file(ifofilename, true))
123 *tdxfilesize = dict_info.index_file_size;
124 sametypesequence=dict_info.sametypesequence;
129 void TreeDict::load_model(gchar **buffer, GtkTreeIter *parent, guint32 count)
133 guint32 offset, size, subentry_count;
135 for (guint32 i=0; i< count; i++) {
136 p1 = *buffer + strlen(*buffer) +1;
137 offset = g_ntohl(get_uint32(p1));
138 p1 += sizeof(guint32);
139 size = g_ntohl(get_uint32(p1));
140 p1 += sizeof(guint32);
141 subentry_count = g_ntohl(get_uint32(p1));
142 p1 += sizeof(guint32);
143 gtk_tree_store_append(model, &iter, parent);
144 gtk_tree_store_set(model, &iter, 0, *buffer, 1, offset, 2, size, -1);
147 load_model(buffer, &iter, subentry_count);
152 /**************************************************/
153 TreeDicts::TreeDicts()
157 TreeDicts::~TreeDicts()
159 for (std::vector<TreeDict *>::iterator it=oTreeDict.begin();
160 it!=oTreeDict.end(); ++it)
164 void TreeDicts::load_dict(const std::string& url)
166 TreeDict *lib = new TreeDict;
168 oTreeDict.push_back(lib);
173 class TreeDictLoader {
175 TreeDictLoader(TreeDicts& td_) : td(td_) {}
176 void operator()(const std::string& url, bool disable) {
184 GtkTreeStore* TreeDicts::Load(const strlist_t& tree_dicts_dirs,
185 const strlist_t& order_list,
186 const strlist_t& disable_list)
188 TreeDictLoader load(*this);
189 for_each_file(tree_dicts_dirs, ".ifo", order_list, disable_list, load);
191 return TreeDict::get_model();
194 gchar* TreeDicts::poGetWordData(guint32 offset, guint32 size, int iTreeDict)
196 return oTreeDict[iTreeDict]->GetWordData(offset, size);