Code

charset: convert strings with fallback
[ncmpc.git] / src / charset.c
1 /*
2  * (c) 2006 by Kalle Wallin <kaw@linux.se>
3  * Copyright (C) 2008 Max Kellermann <max@duempel.org>
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,
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  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
20 #include "charset.h"
22 #include <assert.h>
23 #include <string.h>
24 #include <glib.h>
26 static bool noconvert = true;
27 static const char *charset;
29 const char *
30 charset_init(void)
31 {
32         noconvert = g_get_charset(&charset);
33         return charset;
34 }
36 unsigned
37 utf8_width(const char *str)
38 {
39         assert(str != NULL);
41         if (g_utf8_validate(str, -1, NULL)) {
42                 size_t len = g_utf8_strlen(str, -1);
43                 unsigned width = 0;
44                 gunichar c;
46                 while (len--) {
47                         c = g_utf8_get_char(str);
48                         width += g_unichar_iswide(c) ? 2 : 1;
49                         str += g_unichar_to_utf8(c, NULL);
50                 }
52                 return width;
53         } else
54                 return strlen(str);
55 }
57 char *
58 utf8_to_locale(const char *utf8str)
59 {
60         gchar *str;
61         GError *error;
63         assert(utf8str != NULL);
65         if (noconvert)
66                 return g_strdup(utf8str);
68         str = g_convert_with_fallback(utf8str, strlen(utf8str),
69                                       charset, "utf-8",
70                                       NULL, NULL, NULL,
71                                       &error);
72         if (str == NULL) {
73                 g_error_free(error);
74                 return g_strdup(utf8str);
75         }
77         return str;
78 }
80 char *
81 locale_to_utf8(const char *localestr)
82 {
83         gchar *str;
84         GError *error;
86         assert(localestr != NULL);
88         if (noconvert)
89                 return g_strdup(localestr);
91         str = g_convert_with_fallback(localestr, strlen(localestr),
92                                       "utf-8", charset,
93                                       NULL, NULL, NULL,
94                                       &error);
95         if (str == NULL) {
96                 g_error_free(error);
97                 return g_strdup(localestr);
98         }
100         return str;