new and better working implementations for conversion functions, including better abstraction
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4161 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -4,10 +4,41 @@
|
||||
#include <CharacterSetRoster.h>
|
||||
#include <Errors.h>
|
||||
#include <errno.h>
|
||||
#include <iostream.h>
|
||||
|
||||
using namespace BPrivate;
|
||||
|
||||
typedef char ** input_buffer_t;
|
||||
typedef const char ** input_buffer_t;
|
||||
|
||||
status_t
|
||||
convert_encoding(const char * from, const char * to,
|
||||
const char * src, int32 * srcLen,
|
||||
char * dst, int32 * dstLen,
|
||||
int32 * state)
|
||||
{
|
||||
iconv_t conversion = iconv_open(to,from);
|
||||
if (conversion == (iconv_t)-1) {
|
||||
return B_ERROR;
|
||||
}
|
||||
if (state == 0) {
|
||||
return B_ERROR;
|
||||
}
|
||||
if (*state == 0) {
|
||||
iconv(conversion,0,0,0,0);
|
||||
*state = 1;
|
||||
}
|
||||
input_buffer_t inputBuffer = const_cast<input_buffer_t>(&src);
|
||||
size_t inputLeft = *srcLen;
|
||||
size_t outputLeft = *dstLen;
|
||||
size_t bytesLeft = iconv(conversion,inputBuffer,&inputLeft,&dst,&outputLeft);
|
||||
*srcLen -= inputLeft;
|
||||
*dstLen -= outputLeft;
|
||||
if ((bytesLeft != 0) && (errno != E2BIG) && (errno != EINVAL)) {
|
||||
return B_ERROR;
|
||||
}
|
||||
iconv_close(conversion);
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
status_t
|
||||
convert_to_utf8(uint32 srcEncoding,
|
||||
@@ -17,23 +48,10 @@ convert_to_utf8(uint32 srcEncoding,
|
||||
{
|
||||
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) {
|
||||
cout << "no charset for #: " << srcEncoding << endl;
|
||||
return B_ERROR;
|
||||
}
|
||||
input_buffer_t inputBuffer = const_cast<input_buffer_t>(&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;
|
||||
}
|
||||
iconv_close(conversion);
|
||||
return B_OK;
|
||||
return convert_encoding(charset->GetName(),"UTF-8",src,srcLen,dst,dstLen,state);
|
||||
}
|
||||
|
||||
status_t
|
||||
@@ -44,21 +62,8 @@ convert_from_utf8(uint32 dstEncoding,
|
||||
{
|
||||
const BCharacterSet * charset = BCharacterSetRoster::GetCharacterSetByConversionID(dstEncoding);
|
||||
if (charset == 0) {
|
||||
cout << "no charset for #: " << dstEncoding << endl;
|
||||
return B_ERROR;
|
||||
}
|
||||
iconv_t conversion = iconv_open(charset->GetName(),"UTF-8");
|
||||
if (conversion == (iconv_t)-1) {
|
||||
return B_ERROR;
|
||||
}
|
||||
input_buffer_t inputBuffer = const_cast<input_buffer_t>(&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;
|
||||
}
|
||||
iconv_close(conversion);
|
||||
return B_OK;
|
||||
return convert_encoding("UTF-8",charset->GetName(),src,srcLen,dst,dstLen,state);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user