From b9de54c8ef119e04cf1cc2631c056b46225ff87c Mon Sep 17 00:00:00 2001 From: shatty Date: Thu, 31 Jul 2003 00:04:53 +0000 Subject: [PATCH] written to use iconv.h git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4146 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/support/utf8_conversions.cpp | 47 ++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/kits/support/utf8_conversions.cpp b/src/kits/support/utf8_conversions.cpp index b8684e336f..56cf738ca5 100644 --- a/src/kits/support/utf8_conversions.cpp +++ b/src/kits/support/utf8_conversions.cpp @@ -1,4 +1,11 @@ #include +#include +#include +#include +#include +#include + +using namespace BPrivate; status_t convert_to_utf8(uint32 srcEncoding, @@ -6,8 +13,24 @@ convert_to_utf8(uint32 srcEncoding, char * dst, int32 * dstLen, int32 * state, char substitute = B_SUBSTITUTE) { - // TODO: implement - return B_ERROR; + const BCharacterSet * charset = BCharacterSetRoster::GetCharacterSetByConversionID(srcEncoding); + if (charset == 0) { + return B_ERROR; + } + iconv_t conversion = iconv_open("UTF-8",charset->GetName()); + if (conversion == (iconv_t)-1) { + return B_ERROR; + } + const char ** inputBuffer = const_cast(&src); + size_t charsLeft = *dstLen; + size_t inputLength = *srcLen; + size_t bytesLeft = iconv(conversion,inputBuffer,&inputLength,&dst,&charsLeft); + *srcLen = inputLength; + *dstLen -= charsLeft; + if ((bytesLeft != 0) && (errno != E2BIG) && (errno != EINVAL)) { + return B_ERROR; + } + return B_OK; } status_t @@ -16,6 +39,22 @@ convert_from_utf8(uint32 dstEncoding, char * dst, int32 * dstLen, int32 * state, char substitute = B_SUBSTITUTE) { - // TODO: implement - return B_ERROR; + const BCharacterSet * charset = BCharacterSetRoster::GetCharacterSetByConversionID(dstEncoding); + if (charset == 0) { + return B_ERROR; + } + iconv_t conversion = iconv_open(charset->GetName(),"UTF-8"); + if (conversion == (iconv_t)-1) { + return B_ERROR; + } + const char ** inputBuffer = const_cast(&src); + size_t charsLeft = *dstLen; + size_t inputLength = *srcLen; + size_t bytesLeft = iconv(conversion,inputBuffer,&inputLength,&dst,&charsLeft); + *srcLen = inputLength; + *dstLen -= charsLeft; + if ((bytesLeft != 0) && (errno != E2BIG) && (errno != EINVAL)) { + return B_ERROR; + } + return B_OK; }