blob: cffdeceb94ffdbac998446da98c34f00cba4c2e8 [file] [log] [blame] [raw]
/* A part of the Windows CE C Extra Library (celibc)
Copyright 2007-2015 PC GO Ld.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include <windows.h>
size_t mbstowcs(wchar_t *wcstr, const char *mbstr, size_t count) {
if(!wcstr) return (size_t)(MultiByteToWideChar(CP_ACP, 0, mbstr, -1, NULL, 0) - 1);
if(!count) return 0;
const char *t = mbstr;
while(*t && t - mbstr < count) t++;
SetLastError(0);
int r = MultiByteToWideChar(CP_ACP, 0, mbstr, ((size_t)(t - mbstr) < count) ? -1 : count, wcstr, count);
if(r) return wcstr[r - 1] ? r : r - 1;
if(GetLastError()) return (size_t)-1; // Error
return 0;
}