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