OSDN Git Service

first commit
[slackware/slackbuild.git] / stardict-tools / stardict-tools-3.0.1 / src / kdic.cpp
1 #include "stdio.h"
2 #include "stdlib.h"
3 #include <string.h>
4 #include <sys/stat.h>
5
6
7 #include <gtk/gtk.h>
8 #include <glib.h>
9
10 struct _worditem
11 {
12         gchar *word;
13         gchar *definition;
14 };
15
16 gint stardict_strcmp(const gchar *s1, const gchar *s2)
17 {
18         gint a;
19         a = g_ascii_strcasecmp(s1, s2);
20         if (a == 0)
21                 return strcmp(s1, s2);
22         else
23                 return a;
24 }
25
26 gint comparefunc(gconstpointer a,gconstpointer b)
27 {
28         return stardict_strcmp(((struct _worditem *)a)->word,((struct _worditem *)b)->word);
29 }
30
31 void my_strstrip(char *str, glong linenum)
32 {
33         char *p1, *p2;
34         p1=str;
35         p2=str;
36         while (*p1 != '\0') {
37                 if (*p1 == '\\') {
38                         p1++;
39                         if (*p1 == 'n') {
40                                 *p2='\n';
41                                 p2++;
42                                 p1++;
43                                 continue;
44                         }
45                         else if (*p1 == 'r') {
46                                 p1++;
47                                 continue;
48                         }
49                         else if (*p1 == '\\') {
50                                 *p2='\\';
51                                 p2++;
52                                 p1++;
53                                 continue;
54                         }
55                         else if (*p1 == '\0') {
56                                 g_print("big warining, %ld \\\n", linenum);
57                                 *p2='\\';
58                                 p2++;
59                                 continue;
60                         }
61                         else {
62                                 g_print("warining, %ld \\%c\n", linenum,  *p1);
63                                 *p2='\\';
64                                 p2++;
65                                 *p2=*p1;
66                                 p2++;
67                                 p1++;
68                                 continue;
69                         }
70                 }
71                 else {
72                         *p2 = *p1;
73                         p2++;
74                         p1++;
75                         continue;
76                 }
77         }
78         *p2 = '\0';
79 }
80
81 void convert(char *filename)
82 {                       
83         struct stat stats;
84         if (stat (filename, &stats) == -1)
85         {
86                 printf("file not exist!\n");
87                 return;
88         }
89         gchar *basefilename = g_path_get_basename(filename);
90         FILE *tabfile;
91         tabfile = fopen(filename,"r");
92
93         gchar *buffer = (gchar *)g_malloc (stats.st_size + 1);
94         fread (buffer, 1, stats.st_size, tabfile);
95         fclose (tabfile);
96         buffer[stats.st_size] = '\0';   
97         
98         GArray *array = g_array_sized_new(FALSE,FALSE, sizeof(struct _worditem),20000);
99                 
100         gchar *p, *p1, *p2;
101         p = buffer;
102         struct _worditem worditem;
103         glong linenum=1;
104         while (1) {
105                 if (*p == '\0') {
106                         g_print("over\n");
107                         break;
108                 }
109                 p1 = strchr(p,'\n');
110                 if (!p1) {
111                         g_print("error, no end line\n");
112                         return;
113                 }
114                 *p1 = '\0';
115                 p1++;
116                 p2 = strstr(p,"///");
117                 if (!p2) {
118                         g_print("wrong, no ///, %ld\n", linenum);
119                         p= p1;
120                         linenum++;
121                         continue;
122                 }
123                 *p2 = '\0';
124                 p2+=3;
125                 worditem.word = p;
126                 worditem.definition = p2;
127                 my_strstrip(worditem.definition, linenum);
128                 g_strstrip(worditem.word);
129                 g_strstrip(worditem.definition);
130                 if (!worditem.word[0]) {
131                         g_print("%s-%ld, bad word!!!\n", basefilename, linenum);
132                         return;
133                 }
134                 if (!worditem.definition[0]) {
135                         g_print("%s-%ld, bad definition!!!\n", basefilename, linenum);
136                         p= p1;
137                         linenum++;
138                         continue;
139                 }
140                 g_array_append_val(array, worditem);                    
141                 p= p1;                          
142                 linenum++;
143         }               
144         g_array_sort(array,comparefunc);
145                 
146         gchar idxfilename[256];
147         gchar dicfilename[256];
148         sprintf(idxfilename, "%s.idx", basefilename);
149         sprintf(dicfilename, "%s.dict", basefilename);
150         FILE *idxfile = fopen(idxfilename,"w");
151         FILE *dicfile = fopen(dicfilename,"w");
152
153         
154         glong wordcount = array->len;
155
156         long offset_old;
157         glong tmpglong;
158         gchar *previous_word = "";
159         struct _worditem *pworditem;
160         gulong i=0;
161         glong thedatasize;
162         gchar *insert_word = "\n";
163         gboolean flag;
164         pworditem = &g_array_index(array, struct _worditem, i);
165         gint definition_len;
166         while (i<array->len)
167         {
168                 thedatasize = 0; 
169                 offset_old = ftell(dicfile);
170                 flag = true;
171                 while (flag == true)
172                 {       
173                         definition_len = strlen(pworditem->definition);
174                         fwrite(pworditem->definition, 1 ,definition_len,dicfile);
175                         thedatasize += definition_len;
176                         previous_word = pworditem->word;
177                                                 
178                         i++;
179                         if (i<array->len)
180                         {
181                                 pworditem = &g_array_index(array, struct _worditem, i);
182                                 if (strcmp(previous_word,pworditem->word)==0)
183                                 {
184                                         //g_print("D! %s\n",previous_word);
185                                         flag = true;
186                                         wordcount--;
187                                         fwrite(insert_word,sizeof(gchar),strlen(insert_word),dicfile);
188                                         thedatasize += strlen(insert_word);
189                                 }
190                                 else 
191                                 {
192                                         flag = false;
193                                 }
194                         }
195                         else
196                         {
197                                 flag = false;
198                         }
199                 }
200                 fwrite(previous_word,sizeof(gchar),strlen(previous_word)+1,idxfile);
201                 tmpglong = g_htonl(offset_old);
202                 fwrite(&(tmpglong),sizeof(glong),1,idxfile);
203                 tmpglong = g_htonl(thedatasize);
204                 fwrite(&(tmpglong),sizeof(glong),1,idxfile);    
205         }       
206                 
207         g_print("%s wordcount: %ld\n", basefilename, wordcount);
208
209         g_free(buffer);
210         g_array_free(array,TRUE);
211         
212         fclose(idxfile);
213         fclose(dicfile);
214
215         g_free(basefilename);
216 }
217
218 int main(int argc,char * argv [])
219 {
220         if (argc<2) {
221                 printf("please type this:\n./kdic xiangya-medical.unix.utf8\n");
222                 return FALSE;
223         }
224
225         gtk_set_locale ();
226         g_type_init ();
227         for (int i=1; i< argc; i++)
228                 convert (argv[i]);
229         return FALSE;   
230 }
231