| /* 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 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; |
| } |