In an almost complete effort to port libc++ to Windows, I need the function mbsnrtowcs
, and thought that the easiest way would be to implement it in terms of mbsrtowcs
:
size_t mbsnrtowcs( wchar_t *__restrict__ dst, const char **__restrict__ src,
size_t nmc, size_t len, mbstate_t *__restrict__ ps )
{
char* nmcsrc = new char[nmc+1];
strncpy( nmcsrc, *src, nmc );
nmcsrc[nmc] = '\0';
const size_t result = mbsrtowcs( dst, &nmcsrc, len, ps );
delete[] nmcsrc;
return result;
}
The problem here is that mbsrtowcs
needs &nmcsrc
to be of type const char**
, which is isn't, because it is a string I just copied the first nmc
elements into, and appended a \0
character to. How can I work around this? Strictly speaking, this is compiled as C++, so perhaps a const_cast
would do little harm here? I also have access to c++0X (libc++ is/requires a subset c++0x).
Edit: the Clang error message reads:
M:\Development\Source\libc++\src\support\win32\support.cpp:37:27: error: no matching function for call to 'mbsrtowcs'
const size_t result = mbsrtowcs( dst, &nmcsrc, len, ps );
^~~~~~~~~
M:/Development/mingw64/bin/../lib/clang/3.0/../../../x86_64-w64-mingw32/include\wchar.h:732:18: note: candidate function not viable: no known conversion from 'char **' to 'const char **restrict' for 2nd argument;
size_t __cdecl mbsrtowcs(wchar_t * __restrict__ _Dest,const char ** __restrict__ _PSrc,size_t _Count,mbstate_t * __restrict__ _State) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
^
EDIT2: To fix what was wrong Re. Matthieu, I have changed my naive implementation like so:
size_t mbsnrtowcs( wchar_t *__restrict__ dst, const char **__restrict__ src,
size_t nmc, size_t len, mbstate_t *__restrict__ ps )
{
char* local_src = new char[nmc+1];
char* nmcsrc = local_src;
strncpy( nmcsrc, *src, nmc );
nmcsrc[nmc] = '\0';
const size_t result = mbsrtowcs( dst, const_cast<const char **>(&nmcsrc), len, ps );
// propagate error
if( nmcsrc == NULL )
*src = NULL;
delete[] local_src;
return result;
}
And I have added a FIXME saying that the proper way to do this would be implementing it via mbrtowc.