コード例 #1
0
ファイル: Global.cpp プロジェクト: cljaejung/ComLaser
// wstring -> string
string global::wstr2str(const wstring& wstr)
{
	// Windows Version
#ifdef __WXMSW__
	const int slength = (int)wstr.length() + 1;
	const int len = ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), slength, 0, 0, NULL, FALSE);
	char* buf = new char[len];
	::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), slength, buf, len, NULL, FALSE);
	std::string r(buf);
	delete[] buf;
	return r;
#else
	mbstate_t st = {};
	const unsigned wlen = wstr.length();
	wchar_t wbuf[wlen + 1];
	const size_t copied = wstr.copy(wbuf, wlen);
	wbuf[copied] = L'\0';
	const wchar_t* wptr = wbuf;
	char buf[wlen * sizeof(std::wstring::value_type) + 1];
	const ssize_t res = std::wcsrtombs(buf, &wptr, sizeof(buf), &st);
	return (res >= 0) ? buf : "?";
#endif
}