aboutsummaryrefslogtreecommitdiff
path: root/src/un_wstrings.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/un_wstrings.c')
-rw-r--r--src/un_wstrings.c66
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