| 1 | /* |
| 2 | * Copyright (C) 2005-2021 Team Kodi (https://kodi.tv) |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | * See LICENSE.md for more information. |
| 6 | */ |
| 7 | |
| 8 | #include <memory> |
| 9 | |
| 10 | #include "CharsetConverter.h" |
| 11 | |
| 12 | #include <windows.h> |
| 13 | |
| 14 | namespace KODI |
| 15 | { |
| 16 | namespace PLATFORM |
| 17 | { |
| 18 | namespace WINDOWS |
| 19 | { |
| 20 | std::string FromW(const wchar_t* str, size_t length) |
| 21 | { |
| 22 | int result = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, str, static_cast<int>(length), nullptr, 0, nullptr, nullptr); |
| 23 | if (result == 0) |
| 24 | return std::string(); |
| 25 | |
| 26 | auto newStr = std::make_unique<char[]>(result); |
| 27 | result = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, str, static_cast<int>(length), newStr.get(), result, nullptr, nullptr); |
| 28 | if (result == 0) |
| 29 | return std::string(); |
| 30 | |
| 31 | return std::string(newStr.get(), result); |
| 32 | } |
| 33 | |
| 34 | std::string FromW(const std::wstring& str) |
| 35 | { |
| 36 | return FromW(str.c_str(), str.length()); |
| 37 | } |
| 38 | |
| 39 | std::wstring ToW(const char* str, size_t length) |
| 40 | { |
| 41 | int result = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, static_cast<int>(length), nullptr, 0); |
| 42 | if (result == 0) |
| 43 | return std::wstring(); |
| 44 | |
| 45 | auto newStr = std::make_unique<wchar_t[]>(result); |
| 46 | result = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, static_cast<int>(length), newStr.get(), result); |
| 47 | |
| 48 | if (result == 0) |
| 49 | return std::wstring(); |
| 50 | |
| 51 | return std::wstring(newStr.get(), result); |
| 52 | } |
| 53 | |
| 54 | std::wstring ToW(const std::string& str) |
| 55 | { |
| 56 | return ToW(str.c_str(), str.length()); |
| 57 | } |
| 58 | |
| 59 | } |
| 60 | } |
| 61 | } |