wchar_t * locale_to_wchar(char * str)
{
wchar_t * ptr;
size_t s;
/* first arg == NULL means 'calculate needed space' */
s=mbstowcs(NULL, str, 0);
/* a size of -1 is triggered by an error in encoding; never
happen in ISO-8859-* locales, but possible in UTF-8 */
if(s == -1)
return(NULL);
/* malloc the necessary space */
if((ptr=(wchar_t *)malloc((s + 1) * sizeof(wchar_t))) == NULL)
return(NULL);
/* really do it */
mbstowcs(ptr, str, s);
/* ensure NULL-termination */
ptr[s]=L'\0';
/* remember to free() ptr when done */
return(ptr);
}
char * wchar_to_locale(wchar_t const * str)
{
char * ptr;
size_t s;
/* first arg == NULL means 'calculate needed space' */
s=wcstombs(NULL, str, 0);
/* a size of -1 means there are characters that could not
be converted to current locale */
if(s == -1)
return(NULL);
/* malloc the necessary space */
if((ptr=(char *)malloc(s + 1)) == NULL)
return(NULL);
/* really do it */
wcstombs(ptr, str, s);
/* ensure NULL-termination */
ptr[s]='\0';
/* remember to free() ptr when done */
return(ptr);
}
|