From 5cfe439e488912266dd7fb787c2dd18d19b57020 Mon Sep 17 00:00:00 2001 From: TaurusXin Date: Mon, 12 Feb 2024 21:52:56 +0800 Subject: [PATCH] fix: cannot recognize non-ASCII file name on Windows --- CMakeLists.txt | 16 +++++++++++----- main.cpp | 16 +++++++--------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c22acda..11c89da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,8 @@ add_subdirectory(taglib) FILE(GLOB HEADERS ./*.h) FILE(GLOB SOURCES ./*.cpp) add_executable(ncmdump - ${HEADERS} - ${SOURCES} + ${HEADERS} + ${SOURCES} ) target_link_libraries(ncmdump tag) target_include_directories(ncmdump PRIVATE taglib) @@ -19,9 +19,15 @@ target_include_directories(ncmdump PRIVATE taglib/taglib) target_include_directories(ncmdump PRIVATE taglib/taglib/toolkit) target_include_directories(ncmdump PRIVATE taglib/taglib/mpeg/id3v2) +if(WIN32) + if(CMAKE_COMPILER_IS_GNUCXX) + target_link_options(ncmdump PRIVATE -O2 -municode) + endif() +endif() + include(GNUInstallDirs) install(TARGETS ncmdump - BUNDLE DESTINATION . - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + BUNDLE DESTINATION . + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) diff --git a/main.cpp b/main.cpp index ab82e3f..4c36c26 100644 --- a/main.cpp +++ b/main.cpp @@ -5,7 +5,7 @@ #include #include -#if defined(_WIN32) && defined(_MSC_VER) +#if defined(_WIN32) #include #endif @@ -47,13 +47,13 @@ void processFilesInFolder(const fs::path& folderPath) { } } -#if defined(_WIN32) && defined(_MSC_VER) +#if defined(_WIN32) int wmain(int argc, wchar_t* argv[]) #else int main(int argc, char **argv) #endif { -#if defined(_WIN32) && defined(_MSC_VER) +#if defined(_WIN32) && defined(__MINGW64__) SetConsoleOutputCP(CP_UTF8); #endif @@ -67,7 +67,7 @@ int main(int argc, char **argv) bool folderProvided = false; -#if defined(_WIN32) && defined(_MSC_VER) +#if defined(_WIN32) #define COMPARE_STR(s1, s2) (wcscmp(s1, s2) == 0) #define HELP_SHORT L"-h" #define HELP_LONG L"--help" @@ -95,11 +95,9 @@ int main(int argc, char **argv) return 1; } } else { -#if defined(_WIN32) && defined(_MSC_VER) - int multiByteStrSize = WideCharToMultiByte(CP_UTF8, 0, argv[1], -1, NULL, 0, NULL, NULL); - char *multiByteStr = new char[multiByteStrSize]; - WideCharToMultiByte(CP_UTF8, 0, argv[i], -1, multiByteStr, multiByteStrSize, NULL, NULL); - fs::path path(multiByteStr); +#if defined(_WIN32) + std::wstring wFilePath(argv[i]); + fs::path path(wFilePath); #else fs::path path(argv[i]); #endif