misc/bstr: add bstr_to_wchar for win32

Convenience to avoid strlen above other things.
This commit is contained in:
Kacper Michajłow 2024-03-17 12:54:06 +01:00
parent c1282d4d43
commit 2ee0db4c5d
2 changed files with 26 additions and 0 deletions

View File

@ -467,3 +467,23 @@ bool bstr_decode_hex(void *talloc_ctx, struct bstr hex, struct bstr *out)
*out = (struct bstr){ .start = arr, .len = len }; *out = (struct bstr){ .start = arr, .len = len };
return true; return true;
} }
#ifdef _WIN32
#include <windows.h>
int bstr_to_wchar(void *talloc_ctx, struct bstr s, wchar_t **ret)
{
int count = MultiByteToWideChar(CP_UTF8, 0, s.start, s.len, NULL, 0);
if (count <= 0)
abort();
wchar_t *wbuf = *ret;
if (!wbuf || ta_get_size(wbuf) < (count + 1) * sizeof(wchar_t))
wbuf = talloc_realloc(talloc_ctx, wbuf, wchar_t, count + 1);
MultiByteToWideChar(CP_UTF8, 0, s.start, s.len, wbuf, count);
wbuf[count] = L'\0';
*ret = wbuf;
return count;
}
#endif

View File

@ -223,6 +223,12 @@ static inline bool bstr_eatend0(struct bstr *s, const char *prefix)
return bstr_eatend(s, bstr0(prefix)); return bstr_eatend(s, bstr0(prefix));
} }
#ifdef _WIN32
int bstr_to_wchar(void *talloc_ctx, struct bstr s, wchar_t **ret);
#endif
// create a pair (not single value!) for "%.*s" printf syntax // create a pair (not single value!) for "%.*s" printf syntax
#define BSTR_P(bstr) (int)((bstr).len), ((bstr).start ? (char*)(bstr).start : "") #define BSTR_P(bstr) (int)((bstr).len), ((bstr).start ? (char*)(bstr).start : "")