Code

fb22f0b685f3e6d09d779d2f3111d5035233d5da
[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 #ifdef ENABLE_LOCALE
27 static bool noconvert = true;
28 static const char *charset;
30 const char *
31 charset_init(void)
32 {
33         noconvert = g_get_charset(&charset);
34         return charset;
35         return NULL;
36 }
37 #endif
39 unsigned
40 utf8_width(const char *str)
41 {
42         assert(str != NULL);
44 #if defined(ENABLE_MULTIBYTE) && !defined(ENABLE_WIDE)
45         return g_utf8_strlen(str, -1);
46 #else
47 #ifdef ENABLE_WIDE
48         if (g_utf8_validate(str, -1, NULL)) {
49                 size_t len = g_utf8_strlen(str, -1);
50                 unsigned width = 0;
51                 gunichar c;
53                 while (len--) {
54                         c = g_utf8_get_char(str);
55                         width += g_unichar_iswide(c) ? 2 : 1;
56                         str += g_unichar_to_utf8(c, NULL);
57                 }
59                 return width;
60         } else
61 #endif
62                 return strlen(str);
63 #endif
64 }
66 char *
67 utf8_to_locale(const char *utf8str)
68 {
69 #ifdef ENABLE_LOCALE
70         gchar *str;
72         assert(utf8str != NULL);
74         if (noconvert)
75                 return g_strdup(utf8str);
77         str = g_convert_with_fallback(utf8str, -1,
78                                       charset, "utf-8",
79                                       NULL, NULL, NULL, NULL);
80         if (str == NULL)
81                 return g_strdup(utf8str);
83         return str;
84 #else
85         return g_strdup(utf8str);
86 #endif
87 }
89 char *
90 locale_to_utf8(const char *localestr)
91 {
92 #ifdef ENABLE_LOCALE
93         gchar *str;
95         assert(localestr != NULL);
97         if (noconvert)
98                 return g_strdup(localestr);
100         str = g_convert_with_fallback(localestr, -1,
101                                       "utf-8", charset,
102                                       NULL, NULL, NULL, NULL);
103         if (str == NULL)
104                 return g_strdup(localestr);
106         return str;
107 #else
108         return g_strdup(localestr);
109 #endif