lyrics: lyrics area regex fix for LyricWikia
[ncmpc.git] / src / charset.c
blob1c1b0465755c645e19416c1423cb5f9ef88e75b7
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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.
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)
33 noconvert = g_get_charset(&charset);
34 return charset;
36 #endif
38 #ifdef HAVE_CURSES_ENHANCED
39 static inline unsigned
40 unicode_char_width(gunichar ch)
42 if (g_unichar_iszerowidth(ch))
43 return 0;
45 if (g_unichar_iswide(ch))
46 return 2;
48 return 1;
50 #endif /* HAVE_CURSES_ENHANCED */
52 unsigned
53 utf8_width(const char *str)
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);
72 return width;
73 } else
74 #endif
75 return strlen(str);
76 #endif
79 unsigned
80 locale_width(const char *p)
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
99 gcc_unused
100 static unsigned
101 ascii_cut_width(char *p, unsigned max_width)
103 size_t length = strlen(p);
104 if (length <= (size_t)max_width)
105 return (unsigned)length;
107 p[max_width] = 0;
108 return max_width;
111 gcc_unused
112 static unsigned
113 narrow_cut_width(char *p, unsigned max_width)
115 size_t length = g_utf8_strlen(p, -1);
116 if (length <= (size_t)max_width)
117 return (unsigned)length;
119 *g_utf8_offset_to_pointer(p, max_width) = 0;
120 return max_width;
123 gcc_unused
124 static unsigned
125 wide_cut_width(char *p, unsigned max_width)
127 size_t length = g_utf8_strlen(p, -1);
128 unsigned width = 0, prev_width;
130 while (length-- > 0) {
131 gunichar c = g_utf8_get_char(p);
132 prev_width = width;
133 width += g_unichar_iswide(c) ? 2 : 1;
134 if (width > max_width) {
135 /* too wide - cut the rest off */
136 *p = 0;
137 return prev_width;
140 p += g_unichar_to_utf8(c, NULL);
143 return width;
146 unsigned
147 utf8_cut_width(char *p, unsigned max_width)
149 assert(p != NULL);
151 #ifdef HAVE_CURSES_ENHANCED
152 if (!g_utf8_validate(p, -1, NULL))
153 return ascii_cut_width(p, max_width);
155 return wide_cut_width(p, max_width);
156 #elif defined(ENABLE_MULTIBYTE) && !defined(HAVE_CURSES_ENHANCED)
157 return narrow_cut_width(p, max_width);
158 #else
159 return ascii_cut_width(p, max_width);
160 #endif
163 char *
164 utf8_to_locale(const char *utf8str)
166 #ifdef ENABLE_LOCALE
167 assert(utf8str != NULL);
169 if (noconvert)
170 return g_strdup(utf8str);
172 gchar *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
184 char *
185 locale_to_utf8(const char *localestr)
187 #ifdef ENABLE_LOCALE
188 assert(localestr != NULL);
190 if (noconvert)
191 return g_strdup(localestr);
193 gchar *str = g_convert_with_fallback(localestr, -1,
194 "utf-8", charset,
195 NULL, NULL, NULL, NULL);
196 if (str == NULL)
197 return g_strdup(localestr);
199 return str;
200 #else
201 return g_strdup(localestr);
202 #endif
205 char *
206 replace_utf8_to_locale(char *src)
208 #ifdef ENABLE_LOCALE
209 assert(src != NULL);
211 if (noconvert)
212 return src;
214 return utf8_to_locale(src);
215 #else
216 return src;
217 #endif
221 char *
222 replace_locale_to_utf8(char *src)
224 #ifdef ENABLE_LOCALE
225 assert(src != NULL);
227 if (noconvert)
228 return src;
230 return locale_to_utf8(src);
231 #else
232 return src;
233 #endif