| 124 | } |
| 125 | |
| 126 | std::wstring Encoding::ToWide(std::string const& str) |
| 127 | { |
| 128 | std::wstring wstr; |
| 129 | #if defined(_WIN32) |
| 130 | int const wlength = |
| 131 | MultiByteToWideChar(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.data(), |
| 132 | int(str.size()), nullptr, 0); |
| 133 | if (wlength > 0) { |
| 134 | wchar_t* wdata = new wchar_t[wlength]; |
| 135 | int r = MultiByteToWideChar(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.data(), |
| 136 | int(str.size()), wdata, wlength); |
| 137 | if (r > 0) { |
| 138 | wstr = std::wstring(wdata, wlength); |
| 139 | } |
| 140 | delete[] wdata; |
| 141 | } |
| 142 | #else |
| 143 | size_t pos = 0; |
| 144 | size_t nullPos = 0; |
| 145 | do { |
| 146 | if (pos < str.size() && str.at(pos) != '\0') { |
| 147 | wstr += ToWide(str.c_str() + pos); |
| 148 | } |
| 149 | nullPos = str.find('\0', pos); |
| 150 | if (nullPos != std::string::npos) { |
| 151 | pos = nullPos + 1; |
| 152 | wstr += wchar_t('\0'); |
| 153 | } |
| 154 | } while (nullPos != std::string::npos); |
| 155 | #endif |
| 156 | return wstr; |
| 157 | } |
| 158 | |
| 159 | std::string Encoding::ToNarrow(std::wstring const& str) |
| 160 | { |