in Squiggly/ane/MacFWHunspellANE/HunspellNativeExtension/EncConv.cpp [121:208]
const std::string convU16StrToCharStr(const U16Char_t* src, const char* Encoding)
{
//static char const* const tocode = CHARCONV_ICONV_UTF16;
char const* const tocode = getPlatformEncoding(Encoding);
UErrorCode status = U_ZERO_ERROR;
#ifdef ENCCONV_DEBUG
std::cout << "\t" "convString" << std::endl;
std::cout << "\t\t" "tocode = " << tocode << std::endl;
//std::cout << "\t\t" "fromcode = " << fromcode << std::endl;
#endif
//iconv_t cd = iconv_open(tocode, fromcode);
// Initializing ICU converter
UConverter *conv = ucnv_open(tocode, &status);
#ifdef CHARCONV_DEBUG
std::cout << "\t\t" "aft ucnv_open: status = " << status << std::endl;
#endif
if (conv == NULL)
{ // try default encoding "ISO-8859-1"
//throw std::runtime_error("Unable to create Unicode converter object");
status = U_ZERO_ERROR;
conv = ucnv_open("ISO-8859-1", &status);
}
//still if conv is null simply return blank string
if (conv == NULL)
{
return std::string("");
}
U16Char_t const* srcWrk = src;
const size_t srcSizeInUnits = GetNumOfUnits(src);
const size_t srcSizeInBytes = srcSizeInUnits * sizeof(U16Char_t);
const size_t dstSizeInBytes = MAX(256, (srcSizeInUnits + 1)) * 4; // How much byte buffer is needed? (UTF16 --> MBCS)
char* dst = new char [dstSizeInBytes];
if(dst==NULL)
{
//Fix for #3211945
ucnv_close(conv);
return std::string("");
}
char* dstWrk =(char*)(dst);
size_t srcLeftInBytes = srcSizeInBytes;
size_t dstLeftInBytes = dstSizeInBytes - sizeof(char);
status = U_ZERO_ERROR;
ucnv_fromUChars(conv, dstWrk, dstLeftInBytes, (UChar*)srcWrk, -1, &status);
U16Char_t* reverseConvertedVal = convCharStrToU16Str(dstWrk,Encoding);
if(strcmp((char*)reverseConvertedVal,(char*)src)!=0)
{
EncConv::releaseU16Str(reverseConvertedVal);
//Fix for #3211945
dstWrk = NULL;
ucnv_close(conv);
delete[] dst;
return std::string("");
}
EncConv::releaseU16Str(reverseConvertedVal);
#ifdef CHARCONV_DEBUG
std::cout << "\t\t" "aft iconv: status = " << status << std::endl;
#endif
if (status != U_ZERO_ERROR )
{
// throw std::runtime_error("Unable to convert to string");
*dstWrk = 0;
}
std::string dst2(dst);
//Fix for #3211945
dstWrk = NULL;
delete[] dst;
//const int err = iconv_close(cd);
ucnv_close(conv);
//if (err == -1)
// throw std::runtime_error("Unable to deallocate iconv_t object");
return dst2;
}