ui/winui/util.cpp

Tue, 06 Feb 2024 11:44:48 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 06 Feb 2024 11:44:48 +0100
branch
newapi
changeset 245
06e655d15b17
parent 224
88bc21b19213
permissions
-rw-r--r--

fix usage of wrong type in ui_label_set (WinUI3)

#include "pch.h"

#include "util.h"

#include <stdlib.h>


wchar_t* str2wstr(const char* str, int* newlen) {
    size_t len = strlen(str);

    return str2wstr_len(str, len, newlen);
}

wchar_t* str2wstr_len(const char* str, size_t len, int* newlen) {
    wchar_t* wstr = (wchar_t*)calloc(len + 1, sizeof(wchar_t));
    int wlen = MultiByteToWideChar(
        CP_UTF8,
        0,
        str,
        len,
        wstr,
        len + 1
    );
    if (newlen) {
        *newlen = wlen;
    }
    wstr[wlen] = 0;

    return wstr;
}

char* wchar2utf8(const wchar_t* wstr, size_t wlen) {
    size_t maxlen = wlen * 4;
    char* ret = (char*)malloc(maxlen + 1);
    int ret_len = WideCharToMultiByte(
        CP_UTF8,
        0,
        wstr,
        wlen,
        ret,
        maxlen,
        NULL,
        NULL);
    ret[ret_len] = 0;
    return ret;
}

mercurial