Mercurial > ~mikael > mcabber > hg
comparison mcabber/src/utf8.c @ 930:a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
This is a patch from Reimar Döffinger, slightly modified,
which makes mcabber work better on UTF-8 terminals.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sat, 08 Jul 2006 09:40:05 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
929:f4bf564893b5 | 930:a75f7a13df7b |
---|---|
1 /* | |
2 * utf8.c -- UTF-8 routines | |
3 * | |
4 * Copyright (C) 2006 Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or (at | |
9 * your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
19 * USA | |
20 */ | |
21 | |
22 #include "utf8.h" | |
23 | |
24 char *prev_char(char *str, const char *limit) | |
25 { | |
26 if (str <= limit) | |
27 return str; | |
28 str--; | |
29 if (utf8_mode) | |
30 while ((str > limit) && ((*str & 0xc0) == 0x80)) | |
31 str--; | |
32 return str; | |
33 } | |
34 | |
35 char *next_char(char *str) | |
36 { | |
37 if (!*str) | |
38 return str; | |
39 str++; | |
40 if (utf8_mode) | |
41 while ((*str & 0xc0) == 0x80) | |
42 str++; | |
43 return str; | |
44 } | |
45 | |
46 unsigned get_char(const char *str) | |
47 { | |
48 unsigned char *strp = (unsigned char *)str; | |
49 unsigned c = *strp++; | |
50 unsigned mask = 0x80; | |
51 int len = -1; | |
52 if (!utf8_mode) | |
53 return c; | |
54 while (c & mask) { | |
55 mask >>= 1; | |
56 len++; | |
57 } | |
58 if (len <= 0 || len > 4) | |
59 goto no_utf8; | |
60 c &= mask - 1; | |
61 while ((*strp & 0xc0) == 0x80) { | |
62 if (len-- <= 0) | |
63 goto no_utf8; | |
64 c = (c << 6) | (*strp++ & 0x3f); | |
65 } | |
66 if (len) | |
67 goto no_utf8; | |
68 return c; | |
69 | |
70 no_utf8: | |
71 return *str; | |
72 } | |
73 | |
74 char *put_char(char *str, unsigned c) | |
75 { | |
76 int mask = 0xffffffc0; | |
77 int i = 4; | |
78 char code[5]; | |
79 if (!utf8_mode || c < 128) { | |
80 *str++ = c; | |
81 return str; | |
82 } | |
83 while (c & mask) { | |
84 code[i--] = 0x80 | (c & 0x3f); | |
85 c >>= 6; | |
86 mask >>= 1; | |
87 if (i < 0) { | |
88 *str++ = '?'; | |
89 return str; | |
90 } | |
91 } | |
92 code[i] = (mask << 1) | c; | |
93 for (; i < 5; i++) | |
94 *str++ = code[i]; | |
95 return str; | |
96 } | |
97 | |
98 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |