| 545 | // --------------------------------------------------------------------------- |
| 546 | |
| 547 | static std::string Win32Recode(const char *src, unsigned src_code_page, |
| 548 | unsigned dst_code_page) { |
| 549 | // Convert from source code page to Unicode. |
| 550 | |
| 551 | // Compute the length in wide characters. |
| 552 | int wlen = MultiByteToWideChar(src_code_page, MB_ERR_INVALID_CHARS, src, -1, |
| 553 | nullptr, 0); |
| 554 | if (wlen == 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION) { |
| 555 | return std::string(); |
| 556 | } |
| 557 | |
| 558 | // Do the actual conversion. |
| 559 | std::wstring wbuf; |
| 560 | wbuf.resize(wlen); |
| 561 | MultiByteToWideChar(src_code_page, 0, src, -1, &wbuf[0], wlen); |
| 562 | |
| 563 | // Convert from Unicode to destination code page. |
| 564 | |
| 565 | // Compute the length in chars. |
| 566 | int len = WideCharToMultiByte(dst_code_page, 0, &wbuf[0], -1, nullptr, 0, |
| 567 | nullptr, nullptr); |
| 568 | |
| 569 | // Do the actual conversion. |
| 570 | std::string out; |
| 571 | out.resize(len); |
| 572 | WideCharToMultiByte(dst_code_page, 0, &wbuf[0], -1, &out[0], len, nullptr, |
| 573 | nullptr); |
| 574 | out.resize(strlen(out.c_str())); |
| 575 | |
| 576 | return out; |
| 577 | } |
| 578 | |
| 579 | // --------------------------------------------------------------------------- |
| 580 |
no test coverage detected