From d65dad255bd4db913ccea0f086c311c2d2c5ba09 Mon Sep 17 00:00:00 2001 From: TaurusXin Date: Thu, 21 Mar 2024 13:31:24 +0800 Subject: [PATCH] fix: filename issue with MinGW --- main.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/main.cpp b/main.cpp index 4907a4a..bf1552f 100644 --- a/main.cpp +++ b/main.cpp @@ -48,22 +48,14 @@ void processFilesInFolder(const fs::path& folderPath) { } #if defined(_WIN32) -int wmain(int argc, wchar_t* utf16argv[]) +int wmain(int argc, wchar_t* argv[]) #else int main(int argc, char **argv) #endif { #if defined(_WIN32) -#if defined(__MINGW64__) SetConsoleOutputCP(CP_UTF8); #endif - char **argv = (char**)malloc(sizeof(char *) * argc); - for (int i = 0; i < argc; ++i) { - int utf8_size = WideCharToMultiByte(CP_UTF8, 0, utf16argv[i], -1, NULL, 0, NULL, NULL); - argv[i] = (char*)malloc(utf8_size); - WideCharToMultiByte(CP_UTF8, 0, utf16argv[i], -1, argv[i], utf8_size, NULL, NULL); - } -#endif if (argc <= 1) { displayHelp(); @@ -75,10 +67,17 @@ int main(int argc, char **argv) bool folderProvided = false; +#if defined(_WIN32) +#define COMPARE_STR(s1, s2) (wcscmp(s1, s2) == 0) +#define HELP_SHORT L"-h" +#define HELP_LONG L"--help" +#define FOLDER L"-d" +#else #define COMPARE_STR(s1, s2) (strcmp(s1, s2) == 0) #define HELP_SHORT "-h" #define HELP_LONG "--help" #define FOLDER "-d" +#endif for (int i = 1; i < argc; ++i) { if (COMPARE_STR(argv[i], HELP_SHORT) || COMPARE_STR(argv[i], HELP_LONG)) { displayHelp(); @@ -95,7 +94,14 @@ int main(int argc, char **argv) return 1; } } else { +#if defined(_WIN32) + int utf8_size = WideCharToMultiByte(CP_UTF8, 0, argv[i], -1, NULL, 0, NULL, NULL); + char* u8str = new char[utf8_size]; + WideCharToMultiByte(CP_UTF8, 0, argv[i], -1, u8str, utf8_size, NULL, NULL); + fs::path path(u8str); +#else fs::path path(argv[i]); +#endif files.push_back(path); } }