Пример #1
0
void CGlobalRecords::wide2str16(const ustring& str1, u16string& str2)
{
	size_t					resultSize, inbytesleft, outbytesleft;
	const uint8_t			*inbuf;
	iconv_t					cd;
	unsigned16_t			*outbuf, *origOutbuf = NULL;

	cd = iconv_open(UCS_2_INTERNAL, iconv_code.c_str());
	// user may have changed the conversion since the workbook was opened
	XL_ASSERT(!(cd == (iconv_t)(-1)));

	if(cd != (iconv_t)(-1)) {
		inbytesleft		= str1.size() * sizeof(unichar_t);
		outbytesleft	= str1.size() * sizeof(unsigned16_t);

		inbuf		= (uint8_t *)str1.data();
		origOutbuf	= (unsigned16_t *)calloc(outbytesleft, 1);
		outbuf		= origOutbuf;

		resultSize = iconv(cd, (char **)&inbuf, &inbytesleft, (char **)&outbuf, &outbytesleft);
		iconv_close(cd);
	} else {
		resultSize = (size_t)-1;
	}

	if(resultSize == (size_t)-1) {
		str2 = convFail;
	} else {
		str2.assign(origOutbuf, (size_t)(outbuf - origOutbuf));
	}
	free((void *)origOutbuf);
}
Пример #2
0
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());
	}
}