Code

1f51538b7f0df38abc1b1e2bdacc14bdba3201da
[ncmpc.git] / src / charset.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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.
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 #ifdef ENABLE_WIDE
39 static inline unsigned
40 unicode_char_width(gunichar ch)
41 {
42 #if !GLIB_CHECK_VERSION(2,14,0)
43         if (g_unichar_iszerowidth(ch))
44                 return 0;
45 #endif
47         if (g_unichar_iswide(ch))
48                 return 2;
50         return 1;
51 }
52 #endif /* ENABLE_WIDE */
54 unsigned
55 utf8_width(const char *str)
56 {
57         assert(str != NULL);
59 #if defined(ENABLE_MULTIBYTE) && !defined(ENABLE_WIDE)
60         return g_utf8_strlen(str, -1);
61 #else
62 #ifdef ENABLE_WIDE
63         if (g_utf8_validate(str, -1, NULL)) {
64                 size_t len = g_utf8_strlen(str, -1);
65                 unsigned width = 0;
66                 gunichar c;
68                 while (len--) {
69                         c = g_utf8_get_char(str);
70                         width += unicode_char_width(c);
71                         str += g_unichar_to_utf8(c, NULL);
72                 }
74                 return width;
75         } else
76 #endif
77                 return strlen(str);
78 #endif
79 }
81 unsigned
82 locale_width(const char *p)
83 {
84 #if defined(ENABLE_LOCALE) && defined(ENABLE_MULTIBYTE)
85         char *utf8;
86         unsigned width;
88         if (noconvert)
89                 return utf8_width(p);
91         utf8 = locale_to_utf8(p);
92         width = utf8_width(utf8);
93         g_free(utf8);
95         return width;
96 #else
97         return strlen(p);
98 #endif
99 }
101 static inline unsigned
102 ascii_cut_width(char *p, unsigned max_width)
104         size_t length = strlen(p);
105         if (length <= (size_t)max_width)
106                 return (unsigned)length;
108         p[max_width] = 0;
109         return max_width;
112 static inline 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 static inline unsigned
124 wide_cut_width(char *p, unsigned max_width)
126         size_t length = g_utf8_strlen(p, -1);
127         unsigned width = 0, prev_width;
128         gunichar c;
130         while (length-- > 0) {
131                 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;
138                 }
140                 p += g_unichar_to_utf8(c, NULL);
141         }
143         return width;
146 unsigned
147 utf8_cut_width(char *p, unsigned max_width)
149         assert(p != NULL);
151 #ifdef ENABLE_WIDE
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(ENABLE_WIDE)
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         gchar *str;
169         assert(utf8str != NULL);
171         if (noconvert)
172                 return g_strdup(utf8str);
174         str = g_convert_with_fallback(utf8str, -1,
175                                       charset, "utf-8",
176                                       NULL, NULL, NULL, NULL);
177         if (str == NULL)
178                 return g_strdup(utf8str);
180         return str;
181 #else
182         return g_strdup(utf8str);
183 #endif
186 char *
187 locale_to_utf8(const char *localestr)
189 #ifdef ENABLE_LOCALE
190         gchar *str;
192         assert(localestr != NULL);
194         if (noconvert)
195                 return g_strdup(localestr);
197         str = g_convert_with_fallback(localestr, -1,
198                                       "utf-8", charset,
199                                       NULL, NULL, NULL, NULL);
200         if (str == NULL)
201                 return g_strdup(localestr);
203         return str;
204 #else
205         return g_strdup(localestr);
206 #endif
209 char *
210 replace_utf8_to_locale(char *src)
212 #ifdef ENABLE_LOCALE
213         assert(src != NULL);
215         if (noconvert)
216                 return src;
218         return utf8_to_locale(src);
219 #else
220         return src;
221 #endif
225 char *
226 replace_locale_to_utf8(char *src)
228 #ifdef ENABLE_LOCALE
229         assert(src != NULL);
231         if (noconvert)
232                 return src;
234         return locale_to_utf8(src);
235 #else
236         return src;
237 #endif