Hi,
I am facing some problems with the convertor api and searched the
documentation but couldn't find anything relevant. I have two
functions in my program, one that converts from any encoding to UTF16
and the other than converts from UTF16 to and encoding
char* UTF16ToCharset(const UChar* str, char* charset)
{
UErrorCode status;
UConverter *conv = ucnv_open(charset, &status);
int32_t strLength = u_strlen( (UChar*) str);
int32_t outputLength = UCNV_GET_MAX_BYTES_FOR_STRING(strLength,
ucnv_getMaxCharSize(conv));
char* target = new char[outputLength];
int32_t len = ucnv_fromUChars(conv, target, outputLength, (UChar*)
str, strLength, &status);
ucnv_close(conv);
return target;
}
UChar* CharsetToUTF16(const char* str, char* charset)
{
UErrorCode status;
UConverter *conv = ucnv_open(charset, &status);
int32_t strLength = 0;
if ((codePage == 0x04b0) || (codePage == 0x04b1)) //utf-16
strLength = u_strlen((UChar*) str);
else
strLength = strlen(str);
UChar *target = new UChar[2 * strLength];
int32_t len = ucnv_toUChars(conv, target, 2 *strLength , str,
strLength, &status);
ucnv_close(conv);
return target;
}
These functions are independently called from separate threads from
time to time with one among a few charsets with can be utf-8, utf-16,
ascii and a few more.
The problem I am facing is that ucnv_open fails (returns NULL with
status = 42) from time to time.
I have not been able to figure out why and wanted to know if I could
get some pointers in the right direction. I placed locks using a
common mutex just in case it was a thread issue. But that did not
resolve the problem.
Could someone help me out here.
Thanks a lot in advance,
Aniket
|