[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Lost] [Patch] UTF8-Unterstützung in vterm



+ vterm: UTF8-Unterstuetzung bei der Ausgabe
Index: src/modules/vterm/output.c
===================================================================
--- src/modules/vterm/output.c	(Revision 767)
+++ src/modules/vterm/output.c	(Arbeitskopie)
@@ -151,9 +151,16 @@
 void vterm_process_output(vterminal_t* vterm, char* data, size_t length)
 {
     vterm_output_t* out = &(vterm->output);
+    char cpdata[length + vterm->utf8_buffer_offset];
+    int len;
 
+    if ((len = utf8_to_cp437(vterm, data, length, cpdata)) == 0) {
+        return;
+    }
+
     // Ausgaben durch vt100-Emulation taetigen
-    vt100_process_output(vterm, data, length);
+    vt100_process_output(vterm, cpdata, len);
+    free(cpdata);
 
     // Anzeige aktualisieren falls es sich um aenderungen auf dem aktiven
     // Terminal handelt.
Index: src/modules/vterm/vterm.h
===================================================================
--- src/modules/vterm/vterm.h	(Revision 767)
+++ src/modules/vterm/vterm.h	(Arbeitskopie)
@@ -151,6 +151,12 @@
 
     /// Anzahl der Zeichen im vt100-Puffer
     size_t vt100_buffer_offset;
+
+    /// UTF-8 Puffer
+    char utf8_buffer[4];
+
+    /// Anzahl der Zeichen im utf-8 Puffer
+    size_t utf8_buffer_offset;
 } vterminal_t;
 
 
@@ -204,3 +210,6 @@
 /// Ausgabe in vt100-Emulation verarbeiten
 void vt100_process_output(vterminal_t* vterm, char* data, size_t length);
 
+/// UTF8-String in Codepage437-String verwandeln
+int utf8_to_cp437(vterminal_t* vterm, const char* str, size_t len, char* buf);
+
Index: src/modules/vterm/utf8.c
===================================================================
--- src/modules/vterm/utf8.c	(Revision 0)
+++ src/modules/vterm/utf8.c	(Revision 0)
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2007 The LOST Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the LOST Project
+ * by Antoine Kaufmann.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the LOST Project
+ *     and its contributors.
+ * 4. Neither the name of the LOST Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include "vterm.h"
+
+/**
+ * Zeichentabelle um Unicode-Zeichen in CP437 zu ersetzen
+ */
+struct {
+    wchar_t uc;
+    char    cp;
+} uc_to_cp [] = {
+    {L'ä', 0x84}, // ä
+    {L'ö', 0x94}, // ö
+    {L'ü', 0x81}, // ü
+    {L'Ä', 0x8E}, // Ä
+    {L'Ö', 0x99}, // Ö
+    {L'Ü', 0x9A}, // Ü
+    {L'ß', 0xE1}, // ß
+
+    {L'«', 0xAE}, // «
+    {L'»', 0xAF}, // »
+};
+
+/**
+ * Unicode-Zeichen in der Tabelle nachschlagen und in cp437 umwandeln
+ *
+ * @param wc Unicode-Zeichen
+ *
+ * @return cp437-Zeichen oder 0 wenn keines gefunden wurde
+ */
+static char to_cp437(wchar_t wc)
+{
+    int i;
+
+    // ASCII
+    if (wc <= 0x7F) {
+        return (char) wc;
+    }
+
+    // Tabelle durchsuchen
+    for (i = 0; i < sizeof(uc_to_cp) / sizeof(uc_to_cp[0]); i++) {
+        if (wc == uc_to_cp[i].uc) {
+            return uc_to_cp[i].cp;
+        }
+    }
+    return 0x00;
+}
+
+/**
+ * Wandelt das Zeichen in cp437 um oder legt es in den utf8 Buffer, falls es
+ * ein Teil eines Zeichens ist, das mehrere Bytes belegt
+ *
+ * @param c     Das Zeichen
+ * @param dest  Pointer auf die Speicherstelle in der das cp437-Zeichen
+ *              abgelegt wird
+ *
+ * @return TRUE wenn das Zeichen umgewandelt wurde, FALSE sonst
+ */
+static bool convert_char(vterminal_t* vterm, char c, char* dest)
+{
+    int len = vterm->utf8_buffer_offset + 1;
+    char buf[len];
+    wchar_t wc;
+
+    // Zeichen aus dem Unicode-Puffer kopieren
+    memcpy(buf, vterm->utf8_buffer, vterm->utf8_buffer_offset);
+    buf[len - 1] = c;
+
+    // Versuchen ein Zeichen zu konvertieren
+    if (mbtowc(&wc, buf, len) == -1) {
+        char* p = buf;
+
+        // Wenn das nicht klappt, wird das ganze in den Buffer kopiert
+        if (len > 4) {
+            // Erstes Zeichen abschneiden bei mehr als 4
+            len--;
+            p++;
+        }
+
+        memcpy(vterm->utf8_buffer, p, len);
+        vterm->utf8_buffer_offset = len;
+        return FALSE;
+    } else {
+        *dest = to_cp437(wc);
+        vterm->utf8_buffer_offset = 0;
+        return (*dest != 0);
+    }
+
+}
+
+int utf8_to_cp437(vterminal_t* vterm, const char* str, size_t len, char* buf)
+{
+    int l = 0;
+    int i;
+
+    for (i = 0; i < len; i++) {
+        if (convert_char(vterm, str[i], buf + l)) {
+            l++;
+        }
+    }
+    return l;
+}
+
+