File: charset.c

package info (click to toggle)
ncmpc 0.25-0.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,616 kB
  • sloc: ansic: 12,229; sh: 4,157; makefile: 282; ruby: 28
file content (234 lines) | stat: -rw-r--r-- 4,433 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* ncmpc (Ncurses MPD Client)
 * (c) 2004-2010 The Music Player Daemon Project
 * Project homepage: http://musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "charset.h"

#include <assert.h>
#include <string.h>
#include <glib.h>

#ifdef ENABLE_LOCALE
static bool noconvert = true;
static const char *charset;

const char *
charset_init(void)
{
	noconvert = g_get_charset(&charset);
	return charset;
}
#endif

#ifdef HAVE_CURSES_ENHANCED
static inline unsigned
unicode_char_width(gunichar ch)
{
	if (g_unichar_iszerowidth(ch))
		return 0;

	if (g_unichar_iswide(ch))
		return 2;

	return 1;
}
#endif /* HAVE_CURSES_ENHANCED */

unsigned
utf8_width(const char *str)
{
	assert(str != NULL);

#if defined(ENABLE_MULTIBYTE) && !defined(HAVE_CURSES_ENHANCED)
	return g_utf8_strlen(str, -1);
#else
#ifdef HAVE_CURSES_ENHANCED
	if (g_utf8_validate(str, -1, NULL)) {
		size_t len = g_utf8_strlen(str, -1);
		unsigned width = 0;
		gunichar c;

		while (len--) {
			c = g_utf8_get_char(str);
			width += unicode_char_width(c);
			str += g_unichar_to_utf8(c, NULL);
		}

		return width;
	} else
#endif
		return strlen(str);
#endif
}

unsigned
locale_width(const char *p)
{
#if defined(ENABLE_LOCALE) && defined(ENABLE_MULTIBYTE)
	char *utf8;
	unsigned width;

	if (noconvert)
		return utf8_width(p);

	utf8 = locale_to_utf8(p);
	width = utf8_width(utf8);
	g_free(utf8);

	return width;
#else
	return strlen(p);
#endif
}

gcc_unused
static unsigned
ascii_cut_width(char *p, unsigned max_width)
{
	size_t length = strlen(p);
	if (length <= (size_t)max_width)
		return (unsigned)length;

	p[max_width] = 0;
	return max_width;
}

gcc_unused
static unsigned
narrow_cut_width(char *p, unsigned max_width)
{
	size_t length = g_utf8_strlen(p, -1);
	if (length <= (size_t)max_width)
		return (unsigned)length;

	*g_utf8_offset_to_pointer(p, max_width) = 0;
	return max_width;
}

gcc_unused
static unsigned
wide_cut_width(char *p, unsigned max_width)
{
	size_t length = g_utf8_strlen(p, -1);
	unsigned width = 0, prev_width;

	while (length-- > 0) {
		gunichar c = g_utf8_get_char(p);
		prev_width = width;
		width += g_unichar_iswide(c) ? 2 : 1;
		if (width > max_width) {
			/* too wide - cut the rest off */
			*p = 0;
			return prev_width;
		}

		p += g_unichar_to_utf8(c, NULL);
	}

	return width;
}

unsigned
utf8_cut_width(char *p, unsigned max_width)
{
	assert(p != NULL);

#ifdef HAVE_CURSES_ENHANCED
	if (!g_utf8_validate(p, -1, NULL))
		return ascii_cut_width(p, max_width);

	return wide_cut_width(p, max_width);
#elif defined(ENABLE_MULTIBYTE) && !defined(HAVE_CURSES_ENHANCED)
	return narrow_cut_width(p, max_width);
#else
	return ascii_cut_width(p, max_width);
#endif
}

char *
utf8_to_locale(const char *utf8str)
{
#ifdef ENABLE_LOCALE
	assert(utf8str != NULL);

	if (noconvert)
		return g_strdup(utf8str);

	gchar *str = g_convert_with_fallback(utf8str, -1,
					     charset, "utf-8",
					     NULL, NULL, NULL, NULL);
	if (str == NULL)
		return g_strdup(utf8str);

	return str;
#else
	return g_strdup(utf8str);
#endif
}

char *
locale_to_utf8(const char *localestr)
{
#ifdef ENABLE_LOCALE
	assert(localestr != NULL);

	if (noconvert)
		return g_strdup(localestr);

	gchar *str = g_convert_with_fallback(localestr, -1,
					     "utf-8", charset,
					     NULL, NULL, NULL, NULL);
	if (str == NULL)
		return g_strdup(localestr);

	return str;
#else
	return g_strdup(localestr);
#endif
}

char *
replace_utf8_to_locale(char *src)
{
#ifdef ENABLE_LOCALE
	assert(src != NULL);

	if (noconvert)
		return src;

	return utf8_to_locale(src);
#else
	return src;
#endif
}


char *
replace_locale_to_utf8(char *src)
{
#ifdef ENABLE_LOCALE
	assert(src != NULL);

	if (noconvert)
		return src;

	return locale_to_utf8(src);
#else
	return src;
#endif
}