*** empty log message ***
[dana/urxvt.git] / src / misc.C
index 440bc08..e93dba7 100644 (file)
 #include "rxvt.h"              /* NECESSARY */
 #include "misc.intpro"         /* PROTOS for internal routines */
 
-/* INTPROTO */
 char *
 rxvt_wcstombs (const wchar_t *str, int len)
 {
+  if (len < 0) len = wcslen (str);
+
   mbstate mbs;
   char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1);
 
@@ -49,7 +50,117 @@ rxvt_wcstombs (const wchar_t *str, int len)
   return r;
 }
 
-/* INTPROTO */
+wchar_t *
+rxvt_mbstowcs (const char *str, int len)
+{
+  if (len < 0) len = strlen (str);
+
+  wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t));
+
+  if (mbstowcs (r, str, len + 1) < 0)
+    *r = 0;
+
+  return r;
+}
+
+char *
+rxvt_wcstoutf8 (const wchar_t *str, int len)
+{
+  if (len < 0) len = wcslen (str);
+
+  char *r = (char *)rxvt_malloc (len * 4 + 1);
+  char *p = r;
+
+  while (len--)
+    {
+      unicode_t w = *str++;
+
+      if      (w < 0x000080)
+        *p++ = w;
+      else if (w < 0x000800)
+        *p++ = 0xc0 | ( w >>  6),
+        *p++ = 0x80 | ( w        & 0x3f);
+      else if (w < 0x010000)
+        *p++ = 0xe0 | ( w >> 12       ),
+        *p++ = 0x80 | ((w >>  6) & 0x3f),
+        *p++ = 0x80 | ( w        & 0x3f);
+      else if (w < 0x110000)
+        *p++ = 0xf0 | ( w >> 18),
+        *p++ = 0x80 | ((w >> 12) & 0x3f),
+        *p++ = 0x80 | ((w >>  6) & 0x3f),
+        *p++ = 0x80 | ( w        & 0x3f);
+      else
+        *p++ = '?';
+    }
+
+  *p = 0;
+
+  return r;
+}
+
+wchar_t *
+rxvt_utf8towcs (const char *str, int len)
+{
+  if (len < 0) len = strlen (str);
+
+  wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t));
+  wchar_t *p = r;
+
+  unsigned char *s = (unsigned char *)str;
+
+  while (len)
+    {
+      if (s[0] < 0x80)
+        {
+          *p++ = *s++; len--;
+        }
+      else if (len > 0 
+               && s[0] >= 0xc2 && s[0] <= 0xdf 
+               && (s[1] & 0xc0) == 0x80)
+        {
+          *p++ = ((s[0] & 0x1f) << 6)
+               |  (s[1] & 0x3f);
+          s += 2; len -= 2;
+        }
+      else if (len > 1 
+               && (   (s[0] == 0xe0                 && s[1] >= 0xa0 && s[1] <= 0xbf)
+                   || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf)
+                   || (s[0] == 0xed                 && s[1] >= 0x80 && s[1] <= 0x9f)
+                   || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf)
+                  )
+               && (s[2] & 0xc0) == 0x80)
+        {
+          *p++ = ((s[0] & 0x0f) << 12)
+               | ((s[1] & 0x3f) <<  6)
+               |  (s[2] & 0x3f);
+          s += 3; len -= 3;
+        }
+      else if (len > 2
+               && (   (s[0] == 0xf0                 && s[1] >= 0x90 && s[1] <= 0xbf)
+                   || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf)
+                   || (s[0] == 0xf4                 && s[1] >= 0x80 && s[1] <= 0x8f)
+                  )
+               && (s[2] & 0xc0) == 0x80
+               && (s[3] & 0xc0) == 0x80)
+        {
+          *p++ = ((s[0] & 0x07) << 18) 
+               | ((s[1] & 0x3f) << 12) 
+               | ((s[2] & 0x3f) <<  6) 
+               |  (s[3] & 0x3f);
+          s += 4; len -= 4;
+        }
+      else
+        {
+          *p++ = 0xfffd;
+          s++; len--;
+        }
+    }
+
+  *p = 0;
+
+  return r;
+}
+
 char *
 rxvt_strdup (const char *str)
 {