void CGlobalRecords::char2str16(const string& str1, u16string& str2) { string::const_iterator cBegin, cEnd; size_t len; str2.clear(); // test for UTF cBegin = str1.begin(); cEnd = str1.end(); unsigned8_t c = 0; while(cBegin != cEnd) { c |= *cBegin++; } if(c & 0x80) { const char *inbuf; iconv_t cd; unsigned16_t *outbuf, *origOutbuf; size_t resultSize, inbytesleft, outbytesleft; cd = iconv_open(UCS_2_INTERNAL, "UTF-8"); XL_ASSERT(cd != (iconv_t)(-1)); inbytesleft = str1.size(); outbytesleft = inbytesleft * sizeof(unsigned16_t); inbuf = str1.c_str(); origOutbuf = (unsigned16_t *)calloc(outbytesleft, 1); outbuf = origOutbuf; resultSize = iconv(cd, (char **)&inbuf, &inbytesleft, (char **)&outbuf, &outbytesleft); iconv_close(cd); if(resultSize == (size_t)-1) { str2 = convFail; } else { str2.assign(origOutbuf, (size_t)(outbuf - origOutbuf)); } free((void *)origOutbuf); } else { len = str1.length(); str2.reserve(len); cBegin = str1.begin(); cEnd = str1.end(); while(cBegin != cEnd) { str2.push_back((unsigned16_t)*cBegin++); } XL_ASSERT(str2.length() == str1.length()); } }
void CGlobalRecords::char2str16(const std::string& str1, u16string& str2) { std::string::const_iterator cBegin, cEnd; size_t len; str2.clear(); len = str1.length(); str2.reserve(len); cBegin = str1.begin(); cEnd = str1.end(); while(cBegin != cEnd) { str2.push_back((unsigned16_t)*cBegin++); } XL_ASSERT(str2.length() == str1.length()); }