2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
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 3 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
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file lib/charset.c @brief Character set conversion */
29 #include "configuration.h"
33 /** @brief Low-level converstion routine
34 * @param from Source encoding
35 * @param to Destination encoding
36 * @param ptr First byte to convert
37 * @param n Number of bytes to convert
38 * @return Converted text, 0-terminated; or NULL on error.
40 static void *convert(const char *from, const char *to,
41 const void *ptr, size_t n) {
44 char *buf = 0, *s, *d;
45 size_t bufsize = 0, sl, dl;
47 if((i = iconv_open(to, from)) == (iconv_t)-1)
48 disorder_fatal(errno, "error calling iconv_open");
50 bufsize = bufsize ? 2 * bufsize : 32;
51 buf = xrealloc_noptr(buf, bufsize);
57 /* (void *) to work around FreeBSD's nonstandard iconv prototype */
58 len = iconv(i, (void *)&s, &sl, &d, &dl);
59 } while(len == (size_t)-1 && errno == E2BIG);
61 if(len == (size_t)-1) {
62 disorder_error(errno, "error converting from %s to %s", from, to);
68 /** @brief Convert from the local multibyte encoding to UTF-8
69 * @param mb String in current locale's multibyte encoding
70 * @return Same string in UTF-8
72 char *mb2utf8(const char *mb) {
73 return convert(nl_langinfo(CODESET), "UTF-8", mb, strlen(mb) + 1);
76 /** @brief Convert from UTF-8 to the local multibyte encoding
77 * @param utf8 String in UTF-8
78 * @return Same string in current locale's multibyte encoding
80 char *utf82mb(const char *utf8) {
81 return convert("UTF-8", nl_langinfo(CODESET), utf8, strlen(utf8) + 1);
84 /** @brief Convert from encoding @p from to UTF-8
85 * @param from Source encoding
86 * @param any String in encoding @p from
87 * @return @p any converted to UTF-8
89 char *any2utf8(const char *from, const char *any) {
90 return convert(from, "UTF-8", any, strlen(any) + 1);
93 /** @brief Convert from encoding @p from to the local multibyte encoding
94 * @param from Source encoding
95 * @param any String in encoding @p from
96 * @return @p any converted to current locale's multibyte encoding
98 char *any2mb(const char *from, const char *any) {
99 if(from) return convert(from, nl_langinfo(CODESET), any, strlen(any) + 1);
100 else return xstrdup(any);
103 /** @brief Convert from encoding @p from to encoding @p to
104 * @param from Source encoding
105 * @param to Destination encoding
106 * @param any String in encoding @p from
107 * @return @p any converted to encoding @p to
109 char *any2any(const char *from,
112 if(from || to) return convert(from, to, any, strlen(any) + 1);
113 else return xstrdup(any);
116 /** @brief Truncate a string for display purposes
117 * @param s Pointer to UTF-8 string
118 * @param max Maximum number of columns
119 * @return @p or truncated string (never NULL)
121 * Returns a string that is no longer than @p max graphemes long and is either
122 * (canonically) equal to @p s or is a truncated form of it with an ellipsis
125 * We don't take display width into account (tricky for HTML!) and we don't
126 * attempt to implement the Bidi algorithm. If you have track names for which
127 * either of these matter in practice then get in touch.
129 const char *truncate_for_display(const char *s, long max) {
135 /* Convert to UTF-32 for processing */
136 if(!(s32 = utf8_to_utf32(s, strlen(s), &l32)))
138 it = utf32_iterator_new(s32, l32);
140 graphemes = 0; /* # of graphemes left of it */
141 while(graphemes <= max && utf32_iterator_where(it) < l32) {
142 if(graphemes == max - 1)
143 cut = utf32_iterator_where(it);
144 utf32_iterator_advance(it, 1);
145 if(utf32_iterator_grapheme_boundary(it))
148 if(graphemes > max) { /* we need to cut */
149 s32[cut] = 0x2026; /* HORIZONTAL ELLIPSIS */
151 s = utf32_to_utf8(s32, l32, 0);