Back to index

im-sdk  12.3.91
readdic.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <assert.h>
00003 #include <iconv.h>
00004 #include <glib.h>
00005 
00006 #include "utfchar.h"
00007 #include "hhentry.h"
00008 #include "tree.h"
00009 
00010 #ifdef TEST_RUN
00011 
00012 
00013 void
00014 node_print (void *data)
00015 {
00016   HHItem *p = (HHItem *) data;
00017   hhitem_print_string (p, NULL);
00018   printf ("\n");
00019   return;
00020   
00021 }
00022 
00023 void
00024 my_print (void *data)
00025 {
00026   TreeNode *node = (TreeNode *)data;
00027   
00028   HHItem *p;
00029   int len;
00030   char from_buffer[100], to_buffer[100];
00031   int  from_i = 0, to_i = 0;
00032   char *f_ptr, *t_ptr;
00033   iconv_t cd;
00034   size_t ret;
00035 
00036   
00037   p = node->data;
00038   len = strlen (p->hangul);
00039   
00040   memset (from_buffer, 0, 100); memset (to_buffer, 0, 100);
00041   memcpy (from_buffer, p->hangul, sizeof (UTFCHAR) * (len + 1));
00042   
00043   f_ptr = from_buffer, t_ptr = to_buffer;
00044   from_i = len * sizeof (UTFCHAR) , to_i = 100;
00045   
00046   cd = iconv_open ("EUC-KR", "UTF16");
00047   if (cd == (iconv_t) -1) {
00048     fprintf (stderr, "iconv_open error, returning..\n");
00049     return;
00050   }
00051   ret = iconv (cd, &f_ptr, &from_i, &t_ptr, &to_i);
00052   if (ret == (size_t) -1){
00053     fprintf (stderr, "iconv error, returning...\n");
00054     return;
00055   }
00056   fprintf (stdout, "%s\n", to_buffer);
00057   iconv_close (cd);
00058   return;
00059 }
00060 
00061 unsigned char *
00062 convert_u16_to_u8 (UTFCHAR *u16str)
00063 {
00064   gchar *u8str;
00065   int u16len = _utfchar_length (u16str) * sizeof (UTFCHAR);
00066   int bytes_read = 0;
00067   int bytes_written = 0;
00068   GError *gerror = NULL;
00069   
00070   u8str = g_convert ((const gchar *)u16str,
00071                    u16len,
00072                    "UTF-8", "UTF-16",
00073                    &bytes_read,
00074                    &bytes_written,
00075                    &gerror);
00076   if (gerror)
00077     return NULL;
00078 
00079   return u8str;
00080 }
00081 
00082 int
00083 main (int argc, char **argv)
00084 {
00085 
00086   HHList *hash_table;
00087   int table_size;
00088 
00089   int n_trees;
00090   Tree *p_trees;
00091   TreeNode *p_treenode;
00092   TreeNode *search_result;
00093   UTFCHAR u16_word[10];
00094   int n_hanja = 0;
00095   unsigned char *u8_hangul;
00096 
00097   int i,j;
00098   int hashval;
00099 
00100 
00101   fprintf (stdout, "reading dictionary into hash table...\n");
00102   construct_binary_tree_from_file (argv[1], &p_trees, &n_trees );
00103   fprintf (stdout, "finished reading dictionary\n");
00104 
00105   tree_print (p_trees, (void * (*) (void *)) node_print);
00106   
00107   /*
00108   for (i = 0; i < n_trees; i++){
00109     printf ("Tree id:%d\n", i);
00110 
00111     printf ("===========================================\n");
00112   }
00113   */
00114 
00115   memset (u16_word, 0, sizeof (u16_word));
00116     /*
00117   while (scanf ("%x%x",&u16_word[0], &u16_word[1])){
00118     */
00119   while (scanf ("%x",&u16_word[0])){
00120 
00121     hashval = hash (u16_word);
00122     printf ("hasval : %d\n", hashval);
00123     u8_hangul = convert_u16_to_u8 (u16_word);
00124     search_result = tree_search_hangul (p_trees + hashval, u8_hangul);
00125 
00126     if (search_result){
00127       printf ("found  from tree...\n");
00128       n_hanja = search_result->data->n_hanja;
00129       printf ("%s\n", search_result->data->hangul);
00130       for (i = 0 ; i < n_hanja; i++)
00131        printf ("%s ", search_result->data->hanja_list[i]);
00132       printf ("\n");
00133     } else
00134         printf ("not found.....\n");
00135     /*
00136     tree_print (p_trees + hashval, (void * (*) (void *)) node_print);
00137     */
00138     memset (u16_word, 0, sizeof (u16_word));
00139   }
00140   return 0;
00141 }
00142 
00143 #endif