diff options
author | David Robillard <d@drobilla.net> | 2024-12-10 22:22:44 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2024-12-11 00:22:12 -0500 |
commit | e41951c43a8f6a0bcf6b0fcceebec99f87f7d1e7 (patch) | |
tree | 20be9e945e56acedc3d91c4a36f0c36fab5bb05f /src/win32/win32_util.c | |
parent | ba2c2a08973cb5eae1feabbb9431c897cded03f6 (diff) | |
download | zix-e41951c43a8f6a0bcf6b0fcceebec99f87f7d1e7.tar.gz zix-e41951c43a8f6a0bcf6b0fcceebec99f87f7d1e7.tar.bz2 zix-e41951c43a8f6a0bcf6b0fcceebec99f87f7d1e7.zip |
Support building for Windows with or without UNICODE
Diffstat (limited to 'src/win32/win32_util.c')
-rw-r--r-- | src/win32/win32_util.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/win32/win32_util.c b/src/win32/win32_util.c new file mode 100644 index 0000000..959867c --- /dev/null +++ b/src/win32/win32_util.c @@ -0,0 +1,73 @@ +// Copyright 2019-2024 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#include "win32_util.h" + +#include <limits.h> +#include <windows.h> + +#ifdef UNICODE + +ArgPathChar* +arg_path_new(ZixAllocator* const allocator, const char* const path) +{ + return zix_utf8_to_wchar(allocator, path); +} + +void +arg_path_free(ZixAllocator* const allocator, ArgPathChar* const path) +{ + zix_free(allocator, path); +} + +#else // !defined(UNICODE) + +ArgPathChar* +arg_path_new(ZixAllocator* const allocator, const char* const path) +{ + (void)allocator; + return path; +} + +void +arg_path_free(ZixAllocator* const allocator, ArgPathChar* const path) +{ + (void)allocator; + (void)path; +} + +#endif + +wchar_t* +zix_utf8_to_wchar(ZixAllocator* const allocator, const char* const utf8) +{ + const int rc = utf8 ? MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0) : 0; + if (rc <= 0 || rc == INT_MAX) { + return NULL; + } + + wchar_t* const result = + (wchar_t*)zix_calloc(allocator, (size_t)rc, sizeof(wchar_t)); + if (result) { + MultiByteToWideChar(CP_UTF8, 0, utf8, -1, result, rc); + } + + return result; +} + +char* +zix_wchar_to_utf8(ZixAllocator* const allocator, const wchar_t* const wstr) +{ + const int rc = + wstr ? WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL) : 0; + if (rc <= 0 || rc == INT_MAX) { + return NULL; + } + + char* const result = (char*)zix_calloc(allocator, (size_t)rc, sizeof(char)); + if (result) { + WideCharToMultiByte(CP_UTF8, 0, wstr, -1, result, rc, NULL, NULL); + } + + return result; +} |