function return empty string if inWStr is empty or if the conversion failed
| 63 | |
| 64 | // function return empty string if inWStr is empty or if the conversion failed |
| 65 | std::string |
| 66 | ToUTF8String(std::wstring const& inWStr) |
| 67 | { |
| 68 | if (inWStr.empty()) |
| 69 | { |
| 70 | return std::string(); |
| 71 | } |
| 72 | int char_count = WideCharToMultiByte(CP_UTF8, 0, inWStr.c_str(), -1, NULL, 0, NULL, NULL); |
| 73 | std::unique_ptr<char[]> str(new char[char_count]); |
| 74 | char_count = WideCharToMultiByte(CP_UTF8, 0, inWStr.c_str(), -1, str.get(), char_count, NULL, NULL); |
| 75 | if (char_count == 0) |
| 76 | { |
| 77 | ThrowInvalidArgument("Failed to convert UTF16 string to UTF8."); |
| 78 | } |
| 79 | return str.get(); |
| 80 | } |
| 81 | } // namespace util |
| 82 | } // namespace cppmicroservices |
| 83 | #endif |