written to use iconv.h

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4146 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
shatty 2003-07-31 00:04:53 +00:00
parent dd9bd594bb
commit b9de54c8ef

View File

@ -1,4 +1,11 @@
#include <UTF8.h>
#include <iconv.h>
#include <CharacterSet.h>
#include <CharacterSetRoster.h>
#include <Errors.h>
#include <errno.h>
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<const char**>(&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<const char**>(&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;
}