| 557 | // --------------------------------------------------------------------------- |
| 558 | |
| 559 | static std::string Win32Recode(const char *src, unsigned src_code_page, |
| 560 | unsigned dst_code_page) { |
| 561 | // Convert from source code page to Unicode. |
| 562 | |
| 563 | // Compute the length in wide characters. |
| 564 | int wlen = MultiByteToWideChar(src_code_page, MB_ERR_INVALID_CHARS, src, -1, |
| 565 | nullptr, 0); |
| 566 | if (wlen == 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION) { |
| 567 | return std::string(); |
| 568 | } |
| 569 | |
| 570 | // Do the actual conversion. |
| 571 | std::wstring wbuf; |
| 572 | wbuf.resize(wlen); |
| 573 | MultiByteToWideChar(src_code_page, 0, src, -1, &wbuf[0], wlen); |
| 574 | |
| 575 | // Convert from Unicode to destination code page. |
| 576 | |
| 577 | // Compute the length in chars. |
| 578 | int len = WideCharToMultiByte(dst_code_page, 0, &wbuf[0], -1, nullptr, 0, |
| 579 | nullptr, nullptr); |
| 580 | |
| 581 | // Do the actual conversion. |
| 582 | std::string out; |
| 583 | out.resize(len); |
| 584 | WideCharToMultiByte(dst_code_page, 0, &wbuf[0], -1, &out[0], len, nullptr, |
| 585 | nullptr); |
| 586 | out.resize(strlen(out.c_str())); |
| 587 | |
| 588 | return out; |
| 589 | } |
| 590 | |
| 591 | #endif // _defined(_WIN32) |
| 592 |
no outgoing calls
no test coverage detected