From Michael Platings, "There is a function, convertStringFromCurrentCodePageToUTF8() that would be useful outside the FBX plugin so it belongs in osgDB. I've attached ConvertUTF and ConvertUTF.cpp with the function added for you to commit at your convenience.

"
This commit is contained in:
Robert Osfield 2010-02-25 17:07:34 +00:00
parent 440bd7c3e3
commit 590bf9070d
2 changed files with 76 additions and 0 deletions

View File

@ -38,6 +38,15 @@ extern OSGDB_EXPORT std::string convertUTF16toUTF8(const wchar_t* s);
extern OSGDB_EXPORT std::wstring convertUTF8toUTF16(const std::string& s);
extern OSGDB_EXPORT std::wstring convertUTF8toUTF16(const char* s);
extern OSGDB_EXPORT std::string convertStringFromCurrentCodePageToUTF8(const char* source, unsigned sourceLength);
extern OSGDB_EXPORT std::string convertStringFromUTF8toCurrentCodePage(const char* source, unsigned sourceLength);
extern OSGDB_EXPORT std::string convertStringFromCurrentCodePageToUTF8(const std::string& s);
extern OSGDB_EXPORT std::string convertStringFromCurrentCodePageToUTF8(const char* s);
extern OSGDB_EXPORT std::string convertStringFromUTF8toCurrentCodePage(const std::string& s);
extern OSGDB_EXPORT std::string convertStringFromUTF8toCurrentCodePage(const char* s);
}
#endif

View File

@ -31,6 +31,12 @@ std::string convertUTF16toUTF8(const wchar_t* s){return convertUTF16toUTF8(s, wc
std::wstring convertUTF8toUTF16(const std::string& s){return convertUTF8toUTF16(s.c_str(), s.length());}
std::wstring convertUTF8toUTF16(const char* s){return convertUTF8toUTF16(s, strlen(s));}
std::string convertStringFromCurrentCodePageToUTF8(const std::string& s){return convertStringFromCurrentCodePageToUTF8(s.c_str(), s.length());}
std::string convertStringFromCurrentCodePageToUTF8(const char* s){return convertStringFromCurrentCodePageToUTF8(s, strlen(s));}
std::string convertStringFromUTF8toCurrentCodePage(const std::string& s){return convertStringFromUTF8toCurrentCodePage(s.c_str(), s.length());}
std::string convertStringFromUTF8toCurrentCodePage(const char* s){return convertStringFromUTF8toCurrentCodePage(s, strlen(s));}
std::string convertUTF16toUTF8(const wchar_t* source, unsigned sourceLength)
{
#if defined(WIN32) && !defined(__CYGWIN__)
@ -95,5 +101,66 @@ std::wstring convertUTF8toUTF16(const char* source, unsigned sourceLength)
#endif
}
std::string convertStringFromCurrentCodePageToUTF8(const char* source, unsigned sourceLength)
{
#if defined(WIN32) && !defined(__CYGWIN__)
if (sourceLength == 0)
{
return std::string();
}
int utf16Length = MultiByteToWideChar(CP_ACP, 0, source, sourceLength, 0, 0);
if (utf16Length <= 0)
{
osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl;
return std::string();
}
std::wstring sUTF16(utf16Length, L'\0');
utf16Length = MultiByteToWideChar(CP_ACP, 0, source, sourceLength, &sUTF16[0], utf16Length);
if (utf16Length <= 0)
{
osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl;
return std::string();
}
return convertUTF16toUTF8(sUTF16);
#else
return source;
#endif
}
std::string convertStringFromUTF8toCurrentCodePage(const char* source, unsigned sourceLength)
{
#if defined(WIN32) && !defined(__CYGWIN__)
if (sourceLength == 0)
{
return std::string();
}
std::wstring utf16 = convertUTF8toUTF16(source, sourceLength);
sourceLength = utf16.length();
int destLen = WideCharToMultiByte(CP_ACP, 0, utf16.c_str(), sourceLength, 0, 0, 0, 0);
if (destLen <= 0)
{
osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl;
return std::string();
}
std::string sDest(destLen, '\0');
destLen = WideCharToMultiByte(CP_ACP, 0, utf16.c_str(), sourceLength, 0, 0, 0, 0);
if (destLen <= 0)
{
osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl;
return std::string();
}
return sDest;
#else
return source;
#endif
}
}