Skip to content

Commit

Permalink
Add UTF-8 filename support on Windows to ktxtools (KhronosGroup#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
aqnuep authored Oct 27, 2023
1 parent df5e8c3 commit 7d7802c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ if(MSVC)
# because `/W4;/WX` is returned as a single string.
add_compile_options( /W4;$<$<BOOL:${KTX_WERROR}>:/WX> )
add_compile_options( $<IF:$<CONFIG:Debug>,/Gz,/O2> )
# Enable UTF-8 support
add_compile_options( $<$<C_COMPILER_ID:MSVC>:/utf-8> )
add_compile_options( $<$<CXX_COMPILER_ID:MSVC>:/utf-8> )
elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU"
OR ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
add_compile_options( -Wall -Wextra $<$<BOOL:${KTX_WERROR}>:-Werror>)
Expand Down Expand Up @@ -915,6 +918,7 @@ add_library( objUtil STATIC
utils/sbufstream.h
utils/scapp.h
utils/stdafx.h
utils/platform_utils.h
utils/unused.h
)
target_include_directories(
Expand Down
63 changes: 63 additions & 0 deletions utils/platform_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2022-2023 The Khronos Group Inc.
// Copyright 2022-2023 RasterGrid Kft.
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "stdafx.h"
#include <string>
#include <iostream>
#include <memory>
#include <vector>
#include <fmt/ostream.h>
#include <fmt/printf.h>

#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <shellapi.h>
#endif

#if defined(_WIN32) && !defined(_UNICODE)
// For Windows, we convert the UTF-8 path to a UTF-16 path to force using the APIs
// that correctly handle unicode characters
inline std::wstring DecodeUTF8Path(std::string path) {
std::wstring result;
int len = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), static_cast<int>(path.length()), NULL, 0);
if (len > 0)
{
result.resize(len);
MultiByteToWideChar(CP_UTF8, 0, path.c_str(), static_cast<int>(path.length()), &result[0], len);
}
return result;
}
#else
// For other platforms there is no need for any conversion, they support UTF-8 natively
inline std::string DecodeUTF8Path(std::string path) {
return path;
}
#endif

inline void InitUTF8CLI(int& argc, _TCHAR* argv[]) {
#if defined(_WIN32) && !defined(_UNICODE)
// Windows does not support UTF-8 argv so we have to manually acquire it
static std::vector<std::unique_ptr<_TCHAR[]>> utf8Argv(argc);
LPWSTR commandLine = GetCommandLineW();
LPWSTR* wideArgv = CommandLineToArgvW(commandLine, &argc);
for (int i = 0; i < argc; ++i) {
int byteSize = WideCharToMultiByte(CP_UTF8, 0, wideArgv[i], -1, nullptr, 0, nullptr, nullptr);
utf8Argv[i] = std::make_unique<_TCHAR[]>(byteSize);
WideCharToMultiByte(CP_UTF8, 0, wideArgv[i], -1, utf8Argv[i].get(), byteSize, nullptr, nullptr);
argv[i] = utf8Argv[i].get();
}
#else
// Nothing to do for other platforms
(void)argc;
(void)argv;
#endif
}

0 comments on commit 7d7802c

Please sign in to comment.