Code

charset: renamed my_strlen() to utf8_width()
[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"
21 #include "i18n.h"
23 #include <assert.h>
24 #include <string.h>
25 #include <glib.h>
27 extern void screen_status_printf(const char *format, ...);
29 static bool noconvert = true;
31 void
32 charset_init(bool disable)
33 {
34         noconvert = disable;
35 }
37 unsigned
38 utf8_width(const char *str)
39 {
40         assert(str != NULL);
42         if (g_utf8_validate(str, -1, NULL)) {
43                 size_t len = g_utf8_strlen(str, -1);
44                 unsigned width = 0;
45                 gunichar c;
47                 while (len--) {
48                         c = g_utf8_get_char(str);
49                         width += g_unichar_iswide(c) ? 2 : 1;
50                         str += g_unichar_to_utf8(c, NULL);
51                 }
53                 return width;
54         } else
55                 return strlen(str);
56 }
58 char *
59 utf8_to_locale(const char *utf8str)
60 {
61         gchar *str;
62         gsize rb, wb;
63         GError *error;
65         assert(utf8str != NULL);
67         if (noconvert)
68                 return g_strdup(utf8str);
70         rb = 0; /* bytes read */
71         wb = 0; /* bytes written */
72         error = NULL;
73         str = g_locale_from_utf8(utf8str,
74                                  strlen(utf8str),
75                                  &wb, &rb,
76                                  &error);
77         if (error) {
78                 const char *charset;
80                 g_get_charset(&charset);
81                 screen_status_printf(_("Error: Unable to convert characters to %s"),
82                                      charset);
83                 g_error_free(error);
84                 return g_strdup(utf8str);
85         }
87         return str;
88 }
90 char *
91 locale_to_utf8(const char *localestr)
92 {
93         gchar *str;
94         gsize rb, wb;
95         GError *error;
97         assert(localestr != NULL);
99         if (noconvert)
100                 return g_strdup(localestr);
102         rb = 0; /* bytes read */
103         wb = 0; /* bytes written */
104         error = NULL;
105         str = g_locale_to_utf8(localestr,
106                                strlen(localestr),
107                                &wb, &rb,
108                                &error);
109         if (error) {
110                 screen_status_printf(_("Error: Unable to convert characters to UTF-8"));
111                 g_error_free(error);
112                 return g_strdup(localestr);
113         }
115         return str;