#include <windows.h> | |
size_t wcstombs(char *mbstr, const wchar_t *wcstr, size_t count) { | |
if(!mbstr) return (size_t)(WideCharToMultiByte(CP_ACP, 0, wcstr, -1, NULL, 0, NULL, NULL) - 1); | |
if(!count) return 0; | |
const wchar_t *t = wcstr; | |
while(*t && t - wcstr < count) t++; | |
SetLastError(0); | |
int r = WideCharToMultiByte(CP_ACP, 0, wcstr, ((size_t)(t - wcstr) < count) ? -1 : count, mbstr, count, NULL, NULL); | |
if(r) return mbstr[r - 1] ? r : r - 1; | |
if(GetLastError()) return (size_t)-1; // Error | |
return 0; | |
} |