#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "common_han.h"
#include "common_utf.h"
#include "common_euc.h"
#include "uhang_uni_table.h"
static int node_compare(const void *node1, const void *node2)
{
#ifdef DEBUG_C
int ret;
ret = ((int)(((const hcode_table *)node1)->code) -(int)(((const hcode_table *)node2)->code));
printf("/%d/",ret);
return ret;
#else
return(((int)(((const hcode_table *)node1)->code) -
(int)(((const hcode_table *)node2)->code)));
#endif
}
hcode_type _unified_hangul_to_utf8(hcode_type euc_code)
{
hcode_table *node_ptr, node;
hcode_type utf_code;
int udc_index;
if ((EUC_UDC_SEG1 == euc_code.byte.byte3) ||
(EUC_UDC_SEG2 == euc_code.byte.byte3)) {
if ((euc_code.byte.byte4 < EUC_UDC_OFFSET_START) ||
(EUC_UDC_OFFSET_END < euc_code.byte.byte4)) {
utf_code.code = 0;
return(utf_code);
}
udc_index = (euc_code.byte.byte3 == EUC_UDC_SEG1) ?
0 : EUC_UDC_SEG_GAP;
udc_index += (int)(euc_code.byte.byte4 - EUC_UDC_OFFSET_START);
utf_code = _udcidx_to_utf(udc_index);
if (utf_code.code == UTF_UDC_ERROR)
utf_code.code = UTF8_NON_ID_CHAR;
return(utf_code);
}
node.code = euc_code.word.low;
node_ptr = bsearch( &node,
uhang_uni_tbl, sizeof(uhang_uni_tbl)/sizeof(hcode_table),
sizeof(hcode_table), node_compare);
#ifdef DEBUG
printf("*->%2x %2x %2x*",node_ptr->utf8.unicode.data1,node_ptr->utf8.unicode.data2,node_ptr->utf8.unicode.data3);
#endif
if (node_ptr != NULL)
{
utf_code = _uni_to_utf8(node_ptr->utf8);
#ifdef DEBUG_OLD
printf("*->%2x%2x%2x*",utf_code.byte.byte2,utf_code.byte.byte3,utf_code.byte.byte4);
#endif
return(utf_code);
}
else
{
utf_code.code = UTF8_NON_ID_CHAR;
return(utf_code);
}
}