summaryrefslogtreecommitdiff
path: root/common/strutils.c
diff options
context:
space:
mode:
authorTomas Bzatek <tbzatek@users.sourceforge.net>2008-11-15 22:14:21 +0100
committerTomas Bzatek <tbzatek@users.sourceforge.net>2008-11-15 22:14:21 +0100
commitf1ef4efb60e341a2a8ec72560071656b9d8b927d (patch)
tree71ea4544495fca2a82f78e31beb81b83453e7cef /common/strutils.c
parent1c50d6836601b8a6258dd6e299b3f299aa8d4a0f (diff)
downloadtuxcmd-modules-f1ef4efb60e341a2a8ec72560071656b9d8b927d.tar.xz
Encoding revision
Diffstat (limited to 'common/strutils.c')
-rw-r--r--common/strutils.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/common/strutils.c b/common/strutils.c
index 47494de..ffd95c3 100644
--- a/common/strutils.c
+++ b/common/strutils.c
@@ -193,3 +193,64 @@ char* resolve_relative(const char *source, const char *point_to)
return canon;
}
+
+
+
+// Originally stolen from wine-0.9.19, utf8.c
+// Copyright 2000 Alexandre Julliard
+char*
+wide_to_utf8 (const wchar_t *src)
+{
+#define CONV_BUFF_MAX 32768
+ int len;
+ char *buf, *dst, *ret;
+
+ buf = (char *) malloc (CONV_BUFF_MAX);
+ memset (&buf[0], 0, CONV_BUFF_MAX);
+ dst = buf;
+
+ if (src)
+ for (len = CONV_BUFF_MAX; *src; src++)
+ {
+ wchar_t ch = *src;
+
+ if (ch < 0x80) /* 0x00-0x7f: 1 byte */
+ {
+ if (!len--) {
+ log ("wide_to_utf8: error converting input string, overflow.\n");
+ break; /* overflow */
+ }
+ *dst++ = ch;
+ continue;
+ }
+
+ if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
+ {
+ if ((len -= 2) < 0) {
+ log ("wide_to_utf8: error converting input string, overflow.\n");
+ break; /* overflow */
+ }
+ dst[1] = 0x80 | (ch & 0x3f);
+ ch >>= 6;
+ dst[0] = 0xc0 | ch;
+ dst += 2;
+ continue;
+ }
+
+ /* 0x800-0xffff: 3 bytes */
+ if ((len -= 3) < 0) {
+ log ("wide_to_utf8: error converting input string, overflow.\n");
+ break; /* overflow */
+ }
+ dst[2] = 0x80 | (ch & 0x3f);
+ ch >>= 6;
+ dst[1] = 0x80 | (ch & 0x3f);
+ ch >>= 6;
+ dst[0] = 0xe0 | ch;
+ dst += 3;
+ }
+
+ ret = g_strdup (buf);
+ free (buf);
+ return ret;
+}