6282fbf35167cc7015dafd7822c486934809a0b2
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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.
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 unsigned
39 utf8_width(const char *str)
40 {
41 assert(str != NULL);
43 #if defined(ENABLE_MULTIBYTE) && !defined(ENABLE_WIDE)
44 return g_utf8_strlen(str, -1);
45 #else
46 #ifdef ENABLE_WIDE
47 if (g_utf8_validate(str, -1, NULL)) {
48 size_t len = g_utf8_strlen(str, -1);
49 unsigned width = 0;
50 gunichar c;
52 while (len--) {
53 c = g_utf8_get_char(str);
54 width += g_unichar_iswide(c) ? 2 : 1;
55 str += g_unichar_to_utf8(c, NULL);
56 }
58 return width;
59 } else
60 #endif
61 return strlen(str);
62 #endif
63 }
65 unsigned
66 locale_width(const char *p)
67 {
68 #if defined(ENABLE_LOCALE) && defined(ENABLE_MULTIBYTE)
69 char *utf8;
70 unsigned width;
72 if (noconvert)
73 return utf8_width(p);
75 utf8 = locale_to_utf8(p);
76 width = utf8_width(utf8);
77 g_free(utf8);
79 return width;
80 #else
81 return strlen(str);
82 #endif
83 }
85 static inline unsigned
86 ascii_cut_width(char *p, unsigned max_width)
87 {
88 size_t length = strlen(p);
89 if (length <= (size_t)max_width)
90 return (unsigned)length;
92 p[max_width] = 0;
93 return max_width;
94 }
96 static inline unsigned
97 narrow_cut_width(char *p, unsigned max_width)
98 {
99 size_t length = g_utf8_strlen(p, -1);
100 if (length <= (size_t)max_width)
101 return (unsigned)length;
103 *g_utf8_offset_to_pointer(p, max_width) = 0;
104 return max_width;
105 }
107 static inline unsigned
108 wide_cut_width(char *p, unsigned max_width)
109 {
110 size_t length = g_utf8_strlen(p, -1);
111 unsigned width = 0, prev_width;
112 gunichar c;
114 while (length-- > 0) {
115 c = g_utf8_get_char(p);
116 prev_width = width;
117 width += g_unichar_iswide(c) ? 2 : 1;
118 if (width > max_width) {
119 /* too wide - cut the rest off */
120 *p = 0;
121 return prev_width;
122 }
124 p += g_unichar_to_utf8(c, NULL);
125 }
127 return width;
128 }
130 unsigned
131 utf8_cut_width(char *p, unsigned max_width)
132 {
133 assert(p != NULL);
135 #ifdef ENABLE_WIDE
136 if (!g_utf8_validate(p, -1, NULL))
137 return ascii_cut_width(p, max_width);
139 return wide_cut_width(p, max_width);
140 #elif defined(ENABLE_MULTIBYTE) && !defined(ENABLE_WIDE)
141 return narrow_cut_width(p, max_width);
142 #else
143 return ascii_cut_width(p, max_width);
144 #endif
145 }
147 char *
148 utf8_to_locale(const char *utf8str)
149 {
150 #ifdef ENABLE_LOCALE
151 gchar *str;
153 assert(utf8str != NULL);
155 if (noconvert)
156 return g_strdup(utf8str);
158 str = g_convert_with_fallback(utf8str, -1,
159 charset, "utf-8",
160 NULL, NULL, NULL, NULL);
161 if (str == NULL)
162 return g_strdup(utf8str);
164 return str;
165 #else
166 return g_strdup(utf8str);
167 #endif
168 }
170 char *
171 locale_to_utf8(const char *localestr)
172 {
173 #ifdef ENABLE_LOCALE
174 gchar *str;
176 assert(localestr != NULL);
178 if (noconvert)
179 return g_strdup(localestr);
181 str = g_convert_with_fallback(localestr, -1,
182 "utf-8", charset,
183 NULL, NULL, NULL, NULL);
184 if (str == NULL)
185 return g_strdup(localestr);
187 return str;
188 #else
189 return g_strdup(localestr);
190 #endif
191 }
193 char *
194 replace_utf8_to_locale(char *src)
195 {
196 #ifdef ENABLE_LOCALE
197 assert(src != NULL);
199 if (noconvert)
200 return src;
202 return utf8_to_locale(src);
203 #else
204 return src;
205 #endif
206 }
209 char *
210 replace_locale_to_utf8(char *src)
211 {
212 #ifdef ENABLE_LOCALE
213 assert(src != NULL);
215 if (noconvert)
216 return src;
218 return locale_to_utf8(src);
219 #else
220 return src;
221 #endif
222 }