diff options
author | bonmas14 <bonmas14@gmail.com> | 2025-08-23 01:28:41 +0300 |
---|---|---|
committer | bonmas14 <bonmas14@gmail.com> | 2025-08-23 01:28:41 +0300 |
commit | 8ebdc95621bc61fdf3c98cd7ae4ddca67398df23 (patch) | |
tree | 205413ed09ac001e37267889d429a363c37008f2 /src/un_wstrings.c | |
parent | c96c355b360f18f982459e1a866dcbf5864efdb8 (diff) | |
download | ungrateful-8ebdc95621bc61fdf3c98cd7ae4ddca67398df23.tar.gz ungrateful-8ebdc95621bc61fdf3c98cd7ae4ddca67398df23.zip |
Diffstat (limited to 'src/un_wstrings.c')
-rw-r--r-- | src/un_wstrings.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/un_wstrings.c b/src/un_wstrings.c new file mode 100644 index 0000000..601c809 --- /dev/null +++ b/src/un_wstrings.c @@ -0,0 +1,66 @@ +#if defined(OS_WINDOWS) +wchar* un_wstring_from_string(String utf8_string, Allocator alloc) { + return un_wstring_from_cstring(un_string_to_cstring(utf8_string, un_allocator_get_temporary()), alloc); +} + +wchar* un_wstring_from_cstring(u8 *utf8_string, Allocator alloc) { + u32 error_code, buffer_size, wrote_chars; + wchar *string_buffer; + assert(utf8_string != NULL); + + buffer_size = MultiByteToWideChar( + CP_UTF8, + 0, // or MB_PRECOMPOSED + (LPCCH)utf8_string, + -1, + NULL, + 0 + ); + + if (!buffer_size) { + error_code = GetLastError(); + + switch (error_code) { + case ERROR_NO_UNICODE_TRANSLATION: return NULL; break; + default: + assert(false); + break; + } + + return NULL; + } + + string_buffer = un_memory_alloc(sizeof(wchar) * buffer_size, alloc); + + if (string_buffer == NULL) { + return NULL; + } + + wrote_chars = MultiByteToWideChar( + CP_UTF8, + 0, // or MB_PRECOMPOSED + (LPCCH)utf8_string, + -1, + (LPWSTR)string_buffer, + buffer_size + ); + + assert(wrote_chars == buffer_size); + + return string_buffer; +} +#else + +wchar* un_wstring_from_string(String utf8_string, Allocator alloc) { + UNUSED(utf8_string); + UNUSED(alloc); + __TRAP(); +} + +wchar* un_wstring_from_cstring(u8 *utf8_string, Allocator alloc) { + UNUSED(utf8_string); + UNUSED(alloc); + __TRAP(); +} + +#endif |