diff --git a/include/osgDB/ConvertUTF b/include/osgDB/ConvertUTF index 4f3c17f2e..514b3e00d 100644 --- a/include/osgDB/ConvertUTF +++ b/include/osgDB/ConvertUTF @@ -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 diff --git a/src/osgDB/ConvertUTF.cpp b/src/osgDB/ConvertUTF.cpp index 682cdb230..4af1b3dd0 100644 --- a/src/osgDB/ConvertUTF.cpp +++ b/src/osgDB/ConvertUTF.cpp @@ -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 +} + }