2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "CharsetConverter.h"
18 std::string
FromW(const wchar_t* str
, size_t length
)
20 int result
= WideCharToMultiByte(CP_UTF8
, WC_ERR_INVALID_CHARS
, str
, length
, nullptr, 0, nullptr, nullptr);
24 auto newStr
= std::make_unique
<char[]>(result
);
25 result
= WideCharToMultiByte(CP_UTF8
, WC_ERR_INVALID_CHARS
, str
, length
, newStr
.get(), result
, nullptr, nullptr);
29 return std::string(newStr
.get(), result
);
32 std::string
FromW(const std::wstring
& str
)
34 return FromW(str
.c_str(), str
.length());
37 std::wstring
ToW(const char* str
, size_t length
)
39 int result
= MultiByteToWideChar(CP_UTF8
, MB_ERR_INVALID_CHARS
, str
, length
, nullptr, 0);
41 return std::wstring();
43 auto newStr
= std::make_unique
<wchar_t[]>(result
);
44 result
= MultiByteToWideChar(CP_UTF8
, MB_ERR_INVALID_CHARS
, str
, length
, newStr
.get(), result
);
47 return std::wstring();
49 return std::wstring(newStr
.get(), result
);
52 std::wstring
ToW(const std::string
& str
)
54 return ToW(str
.c_str(), str
.length());