| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgDB/ConvertUTF> |
|---|
| 15 | #include <osg/Notify> |
|---|
| 16 | |
|---|
| 17 | #include <string.h> |
|---|
| 18 | #include <wchar.h> |
|---|
| 19 | |
|---|
| 20 | #if defined(WIN32) && !defined(__CYGWIN__) |
|---|
| 21 | #define WIN32_LEAN_AND_MEAN |
|---|
| 22 | #include <windows.h> |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | namespace osgDB |
|---|
| 26 | { |
|---|
| 27 | |
|---|
| 28 | std::string convertUTF16toUTF8(const std::wstring& s){return convertUTF16toUTF8(s.c_str(), s.length());} |
|---|
| 29 | std::string convertUTF16toUTF8(const wchar_t* s){return convertUTF16toUTF8(s, wcslen(s));} |
|---|
| 30 | |
|---|
| 31 | std::wstring convertUTF8toUTF16(const std::string& s){return convertUTF8toUTF16(s.c_str(), s.length());} |
|---|
| 32 | std::wstring convertUTF8toUTF16(const char* s){return convertUTF8toUTF16(s, strlen(s));} |
|---|
| 33 | |
|---|
| 34 | std::string convertUTF16toUTF8(const wchar_t* source, unsigned sourceLength) |
|---|
| 35 | { |
|---|
| 36 | #if defined(WIN32) && !defined(__CYGWIN__) |
|---|
| 37 | if (sourceLength == 0) |
|---|
| 38 | { |
|---|
| 39 | return std::string(); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | int destLen = WideCharToMultiByte(CP_UTF8, 0, source, sourceLength, 0, 0, 0, 0); |
|---|
| 43 | if (destLen <= 0) |
|---|
| 44 | { |
|---|
| 45 | osg::notify(osg::WARN) << "Cannot convert UTF-16 string to UTF-8." << std::endl; |
|---|
| 46 | return std::string(); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | std::string sDest(destLen, '\0'); |
|---|
| 50 | destLen = WideCharToMultiByte(CP_UTF8, 0, source, sourceLength, &sDest[0], destLen, 0, 0); |
|---|
| 51 | |
|---|
| 52 | if (destLen <= 0) |
|---|
| 53 | { |
|---|
| 54 | osg::notify(osg::WARN) << "Cannot convert UTF-16 string to UTF-8." << std::endl; |
|---|
| 55 | return std::string(); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | return sDest; |
|---|
| 59 | #else |
|---|
| 60 | |
|---|
| 61 | osg::notify(osg::WARN) << "ConvertUTF16toUTF8 not implemented." << std::endl; |
|---|
| 62 | return std::string(); |
|---|
| 63 | #endif |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | std::wstring convertUTF8toUTF16(const char* source, unsigned sourceLength) |
|---|
| 67 | { |
|---|
| 68 | #if defined(WIN32) && !defined(__CYGWIN__) |
|---|
| 69 | if (sourceLength == 0) |
|---|
| 70 | { |
|---|
| 71 | return std::wstring(); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | int destLen = MultiByteToWideChar(CP_UTF8, 0, source, sourceLength, 0, 0); |
|---|
| 75 | if (destLen <= 0) |
|---|
| 76 | { |
|---|
| 77 | osg::notify(osg::WARN) << "Cannot convert UTF-8 string to UTF-16." << std::endl; |
|---|
| 78 | return std::wstring(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | std::wstring sDest(destLen, L'\0'); |
|---|
| 82 | destLen = MultiByteToWideChar(CP_UTF8, 0, source, sourceLength, &sDest[0], destLen); |
|---|
| 83 | |
|---|
| 84 | if (destLen <= 0) |
|---|
| 85 | { |
|---|
| 86 | osg::notify(osg::WARN) << "Cannot convert UTF-8 string to UTF-16." << std::endl; |
|---|
| 87 | return std::wstring(); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | return sDest; |
|---|
| 91 | #else |
|---|
| 92 | |
|---|
| 93 | osg::notify(osg::WARN) << "ConvertUTF8toUTF16 not implemented." << std::endl; |
|---|
| 94 | return std::wstring(); |
|---|
| 95 | #endif |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | } |
|---|