UNIXworkcode

1 #include "pch.h" 2 3 #include "util.h" 4 5 #include <stdlib.h> 6 7 8 wchar_t* str2wstr(const char* str, int* newlen) { 9 size_t len = strlen(str); 10 11 return str2wstr_len(str, len, newlen); 12 } 13 14 wchar_t* str2wstr_len(const char* str, size_t len, int* newlen) { 15 wchar_t* wstr = (wchar_t*)calloc(len + 1, sizeof(wchar_t)); 16 int wlen = MultiByteToWideChar( 17 CP_UTF8, 18 0, 19 str, 20 len, 21 wstr, 22 len + 1 23 ); 24 if (newlen) { 25 *newlen = wlen; 26 } 27 wstr[wlen] = 0; 28 29 return wstr; 30 } 31 32 char* wchar2utf8(const wchar_t* wstr, size_t wlen) { 33 size_t maxlen = wlen * 4; 34 char* ret = (char*)malloc(maxlen + 1); 35 int ret_len = WideCharToMultiByte( 36 CP_UTF8, 37 0, 38 wstr, 39 wlen, 40 ret, 41 maxlen, 42 NULL, 43 NULL); 44 ret[ret_len] = 0; 45 return ret; 46 } 47