chiark / gitweb /
Loosen playlist command rights.
[disorder] / lib / charset.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
460b9539 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 2 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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
14ad73b9 20/** @file lib/charset.c @brief Character set conversion */
460b9539 21
05b75f8d 22#include "common.h"
460b9539 23
24#include <iconv.h>
460b9539 25#include <errno.h>
26#include <langinfo.h>
27
28#include "mem.h"
29#include "log.h"
30#include "charset.h"
31#include "configuration.h"
460b9539 32#include "vector.h"
11f598d1 33#include "unicode.h"
460b9539 34
14ad73b9
RK
35/** @brief Low-level converstion routine
36 * @param from Source encoding
37 * @param to Destination encoding
38 * @param ptr First byte to convert
39 * @param n Number of bytes to convert
40 * @return Converted text, 0-terminated; or NULL on error.
41 */
460b9539 42static void *convert(const char *from, const char *to,
43 const void *ptr, size_t n) {
44 iconv_t i;
45 size_t len;
46 char *buf = 0, *s, *d;
47 size_t bufsize = 0, sl, dl;
48
49 if((i = iconv_open(to, from)) == (iconv_t)-1)
50 fatal(errno, "error calling iconv_open");
51 do {
52 bufsize = bufsize ? 2 * bufsize : 32;
53 buf = xrealloc_noptr(buf, bufsize);
54 iconv(i, 0, 0, 0, 0);
55 s = (char *)ptr;
56 sl = n;
57 d = buf;
58 dl = bufsize;
59 /* (void *) to work around FreeBSD's nonstandard iconv prototype */
60 len = iconv(i, (void *)&s, &sl, &d, &dl);
61 } while(len == (size_t)-1 && errno == E2BIG);
62 iconv_close(i);
63 if(len == (size_t)-1) {
64 error(errno, "error converting from %s to %s", from, to);
65 return 0;
66 }
67 return buf;
68}
69
14ad73b9 70/** @brief Convert from the local multibyte encoding to UTF-8 */
460b9539 71char *mb2utf8(const char *mb) {
72 return convert(nl_langinfo(CODESET), "UTF-8", mb, strlen(mb) + 1);
73}
74
14ad73b9 75/** @brief Convert from UTF-8 to the local multibyte encoding */
460b9539 76char *utf82mb(const char *utf8) {
77 return convert("UTF-8", nl_langinfo(CODESET), utf8, strlen(utf8) + 1);
78}
79
14ad73b9 80/** @brief Convert from encoding @p from to UTF-8 */
460b9539 81char *any2utf8(const char *from, const char *any) {
82 return convert(from, "UTF-8", any, strlen(any) + 1);
83}
84
14ad73b9 85/** @brief Convert from encoding @p from to the local multibyte encoding */
460b9539 86char *any2mb(const char *from, const char *any) {
87 if(from) return convert(from, nl_langinfo(CODESET), any, strlen(any) + 1);
88 else return xstrdup(any);
89}
90
14ad73b9 91/** @brief Convert from encoding @p from to encoding @p to */
460b9539 92char *any2any(const char *from,
93 const char *to,
94 const char *any) {
95 if(from || to) return convert(from, to, any, strlen(any) + 1);
96 else return xstrdup(any);
97}
98
61507e3c
RK
99/** @brief Truncate a string for display purposes
100 * @param s Pointer to UTF-8 string
101 * @param max Maximum number of columns
102 * @return @p or truncated string (never NULL)
103 *
11f598d1
RK
104 * Returns a string that is no longer than @p max graphemes long and is either
105 * (canonically) equal to @p s or is a truncated form of it with an ellipsis
106 * appended.
107 *
108 * We don't take display width into account (tricky for HTML!) and we don't
109 * attempt to implement the Bidi algorithm. If you have track names for which
110 * either of these matter in practice then get in touch.
61507e3c
RK
111 */
112const char *truncate_for_display(const char *s, long max) {
11f598d1
RK
113 uint32_t *s32;
114 size_t l32, cut;
115 utf32_iterator it;
baee3e3a 116 long graphemes;
61507e3c 117
11f598d1
RK
118 /* Convert to UTF-32 for processing */
119 if(!(s32 = utf8_to_utf32(s, strlen(s), &l32)))
120 return 0;
121 it = utf32_iterator_new(s32, l32);
122 cut = l32;
baee3e3a
RK
123 graphemes = 0; /* # of graphemes left of it */
124 while(graphemes <= max && utf32_iterator_where(it) < l32) {
125 if(graphemes == max - 1)
126 cut = utf32_iterator_where(it);
11f598d1
RK
127 utf32_iterator_advance(it, 1);
128 if(utf32_iterator_grapheme_boundary(it))
baee3e3a 129 ++graphemes;
11f598d1 130 }
baee3e3a 131 if(graphemes > max) { /* we need to cut */
11f598d1
RK
132 s32[cut] = 0x2026; /* HORIZONTAL ELLIPSIS */
133 l32 = cut + 1;
134 s = utf32_to_utf8(s32, l32, 0);
61507e3c 135 }
11f598d1
RK
136 xfree(s32);
137 return s;
61507e3c
RK
138}
139
460b9539 140/*
141Local Variables:
142c-basic-offset:2
143comment-column:40
11f598d1
RK
144fill-column:79
145indent-tabs-mode:nil
460b9539 146End:
147*/