Avoid using wmain on Windows

This commit is contained in:
Chris Robinson
2017-08-30 16:33:44 -07:00
parent 0408f9b7df
commit 67f183f206
5 changed files with 55 additions and 56 deletions
-3
View File
@@ -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
+47 -50
View File
@@ -12,80 +12,77 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
static char *ToUTF8(const wchar_t *from)
{
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;
}
static WCHAR *FromUTF8(const char *str)
{
WCHAR *out = NULL;
int len;
if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
{
out = calloc(sizeof(WCHAR), len);
MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
out[len-1] = 0;
}
return out;
}
#include <shellapi.h>
static FILE *my_fopen(const char *fname, const char *mode)
{
WCHAR *wname=NULL, *wmode=NULL;
int namelen, modelen;
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);
namelen = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0);
modelen = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
if(namelen <= 0 || modelen <= 0)
{
fprintf(stderr, "Failed to convert UTF-8 fname \"%s\", mode \"%s\"\n", fname, mode);
return NULL;
}
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);
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;
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);
atexit(cleanup_arglist);
arglist = calloc(sizeof(*arglist), argc+1);
for(i = 0;i < argc;i++)
arglist[i] = ToUTF8(wargv[i]);
return main(argc, 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
+4 -3
View File
@@ -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)
+2
View File
@@ -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]);
+2
View File
@@ -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");