1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21#include "utf8.h"
22
23#ifdef LOVE_WINDOWS
24
25namespace love
26{
27
28std::string to_utf8(LPCWSTR wstr)
29{
30 size_t wide_len = wcslen(wstr)+1;
31
32 // Get size in UTF-8.
33 int utf8_size = WideCharToMultiByte(CP_UTF8, 0, wstr, wide_len, 0, 0, 0, 0);
34
35 char *utf8_str = new char[utf8_size];
36
37 // Convert to UTF-8.
38 int ok = WideCharToMultiByte(CP_UTF8, 0, wstr, wide_len, utf8_str, utf8_size, 0, 0);
39
40 std::string ret;
41 if (ok)
42 ret = utf8_str;
43
44 delete[] utf8_str;
45 return ret;
46}
47
48std::wstring to_widestr(const std::string &str)
49{
50 if (str.empty())
51 return std::wstring();
52
53 int wide_size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int) str.length(), nullptr, 0);
54
55 if (wide_size == 0)
56 return std::wstring();
57
58 std::wstring widestr;
59 widestr.resize(wide_size);
60
61 int ok = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int) str.length(), &widestr[0], widestr.length());
62
63 if (!ok)
64 return std::wstring();
65
66 return widestr;
67}
68
69void replace_char(std::string &str, char find, char replace)
70{
71 int length = str.length();
72
73 for (int i = 0; i<length; i++)
74 {
75 if (str[i] == find)
76 str[i] = replace;
77 }
78}
79
80} // love
81
82#endif // LOVE_WINDOWS
83