Back to index

im-sdk  12.3.91
utfchar.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <assert.h>
00005 #include <iconv.h>
00006 #include "utfchar.h"
00007   
00008 int 
00009 _utfchar_concat (UTFCHAR *dest, UTFCHAR *str1, UTFCHAR *str2)
00010 {
00011       int i;
00012       for (i = 0; *str1; i++) {
00013          *dest++ = *str1++;
00014       }
00015       for (i = 0; *str2; i++) {
00016          *dest++ = *str2++;
00017       }
00018       *dest = 0;
00019       return i;
00020 }
00021 
00022 UTFCHAR *
00023 _utfchar_dup (UTFCHAR *src)
00024 {
00025   UTFCHAR *new_utfchar;
00026   int n_len;
00027   n_len = _utfchar_length (src);
00028   if (n_len == 0)
00029     return NULL;
00030   new_utfchar = (UTFCHAR *) calloc (n_len + 1, sizeof (UTFCHAR ));
00031   _utfchar_copy (new_utfchar, src);
00032   return new_utfchar;
00033 }
00034 
00035 int
00036 _utfchar_copy (UTFCHAR * dest, UTFCHAR * original)
00037 {
00038   int i;
00039   if (dest == NULL || original == NULL)
00040     return -1;
00041   for (i = 0; *original; i++) {
00042     *dest++ = *original++;
00043   }
00044   *dest = 0;
00045   return i;
00046 }
00047                                                                                                   
00048 int
00049 _utfchar_length (UTFCHAR * p)
00050 {
00051   int i;
00052   assert (p != NULL);
00053   if (p == NULL)
00054     return 0;
00055   for (i = 0; *p; i++)
00056     p++;
00057   return i;
00058 }
00059 
00060 int
00061 _utfchar_comp (UTFCHAR *s1, UTFCHAR *s2)
00062 {
00063   UTFCHAR *p1, *p2;
00064   
00065   p1 = s1, p2 = s2;
00066   
00067   while (*p1 && *p2){
00068     if (*p1 > *p2)
00069       return 1;
00070     else if (*p1 < *p2)
00071       return -1;
00072     p1++, p2++;
00073   }
00074   if (!*p1 && *p2)
00075     return -1;
00076   if (!*p2 && *p1)
00077     return 1;
00078   return 0;
00079 }
00080 
00081 void
00082 _utfchar_print (UTFCHAR *utf_string)
00083 {
00084   int len;
00085   char from_buffer[100], to_buffer[100];
00086   int  from_i = 0, to_i = 0;
00087   char *f_ptr, *t_ptr;
00088   iconv_t cd;
00089   size_t ret;
00090 
00091   len = _utfchar_length (utf_string);
00092   
00093   memset (from_buffer, 0, 100); memset (to_buffer, 0, 100);
00094   memcpy (from_buffer, utf_string, sizeof (UTFCHAR) * (len + 1));
00095   
00096   f_ptr = from_buffer, t_ptr = to_buffer;
00097   from_i = len * sizeof (UTFCHAR) , to_i = 100;
00098   
00099   cd = iconv_open ("EUC-KR", "UTF16");
00100   if (cd == (iconv_t) -1) {
00101     fprintf (stderr, "_utfchar_print error: iconv_open error,"
00102             "returning..\n");
00103     return;
00104   }
00105   ret = iconv (cd, &f_ptr, &from_i, &t_ptr, &to_i);
00106   if (ret == (size_t) -1){
00107     fprintf (stderr, "_utfchar_print error: iconv error,"
00108             "returning...\n");
00109     return;
00110   }
00111   fprintf (stdout, "%s", to_buffer);
00112   iconv_close (cd);
00113   return;
00114 }
00115 
00116 UTFCHAR *
00117 _utfchar_convert_u8_to_u16 (unsigned char *u8str)
00118 {
00119   char *inbuf, *outbuf;
00120   int inlen, outlen, outlen2;
00121   char *inptr, *outptr;
00122   UTFCHAR *p16char;
00123   iconv_t cd;
00124   size_t iconv_ret;
00125 
00126   assert (u8str != NULL);
00127   if (!u8str || strlen (u8str) == 0){
00128     fprintf (stdout, "_dictionary_convert_u8_to_u16 error: "
00129             "u8str is null or zero length");
00130     return NULL;
00131   }
00132   inlen = strlen (u8str);
00133   inbuf = (unsigned char *) calloc (inlen + 1, sizeof (unsigned char));
00134   strcpy (inbuf, u8str);
00135   /* just to be safe enough to hold converted string */
00136   outbuf =
00137     (unsigned char *) calloc (2 * (inlen + 1), sizeof (unsigned char));
00138   inptr = inbuf;
00139   outptr = outbuf;
00140   inlen = strlen (u8str);
00141   outlen2 = outlen = inlen * 2;
00142 
00143   cd = iconv_open ("UTF-16", "UTF-8");
00144   if (cd == (iconv_t) -1){
00145     fprintf (stdout, "_utfchar_convert_u8_to_u16 error :"
00146             "iconv_open failed\n");
00147     return NULL;
00148   }
00149   
00150   iconv_ret = iconv (cd, &inptr, &inlen, &outptr, &outlen);
00151   if (iconv_ret == (size_t) -1){
00152     fprintf (stdout, "_utfchar_convert_u8_to_u16 error :"
00153             "iconv failed\n");
00154     iconv_close (cd);
00155     free (inbuf);
00156     free (outbuf);
00157     
00158     return NULL;
00159   }
00160   
00161   if ((outbuf[0] == (char) 0xff) &&
00162       (outbuf[1] == (char) 0xff || outbuf[1] == (char) 0xfe)) {
00163     p16char =
00164       (UTFCHAR *) calloc ((outlen2 - outlen - 1) / 2 +1, sizeof (UTFCHAR));
00165     if(!p16char){
00166       fprintf (stdout, "_dictionary_convert_u8_to_u16 error: "
00167               "memory allocation error\n");
00168       iconv_close (cd);
00169       free (inbuf);
00170       free (outbuf);
00171     
00172       return NULL;
00173     }
00174     memcpy (p16char, outbuf + 2, outlen2 - outlen - 2);
00175     
00176   } else {
00177     
00178     p16char =
00179       (UTFCHAR *) calloc ((outlen2 - outlen)/2 + 1, sizeof (UTFCHAR));
00180     if(!p16char){
00181       fprintf (stdout, "_dictionary_convert_u8_to_u16 error: "
00182               "memory allocation error\n");
00183       iconv_close (cd);
00184       free (inbuf);
00185       free (outbuf);
00186     
00187       return NULL;
00188     }
00189 
00190     memcpy (p16char, outbuf, outlen2 - outlen);
00191   }
00192 
00193   free (inbuf);
00194   free (outbuf);
00195   iconv_close (cd);
00196   
00197   return p16char;
00198 }
00199 
00200 unsigned char *
00201 _utfchar_convert_u16_to_u8 (UTFCHAR *u16str)
00202 {
00203   char inbuf[1024], outbuf[1024];
00204 #if 0  
00205   char *inbuf, *outbuf;
00206 #endif
00207   int inlen, outlen, outlen2;
00208   char *inptr, *outptr;
00209   unsigned char *p8char;
00210   iconv_t cd;
00211   size_t iconv_ret;
00212 
00213   assert (u16str != NULL);
00214   if (!u16str || _utfchar_length (u16str) == 0){
00215     fprintf (stdout, "_dictionary_convert_u16_to_u8 error: "
00216             "u16str is null or zero length\n");
00217     return NULL;
00218 
00219   }
00220 #if 0
00221   inlen = (_utfchar_length (u16str) + 1) * sizeof (UTFCHAR);
00222   inbuf =
00223     (char *) calloc (_utfchar_length (u16str) + 1, sizeof (UTFCHAR));
00224   memcpy (inbuf, u16str, sizeof (UTFCHAR) * inlen);
00225   inptr = inbuf;
00226 #endif
00227   inlen = sizeof (inbuf);
00228   memset (inbuf, 0, inlen);
00229   memcpy (inbuf, u16str, _utfchar_length (u16str) * sizeof (UTFCHAR ));
00230   inptr = inbuf;
00231   
00232 #if 0
00233   /* just to be safe enough to hold converted string */
00234   outbuf =
00235     (unsigned char *) calloc (2 * inlen, sizeof (unsigned char));
00236   outlen2 = outlen = inlen * 2;
00237   outptr = outbuf;
00238 #endif
00239   
00240   outlen2 = outlen = sizeof (outbuf);
00241   memset (outbuf, 0, outlen);
00242   outptr = outbuf;
00243 
00244   cd = iconv_open ("UTF-8", "UTF-16");
00245   if (cd == (iconv_t) -1){
00246     fprintf (stdout, "_dictionary_convert_u16_to_u8 error: "
00247             "iconv_open failed\n");
00248     return NULL;
00249   }
00250   
00251   iconv_ret = iconv (cd, &inptr, &inlen, &outptr, &outlen);
00252   
00253   if (iconv_ret == (size_t) -1){
00254     fprintf (stdout, "_dictionary_convert_u16_to_u8 error: "
00255             "iconv failed\n");
00256     if (cd != (iconv_t) -1)
00257       iconv_close (cd);
00258     return NULL;
00259   }
00260   
00261   p8char =
00262     (unsigned char *)calloc (outlen2 - outlen + 1, sizeof (unsigned char));
00263   if(!p8char){
00264     fprintf (stdout, "_dictionary_convert_u16_to_u8 error: "
00265             "memory allocation error\n");
00266     return NULL;
00267   }
00268   memcpy (p8char, outbuf, outlen2 - outlen);
00269 #if 0
00270   free (inbuf);
00271   free (outbuf);
00272 #endif
00273   iconv_close (cd);
00274   return p8char;
00275 
00276 }