From cd254cd799ab497fbf036715c8367c0782870cad Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sat, 4 Jan 2014 02:45:01 +0100 Subject: [PATCH] Remove kernel/utf8.{cpp,h}. --- kernel/Makefile | 1 - kernel/logterminal.cpp | 21 +++++++----- kernel/utf8.cpp | 75 ------------------------------------------ kernel/utf8.h | 36 -------------------- 4 files changed, 13 insertions(+), 120 deletions(-) delete mode 100644 kernel/utf8.cpp delete mode 100644 kernel/utf8.h diff --git a/kernel/Makefile b/kernel/Makefile index a4b305a8..67220ab0 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -138,7 +138,6 @@ time.o \ timer.o \ uart.o \ user-timer.o \ -utf8.o \ vga.o \ vgatextbuffer.o \ video.o \ diff --git a/kernel/logterminal.cpp b/kernel/logterminal.cpp index 5b76df39..30ca400b 100644 --- a/kernel/logterminal.cpp +++ b/kernel/logterminal.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -48,7 +49,6 @@ #include #include -#include "utf8.h" #include "logterminal.h" namespace Sortix { @@ -299,9 +299,11 @@ void LogTerminal::QueueUnicode(uint32_t unicode) // if it is unprintable (it's an encoded keystroke)? if ( !KBKEY_DECODE(unicode) && echomode ) { - char utf8buf[6]; - unsigned numbytes = UTF8::Encode(unicode, utf8buf); - Log::PrintData(utf8buf, numbytes); + mbstate_t ps; + memset(&ps, 0, sizeof(ps)); + char utf8buf[MB_CUR_MAX]; + size_t num_bytes = wcrtomb(utf8buf, (wchar_t) unicode, &ps); + Log::PrintData(utf8buf, num_bytes); } bool commit = !linemode || wasenter; @@ -356,18 +358,21 @@ ssize_t LogTerminal::read(ioctx_t* ctx, uint8_t* userbuf, size_t count) uint8_t* buf; size_t bufsize; uint8_t codepointbuf[4]; - char utf8buf[6]; + char utf8buf[MB_CUR_MAX]; if ( termmode & TERMMODE_UTF8 ) { - unsigned numbytes = UTF8::Encode(codepoint, utf8buf); - if ( !numbytes ) + mbstate_t ps; + memset(&ps, 0, sizeof(ps)); + size_t num_bytes = wcrtomb(utf8buf, (wchar_t) codepoint, &ps); + if ( num_bytes == (size_t) -1) { Log::PrintF("Warning: logterminal driver dropping invalid " "codepoint 0x%x\n", codepoint); + num_bytes = 0; } buf = (uint8_t*) utf8buf; - bufsize = numbytes; + bufsize = num_bytes; } else { diff --git a/kernel/utf8.cpp b/kernel/utf8.cpp deleted file mode 100644 index 2d16b96c..00000000 --- a/kernel/utf8.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/******************************************************************************* - - Copyright(C) Jonas 'Sortie' Termansen 2012. - - This file is part of Sortix. - - Sortix is free software: you can redistribute it and/or modify it under the - terms of the GNU General Public License as published by the Free Software - Foundation, either version 3 of the License, or (at your option) any later - version. - - Sortix is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - details. - - You should have received a copy of the GNU General Public License along with - Sortix. If not, see . - - utf8.cpp - Encodes UTF-32 strings in UTF-8. - -*******************************************************************************/ - -#include - -#include - -#include "utf8.h" - -namespace Sortix -{ - namespace UTF8 - { - unsigned Encode(uint32_t unicode, char* dest) - { - uint8_t* buf = (uint8_t*) dest; - unsigned bytes = 1; - unsigned bits = 7; - if ( (1U<<7U) <= unicode ) { bytes = 2; bits = 11; } - if ( (1U<<11U) <= unicode ) { bytes = 3; bits = 16; } - if ( (1U<<16U) <= unicode ) { bytes = 4; bits = 21; } - if ( (1U<<21U) <= unicode ) { bytes = 5; bits = 26; } - if ( (1U<<26U) <= unicode ) { bytes = 6; bits = 31; } - if ( (1U<<31U) <= unicode ) { errno = EINVAL; return 0; } - - uint8_t prefix; - unsigned prefixavai; - switch ( bytes ) - { - case 1: prefixavai = 7; prefix = 0b0U << prefixavai; break; - case 2: prefixavai = 5; prefix = 0b110U << prefixavai; break; - case 3: prefixavai = 4; prefix = 0b1110U << prefixavai; break; - case 4: prefixavai = 3; prefix = 0b11110U << prefixavai; break; - case 5: prefixavai = 2; prefix = 0b111110U << prefixavai; break; - case 6: prefixavai = 1; prefix = 0b1111110U << prefixavai; break; - } - - // Put the first bits in the unused area of the prefix. - prefix |= unicode >> (bits - prefixavai); - *buf++ = prefix; - unsigned bitsleft = bits - prefixavai; - - while ( bitsleft ) - { - bitsleft -= 6; - uint8_t elembits = (unicode>>bitsleft) & ((1U<<6U)-1U); - uint8_t elem = (0b10U<<6U) | elembits; - *buf++ = elem; - } - - return bytes; - } - } -} diff --git a/kernel/utf8.h b/kernel/utf8.h deleted file mode 100644 index 419dd4b6..00000000 --- a/kernel/utf8.h +++ /dev/null @@ -1,36 +0,0 @@ -/******************************************************************************* - - Copyright(C) Jonas 'Sortie' Termansen 2012. - - This file is part of Sortix. - - Sortix is free software: you can redistribute it and/or modify it under the - terms of the GNU General Public License as published by the Free Software - Foundation, either version 3 of the License, or (at your option) any later - version. - - Sortix is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - details. - - You should have received a copy of the GNU General Public License along with - Sortix. If not, see . - - utf8.h - Encodes UTF-32 strings in UTF-8. - -*******************************************************************************/ - -#ifndef SORTIX_UTF8_H -#define SORTIX_UTF8_H - -namespace Sortix -{ - namespace UTF8 - { - unsigned Encode(uint32_t unicode, char* dest); - } -} - -#endif