[0c6b92a] | 1 | #include "./win_utils.hh"
|
---|
| 2 |
|
---|
| 3 | std::wstring utf8ToUtf16(std::string input) {
|
---|
| 4 | unsigned int len = MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1, NULL, 0);
|
---|
| 5 | WCHAR *output = new WCHAR[len];
|
---|
| 6 | MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1, output, len);
|
---|
| 7 | std::wstring res(output);
|
---|
| 8 | delete output;
|
---|
| 9 | return res;
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | std::string utf16ToUtf8(const WCHAR *input, size_t length) {
|
---|
| 13 | unsigned int len = WideCharToMultiByte(CP_UTF8, 0, input, length, NULL, 0, NULL, NULL);
|
---|
| 14 | char *output = new char[len + 1];
|
---|
| 15 | WideCharToMultiByte(CP_UTF8, 0, input, length, output, len, NULL, NULL);
|
---|
| 16 | output[len] = '\0';
|
---|
| 17 | std::string res(output);
|
---|
| 18 | delete output;
|
---|
| 19 | return res;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | std::string normalizePath(std::string path) {
|
---|
| 23 | // Prevent truncation to MAX_PATH characters by adding the \\?\ prefix
|
---|
| 24 | std::wstring p = utf8ToUtf16("\\\\?\\" + path);
|
---|
| 25 |
|
---|
| 26 | // Get the required length for the output
|
---|
| 27 | unsigned int len = GetLongPathNameW(p.data(), NULL, 0);
|
---|
| 28 | if (!len) {
|
---|
| 29 | return path;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | // Allocate output array and get long path
|
---|
| 33 | WCHAR *output = new WCHAR[len];
|
---|
| 34 | len = GetLongPathNameW(p.data(), output, len);
|
---|
| 35 | if (!len) {
|
---|
| 36 | delete output;
|
---|
| 37 | return path;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | // Convert back to utf8
|
---|
| 41 | std::string res = utf16ToUtf8(output + 4, len - 4);
|
---|
| 42 | delete output;
|
---|
| 43 | return res;
|
---|
| 44 | }
|
---|