1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "charset.h"
22 #include <assert.h>
23 #include <string.h>
24 #include <glib.h>
26 #ifdef ENABLE_LOCALE
27 static bool noconvert = true;
28 static const char *charset;
30 const char *
31 charset_init(void)
32 {
33 noconvert = g_get_charset(&charset);
34 return charset;
35 }
36 #endif
38 #ifdef HAVE_CURSES_ENHANCED
39 static inline unsigned
40 unicode_char_width(gunichar ch)
41 {
42 if (g_unichar_iszerowidth(ch))
43 return 0;
45 if (g_unichar_iswide(ch))
46 return 2;
48 return 1;
49 }
50 #endif /* HAVE_CURSES_ENHANCED */
52 unsigned
53 utf8_width(const char *str)
54 {
55 assert(str != NULL);
57 #if defined(ENABLE_MULTIBYTE) && !defined(HAVE_CURSES_ENHANCED)
58 return g_utf8_strlen(str, -1);
59 #else
60 #ifdef HAVE_CURSES_ENHANCED
61 if (g_utf8_validate(str, -1, NULL)) {
62 size_t len = g_utf8_strlen(str, -1);
63 unsigned width = 0;
64 gunichar c;
66 while (len--) {
67 c = g_utf8_get_char(str);
68 width += unicode_char_width(c);
69 str += g_unichar_to_utf8(c, NULL);
70 }
72 return width;
73 } else
74 #endif
75 return strlen(str);
76 #endif
77 }
79 unsigned
80 locale_width(const char *p)
81 {
82 #if defined(ENABLE_LOCALE) && defined(ENABLE_MULTIBYTE)
83 char *utf8;
84 unsigned width;
86 if (noconvert)
87 return utf8_width(p);
89 utf8 = locale_to_utf8(p);
90 width = utf8_width(utf8);
91 g_free(utf8);
93 return width;
94 #else
95 return strlen(p);
96 #endif
97 }
99 static inline unsigned
100 ascii_cut_width(char *p, unsigned max_width)
101 {
102 size_t length = strlen(p);
103 if (length <= (size_t)max_width)
104 return (unsigned)length;
106 p[max_width] = 0;
107 return max_width;
108 }
110 static inline unsigned
111 narrow_cut_width(char *p, unsigned max_width)
112 {
113 size_t length = g_utf8_strlen(p, -1);
114 if (length <= (size_t)max_width)
115 return (unsigned)length;
117 *g_utf8_offset_to_pointer(p, max_width) = 0;
118 return max_width;
119 }
121 static inline unsigned
122 wide_cut_width(char *p, unsigned max_width)
123 {
124 size_t length = g_utf8_strlen(p, -1);
125 unsigned width = 0, prev_width;
126 gunichar c;
128 while (length-- > 0) {
129 c = g_utf8_get_char(p);
130 prev_width = width;
131 width += g_unichar_iswide(c) ? 2 : 1;
132 if (width > max_width) {
133 /* too wide - cut the rest off */
134 *p = 0;
135 return prev_width;
136 }
138 p += g_unichar_to_utf8(c, NULL);
139 }
141 return width;
142 }
144 unsigned
145 utf8_cut_width(char *p, unsigned max_width)
146 {
147 assert(p != NULL);
149 #ifdef HAVE_CURSES_ENHANCED
150 if (!g_utf8_validate(p, -1, NULL))
151 return ascii_cut_width(p, max_width);
153 return wide_cut_width(p, max_width);
154 #elif defined(ENABLE_MULTIBYTE) && !defined(HAVE_CURSES_ENHANCED)
155 return narrow_cut_width(p, max_width);
156 #else
157 return ascii_cut_width(p, max_width);
158 #endif
159 }
161 char *
162 utf8_to_locale(const char *utf8str)
163 {
164 #ifdef ENABLE_LOCALE
165 gchar *str;
167 assert(utf8str != NULL);
169 if (noconvert)
170 return g_strdup(utf8str);
172 str = g_convert_with_fallback(utf8str, -1,
173 charset, "utf-8",
174 NULL, NULL, NULL, NULL);
175 if (str == NULL)
176 return g_strdup(utf8str);
178 return str;
179 #else
180 return g_strdup(utf8str);
181 #endif
182 }
184 char *
185 locale_to_utf8(const char *localestr)
186 {
187 #ifdef ENABLE_LOCALE
188 gchar *str;
190 assert(localestr != NULL);
192 if (noconvert)
193 return g_strdup(localestr);
195 str = g_convert_with_fallback(localestr, -1,
196 "utf-8", charset,
197 NULL, NULL, NULL, NULL);
198 if (str == NULL)
199 return g_strdup(localestr);
201 return str;
202 #else
203 return g_strdup(localestr);
204 #endif
205 }
207 char *
208 replace_utf8_to_locale(char *src)
209 {
210 #ifdef ENABLE_LOCALE
211 assert(src != NULL);
213 if (noconvert)
214 return src;
216 return utf8_to_locale(src);
217 #else
218 return src;
219 #endif
220 }
223 char *
224 replace_locale_to_utf8(char *src)
225 {
226 #ifdef ENABLE_LOCALE
227 assert(src != NULL);
229 if (noconvert)
230 return src;
232 return locale_to_utf8(src);
233 #else
234 return src;
235 #endif
236 }