| | 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 |
| | 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 | } |
| | 166 | |