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 methods of common for dictionaries structures
29 static void parse_description(const char *p, long len, std::string &description)
33 while (p1 - p < len) {
36 if ((*p1 == 'b' || *p1 == 'B') && (*(p1+1)=='r' || *(p1+1)=='R') && *(p1+2)=='>') {
49 //looks not optimal, TODO: refactor
50 bool DictInfo::load_from_ifo_file(const std::string& ifofilename,
53 ifo_file_name=ifofilename;
55 if (!g_file_get_contents(ifofilename.c_str(), &buffer, NULL, NULL))
58 #define TREEDICT_MAGIC_DATA "StarDict's treedict ifo file\nversion="
59 #define DICT_MAGIC_DATA "StarDict's dict ifo file\nversion="
60 const gchar *magic_data=istreedict ? TREEDICT_MAGIC_DATA : DICT_MAGIC_DATA;
61 if (!g_str_has_prefix(buffer, magic_data)) {
66 bool is_dict_300 = false;
69 p1 = buffer + sizeof(TREEDICT_MAGIC_DATA) -1;
70 #define TREEDICT_VERSION_242 "2.4.2\n"
71 if (g_str_has_prefix(p1, TREEDICT_VERSION_242)) {
72 p1 += sizeof(TREEDICT_VERSION_242) -2;
74 g_print("Load %s failed: Unknown version.\n", ifofilename.c_str());
79 p1 = buffer + sizeof(DICT_MAGIC_DATA) -1;
80 #define DICT_VERSION_242 "2.4.2\n"
81 #define DICT_VERSION_300 "3.0.0\n"
82 if (g_str_has_prefix(p1, DICT_VERSION_242)) {
83 p1 += sizeof(DICT_VERSION_242) -2;
84 } else if (g_str_has_prefix(p1, DICT_VERSION_300)) {
85 p1 += sizeof(DICT_VERSION_300) -2;
88 g_print("Load %s failed: Unknown version.\n", ifofilename.c_str());
97 p2 = strstr(p1,"\nidxoffsetbits=");
99 p2 = p2 + sizeof("\nidxoffsetbits=") -1;
100 if (g_str_has_prefix(p2, "64\n")) {
102 g_print("Load %s failed: not supported presently.\n", ifofilename.c_str());
109 p2 = strstr(p1,"\nwordcount=");
115 p3 = strchr(p2+ sizeof("\nwordcount=")-1,'\n');
116 gchar *tmpstr = (gchar *)g_memdup(p2+sizeof("\nwordcount=")-1, p3-(p2+sizeof("\nwordcount=")-1)+1);
117 tmpstr[p3-(p2+sizeof("\nwordcount=")-1)] = '\0';
118 wordcount = atol(tmpstr);
121 p2 = strstr(p1,"\nsynwordcount=");
123 p3 = strchr(p2+ sizeof("\nsynwordcount=")-1,'\n');
124 gchar *tmpstr = (gchar *)g_memdup(p2+sizeof("\nsynwordcount=")-1, p3-(p2+sizeof("\nsynwordcount=")-1)+1);
125 tmpstr[p3-(p2+sizeof("\nsynwordcount=")-1)] = '\0';
126 synwordcount = atol(tmpstr);
133 p2 = strstr(p1,"\ntdxfilesize=");
138 p3 = strchr(p2+ sizeof("\ntdxfilesize=")-1,'\n');
139 tmpstr = (gchar *)g_memdup(p2+sizeof("\ntdxfilesize=")-1, p3-(p2+sizeof("\ntdxfilesize=")-1)+1);
140 tmpstr[p3-(p2+sizeof("\ntdxfilesize=")-1)] = '\0';
141 index_file_size = atol(tmpstr);
144 p2 = strstr(p1,"\nidxfilesize=");
150 p3 = strchr(p2+ sizeof("\nidxfilesize=")-1,'\n');
151 tmpstr = (gchar *)g_memdup(p2+sizeof("\nidxfilesize=")-1, p3-(p2+sizeof("\nidxfilesize=")-1)+1);
152 tmpstr[p3-(p2+sizeof("\nidxfilesize=")-1)] = '\0';
153 index_file_size = atol(tmpstr);
156 p2 = strstr(p1,"\ndicttype=");
158 p2+=sizeof("\ndicttype=")-1;
159 p3 = strchr(p2, '\n');
160 dicttype.assign(p2, p3-p2);
164 p2 = strstr(p1,"\nbookname=");
171 p2 = p2 + sizeof("\nbookname=") -1;
172 p3 = strchr(p2, '\n');
173 bookname.assign(p2, p3-p2);
175 p2 = strstr(p1,"\nauthor=");
177 p2 = p2 + sizeof("\nauthor=") -1;
178 p3 = strchr(p2, '\n');
179 author.assign(p2, p3-p2);
182 p2 = strstr(p1,"\nemail=");
184 p2 = p2 + sizeof("\nemail=") -1;
185 p3 = strchr(p2, '\n');
186 email.assign(p2, p3-p2);
189 p2 = strstr(p1,"\nwebsite=");
191 p2 = p2 + sizeof("\nwebsite=") -1;
192 p3 = strchr(p2, '\n');
193 website.assign(p2, p3-p2);
196 p2 = strstr(p1,"\ndate=");
198 p2 = p2 + sizeof("\ndate=") -1;
199 p3 = strchr(p2, '\n');
200 date.assign(p2, p3-p2);
203 p2 = strstr(p1,"\ndescription=");
205 p2 = p2 + sizeof("\ndescription=")-1;
206 p3 = strchr(p2, '\n');
207 parse_description(p2, p3-p2, description);
210 p2 = strstr(p1,"\nsametypesequence=");
212 p2+=sizeof("\nsametypesequence=")-1;
213 p3 = strchr(p2, '\n');
214 sametypesequence.assign(p2, p3-p2);