mirror of
https://github.com/mdbtools/mdbtools.git
synced 2026-03-10 00:20:54 +08:00
Add (fake) g_unichar_to_utf8
This commit is contained in:
@@ -45,6 +45,7 @@ typedef void * gpointer;
|
|||||||
typedef const void * gconstpointer;
|
typedef const void * gconstpointer;
|
||||||
typedef uint8_t guint8;
|
typedef uint8_t guint8;
|
||||||
typedef guint32 GQuark;
|
typedef guint32 GQuark;
|
||||||
|
typedef guint32 gunichar;
|
||||||
|
|
||||||
typedef guint (*GHashFunc)(gconstpointer);
|
typedef guint (*GHashFunc)(gconstpointer);
|
||||||
typedef int (*GCompareFunc)(gconstpointer, gconstpointer);
|
typedef int (*GCompareFunc)(gconstpointer, gconstpointer);
|
||||||
@@ -153,6 +154,7 @@ gchar *g_strdelimit(gchar *string, const gchar *delimiters, gchar new_delimiter)
|
|||||||
void g_printerr(const gchar *format, ...);
|
void g_printerr(const gchar *format, ...);
|
||||||
|
|
||||||
/* conversion */
|
/* conversion */
|
||||||
|
gint g_unichar_to_utf8(gunichar c, gchar *dst);
|
||||||
gchar *g_locale_to_utf8(const gchar *opsysstring, size_t len,
|
gchar *g_locale_to_utf8(const gchar *opsysstring, size_t len,
|
||||||
size_t *bytes_read, size_t *bytes_written, GError **error);
|
size_t *bytes_read, size_t *bytes_written, GError **error);
|
||||||
|
|
||||||
|
|||||||
@@ -223,6 +223,22 @@ gchar *g_string_free (GString *string, gboolean free_segment) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* conversion */
|
/* conversion */
|
||||||
|
gint g_unichar_to_utf8(gunichar u, gchar *dst) {
|
||||||
|
if (u >= 0x0800) {
|
||||||
|
*dst++ = 0xE0 | ((u & 0xF000) >> 12);
|
||||||
|
*dst++ = 0x80 | ((u & 0x0FC0) >> 6);
|
||||||
|
*dst++ = 0x80 | ((u & 0x003F) >> 0);
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
if (u >= 0x0080) {
|
||||||
|
*dst++ = 0xC0 | ((u & 0x07C0) >> 6);
|
||||||
|
*dst++ = 0x80 | ((u & 0x003F) >> 0);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
*dst++ = (u & 0x7F);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
gchar *g_locale_to_utf8(const gchar *opsysstring, size_t len,
|
gchar *g_locale_to_utf8(const gchar *opsysstring, size_t len,
|
||||||
size_t *bytes_read, size_t *bytes_written, GError **error) {
|
size_t *bytes_read, size_t *bytes_written, GError **error) {
|
||||||
if (len == (size_t)-1)
|
if (len == (size_t)-1)
|
||||||
@@ -235,18 +251,8 @@ gchar *g_locale_to_utf8(const gchar *opsysstring, size_t len,
|
|||||||
gchar *utf8 = malloc(3*len+1);
|
gchar *utf8 = malloc(3*len+1);
|
||||||
gchar *dst = utf8;
|
gchar *dst = utf8;
|
||||||
for (size_t i=0; i<len; i++) {
|
for (size_t i=0; i<len; i++) {
|
||||||
wchar_t u = utf16[i];
|
|
||||||
// u >= 0x10000 requires surrogate pairs, ignore
|
// u >= 0x10000 requires surrogate pairs, ignore
|
||||||
if (u >= 0x0800) {
|
dst += g_unichar_to_utf8(utf16[i], dst);
|
||||||
*dst++ = 0xE0 | ((u & 0xF000) >> 12);
|
|
||||||
*dst++ = 0x80 | ((u & 0x0FC0) >> 6);
|
|
||||||
*dst++ = 0x80 | ((u & 0x003F) >> 0);
|
|
||||||
} else if (u >= 0x0080) {
|
|
||||||
*dst++ = 0xC0 | ((u & 0x07C0) >> 6);
|
|
||||||
*dst++ = 0x80 | ((u & 0x003F) >> 0);
|
|
||||||
} else {
|
|
||||||
*dst++ = (u & 0x7F);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
*dst++ = '\0';
|
*dst++ = '\0';
|
||||||
free(utf16);
|
free(utf16);
|
||||||
|
|||||||
Reference in New Issue
Block a user