| 52 | } |
| 53 | |
| 54 | std::string ToString(const std::wstring& source, bool utf8) |
| 55 | { |
| 56 | std::string result; |
| 57 | if (source.length() > 0) { |
| 58 | UINT codepage = CP_UTF8; |
| 59 | if (!utf8) { |
| 60 | codepage = AreFileApisANSI() ? GetACP() : GetOEMCP(); |
| 61 | } |
| 62 | int sizeRequired = ::WideCharToMultiByte(codepage, 0, &source[0], -1, nullptr, 0, |
| 63 | nullptr, nullptr); |
| 64 | if (sizeRequired == 0) { |
| 65 | throw windows_error("failed to convert string to multibyte"); |
| 66 | } |
| 67 | // the size returned by WideCharToMultiByte contains zero termination IF -1 is |
| 68 | // specified for the length. we don't want that \0 in the string because then the |
| 69 | // length field would be wrong. Because madness |
| 70 | result.resize(sizeRequired - 1, '\0'); |
| 71 | ::WideCharToMultiByte(codepage, 0, &source[0], (int)source.size(), &result[0], |
| 72 | sizeRequired, nullptr, nullptr); |
| 73 | } |
| 74 | |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | std::wstring ToWString(const std::string& source, bool utf8) |
| 79 | { |
no test coverage detected