| 165 | }; |
| 166 | |
| 167 | std::string UTF8ToCodePageWin(const std::string& str, unsigned int code_page) { |
| 168 | int wide_len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0); |
| 169 | if (wide_len <= 0) return ""; |
| 170 | |
| 171 | std::wstring wide_str(wide_len, L'\0'); |
| 172 | MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wide_str[0], wide_len); |
| 173 | |
| 174 | int local_len = WideCharToMultiByte( |
| 175 | code_page, 0, wide_str.c_str(), -1, nullptr, 0, nullptr, nullptr); |
| 176 | if (local_len <= 0) return ""; |
| 177 | |
| 178 | std::string local_str(local_len, '\0'); |
| 179 | WideCharToMultiByte(code_page, |
| 180 | 0, |
| 181 | wide_str.c_str(), |
| 182 | -1, |
| 183 | &local_str[0], |
| 184 | local_len, |
| 185 | nullptr, |
| 186 | nullptr); |
| 187 | |
| 188 | if (!local_str.empty() && local_str.back() == '\0') { |
| 189 | local_str.pop_back(); |
| 190 | } |
| 191 | |
| 192 | return local_str; |
| 193 | } |
| 194 | |
| 195 | #endif |
| 196 | |