chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / utf8.c
index 9d04d19925e756f3dc409a2fff4a2cc0174675c9..aaa5ffc69cb13d6a79c1a2e0e9a8f14eef937d75 100644 (file)
@@ -10,6 +10,24 @@ static char rcsid[] = "$Id$";
 
 /*
  * $Log$
+ * Revision 1.9  2008/02/27 01:31:14  james
+ * *** empty log message ***
+ *
+ * Revision 1.8  2008/02/27 00:54:16  james
+ * *** empty log message ***
+ *
+ * Revision 1.7  2008/02/26 23:56:12  james
+ * *** empty log message ***
+ *
+ * Revision 1.6  2008/02/26 23:23:17  james
+ * *** empty log message ***
+ *
+ * Revision 1.5  2008/02/24 00:42:53  james
+ * *** empty log message ***
+ *
+ * Revision 1.4  2008/02/23 13:05:58  staffcvs
+ * *** empty log message ***
+ *
  * Revision 1.3  2008/02/23 11:48:37  james
  * *** empty log message ***
  *
@@ -33,21 +51,19 @@ utf8_flush (Context * c)
   switch (u->utf_ptr)
     {
     case 1:
-      log_f (c->l, "%s:%d invalid utf-8 sequence: \\%03o",
-             __FILE__, __LINE__, u->utf_buf[0]);
+      log_f (c->l, "<invalid utf-8 sequence: \\%03o>", u->utf_buf[0]);
       break;
     case 2:
-      log_f (c->l, "%s:%d invalid utf-8 sequence: \\%03o \\%03o",
-             __FILE__, __LINE__, u->utf_buf[0], u->utf_buf[1]);
+      log_f (c->l, "<invalid utf-8 sequence: \\%03o \\%03o>",
+             u->utf_buf[0], u->utf_buf[1]);
       break;
     case 3:
-      log_f (c->l, "%s:%d invalid utf-8 sequence: \\%03o \\%03o \\%03o",
-             __FILE__, __LINE__, u->utf_buf[0], u->utf_buf[1], u->utf_buf[2]);
+      log_f (c->l, "<invalid utf-8 sequence: \\%03o \\%03o \\%03o>",
+             u->utf_buf[0], u->utf_buf[1], u->utf_buf[2]);
       break;
     case 4:
       log_f (c->l,
-             "%s:%d invalid utf-8 sequence: \\%03o \\%03o \\%03o \\%03o",
-             __FILE__, __LINE__,
+             "<invalid utf-8 sequence: \\%03o \\%03o \\%03o \\%03o>",
              u->utf_buf[0], u->utf_buf[1], u->utf_buf[2], u->utf_buf[3]);
       break;
     }
@@ -60,7 +76,7 @@ utf8_flush (Context * c)
 }
 
 void
-utf8_parse (Context * c, int ch)
+utf8_parse (Context * c, uint32_t ch)
 {
   UTF8 *u = c->u;
 
@@ -73,7 +89,8 @@ utf8_parse (Context * c, int ch)
 
   if (!u->in_utf8)
     {
-      /*FIXME: for the moment we bodge utf8 support */
+      /*FIXME: for the moment we bodge utf8 support - need to do */
+      /* L->R and R->L and double width characters */
       if (ch == 0xb9)
         {                       /*CSI, not a valid utf8 start char */
           vt102_parse_char (c, ch);
@@ -122,7 +139,7 @@ utf8_parse (Context * c, int ch)
           u->in_utf8--;
 
           if (!u->in_utf8)
-            vt102_parse_char (c, ch);
+            vt102_parse_char (c, u->ch);
         }
     }
 }
@@ -139,3 +156,48 @@ utf8_new (void)
   ret->in_utf8 = 0;
 
 }
+
+int
+utf8_encode (char *ptr, int ch)
+{
+
+  if (ch < 0x80)
+    {
+      ptr[0] = ch;
+      return 1;
+    }
+  else if (ch < 0x800)
+    {
+      ptr[0] = 0xc0 | (ch >> 6);
+      ptr[1] = 0x80 | (ch & 0x3f);
+      return 2;
+    }
+  else if (ch < 0x10000)
+    {
+      ptr[0] = 0xe0 | (ch >> 12);
+      ptr[1] = 0x80 | ((ch >> 6) & 0x3f);
+      ptr[2] = 0x80 | (ch & 0x3f);
+      return 3;
+    }
+  else if (ch < 0x1fffff)
+    {
+      ptr[0] = 0xf0 | (ch >> 18);
+      ptr[1] = 0x80 | ((ch >> 12) & 0x3f);
+      ptr[2] = 0x80 | ((ch >> 6) & 0x3f);
+      ptr[3] = 0x80 | (ch & 0x3f);
+      return 4;
+    }
+  return 0;
+}
+
+void
+utf8_emit (TTY * t, int ch)
+{
+  uint8_t buf[4];
+  int i;
+  i = utf8_encode (buf, ch);
+  if (!i)
+    return;
+
+  t->xmit (t, buf, i);
+}