From a3a5f6e07ea94257392ad903097b09d961f5cd5a Mon Sep 17 00:00:00 2001 From: Brad Parker Date: Wed, 27 Dec 2017 02:55:53 +0000 Subject: [PATCH] win32: add Unicode versions of getenv, GetModuleFileName, LoadLibrary and _mkdir --- file_path_special.c | 45 ++++++++++++++++++++++++++++++-- libretro-common/dynamic/dylib.c | 17 ++++++++++++ libretro-common/file/file_path.c | 11 ++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/file_path_special.c b/file_path_special.c index 9113f27dc6..4b2b38ff79 100644 --- a/file_path_special.c +++ b/file_path_special.c @@ -13,6 +13,14 @@ * If not, see . */ +/* Assume W-functions do not work below Win2K and Xbox platforms */ +#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX) + +#ifndef LEGACY_WIN32 +#define LEGACY_WIN32 +#endif + +#endif #ifdef _WIN32 #include @@ -45,6 +53,7 @@ #include #include #include +#include #ifdef HAVE_CONFIG_H #include "config.h" @@ -171,6 +180,7 @@ void fill_pathname_abbreviate_special(char *out_path, bool fill_pathname_application_data(char *s, size_t len) { #if defined(_WIN32) && !defined(_XBOX) +#ifdef LEGACY_WIN32 const char *appdata = getenv("APPDATA"); if (appdata) @@ -178,6 +188,21 @@ bool fill_pathname_application_data(char *s, size_t len) strlcpy(s, appdata, len); return true; } +#else + const wchar_t *appdataW = _wgetenv(L"APPDATA"); + + if (appdataW) + { + char *appdata = utf16_to_utf8_string_alloc(appdataW); + + if (appdata) + { + strlcpy(s, appdata, len); + free(appdata); + return true; + } + } +#endif #elif defined(OSX) const char *appdata = getenv("HOME"); @@ -224,7 +249,8 @@ void fill_pathname_application_path(char *s, size_t len) #endif #ifdef _WIN32 DWORD ret; - wchar_t ws[PATH_MAX_LENGTH] = {0}; + char *str; + wchar_t wstr[PATH_MAX_LENGTH] = {0}; #endif #ifdef __HAIKU__ image_info info; @@ -236,7 +262,22 @@ void fill_pathname_application_path(char *s, size_t len) return; #ifdef _WIN32 - ret = GetModuleFileName(GetModuleHandle(NULL), s, len - 1); +#ifdef LEGACY_WIN32 + ret = GetModuleFileNameA(GetModuleHandle(NULL), s, len); +#else + ret = GetModuleFileNameW(GetModuleHandle(NULL), wstr, ARRAY_SIZE(wstr)); + + if (*wstr) + { + str = utf16_to_utf8_string_alloc(wstr); + + if (str) + { + strlcpy(s, str, len); + free(str); + } + } +#endif s[ret] = '\0'; #elif defined(__APPLE__) if (bundle) diff --git a/libretro-common/dynamic/dylib.c b/libretro-common/dynamic/dylib.c index ab10226032..d4864a7057 100644 --- a/libretro-common/dynamic/dylib.c +++ b/libretro-common/dynamic/dylib.c @@ -23,6 +23,7 @@ #include #include #include +#include #ifdef NEED_DYNAMIC @@ -33,6 +34,15 @@ #include #endif +/* Assume W-functions do not work below Win2K and Xbox platforms */ +#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX) + +#ifndef LEGACY_WIN32 +#define LEGACY_WIN32 +#endif + +#endif + #ifdef _WIN32 static char last_dyn_error[512]; @@ -65,7 +75,14 @@ dylib_t dylib_load(const char *path) { #ifdef _WIN32 int prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); +#ifdef LEGACY_WIN32 dylib_t lib = LoadLibrary(path); +#else + wchar_t *pathW = utf8_to_utf16_string_alloc(path); + dylib_t lib = LoadLibraryW(pathW); + + free(pathW); +#endif SetErrorMode(prevmode); diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index b3c21c64e7..0086c6bda1 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -275,7 +275,18 @@ bool path_mkdir(const char *dir) if (norecurse) { #if defined(_WIN32) +#ifdef LEGACY_WIN32 int ret = _mkdir(dir); +#else + wchar_t *dirW = utf8_to_utf16_string_alloc(dir); + int ret = -1; + + if (dirW) + { + ret = _wmkdir(dirW); + free(dirW); + } +#endif #elif defined(IOS) int ret = mkdir(dir, 0755); #elif defined(VITA) || defined(PSP)