Back to index

im-sdk  12.3.91
symbol.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <iconv.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <IMProtocolStruct.h>
00006 #include <assert.h>
00007 #include "symbol.h"
00008 
00009 static int g_symbol_table_size = 0;
00010 static SymbolGroup *special_symbols = NULL;
00011 
00012 static void
00013 composer_symbol_table_init ()
00014 {
00015   int i;
00016   if (special_symbols)
00017     return;
00018   
00019   g_symbol_table_size =
00020     sizeof (symbol_group_names) / sizeof (symbol_group_names[0]);
00021   special_symbols =
00022     (SymbolGroup *) calloc (g_symbol_table_size, sizeof (SymbolGroup));
00023   
00024   for (i = 0 ; i < g_symbol_table_size; i++){
00025     special_symbols[i].symbol_key = symbol_group_names[i];
00026     special_symbols[i].symbol_string = special[i];
00027   }
00028 }
00029 
00030 
00031 Boolean
00032 composer_symbol_menu_lookup_start (int *number_of_candidates,
00033                                UTFCHAR ***symbol_list_return)
00034 {
00035   iconv_t cd;
00036   size_t iconv_return;
00037   /* not used
00038   char inbuffer[1024];
00039   */
00040   char outbuffer[1024];
00041   char *inptr, *outptr;
00042   size_t inlen, outlen;
00043   int i;
00044 
00045   assert (number_of_candidates != NULL);
00046   assert (symbol_list_return != NULL);
00047 
00048   if (!g_symbol_table_size)
00049     composer_symbol_table_init ();
00050 
00051   *number_of_candidates = g_symbol_table_size;
00052   *symbol_list_return = 
00053     (UTFCHAR **) calloc (*number_of_candidates, sizeof (UTFCHAR *));
00054 
00055 
00056   cd = iconv_open ("UTF16", "UTF-8");
00057   if (cd == (iconv_t) -1){
00058     fprintf (stderr, "composer_symbol_menu_lookup_start: Iconv open failed\n");
00059     return False;
00060   }
00061   /*
00062     BEGIN: dummy conversion to flush BOM for the first time
00063   */
00064   
00065   inptr = special_symbols[0].symbol_key, outptr = outbuffer;
00066   inlen = strlen (special_symbols[0].symbol_key), outlen = sizeof (outbuffer);
00067   
00068   iconv (cd, &inptr, &inlen, &outptr, &outlen);
00069   /*
00070     END: dummy conversion to flush BOM for the first time
00071   */
00072   
00073   for (i = 0 ; i < g_symbol_table_size; i++){
00074     inptr = special_symbols[i].symbol_key;
00075     outptr = outbuffer;
00076     inlen = strlen (special_symbols[i].symbol_key);
00077     outlen = sizeof (outbuffer);
00078     
00079     iconv_return = iconv (cd, &inptr, &inlen, &outptr, &outlen);
00080 
00081     if (iconv_return == (size_t) -1){
00082       continue;
00083     }
00084     
00085     (*symbol_list_return)[i] =
00086       (UTFCHAR *) calloc (sizeof (outbuffer) - outlen + 2, 1);
00087     
00088     memcpy ((*symbol_list_return)[i],
00089            outbuffer, sizeof (outbuffer) - outlen);
00090   }
00091   iconv_close (cd);
00092   return True;
00093 }
00094 
00095 Boolean
00096 composer_symbol_detail_lookup_start (int n_group_id,
00097                                  int *number_of_candidates,
00098                                  UTFCHAR ***symbol_list_return)
00099 {
00100   iconv_t cd;
00101   size_t iconv_return;
00102   /* not used
00103   char inbuffer[1024];
00104   */
00105   char outbuffer[1024];
00106   char *inptr, *outptr;
00107   size_t inlen, outlen;
00108   int i;
00109 
00110   char *ptr;
00111 
00112   assert (number_of_candidates != NULL);
00113   assert (symbol_list_return != NULL);
00114   assert ((n_group_id >= 0) && (n_group_id < g_symbol_table_size) );
00115 
00116   if (!g_symbol_table_size)
00117     composer_symbol_table_init ();
00118 
00119   if (n_group_id < 0 || n_group_id >= g_symbol_table_size)
00120     return False;
00121 
00122   cd = iconv_open ("UTF16", "UTF-8");
00123   if (cd == (iconv_t) -1){
00124     fprintf (stderr, "composer_symbol_menu_lookup_start: Iconv open failed\n");
00125     return False;
00126   }
00127   /*
00128     BEGIN: dummy conversion to flush BOM for the first time
00129   */
00130   
00131   inptr = special_symbols[0].symbol_key;
00132   outptr = outbuffer;
00133   inlen = strlen (special_symbols[0].symbol_key), outlen = sizeof (outbuffer);
00134   
00135   iconv (cd, &inptr, &inlen, &outptr, &outlen);
00136   
00137   /*
00138     END: dummy conversion to flush BOM for the first time
00139   */
00140 
00141   memset (outbuffer, 0, sizeof (outbuffer));
00142   
00143   inptr = special_symbols[n_group_id].symbol_string;
00144   outptr = outbuffer;
00145   inlen = strlen (special_symbols[n_group_id].symbol_string);
00146   outlen = sizeof (outbuffer);
00147   
00148   iconv_return = iconv (cd, &inptr, &inlen, &outptr, &outlen);
00149   if (iconv_return == (size_t) -1){
00150     /* if iconv fails, then I need to do some clean up
00151        before returns ...*/
00152     fprintf (stderr, "composer_symbol_detail_lookup_start : "
00153             "iconv failed\n");
00154     return False;
00155   }
00156   iconv_close (cd);
00157   
00158   *number_of_candidates = (sizeof (outbuffer) - outlen) / 2;
00159   *symbol_list_return =
00160     (UTFCHAR **) calloc (*number_of_candidates, sizeof (UTFCHAR *));
00161   
00162   for (i = 0, ptr = outbuffer
00163         ; i < *number_of_candidates ; i++, ptr += 2){
00164     (*symbol_list_return)[i] = (UTFCHAR *) calloc (2, sizeof (UTFCHAR));
00165     memcpy ((*symbol_list_return)[i], ptr, sizeof (UTFCHAR));
00166   }
00167 
00168 
00169   return True;
00170 }
00171 
00172 #ifdef TEST_RUN
00173 void
00174 _utfchar_print (UTFCHAR *utf_string)
00175 {
00176   int len;
00177   char from_buffer[100], to_buffer[100];
00178   int  from_i = 0, to_i = 0;
00179   char *f_ptr, *t_ptr;
00180   iconv_t cd;
00181   size_t ret;
00182 
00183   len = _utfchar_length (utf_string);
00184   
00185   memset (from_buffer, 0, 100); memset (to_buffer, 0, 100);
00186   memcpy (from_buffer, utf_string, sizeof (UTFCHAR) * (len + 1));
00187   
00188   f_ptr = from_buffer, t_ptr = to_buffer;
00189   from_i = len * sizeof (UTFCHAR) , to_i = 100;
00190   
00191   cd = iconv_open ("EUC-KR", "UTF16");
00192   if (cd == (iconv_t) -1) {
00193     fprintf (stderr, "iconv_open error, returning..\n");
00194     return;
00195   }
00196   ret = iconv (cd, &f_ptr, &from_i, &t_ptr, &to_i);
00197   if (ret == (size_t) -1){
00198     fprintf (stderr, "iconv error, returning...\n");
00199     return;
00200   }
00201   fprintf (stdout, "%s", to_buffer);
00202   iconv_close (cd);
00203   return;
00204 }
00205 int
00206 _utfchar_length(UTFCHAR * p)
00207 {
00208       int i;
00209               assert (p != NULL);
00210               if (p == NULL)
00211                      return 0;
00212       for (i = 0; *p; i++)
00213          p++;
00214       return i;
00215 }
00216 
00217 int
00218 main (int argc, char **argv)
00219 {
00220   UTFCHAR **symbol_list_return;
00221   int number_of_candidates;
00222   Bool mthd_return;
00223   int i;
00224   
00225   mthd_return = composer_symbol_menu_lookup_start (&number_of_candidates,
00226                                              &symbol_list_return);
00227   for (i = 0 ; i < number_of_candidates; i++){
00228     _utfchar_print (symbol_list_return [i]);
00229     printf ("\n");
00230   }
00231 
00232   return 0;
00233 }
00234 #endif