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:
parent
dd9bd594bb
commit
b9de54c8ef
@ -1,4 +1,11 @@
|
|||||||
#include <UTF8.h>
|
#include <UTF8.h>
|
||||||
|
#include <iconv.h>
|
||||||
|
#include <CharacterSet.h>
|
||||||
|
#include <CharacterSetRoster.h>
|
||||||
|
#include <Errors.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
using namespace BPrivate;
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
convert_to_utf8(uint32 srcEncoding,
|
convert_to_utf8(uint32 srcEncoding,
|
||||||
@ -6,8 +13,24 @@ convert_to_utf8(uint32 srcEncoding,
|
|||||||
char * dst, int32 * dstLen,
|
char * dst, int32 * dstLen,
|
||||||
int32 * state, char substitute = B_SUBSTITUTE)
|
int32 * state, char substitute = B_SUBSTITUTE)
|
||||||
{
|
{
|
||||||
// TODO: implement
|
const BCharacterSet * charset = BCharacterSetRoster::GetCharacterSetByConversionID(srcEncoding);
|
||||||
return B_ERROR;
|
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
|
status_t
|
||||||
@ -16,6 +39,22 @@ convert_from_utf8(uint32 dstEncoding,
|
|||||||
char * dst, int32 * dstLen,
|
char * dst, int32 * dstLen,
|
||||||
int32 * state, char substitute = B_SUBSTITUTE)
|
int32 * state, char substitute = B_SUBSTITUTE)
|
||||||
{
|
{
|
||||||
// TODO: implement
|
const BCharacterSet * charset = BCharacterSetRoster::GetCharacterSetByConversionID(dstEncoding);
|
||||||
return B_ERROR;
|
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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user