16 gint stardict_strcmp(const gchar *s1, const gchar *s2)
19 a = g_ascii_strcasecmp(s1, s2);
21 return strcmp(s1, s2);
26 gint comparefunc(gconstpointer a,gconstpointer b)
28 return stardict_strcmp(((struct _worditem *)a)->word,((struct _worditem *)b)->word);
31 void my_strstrip(char *str, glong linenum)
45 else if (*p1 == 'r') {
49 else if (*p1 == '\\') {
55 else if (*p1 == '\0') {
56 g_print("big warining, %ld \\\n", linenum);
62 g_print("warining, %ld \\%c\n", linenum, *p1);
81 void convert(char *filename)
84 if (stat (filename, &stats) == -1)
86 printf("file not exist!\n");
89 gchar *basefilename = g_path_get_basename(filename);
91 tabfile = fopen(filename,"r");
93 gchar *buffer = (gchar *)g_malloc (stats.st_size + 1);
94 fread (buffer, 1, stats.st_size, tabfile);
96 buffer[stats.st_size] = '\0';
98 GArray *array = g_array_sized_new(FALSE,FALSE, sizeof(struct _worditem),20000);
102 struct _worditem worditem;
111 g_print("error, no end line\n");
116 p2 = strstr(p,"///");
118 g_print("wrong, no ///, %ld\n", linenum);
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);
134 if (!worditem.definition[0]) {
135 g_print("%s-%ld, bad definition!!!\n", basefilename, linenum);
140 g_array_append_val(array, worditem);
144 g_array_sort(array,comparefunc);
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");
154 glong wordcount = array->len;
158 gchar *previous_word = "";
159 struct _worditem *pworditem;
162 gchar *insert_word = "\n";
164 pworditem = &g_array_index(array, struct _worditem, i);
169 offset_old = ftell(dicfile);
173 definition_len = strlen(pworditem->definition);
174 fwrite(pworditem->definition, 1 ,definition_len,dicfile);
175 thedatasize += definition_len;
176 previous_word = pworditem->word;
181 pworditem = &g_array_index(array, struct _worditem, i);
182 if (strcmp(previous_word,pworditem->word)==0)
184 //g_print("D! %s\n",previous_word);
187 fwrite(insert_word,sizeof(gchar),strlen(insert_word),dicfile);
188 thedatasize += strlen(insert_word);
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);
207 g_print("%s wordcount: %ld\n", basefilename, wordcount);
210 g_array_free(array,TRUE);
215 g_free(basefilename);
218 int main(int argc,char * argv [])
221 printf("please type this:\n./kdic xiangya-medical.unix.utf8\n");
227 for (int i=1; i< argc; i++)