aboutsummaryrefslogtreecommitdiff
path: root/src/un_wstrings.c
blob: 601c80900680064affd8cc1f4e5a86067d2ee612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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