| 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 convertStringFromCurrentCodePageToUTF8(const std::string& s){return convertStringFromCurrentCodePageToUTF8(s.c_str(), s.length());} |
|---|
| 35 | std::string convertStringFromCurrentCodePageToUTF8(const char* s){return convertStringFromCurrentCodePageToUTF8(s, strlen(s));} |
|---|
| 36 | |
|---|
| 37 | std::string convertStringFromUTF8toCurrentCodePage(const std::string& s){return convertStringFromUTF8toCurrentCodePage(s.c_str(), s.length());} |
|---|
| 38 | std::string convertStringFromUTF8toCurrentCodePage(const char* s){return convertStringFromUTF8toCurrentCodePage(s, strlen(s));} |
|---|
| 39 | |
|---|
| 40 | std::string convertUTF16toUTF8(const wchar_t* source, unsigned sourceLength) |
|---|
| 41 | { |
|---|
| 42 | #if defined(WIN32) && !defined(__CYGWIN__) |
|---|
| 43 | if (sourceLength == 0) |
|---|
| 44 | { |
|---|
| 45 | return std::string(); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | int destLen = WideCharToMultiByte(CP_UTF8, 0, source, sourceLength, 0, 0, 0, 0); |
|---|
| 49 | if (destLen <= 0) |
|---|
| 50 | { |
|---|
| 51 | osg::notify(osg::WARN) << "Cannot convert UTF-16 string to UTF-8." << std::endl; |
|---|
| 52 | return std::string(); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | std::string sDest(destLen, '\0'); |
|---|
| 56 | destLen = WideCharToMultiByte(CP_UTF8, 0, source, sourceLength, &sDest[0], destLen, 0, 0); |
|---|
| 57 | |
|---|
| 58 | if (destLen <= 0) |
|---|
| 59 | { |
|---|
| 60 | osg::notify(osg::WARN) << "Cannot convert UTF-16 string to UTF-8." << std::endl; |
|---|
| 61 | return std::string(); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | return sDest; |
|---|
| 65 | #else |
|---|
| 66 | |
|---|
| 67 | osg::notify(osg::WARN) << "ConvertUTF16toUTF8 not implemented." << std::endl; |
|---|
| 68 | return std::string(); |
|---|
| 69 | #endif |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | std::wstring convertUTF8toUTF16(const char* source, unsigned sourceLength) |
|---|
| 73 | { |
|---|
| 74 | #if defined(WIN32) && !defined(__CYGWIN__) |
|---|
| 75 | if (sourceLength == 0) |
|---|
| 76 | { |
|---|
| 77 | return std::wstring(); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | int destLen = MultiByteToWideChar(CP_UTF8, 0, source, sourceLength, 0, 0); |
|---|
| 81 | if (destLen <= 0) |
|---|
| 82 | { |
|---|
| 83 | osg::notify(osg::WARN) << "Cannot convert UTF-8 string to UTF-16." << std::endl; |
|---|
| 84 | return std::wstring(); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | std::wstring sDest(destLen, L'\0'); |
|---|
| 88 | destLen = MultiByteToWideChar(CP_UTF8, 0, source, sourceLength, &sDest[0], destLen); |
|---|
| 89 | |
|---|
| 90 | if (destLen <= 0) |
|---|
| 91 | { |
|---|
| 92 | osg::notify(osg::WARN) << "Cannot convert UTF-8 string to UTF-16." << std::endl; |
|---|
| 93 | return std::wstring(); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | return sDest; |
|---|
| 97 | #else |
|---|
| 98 | |
|---|
| 99 | osg::notify(osg::WARN) << "ConvertUTF8toUTF16 not implemented." << std::endl; |
|---|
| 100 | return std::wstring(); |
|---|
| 101 | #endif |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | std::string convertStringFromCurrentCodePageToUTF8(const char* source, unsigned sourceLength) |
|---|
| 105 | { |
|---|
| 106 | #if defined(WIN32) && !defined(__CYGWIN__) |
|---|
| 107 | if (sourceLength == 0) |
|---|
| 108 | { |
|---|
| 109 | return std::string(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | int utf16Length = MultiByteToWideChar(CP_ACP, 0, source, sourceLength, 0, 0); |
|---|
| 113 | if (utf16Length <= 0) |
|---|
| 114 | { |
|---|
| 115 | osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl; |
|---|
| 116 | return std::string(); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | std::wstring sUTF16(utf16Length, L'\0'); |
|---|
| 120 | utf16Length = MultiByteToWideChar(CP_ACP, 0, source, sourceLength, &sUTF16[0], utf16Length); |
|---|
| 121 | if (utf16Length <= 0) |
|---|
| 122 | { |
|---|
| 123 | osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl; |
|---|
| 124 | return std::string(); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | return convertUTF16toUTF8(sUTF16); |
|---|
| 128 | #else |
|---|
| 129 | return source; |
|---|
| 130 | #endif |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | std::string convertStringFromUTF8toCurrentCodePage(const char* source, unsigned sourceLength) |
|---|
| 134 | { |
|---|
| 135 | #if defined(WIN32) && !defined(__CYGWIN__) |
|---|
| 136 | if (sourceLength == 0) |
|---|
| 137 | { |
|---|
| 138 | return std::string(); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | std::wstring utf16 = convertUTF8toUTF16(source, sourceLength); |
|---|
| 142 | sourceLength = utf16.length(); |
|---|
| 143 | |
|---|
| 144 | int destLen = WideCharToMultiByte(CP_ACP, 0, utf16.c_str(), sourceLength, 0, 0, 0, 0); |
|---|
| 145 | if (destLen <= 0) |
|---|
| 146 | { |
|---|
| 147 | osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl; |
|---|
| 148 | return std::string(); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | std::string sDest(destLen, '\0'); |
|---|
| 152 | destLen = WideCharToMultiByte(CP_ACP, 0, utf16.c_str(), sourceLength, 0, 0, 0, 0); |
|---|
| 153 | if (destLen <= 0) |
|---|
| 154 | { |
|---|
| 155 | osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl; |
|---|
| 156 | return std::string(); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | return sDest; |
|---|
| 160 | #else |
|---|
| 161 | return source; |
|---|
| 162 | #endif |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | } |
|---|