17 gint stardict_strcmp(const gchar *s1, const gchar *s2)
20 a = g_ascii_strcasecmp(s1, s2);
22 return strcmp(s1, s2);
27 gint comparefunc(gconstpointer a,gconstpointer b)
30 x = stardict_strcmp(((struct _worditem *)a)->word,((struct _worditem *)b)->word);
32 return ((struct _worditem *)a)->definition - ((struct _worditem *)b)->definition;
37 void to_pinyin(gchar *str)
42 if (*p1 == 'u' && *(p1+1) == ':') {
57 void to_definition(gchar *str)
66 void convert(char *filename)
69 if (stat (filename, &stats) == -1)
71 printf("file not exist!\n");
74 gchar *basefilename = g_path_get_basename(filename);
76 tabfile = fopen(filename,"r");
78 gchar *buffer = (gchar *)g_malloc (stats.st_size + 1);
79 fread (buffer, 1, stats.st_size, tabfile);
81 buffer[stats.st_size] = '\0';
83 GArray *array = g_array_sized_new(FALSE,FALSE, sizeof(struct _worditem),20000);
85 gchar *p, *p1, *p2, *p3;
87 if ((guchar)*p==0xEF && (guchar)*(p+1)==0xBB && (guchar)*(p+2)==0xBF) // UTF-8 order characters.
89 struct _worditem worditem;
98 g_print("error, no end line\n");
105 g_print("error, no [, %ld\n", linenum);
110 p3 = strchr(p2, ']');
112 g_print("error, no ], %ld\n", linenum);
119 worditem.pinyin = p2;
121 worditem.definition = p3;
122 g_strstrip(worditem.word);
123 g_strstrip(worditem.pinyin);
124 g_strstrip(worditem.definition);
125 if (!worditem.word[0]) {
126 g_print("%s-%ld, bad word!!!\n", basefilename, linenum);
131 if (!worditem.pinyin[0]) {
132 g_print("%s-%ld, bad pinyin!!!\n", basefilename, linenum);
134 if (!worditem.definition[0]) {
135 g_print("%s-%ld, bad definition!!!\n", basefilename, linenum);
137 if (!worditem.pinyin[0] && !worditem.definition[0]) {
138 g_print("%s-%ld, bad pinyin and definition!!!\n", basefilename, linenum);
143 g_array_append_val(array, worditem);
147 g_array_sort(array,comparefunc);
149 gchar idxfilename[256];
150 gchar dicfilename[256];
151 sprintf(idxfilename, "%s.idx", basefilename);
152 sprintf(dicfilename, "%s.dict", basefilename);
153 FILE *idxfile = fopen(idxfilename,"w");
154 FILE *dicfile = fopen(dicfilename,"w");
158 struct _worditem *pworditem;
162 for (i=0; i< array->len; i++) {
163 offset_old = ftell(dicfile);
164 pworditem = &g_array_index(array, struct _worditem, i);
165 pinyin_len = strlen(pworditem->pinyin);
166 fwrite(pworditem->pinyin, 1 , pinyin_len+1,dicfile);
167 definition_len = strlen(pworditem->definition);
168 fwrite(pworditem->definition, 1 ,definition_len,dicfile);
169 fwrite(pworditem->word,sizeof(gchar),strlen(pworditem->word)+1,idxfile);
170 tmpglong = g_htonl(offset_old);
171 fwrite(&(tmpglong),sizeof(guint32),1,idxfile);
172 tmpglong = g_htonl(pinyin_len+1+ definition_len);
173 fwrite(&(tmpglong),sizeof(guint32),1,idxfile);
177 g_print("%s wordcount: %d\n", basefilename, array->len);
180 g_array_free(array,TRUE);
183 sprintf(command, "dictzip %s.dict", basefilename);
186 g_free(basefilename);
189 int main(int argc,char * argv [])
192 printf("please type this:\n./cedict cedict.gb.utf8\n");
198 for (int i=1; i< argc; i++)