Imported version 0.2-1
[mstardict] / src / lib / common.cpp
1 /*
2  * This file part of StarDict - A international dictionary for GNOME.
3  * http://stardict.sourceforge.net
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 /*
21  * implementation of methods of common for dictionaries structures
22  */
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include "common.hpp"
28
29 static void parse_description(const char *p, long len, std::string &description)
30 {
31         description.clear();
32         const char *p1 = p;
33         while (p1 - p < len) {
34                 if (*p1 == '<') {
35                         p1++;
36                         if ((*p1 == 'b' || *p1 == 'B') && (*(p1+1)=='r' || *(p1+1)=='R') && *(p1+2)=='>') {
37                                 description += '\n';
38                                 p1+=3;
39                         } else {
40                                 description += '<';
41                         }
42                 } else {
43                         description += *p1;
44                         p1++;
45                 }
46         }
47 }
48
49 //looks not optimal, TODO: refactor
50 bool DictInfo::load_from_ifo_file(const std::string& ifofilename,
51                                   bool istreedict)
52 {
53         ifo_file_name=ifofilename;
54         gchar *buffer;
55         if (!g_file_get_contents(ifofilename.c_str(), &buffer, NULL, NULL))
56                 return false;
57
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)) {
62                 g_free(buffer);
63                 return false;
64         }
65
66         bool is_dict_300 = false;
67         gchar *p1;
68         if (istreedict) {
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;
73                 } else {
74                         g_print("Load %s failed: Unknown version.\n", ifofilename.c_str());
75                         g_free(buffer);
76                         return false;
77                 }
78         } else {
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;
86                         is_dict_300 = true;
87                 } else {
88                         g_print("Load %s failed: Unknown version.\n", ifofilename.c_str());
89                         g_free(buffer);
90                         return false;
91                 }
92         }
93
94         gchar *p2,*p3;
95
96         if (is_dict_300) {
97                 p2 = strstr(p1,"\nidxoffsetbits=");
98                 if (p2) {
99                         p2 = p2 + sizeof("\nidxoffsetbits=") -1;
100                         if (g_str_has_prefix(p2, "64\n")) {
101                                 // TODO
102                                 g_print("Load %s failed: not supported presently.\n", ifofilename.c_str());
103                                 g_free(buffer);
104                                 return false;
105                         }
106                 }
107         }
108
109         p2 = strstr(p1,"\nwordcount=");
110         if (!p2) {
111                 g_free(buffer);
112                 return false;
113         }
114
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);
119         g_free(tmpstr);
120
121         p2 = strstr(p1,"\nsynwordcount=");
122         if (p2) {
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);
127                 g_free(tmpstr);
128         } else {
129                 synwordcount = 0;
130         }
131
132         if (istreedict) {
133                 p2 = strstr(p1,"\ntdxfilesize=");
134                 if (!p2) {
135                         g_free(buffer);
136                         return false;
137                 }
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);
142                 g_free(tmpstr);
143         } else {
144                 p2 = strstr(p1,"\nidxfilesize=");
145                 if (!p2) {
146                         g_free(buffer);
147                         return false;
148                 }
149
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);
154                 g_free(tmpstr);
155
156                 p2 = strstr(p1,"\ndicttype=");
157                 if (p2) {
158                         p2+=sizeof("\ndicttype=")-1;
159                         p3 = strchr(p2, '\n');
160                         dicttype.assign(p2, p3-p2);
161                 }
162         }
163
164         p2 = strstr(p1,"\nbookname=");
165
166         if (!p2) {
167                 g_free(buffer);
168                 return false;
169         }
170
171         p2 = p2 + sizeof("\nbookname=") -1;
172         p3 = strchr(p2, '\n');
173         bookname.assign(p2, p3-p2);
174
175         p2 = strstr(p1,"\nauthor=");
176         if (p2) {
177                 p2 = p2 + sizeof("\nauthor=") -1;
178                 p3 = strchr(p2, '\n');
179                 author.assign(p2, p3-p2);
180         }
181
182         p2 = strstr(p1,"\nemail=");
183         if (p2) {
184                 p2 = p2 + sizeof("\nemail=") -1;
185                 p3 = strchr(p2, '\n');
186                 email.assign(p2, p3-p2);
187         }
188
189         p2 = strstr(p1,"\nwebsite=");
190         if (p2) {
191                 p2 = p2 + sizeof("\nwebsite=") -1;
192                 p3 = strchr(p2, '\n');
193                 website.assign(p2, p3-p2);
194         }
195
196         p2 = strstr(p1,"\ndate=");
197         if (p2) {
198                 p2 = p2 + sizeof("\ndate=") -1;
199                 p3 = strchr(p2, '\n');
200                 date.assign(p2, p3-p2);
201         }
202
203         p2 = strstr(p1,"\ndescription=");
204         if (p2) {
205                 p2 = p2 + sizeof("\ndescription=")-1;
206                 p3 = strchr(p2, '\n');
207                 parse_description(p2, p3-p2, description);
208         }
209
210         p2 = strstr(p1,"\nsametypesequence=");
211         if (p2) {
212                 p2+=sizeof("\nsametypesequence=")-1;
213                 p3 = strchr(p2, '\n');
214                 sametypesequence.assign(p2, p3-p2);
215         }
216
217         g_free(buffer);
218
219         return true;
220 }
221