Back to index

im-sdk  12.3.91
utfchar.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.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