From 26a3fc404adbfc887e1d9ed81a31956c4ce85d5f Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Mon, 6 Jan 2003 01:35:05 +0000 Subject: [PATCH] add LcharToUTF8 --- stepmania/src/RageUtil.cpp | 45 ++++++++++++++++++++++++++++++++++++++ stepmania/src/RageUtil.h | 1 + 2 files changed, 46 insertions(+) diff --git a/stepmania/src/RageUtil.cpp b/stepmania/src/RageUtil.cpp index e1eed3f004..1820d1ca44 100644 --- a/stepmania/src/RageUtil.cpp +++ b/stepmania/src/RageUtil.cpp @@ -917,3 +917,48 @@ lstring CStringToLstring(const CString &str) return ret; } + +int unichar_to_utf8 (longchar c, char *outbuf) +{ + unsigned int len = 0; + int first; + + if (c < 0x80) { + first = 0; + len = 1; + } else if (c < 0x800) { + first = 0xc0; + len = 2; + } else if (c < 0x10000) { + first = 0xe0; + len = 3; + } else if (c < 0x200000) { + first = 0xf0; + len = 4; + } else if (c < 0x4000000) { + first = 0xf8; + len = 5; + } else { + first = 0xfc; + len = 6; + } + + if (outbuf) + { + for (int i = len - 1; i > 0; --i) + { + outbuf[i] = char((c & 0x3f) | 0x80); + c >>= 6; + } + outbuf[0] = char(c | first); + } + + return len; +} + +CString LcharToUTF8( longchar c ) +{ + char buf[6]; + int cnt = unichar_to_utf8(c, buf); + return CString(buf, cnt); +} diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index 9fcf383965..6189a262fb 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -154,6 +154,7 @@ typedef int longchar; typedef basic_string lstring; lstring CStringToLstring(const CString &str); +CString LcharToUTF8( longchar c ); // Splits a CString into an CStringArray according the Deliminator. void split(