From 8b6b2a968c2f6927b430e4fcc53a01be04ae47a7 Mon Sep 17 00:00:00 2001 From: Takase <20792268+takase1121@users.noreply.github.com> Date: Fri, 17 Jun 2022 21:31:52 +0800 Subject: [PATCH] make system.* functions support UTF8 filenames (#1042) * make system.* functions support UTF8 filenames * move utfconv.h into ifdef guard * fix wrong null check --- src/utfconv.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/utfconv.h diff --git a/src/utfconv.h b/src/utfconv.h new file mode 100644 index 000000000..059b30715 --- /dev/null +++ b/src/utfconv.h @@ -0,0 +1,57 @@ +#ifndef MBSEC_H +#define MBSEC_H + +#ifdef _WIN32 + +#include +#include + +#define UTFCONV_ERROR_INVALID_CONVERSION "Input contains invalid byte sequences." + +LPWSTR utfconv_utf8towc(const char *str) { + LPWSTR output; + int len; + + // len includes \0 + len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); + if (len == 0) + return NULL; + + output = (LPWSTR) malloc(sizeof(WCHAR) * len); + if (output == NULL) + return NULL; + + len = MultiByteToWideChar(CP_UTF8, 0, str, -1, output, len); + if (len == 0) { + free(output); + return NULL; + } + + return output; +} + +char *utfconv_wctoutf8(LPCWSTR str) { + char *output; + int len; + + // len includes \0 + len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL); + if (len == 0) + return NULL; + + output = (char *) malloc(sizeof(char) * len); + if (output == NULL) + return NULL; + + len = WideCharToMultiByte(CP_UTF8, 0, str, -1, output, len, NULL, NULL); + if (len == 0) { + free(output); + return NULL; + } + + return output; +} + +#endif + +#endif