diff options
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | common/win_main_utf8.h | 93 | ||||
-rw-r--r-- | native-tools/CMakeLists.txt | 7 | ||||
-rw-r--r-- | native-tools/bsincgen.c | 2 | ||||
-rw-r--r-- | utils/makehrtf.c | 2 |
5 files changed, 53 insertions, 54 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 626ba560..29a51f31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1418,9 +1418,6 @@ IF(ALSOFT_UTILS) IF(HAVE_LIBM) TARGET_LINK_LIBRARIES(makehrtf m) ENDIF() - IF(WIN32 AND CMAKE_COMPILER_IS_GNUCC) - SET_PROPERTY(TARGET makehrtf APPEND_STRING PROPERTY LINK_FLAGS " -municode") - ENDIF() IF(ALSOFT_INSTALL) INSTALL(TARGETS openal-info makehrtf diff --git a/common/win_main_utf8.h b/common/win_main_utf8.h index d83aa610..1299e49e 100644 --- a/common/win_main_utf8.h +++ b/common/win_main_utf8.h @@ -12,80 +12,77 @@ #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include <windows.h> +#include <shellapi.h> -static char *ToUTF8(const wchar_t *from) +static FILE *my_fopen(const char *fname, const char *mode) { - char *out = NULL; - int len; - if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0) - { - out = calloc(sizeof(*out), len); - WideCharToMultiByte(CP_UTF8, 0, from, -1, out, len, NULL, NULL); - out[len-1] = 0; - } - return out; -} + WCHAR *wname=NULL, *wmode=NULL; + int namelen, modelen; + FILE *file = NULL; -static WCHAR *FromUTF8(const char *str) -{ - WCHAR *out = NULL; - int len; + namelen = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0); + modelen = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0); - if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0) + if(namelen <= 0 || modelen <= 0) { - out = calloc(sizeof(WCHAR), len); - MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len); - out[len-1] = 0; + fprintf(stderr, "Failed to convert UTF-8 fname \"%s\", mode \"%s\"\n", fname, mode); + return NULL; } - return out; -} + wname = calloc(sizeof(WCHAR), namelen+modelen); + wmode = wname + namelen; + MultiByteToWideChar(CP_UTF8, 0, fname, -1, wname, -1); + MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, -1); -static FILE *my_fopen(const char *fname, const char *mode) -{ - WCHAR *wname=NULL, *wmode=NULL; - FILE *file = NULL; - - wname = FromUTF8(fname); - wmode = FromUTF8(mode); - if(!wname) - fprintf(stderr, "Failed to convert UTF-8 filename: \"%s\"\n", fname); - else if(!wmode) - fprintf(stderr, "Failed to convert UTF-8 mode: \"%s\"\n", mode); - else - file = _wfopen(wname, wmode); + file = _wfopen(wname, wmode); free(wname); - free(wmode); return file; } #define fopen my_fopen -#define main my_main -int main(int argc, char *argv[]); - static char **arglist; static void cleanup_arglist(void) { - int i; - for(i = 0;arglist[i];i++) - free(arglist[i]); free(arglist); } -int wmain(int argc, const wchar_t *wargv[]) +static void GetUnicodeArgs(int *argc, char ***argv) { - int i; + size_t total; + wchar_t **args; + int nargs, i; - atexit(cleanup_arglist); - arglist = calloc(sizeof(*arglist), argc+1); - for(i = 0;i < argc;i++) - arglist[i] = ToUTF8(wargv[i]); + args = CommandLineToArgvW(GetCommandLineW(), &nargs); + if(!args) + { + fprintf(stderr, "Failed to get command line args: %ld\n", GetLastError()); + exit(EXIT_FAILURE); + } + + total = sizeof(**argv) * nargs; + for(i = 0;i < nargs;i++) + total += WideCharToMultiByte(CP_UTF8, 0, args[i], -1, NULL, 0, NULL, NULL); - return main(argc, arglist); + atexit(cleanup_arglist); + arglist = *argv = calloc(1, total); + (*argv)[0] = (char*)(*argv + nargs); + for(i = 0;i < nargs-1;i++) + { + int len = WideCharToMultiByte(CP_UTF8, 0, args[i], -1, (*argv)[i], -1, NULL, NULL); + (*argv)[i+1] = (*argv)[i] + len; + } + WideCharToMultiByte(CP_UTF8, 0, args[i], -1, (*argv)[i], -1, NULL, NULL); + *argc = nargs; } +#define GET_UNICODE_ARGS(argc, argv) GetUnicodeArgs(argc, argv) + +#else + +/* Do nothing. */ +#define GET_UNICODE_ARGS(argc, argv) #endif diff --git a/native-tools/CMakeLists.txt b/native-tools/CMakeLists.txt index 3c66c8bc..c3055f8d 100644 --- a/native-tools/CMakeLists.txt +++ b/native-tools/CMakeLists.txt @@ -4,6 +4,10 @@ project(native-tools) include(CheckLibraryExists) +if(WIN32) + add_definitions("-D_WIN32") +endif(WIN32) + check_library_exists(m pow "" HAVE_LIBM) add_executable(bin2h bin2h.c) @@ -20,6 +24,3 @@ set_target_properties(bsincgen PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CM if(HAVE_LIBM) target_link_libraries(bsincgen m) endif(HAVE_LIBM) -if(WIN32 AND CMAKE_COMPILER_IS_GNUCC) - set_property(TARGET bsincgen APPEND_STRING PROPERTY LINK_FLAGS " -municode") -endif(WIN32 AND CMAKE_COMPILER_IS_GNUCC) diff --git a/native-tools/bsincgen.c b/native-tools/bsincgen.c index 754e0709..e502cdd0 100644 --- a/native-tools/bsincgen.c +++ b/native-tools/bsincgen.c @@ -362,6 +362,8 @@ int main(int argc, char *argv[]) {
FILE *output;
+ GET_UNICODE_ARGS(&argc, &argv);
+
if(argc > 2)
{
fprintf(stderr, "Usage: %s [output file]\n", argv[0]);
diff --git a/utils/makehrtf.c b/utils/makehrtf.c index 516318e8..ca810b84 100644 --- a/utils/makehrtf.c +++ b/utils/makehrtf.c @@ -2885,6 +2885,8 @@ int main(int argc, char *argv[]) double limit; int opt; + GET_UNICODE_ARGS(&argc, &argv); + if(argc < 2) { fprintf(stdout, "HRTF Processing and Composition Utility\n\n"); |