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.
27 static bool noconvert
= true;
28 static const char *charset
;
33 noconvert
= g_get_charset(&charset
);
38 #ifdef HAVE_CURSES_ENHANCED
39 static inline unsigned
40 unicode_char_width(gunichar ch
)
42 if (g_unichar_iszerowidth(ch
))
45 if (g_unichar_iswide(ch
))
50 #endif /* HAVE_CURSES_ENHANCED */
53 utf8_width(const char *str
)
57 #if defined(ENABLE_MULTIBYTE) && !defined(HAVE_CURSES_ENHANCED)
58 return g_utf8_strlen(str
, -1);
60 #ifdef HAVE_CURSES_ENHANCED
61 if (g_utf8_validate(str
, -1, NULL
)) {
62 size_t len
= g_utf8_strlen(str
, -1);
67 c
= g_utf8_get_char(str
);
68 width
+= unicode_char_width(c
);
69 str
+= g_unichar_to_utf8(c
, NULL
);
80 locale_width(const char *p
)
82 #if defined(ENABLE_LOCALE) && defined(ENABLE_MULTIBYTE)
89 utf8
= locale_to_utf8(p
);
90 width
= utf8_width(utf8
);
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
;
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;
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
);
133 width
+= g_unichar_iswide(c
) ? 2 : 1;
134 if (width
> max_width
) {
135 /* too wide - cut the rest off */
140 p
+= g_unichar_to_utf8(c
, NULL
);
147 utf8_cut_width(char *p
, unsigned max_width
)
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
);
159 return ascii_cut_width(p
, max_width
);
164 utf8_to_locale(const char *utf8str
)
167 assert(utf8str
!= NULL
);
170 return g_strdup(utf8str
);
172 gchar
*str
= g_convert_with_fallback(utf8str
, -1,
174 NULL
, NULL
, NULL
, NULL
);
176 return g_strdup(utf8str
);
180 return g_strdup(utf8str
);
185 locale_to_utf8(const char *localestr
)
188 assert(localestr
!= NULL
);
191 return g_strdup(localestr
);
193 gchar
*str
= g_convert_with_fallback(localestr
, -1,
195 NULL
, NULL
, NULL
, NULL
);
197 return g_strdup(localestr
);
201 return g_strdup(localestr
);
206 replace_utf8_to_locale(char *src
)
214 return utf8_to_locale(src
);
222 replace_locale_to_utf8(char *src
)
230 return locale_to_utf8(src
);