Added wide character to utf8 and vice versa conversion functions.

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Klaas Freitag 2012-10-12 18:18:21 +02:00 committed by Andreas Schneider
parent 201d0ec97c
commit 0e804ca685
2 changed files with 65 additions and 10 deletions

View file

@ -197,7 +197,7 @@ char *c_lowercase(const char* str) {
}
/* Convert a wide multibyte String to UTF8 */
const char* c_utf8(const _TCHAR *wstr)
const char* c_utf8(const mbchar_t *wstr)
{
const char *dst = NULL;
@ -221,16 +221,16 @@ const char* c_utf8(const _TCHAR *wstr)
}
/* Convert a an UTF8 string to multibyte */
const _TCHAR* c_multibyte(const char *str)
const mbchar_t* c_multibyte(const char *str)
{
#ifdef _WIN32
_TCHAR *wstrTo = NULL;
mbchar_t *wstrTo = NULL;
if(!str) return NULL;
size_t len = strlen( str );
int size_needed = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0);
if(size_needed > 0) {
int size_char = (size_needed+1)*sizeof(_TCHAR);
int size_char = (size_needed+1)*sizeof(mbchar_t);
wstrTo = c_malloc(size_char);
memset( (void*)wstrTo, 0, size_char);
MultiByteToWideChar(CP_UTF8, 0, str, -1, wstrTo, size_needed);
@ -250,7 +250,7 @@ void c_free_utf8(char* buf)
#endif
}
void c_free_multibyte(const _TCHAR* buf)
void c_free_multibyte(const mbchar_t* buf)
{
#ifdef _WIN32
SAFE_FREE(buf);

View file

@ -142,35 +142,90 @@ char *c_lowercase(const char* str);
/**
* @brief Convert a multibyte string to utf8 (Win32).
*
* This function is part of the multi platform abstraction of basic file
* operations to handle various platform encoding correctly.
*
* Instead of using the standard file operations the multi platform aliases
* defined in c_private.h have to be used instead.
*
* To convert path names returned by these functions to the internally used
* utf8 format this function has to be used. The returned string has to
* be freed by c_free_utf(). On some platforms this method allocates memory
* and on others not but it has never to be cared about as long as
* c_free_utf8() is used.
*
* @param str The multibyte encoded string to convert
*
* @return The malloced converted string or NULL on error.
*
* @see c_free_utf8()
* @see c_multibyte()
*
*/
const char* c_utf8(const _TCHAR *str);
const char* c_utf8(const mbchar_t *str);
/**
* @brief Convert a utf8 encoded string to multibyte (Win32).
*
* This function is part of the multi platform abstraction of basic file
* operations to handle various platform encoding correctly.
*
* Instead of using the standard file operations the multi platform aliases
* defined in c_private.h have to be used instead.
*
* To convert path names as input for the cross platform functions from the
* internally used utf8 format, this function has to be used.
* The returned string has to be freed by c_free_multibyte(). On some
* platforms this method allocates memory and on others not but it has never
* sto be cared about as long as c_free_multibyte() is used.
*
* @param str The utf8 string to convert.
*
* @return The malloced converted multibyte string or NULL on error.
*
* @see c_free_multibyte()
* @see c_utf8()
*
*/
const _TCHAR* c_multibyte(const char *wstr);
const mbchar_t* c_multibyte(const char *wstr);
/**
* @brief Free buffer malloced by c_utf8.
* @brief Free buffer malloced by c_utf8().
*
* This function is part of the multi platform abstraction of basic file
* operations to handle various platform encoding correctly.
*
* Instead of using the standard file operations the multi platform aliases
* defined in c_private.h have to be used instead.
*
* This function frees the memory that was allocated by a previous call to
* c_utf8().
*
* @param buf The buffer to free.
*
* @see c_utf8()
*
*/
void c_free_utf8(char* buf);
/**
* @brief Free buffer malloced by c_multibyte.
* @brief Free buffer malloced by c_multibyte().
*
* This function is part of the multi platform abstraction of basic file
* operations to handle various platform encoding correctly.
*
* Instead of using the standard file operations the multi platform aliases
* defined in c_private.h have to be used instead.
*
* This function frees the memory that was allocated by a previous call to
* c_multibyte().
*
* @param buf The buffer to free.
*
* @see c_multibyte()
*
*/
void c_free_multibyte(const _TCHAR* buf);
void c_free_multibyte(const mbchar_t* buf);
/**
* }@