Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
5aff007d | 3 | * Copyright (C) 2004, 2005, 2007, 2008 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 | |
22 | #include <iconv.h> | |
460b9539 | 23 | #include <errno.h> |
24 | #include <langinfo.h> | |
25 | ||
26 | #include "mem.h" | |
27 | #include "log.h" | |
28 | #include "charset.h" | |
29 | #include "configuration.h" | |
460b9539 | 30 | #include "vector.h" |
11f598d1 | 31 | #include "unicode.h" |
460b9539 | 32 | |
14ad73b9 RK |
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. | |
39 | */ | |
460b9539 | 40 | static void *convert(const char *from, const char *to, |
41 | const void *ptr, size_t n) { | |
42 | iconv_t i; | |
43 | size_t len; | |
44 | char *buf = 0, *s, *d; | |
45 | size_t bufsize = 0, sl, dl; | |
46 | ||
47 | if((i = iconv_open(to, from)) == (iconv_t)-1) | |
48 | fatal(errno, "error calling iconv_open"); | |
49 | do { | |
50 | bufsize = bufsize ? 2 * bufsize : 32; | |
51 | buf = xrealloc_noptr(buf, bufsize); | |
52 | iconv(i, 0, 0, 0, 0); | |
53 | s = (char *)ptr; | |
54 | sl = n; | |
55 | d = buf; | |
56 | dl = 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); | |
60 | iconv_close(i); | |
61 | if(len == (size_t)-1) { | |
62 | error(errno, "error converting from %s to %s", from, to); | |
63 | return 0; | |
64 | } | |
65 | return buf; | |
66 | } | |
67 | ||
14ad73b9 | 68 | /** @brief Convert from the local multibyte encoding to UTF-8 */ |
460b9539 | 69 | char *mb2utf8(const char *mb) { |
70 | return convert(nl_langinfo(CODESET), "UTF-8", mb, strlen(mb) + 1); | |
71 | } | |
72 | ||
14ad73b9 | 73 | /** @brief Convert from UTF-8 to the local multibyte encoding */ |
460b9539 | 74 | char *utf82mb(const char *utf8) { |
75 | return convert("UTF-8", nl_langinfo(CODESET), utf8, strlen(utf8) + 1); | |
76 | } | |
77 | ||
14ad73b9 | 78 | /** @brief Convert from encoding @p from to UTF-8 */ |
460b9539 | 79 | char *any2utf8(const char *from, const char *any) { |
80 | return convert(from, "UTF-8", any, strlen(any) + 1); | |
81 | } | |
82 | ||
14ad73b9 | 83 | /** @brief Convert from encoding @p from to the local multibyte encoding */ |
460b9539 | 84 | char *any2mb(const char *from, const char *any) { |
85 | if(from) return convert(from, nl_langinfo(CODESET), any, strlen(any) + 1); | |
86 | else return xstrdup(any); | |
87 | } | |
88 | ||
14ad73b9 | 89 | /** @brief Convert from encoding @p from to encoding @p to */ |
460b9539 | 90 | char *any2any(const char *from, |
91 | const char *to, | |
92 | const char *any) { | |
93 | if(from || to) return convert(from, to, any, strlen(any) + 1); | |
94 | else return xstrdup(any); | |
95 | } | |
96 | ||
61507e3c RK |
97 | /** @brief Truncate a string for display purposes |
98 | * @param s Pointer to UTF-8 string | |
99 | * @param max Maximum number of columns | |
100 | * @return @p or truncated string (never NULL) | |
101 | * | |
11f598d1 RK |
102 | * Returns a string that is no longer than @p max graphemes long and is either |
103 | * (canonically) equal to @p s or is a truncated form of it with an ellipsis | |
104 | * appended. | |
105 | * | |
106 | * We don't take display width into account (tricky for HTML!) and we don't | |
107 | * attempt to implement the Bidi algorithm. If you have track names for which | |
108 | * either of these matter in practice then get in touch. | |
61507e3c RK |
109 | */ |
110 | const char *truncate_for_display(const char *s, long max) { | |
11f598d1 RK |
111 | uint32_t *s32; |
112 | size_t l32, cut; | |
113 | utf32_iterator it; | |
baee3e3a | 114 | long graphemes; |
61507e3c | 115 | |
11f598d1 RK |
116 | /* Convert to UTF-32 for processing */ |
117 | if(!(s32 = utf8_to_utf32(s, strlen(s), &l32))) | |
118 | return 0; | |
119 | it = utf32_iterator_new(s32, l32); | |
120 | cut = l32; | |
baee3e3a RK |
121 | graphemes = 0; /* # of graphemes left of it */ |
122 | while(graphemes <= max && utf32_iterator_where(it) < l32) { | |
123 | if(graphemes == max - 1) | |
124 | cut = utf32_iterator_where(it); | |
11f598d1 RK |
125 | utf32_iterator_advance(it, 1); |
126 | if(utf32_iterator_grapheme_boundary(it)) | |
baee3e3a | 127 | ++graphemes; |
11f598d1 | 128 | } |
baee3e3a | 129 | if(graphemes > max) { /* we need to cut */ |
11f598d1 RK |
130 | s32[cut] = 0x2026; /* HORIZONTAL ELLIPSIS */ |
131 | l32 = cut + 1; | |
132 | s = utf32_to_utf8(s32, l32, 0); | |
61507e3c | 133 | } |
11f598d1 RK |
134 | xfree(s32); |
135 | return s; | |
61507e3c RK |
136 | } |
137 | ||
460b9539 | 138 | /* |
139 | Local Variables: | |
140 | c-basic-offset:2 | |
141 | comment-column:40 | |
11f598d1 RK |
142 | fill-column:79 |
143 | indent-tabs-mode:nil | |
460b9539 | 144 | End: |
145 | */ |