mirror of
https://github.com/nextcloud/desktop.git
synced 2024-10-28 07:54:49 +03:00
Added wide character to utf8 and vice versa conversion functions.
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
parent
8b7cab119b
commit
bd4c9b3a7f
2 changed files with 107 additions and 0 deletions
|
@ -25,10 +25,20 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <limits.h>
|
||||
#include <sys/types.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "c_string.h"
|
||||
#include "c_alloc.h"
|
||||
#include "c_macro.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
int c_streq(const char *a, const char *b) {
|
||||
register const char *s1 = a;
|
||||
register const char *s2 = b;
|
||||
|
@ -186,3 +196,65 @@ char *c_lowercase(const char* str) {
|
|||
return new;
|
||||
}
|
||||
|
||||
/* Convert a wide multibyte String to UTF8 */
|
||||
const char* c_utf8(const _TCHAR *wstr)
|
||||
{
|
||||
const char *dst = NULL;
|
||||
|
||||
#ifdef _WIN32
|
||||
char *mdst = NULL;
|
||||
|
||||
if(!wstr) return NULL;
|
||||
size_t len = _tcslen( wstr );
|
||||
/* Call once to get the required size. */
|
||||
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr, len, NULL, 0, NULL, NULL);
|
||||
if( size_needed > 0 ) {
|
||||
mdst = c_malloc(1+size_needed);
|
||||
memset( mdst, 0, 1+size_needed);
|
||||
WideCharToMultiByte(CP_UTF8, 0, wstr, len, mdst, size_needed, NULL, NULL);
|
||||
dst = mdst;
|
||||
}
|
||||
#else
|
||||
dst = wstr;
|
||||
#endif
|
||||
return dst;
|
||||
}
|
||||
|
||||
/* Convert a an UTF8 string to multibyte */
|
||||
const _TCHAR* c_multibyte(const char *str)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
_TCHAR *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);
|
||||
wstrTo = c_malloc(size_char);
|
||||
memset( (void*)wstrTo, 0, size_char);
|
||||
MultiByteToWideChar(CP_UTF8, 0, str, -1, wstrTo, size_needed);
|
||||
}
|
||||
return wstrTo;
|
||||
#else
|
||||
return str;
|
||||
#endif
|
||||
}
|
||||
|
||||
void c_free_utf8(char* buf)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SAFE_FREE(buf);
|
||||
#else
|
||||
(void)buf;
|
||||
#endif
|
||||
}
|
||||
|
||||
void c_free_multibyte(const _TCHAR* buf)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SAFE_FREE(buf);
|
||||
#else
|
||||
(void)buf;
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -33,6 +33,8 @@
|
|||
#ifndef _C_STR_H
|
||||
#define _C_STR_H
|
||||
|
||||
#include "c_private.h"
|
||||
|
||||
struct c_strlist_s; typedef struct c_strlist_s c_strlist_t;
|
||||
|
||||
/**
|
||||
|
@ -137,6 +139,39 @@ char *c_uppercase(const char* str);
|
|||
*/
|
||||
char *c_lowercase(const char* str);
|
||||
|
||||
/**
|
||||
* @brief Convert a multibyte string to utf8 (Win32).
|
||||
*
|
||||
* @param str The multibyte encoded string to convert
|
||||
*
|
||||
* @return The malloced converted string or NULL on error.
|
||||
*/
|
||||
const char* c_utf8(const _TCHAR *str);
|
||||
|
||||
/**
|
||||
* @brief Convert a utf8 encoded string to multibyte (Win32).
|
||||
*
|
||||
* @param str The utf8 string to convert.
|
||||
*
|
||||
* @return The malloced converted multibyte string or NULL on error.
|
||||
*/
|
||||
const _TCHAR* c_multibyte(const char *wstr);
|
||||
|
||||
/**
|
||||
* @brief Free buffer malloced by c_utf8.
|
||||
*
|
||||
* @param buf The buffer to free.
|
||||
*
|
||||
*/
|
||||
void c_free_utf8(char* buf);
|
||||
|
||||
/**
|
||||
* @brief Free buffer malloced by c_multibyte.
|
||||
*
|
||||
* @param buf The buffer to free.
|
||||
*/
|
||||
void c_free_multibyte(const _TCHAR* buf);
|
||||
|
||||
/**
|
||||
* }@
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue