chiark / gitweb /
feb4469e5950d0c7de9837ee3fa49d80e22ecff2
[disorder] / lib / charset.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2005, 2007, 2008, 2013 Richard Kettlewell
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 3 of the License, or
8  * (at your option) any later version.
9  * 
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.
14  * 
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/>.
17  */
18 /** @file lib/charset.c @brief Character set conversion */
19
20 #include "common.h"
21
22 #if HAVE_ICONV_H
23 # include <iconv.h>
24 #endif
25 #include <errno.h>
26 #if HAVE_LANGINFO_H
27 # include <langinfo.h>
28 #endif
29
30 #include "mem.h"
31 #include "log.h"
32 #include "charset.h"
33 #include "configuration.h"
34 #include "vector.h"
35 #include "unicode.h"
36
37 /** @brief Low-level converstion routine
38  * @param from Source encoding
39  * @param to Destination encoding
40  * @param ptr First byte to convert
41  * @param n Number of bytes to convert
42  * @return Converted text, 0-terminated; or NULL on error.
43  */
44 static void *convert(const char *from, const char *to,
45                      const void *ptr, size_t n) {
46   iconv_t i;
47   size_t len;
48   char *buf = 0, *s, *d;
49   size_t bufsize = 0, sl, dl;
50
51   if((i = iconv_open(to, from)) == (iconv_t)-1)
52     disorder_fatal(errno, "error calling iconv_open");
53   do {
54     bufsize = bufsize ? 2 * bufsize : 32;
55     buf = xrealloc_noptr(buf, bufsize);
56     iconv(i, 0, 0, 0, 0);
57     s = (char *)ptr;
58     sl = n;
59     d = buf;
60     dl = bufsize;
61     /* (void *) to work around FreeBSD's nonstandard iconv prototype */
62     len = iconv(i, (void *)&s, &sl, &d, &dl);
63   } while(len == (size_t)-1 && errno == E2BIG);
64   iconv_close(i);
65   if(len == (size_t)-1) {
66     disorder_error(errno, "error converting from %s to %s", from, to);
67     return 0;
68   }
69   return buf;
70 }
71
72 /** @brief Convert from the local multibyte encoding to UTF-8
73  * @param mb String in current locale's multibyte encoding
74  * @return Same string in UTF-8
75  */
76 char *mb2utf8(const char *mb) {
77   return convert(nl_langinfo(CODESET), "UTF-8", mb, strlen(mb) + 1);
78 }
79
80 /** @brief Convert from UTF-8 to the local multibyte encoding
81  * @param utf8 String in UTF-8
82  * @return Same string in current locale's multibyte encoding
83  */
84 char *utf82mb(const char *utf8) {
85   return convert("UTF-8", nl_langinfo(CODESET), utf8, strlen(utf8) + 1);
86 }
87
88 /** @brief Convert from encoding @p from to UTF-8
89  * @param from Source encoding
90  * @param any String in encoding @p from
91  * @return @p any converted to UTF-8
92  */
93 char *any2utf8(const char *from, const char *any) {
94   return convert(from, "UTF-8", any, strlen(any) + 1);
95 }
96
97 /** @brief Convert from encoding @p from to the local multibyte encoding
98  * @param from Source encoding
99  * @param any String in encoding @p from
100  * @return @p any converted to current locale's multibyte encoding
101  */
102 char *any2mb(const char *from, const char *any) {
103   if(from) return convert(from, nl_langinfo(CODESET), any, strlen(any) + 1);
104   else return xstrdup(any);
105 }
106
107 /** @brief Convert from encoding @p from to encoding @p to
108  * @param from Source encoding
109  * @param to Destination encoding
110  * @param any String in encoding @p from
111  * @return @p any converted to encoding @p to
112  */
113 char *any2any(const char *from,
114               const char *to,
115               const char *any) {
116   if(from || to) return convert(from, to, any, strlen(any) + 1);
117   else return xstrdup(any);
118 }
119
120 /** @brief Truncate a string for display purposes
121  * @param s Pointer to UTF-8 string
122  * @param max Maximum number of columns
123  * @return @p or truncated string (never NULL)
124  *
125  * Returns a string that is no longer than @p max graphemes long and is either
126  * (canonically) equal to @p s or is a truncated form of it with an ellipsis
127  * appended.
128  *
129  * We don't take display width into account (tricky for HTML!) and we don't
130  * attempt to implement the Bidi algorithm.  If you have track names for which
131  * either of these matter in practice then get in touch.
132  */
133 const char *truncate_for_display(const char *s, long max) {
134   uint32_t *s32;
135   size_t l32, cut;
136   utf32_iterator it;
137   long graphemes;
138
139   /* Convert to UTF-32 for processing */
140   if(!(s32 = utf8_to_utf32(s, strlen(s), &l32)))
141     return 0;
142   it = utf32_iterator_new(s32, l32);
143   cut = l32;
144   graphemes = 0;                        /* # of graphemes left of it */
145   while(graphemes <= max && utf32_iterator_where(it) < l32) {
146     if(graphemes == max - 1)
147       cut = utf32_iterator_where(it);
148     utf32_iterator_advance(it, 1);
149     if(utf32_iterator_grapheme_boundary(it))
150       ++graphemes;
151   }
152   if(graphemes > max) {                 /* we need to cut */
153     s32[cut] = 0x2026;                  /* HORIZONTAL ELLIPSIS */
154     l32 = cut + 1;
155     s = utf32_to_utf8(s32, l32, 0);
156   }
157   xfree(s32);
158   return s;
159 }
160
161 /*
162 Local Variables:
163 c-basic-offset:2
164 comment-column:40
165 fill-column:79
166 indent-tabs-mode:nil
167 End:
168 */