示例#1
0
文件: xstring.cpp 项目: Klozz/desmume
    static std::string ToUtf8(const std::wstring& widestring)
    {
        size_t widesize = widestring.length();

        if (sizeof(wchar_t) == 2)
        {
            size_t utf8size = 3 * widesize + 1;
            char* utf8stringnative = new char[utf8size];
            const UTF16* sourcestart = reinterpret_cast<const UTF16*>(widestring.c_str());
            const UTF16* sourceend = sourcestart + widesize;
            UTF8* targetstart = reinterpret_cast<UTF8*>(utf8stringnative);
            UTF8* targetend = targetstart + utf8size;
            ConversionResult res = ConvertUTF16toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
            if (res != conversionOK)
            {
                delete [] utf8stringnative;
                throw std::exception();
            }
            *targetstart = 0;
            std::string resultstring(utf8stringnative);
            delete [] utf8stringnative;
            return resultstring;
        }
        else if (sizeof(wchar_t) == 4)
        {
            size_t utf8size = 4 * widesize + 1;
            char* utf8stringnative = new char[utf8size];
            const UTF32* sourcestart = reinterpret_cast<const UTF32*>(widestring.c_str());
            const UTF32* sourceend = sourcestart + widesize;
            UTF8* targetstart = reinterpret_cast<UTF8*>(utf8stringnative);
            UTF8* targetend = targetstart + utf8size;
            ConversionResult res = ConvertUTF32toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
            if (res != conversionOK)
            {
                delete [] utf8stringnative;
                throw std::exception();
            }
            *targetstart = 0;
            std::string resultstring(utf8stringnative);
            delete [] utf8stringnative;
            return resultstring;
        }
        else
        {
            throw std::exception();
        }
        return "";
    }
示例#2
0
文件: xstring.cpp 项目: Klozz/desmume
 static std::wstring FromUtf8(const std::string& utf8string)
 {
     size_t widesize = utf8string.length();
     if (sizeof(wchar_t) == 2)
     {
         wchar_t* widestringnative = new wchar_t[widesize+1];
         const UTF8* sourcestart = reinterpret_cast<const UTF8*>(utf8string.c_str());
         const UTF8* sourceend = sourcestart + widesize;
         UTF16* targetstart = reinterpret_cast<UTF16*>(widestringnative);
         UTF16* targetend = targetstart + widesize+1;
         ConversionResult res = ConvertUTF8toUTF16(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
         if (res != conversionOK)
         {
             delete [] widestringnative;
             throw std::exception();
         }
         *targetstart = 0;
         std::wstring resultstring(widestringnative);
         delete [] widestringnative;
         return resultstring;
     }
     else if (sizeof(wchar_t) == 4)
     {
         wchar_t* widestringnative = new wchar_t[widesize+1];
         const UTF8* sourcestart = reinterpret_cast<const UTF8*>(utf8string.c_str());
         const UTF8* sourceend = sourcestart + widesize;
         UTF32* targetstart = reinterpret_cast<UTF32*>(widestringnative);
         UTF32* targetend = targetstart + widesize+1;
         ConversionResult res = ConvertUTF8toUTF32(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
         if (res != conversionOK)
         {
             delete [] widestringnative;
             throw std::exception();
         }
         *targetstart = 0;
         std::wstring resultstring(widestringnative);
         delete [] widestringnative;
         return resultstring;
     }
     else
     {
         throw std::exception();
     }
     return L"";
 }
示例#3
0
//Unicode ת Utf8 
std::string CharTransform::Unicode2Utf8(const std::wstring& widestring) 
{ 
	int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL); 
	if (utf8size == 0) 
	{ 
		throw std::exception("Error in conversion."); 
	} 
	std::vector<char> resultstring(utf8size); 
	int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL); 
	if (convresult != utf8size) 
	{ 
		throw std::exception("La falla!"); 
	} 
	return std::string(&resultstring[0]); 
} 
示例#4
0
		WString Converter::Acsi2WideByte(const AString& strascii){
			int widesize = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);
			if (widesize == ERROR_NO_UNICODE_TRANSLATION)	{
				throw std::exception("Invalid UTF-8 sequence.");
			}
			if (widesize == 0)	{
				throw std::exception("Error in conversion.");
			}
				std::vector<wchar_t> resultstring(widesize);
			int convresult = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);
	
			if (convresult != widesize)	{
				throw std::exception("La falla!");
			}
	
			return WString(&resultstring[0]);
		}
示例#5
0
		AString Converter::WideByte2Acsi(const WString& wstrcode){
			int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);
			if (asciisize == ERROR_NO_UNICODE_TRANSLATION)	{
				throw std::exception("Invalid UTF-8 sequence.");
			}
			if (asciisize == 0)	{
				throw std::exception("Error in conversion.");
			}
			std::vector<char> resultstring(asciisize);
			int convresult =::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);
	
			if (convresult != asciisize)	{
				throw std::exception("La falla!");
			}
	
			return AString(&resultstring[0]);
		}
示例#6
0
std::wstring CharTransform::Utf82Unicode(const std::string& utf8string)
{
	int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0); 
	if (widesize == ERROR_NO_UNICODE_TRANSLATION) 
	{ 
		throw std::exception("Invalid UTF-8 sequence."); 
	} 
	if (widesize == 0) 
	{ 
		throw std::exception("Error in conversion."); 
	} 
	std::vector<wchar_t> resultstring(widesize); 
	int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize); 
	if (convresult != widesize) 
	{ 
		throw std::exception("La falla!"); 
	} 
	return std::wstring(&resultstring[0]); 
}