| 108 | } |
| 109 | |
| 110 | std::string fromNative(const std::wstring& in) |
| 111 | { |
| 112 | if (in.empty()) |
| 113 | return std::string(); |
| 114 | |
| 115 | // The first call determines the length of the conversion. The second does the |
| 116 | // actual conversion. |
| 117 | int len = WideCharToMultiByte(CP_UTF8, 0, in.data(), in.length(), nullptr, 0, nullptr, nullptr); |
| 118 | std::string out(len, 0); |
| 119 | if (WideCharToMultiByte(CP_UTF8, 0, in.data(), in.length(), out.data(), len, |
| 120 | nullptr, nullptr) == 0) |
| 121 | { |
| 122 | int err = GetLastError(); |
| 123 | char buf[200] {}; |
| 124 | len = FormatMessageA(0, 0, GetLastError(), 0, buf, 199, 0); |
| 125 | throw pdal_error("Can't convert UTF16 to UTF8: " + std::string(buf, len)); |
| 126 | } |
| 127 | return out; |
| 128 | } |
| 129 | #else // Unix, OSX, MinGW |
| 130 | std::string toNative(const std::string& in) |
| 131 | { |