From 559d3fba6f7dc6413965a430cb47244d6963e4de Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 14 Oct 2008 09:04:24 +0000 Subject: [PATCH] adding ase_wctomb --- ase/configure.ac | 7 +++++++ ase/include/ase/cmn/chr.h | 23 +++++++++++++++++++++-- ase/lib/cmn/chr.c | 33 ++++++++++++++++++++++----------- ase/lib/cmn/tio.c | 2 +- 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/ase/configure.ac b/ase/configure.ac index f7028171..f33e53ed 100644 --- a/ase/configure.ac +++ b/ase/configure.ac @@ -76,12 +76,19 @@ dnl Checks for header files. AC_HEADER_STDC AC_CHECK_HEADERS([stddef.h wchar.h wctype.h]) +dnl Checks data types AC_CHECK_TYPE([wchar_t], [AC_DEFINE([HAVE_WCHAR_T_IN_STDDEF_H], [],[wchar_t is available in stddef.h])], [], [#include ]) +AC_CHECK_TYPES(mbstate_t,,,[#include ]) + +dnl check functions +AC_CHECK_FUNCS(wcrtomb mbrtowc) + + dnl Checks the size of primitive data types AC_CHECK_SIZEOF(char) AC_CHECK_SIZEOF(short) diff --git a/ase/include/ase/cmn/chr.h b/ase/include/ase/cmn/chr.h index 18950376..2ac39633 100644 --- a/ase/include/ase/cmn/chr.h +++ b/ase/include/ase/cmn/chr.h @@ -37,8 +37,27 @@ extern "C" { extern ase_ccls_t* ase_ccls; -ase_bool_t ase_ccls_is (ase_cint_t c, int type); -ase_cint_t ase_ccls_to (ase_cint_t c, int type); +ase_bool_t ase_ccls_is ( + ase_cint_t c, + ase_ccls_type_t type +); + +ase_cint_t ase_ccls_to ( + ase_cint_t c, + ase_ccls_type_t type +); + +ase_size_t ase_wctomb ( + ase_wchar_t wc, + ase_mchar_t* mb, + ase_size_t mblen +); + +ase_size_t ase_mbtowc ( + const ase_mchar_t* mb, + ase_size_t mblen, + ase_wchar_t* wc +); #ifdef __cplusplus } diff --git a/ase/lib/cmn/chr.c b/ase/lib/cmn/chr.c index 878bf24f..5229feeb 100644 --- a/ase/lib/cmn/chr.c +++ b/ase/lib/cmn/chr.c @@ -22,7 +22,7 @@ static ase_bool_t is_graph (ase_cint_t c) { return isgraph(c); } static ase_bool_t is_cntrl (ase_cint_t c) { return iscntrl(c); } static ase_bool_t is_punct (ase_cint_t c) { return ispunct(c); } -ase_bool_t ase_ccls_is (ase_cint_t c, int type) +ase_bool_t ase_ccls_is (ase_cint_t c, ase_ccls_type_t type) { /* TODO: use GetStringTypeW/A for WIN32 to implement these */ @@ -46,7 +46,7 @@ ase_bool_t ase_ccls_is (ase_cint_t c, int type) return f[type] (c); } -ase_cint_t ase_ccls_to (ase_cint_t c, int type) +ase_cint_t ase_ccls_to (ase_cint_t c, ase_ccls_type_t type) { ASE_ASSERTX (type >= ASE_CCLS_UPPER && type <= ASE_CCLS_LOWER, "The character type should be one of ASE_CCLS_UPPER and ASE_CCLS_LOWER"); @@ -60,7 +60,7 @@ ase_cint_t ase_ccls_to (ase_cint_t c, int type) #include -ase_bool_t ase_ccls_is (ase_cint_t c, int type) +ase_bool_t ase_ccls_is (ase_cint_t c, ase_ccls_type_t type) { static const char* name[] = { @@ -99,7 +99,7 @@ ase_bool_t ase_ccls_is (ase_cint_t c, int type) return iswctype (c, desc[type]); } -ase_cint_t ase_ccls_to (ase_cint_t c, int type) +ase_cint_t ase_ccls_to (ase_cint_t c, ase_ccls_type_t type) { static const char* name[] = { @@ -124,12 +124,12 @@ ase_cint_t ase_ccls_to (ase_cint_t c, int type) #error unsupported character type #endif -static ase_bool_t ccls_is (void* data, ase_cint_t c, int type) +static ase_bool_t ccls_is (void* data, ase_cint_t c, ase_ccls_type_t type) { return ase_ccls_is (c, type); } -static ase_cint_t ccls_to (void* data, ase_cint_t c, int type) +static ase_cint_t ccls_to (void* data, ase_cint_t c, ase_ccls_type_t type) { return ase_ccls_to (c, type); } @@ -143,14 +143,25 @@ static ase_ccls_t ccls = ase_ccls_t* ase_ccls = &ccls; - -#if 0 -int ase_wctomb (ase_wchar_t wc, ase_mchar_t* mb, int mblen) +ase_size_t ase_wctomb (ase_wchar_t wc, ase_mchar_t* mb, ase_size_t mblen) { - if (mblen < MB_CUR_MAX) return -1; - return wctomb (mb, wc); +#ifdef HAVE_WCRTOMB + mbstate_t mbs; + + if (mblen < MB_CUR_MAX) + { + /* buffer too small */ + return -1; + } + + /* TODO: it may end with EILSEQ */ + return wcrtomb (mb, wc, &mbs); +#else + #error Not Supported +#endif } +#if 0 ase_wchar_t ase_mbtowc (ase_mchar_t* mb, int mblen) { } diff --git a/ase/lib/cmn/tio.c b/ase/lib/cmn/tio.c index 1ac5ae7c..eee4eb56 100644 --- a/ase/lib/cmn/tio.c +++ b/ase/lib/cmn/tio.c @@ -81,7 +81,7 @@ const ase_char_t* ase_tio_geterrstr (ase_tio_t* tio) ASE_T("no error"), ASE_T("out of memory"), ASE_T("no more space"), - ASE_T("illegal utf-8 sequence"), + ASE_T("illegal sequence"), ASE_T("no input function attached"), ASE_T("input function returned an error"), ASE_T("input function failed to open"),