From b693269fd9740a3074b84da5645f6bd9daa6486e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 2 Oct 2008 19:00:35 +0200 Subject: [PATCH] moved code to charset.c Move everything which deals with UTF-8 strings and character set conversion to charset.c, header charset.h. --- src/Makefile.am | 2 + src/charset.c | 116 +++++++++++++++++++++++++++++++++++++++++++ src/charset.h | 36 ++++++++++++++ src/list_window.c | 1 + src/main.c | 2 +- src/mpdclient.c | 2 +- src/options.c | 3 +- src/screen.c | 1 + src/screen_artist.c | 2 +- src/screen_browser.c | 1 + src/screen_file.c | 1 + src/screen_search.c | 2 +- src/strfsong.c | 1 + src/support.c | 96 +---------------------------------- src/support.h | 13 ----- src/utils.c | 2 +- src/wreadln.c | 5 +- 17 files changed, 169 insertions(+), 117 deletions(-) create mode 100644 src/charset.c create mode 100644 src/charset.h diff --git a/src/Makefile.am b/src/Makefile.am index 59af3b5..68af70d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,6 +27,7 @@ ncmpc_headers = \ list_window.h\ colors.h\ support.h\ + charset.h \ wreadln.h\ strfsong.h\ utils.h\ @@ -66,6 +67,7 @@ ncmpc_SOURCES = \ list_window.c\ colors.c\ support.c\ + charset.c \ wreadln.c\ strfsong.c\ utils.c\ diff --git a/src/charset.c b/src/charset.c new file mode 100644 index 0000000..41269f3 --- /dev/null +++ b/src/charset.c @@ -0,0 +1,116 @@ +/* + * (c) 2006 by Kalle Wallin + * Copyright (C) 2008 Max Kellermann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "charset.h" +#include "i18n.h" + +#include +#include +#include + +extern void screen_status_printf(const char *format, ...); + +static bool noconvert = true; + +void +charset_init(bool disable) +{ + noconvert = disable; +} + +size_t +my_strlen(const char *str) +{ + assert(str != NULL); + + if (g_utf8_validate(str, -1, NULL)) { + size_t len = g_utf8_strlen(str, -1); + size_t width = 0; + gunichar c; + + while (len--) { + c = g_utf8_get_char(str); + width += g_unichar_iswide(c) ? 2 : 1; + str += g_unichar_to_utf8(c, NULL); + } + + return width; + } else + return strlen(str); +} + +char * +utf8_to_locale(const char *utf8str) +{ + gchar *str; + gsize rb, wb; + GError *error; + + assert(utf8str != NULL); + + if (noconvert) + return g_strdup(utf8str); + + rb = 0; /* bytes read */ + wb = 0; /* bytes written */ + error = NULL; + str = g_locale_from_utf8(utf8str, + strlen(utf8str), + &wb, &rb, + &error); + if (error) { + const char *charset; + + g_get_charset(&charset); + screen_status_printf(_("Error: Unable to convert characters to %s"), + charset); + g_error_free(error); + return g_strdup(utf8str); + } + + return str; +} + +char * +locale_to_utf8(const char *localestr) +{ + gchar *str; + gsize rb, wb; + GError *error; + + assert(localestr != NULL); + + if (noconvert) + return g_strdup(localestr); + + rb = 0; /* bytes read */ + wb = 0; /* bytes written */ + error = NULL; + str = g_locale_to_utf8(localestr, + strlen(localestr), + &wb, &rb, + &error); + if (error) { + screen_status_printf(_("Error: Unable to convert characters to UTF-8")); + g_error_free(error); + return g_strdup(localestr); + } + + return str; +} diff --git a/src/charset.h b/src/charset.h new file mode 100644 index 0000000..1199377 --- /dev/null +++ b/src/charset.h @@ -0,0 +1,36 @@ +/* + * (c) 2006 by Kalle Wallin + * Copyright (C) 2008 Max Kellermann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef CHARSET_H +#define CHARSET_H + +#include +#include + +void charset_init(bool disable); + +/** + * Returns the number of terminal cells occupied by this string. + */ +size_t my_strlen(const char *str); + +char *utf8_to_locale(const char *str); +char *locale_to_utf8(const char *str); + +#endif diff --git a/src/list_window.c b/src/list_window.c index 82df135..001b606 100644 --- a/src/list_window.c +++ b/src/list_window.c @@ -21,6 +21,7 @@ #include "list_window.h" #include "config.h" #include "options.h" +#include "charset.h" #include "support.h" #include "command.h" #include "colors.h" diff --git a/src/main.c b/src/main.c index 6d83dac..0a9f7cb 100644 --- a/src/main.c +++ b/src/main.c @@ -21,7 +21,7 @@ #include "config.h" #include "ncmpc.h" #include "mpdclient.h" -#include "support.h" +#include "charset.h" #include "options.h" #include "conf.h" #include "command.h" diff --git a/src/mpdclient.c b/src/mpdclient.c index 9beac9e..df636a5 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -19,7 +19,7 @@ #include "mpdclient.h" #include "screen_utils.h" #include "config.h" -#include "support.h" +#include "charset.h" #include "options.h" #include "strfsong.h" diff --git a/src/options.c b/src/options.c index 421eda7..9cb1b16 100644 --- a/src/options.c +++ b/src/options.c @@ -19,12 +19,13 @@ #include "options.h" #include "config.h" #include "defaults.h" -#include "support.h" +#include "charset.h" #include "command.h" #include "conf.h" #include #include +#include #define MAX_LONGOPT_LENGTH 32 diff --git a/src/screen.c b/src/screen.c index 5239b82..8e6ddef 100644 --- a/src/screen.c +++ b/src/screen.c @@ -24,6 +24,7 @@ #include "config.h" #include "i18n.h" #include "support.h" +#include "charset.h" #include "mpdclient.h" #include "utils.h" #include "command.h" diff --git a/src/screen_artist.c b/src/screen_artist.c index 351dcf0..7d3a189 100644 --- a/src/screen_artist.c +++ b/src/screen_artist.c @@ -18,7 +18,7 @@ #include "i18n.h" #include "options.h" -#include "support.h" +#include "charset.h" #include "mpdclient.h" #include "utils.h" #include "strfsong.h" diff --git a/src/screen_browser.c b/src/screen_browser.c index 5228515..b322100 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -20,6 +20,7 @@ #include "screen_browser.h" #include "i18n.h" #include "options.h" +#include "charset.h" #include "support.h" #include "strfsong.h" #include "screen_utils.h" diff --git a/src/screen_file.c b/src/screen_file.c index 0e1800a..baec535 100644 --- a/src/screen_file.c +++ b/src/screen_file.c @@ -19,6 +19,7 @@ #include "config.h" #include "i18n.h" #include "options.h" +#include "charset.h" #include "support.h" #include "mpdclient.h" #include "command.h" diff --git a/src/screen_search.c b/src/screen_search.c index af34baf..a1f5045 100644 --- a/src/screen_search.c +++ b/src/screen_search.c @@ -18,7 +18,7 @@ #include "i18n.h" #include "options.h" -#include "support.h" +#include "charset.h" #include "mpdclient.h" #include "strfsong.h" #include "command.h" diff --git a/src/strfsong.c b/src/strfsong.c index f6b6793..fcaf4ad 100644 --- a/src/strfsong.c +++ b/src/strfsong.c @@ -24,6 +24,7 @@ #include "strfsong.h" #include "support.h" +#include "charset.h" #include diff --git a/src/support.c b/src/support.c index 94aab41..ebbff73 100644 --- a/src/support.c +++ b/src/support.c @@ -19,43 +19,15 @@ */ #include "support.h" -#include "i18n.h" +#include "charset.h" #include "config.h" #include -#include #include -#include -#include #include #define BUFSIZE 1024 -extern void screen_status_printf(const char *format, ...); - -static gboolean noconvert = TRUE; - -size_t -my_strlen(const char *str) -{ - assert(str != NULL); - - if (g_utf8_validate(str, -1, NULL)) { - size_t len = g_utf8_strlen(str, -1); - size_t width = 0; - gunichar c; - - while (len--) { - c = g_utf8_get_char(str); - width += g_unichar_iswide(c) ? 2 : 1; - str += g_unichar_to_utf8(c, NULL); - } - - return width; - } else - return strlen(str); -} - char * remove_trailing_slash(char *path) { @@ -169,69 +141,3 @@ strscroll(char *str, char *separator, int width, scroll_state_t *st) g_free(tmp); return buf; } - -void -charset_init(gboolean disable) -{ - noconvert = disable; -} - -char * -utf8_to_locale(const char *utf8str) -{ - gchar *str; - gsize rb, wb; - GError *error; - - assert(utf8str != NULL); - - if (noconvert) - return g_strdup(utf8str); - - rb = 0; /* bytes read */ - wb = 0; /* bytes written */ - error = NULL; - str = g_locale_from_utf8(utf8str, - strlen(utf8str), - &wb, &rb, - &error); - if (error) { - const char *charset; - - g_get_charset(&charset); - screen_status_printf(_("Error: Unable to convert characters to %s"), - charset); - g_error_free(error); - return g_strdup(utf8str); - } - - return str; -} - -char * -locale_to_utf8(const char *localestr) -{ - gchar *str; - gsize rb, wb; - GError *error; - - assert(localestr != NULL); - - if (noconvert) - return g_strdup(localestr); - - rb = 0; /* bytes read */ - wb = 0; /* bytes written */ - error = NULL; - str = g_locale_to_utf8(localestr, - strlen(localestr), - &wb, &rb, - &error); - if (error) { - screen_status_printf(_("Error: Unable to convert characters to UTF-8")); - g_error_free(error); - return g_strdup(localestr); - } - - return str; -} diff --git a/src/support.h b/src/support.h index 3377f9a..0679ddc 100644 --- a/src/support.h +++ b/src/support.h @@ -24,17 +24,4 @@ typedef struct { char *strscroll(char *str, char *separator, int width, scroll_state_t *st); -void charset_init(gboolean disable); -char *utf8_to_locale(const char *str); -char *locale_to_utf8(const char *str); - -/** - * Returns the number of terminal cells occupied by this string. - */ -size_t my_strlen(const char *str); - -/* number of bytes in str */ -size_t my_strsize(char *str); - - #endif diff --git a/src/utils.c b/src/utils.c index 5f5940b..fd7a064 100644 --- a/src/utils.c +++ b/src/utils.c @@ -20,7 +20,7 @@ #include "utils.h" #include "options.h" -#include "support.h" +#include "charset.h" #include #include diff --git a/src/wreadln.c b/src/wreadln.c index cc70389..840b73b 100644 --- a/src/wreadln.c +++ b/src/wreadln.c @@ -18,9 +18,9 @@ * */ -#include "config.h" - #include "wreadln.h" +#include "charset.h" +#include "config.h" #include #include @@ -59,7 +59,6 @@ wrln_gcmp_post_cb_t wrln_post_completion_callback = NULL; extern void sigstop(void); extern void screen_bell(void); -extern size_t my_strlen(char *str); #ifndef USE_NCURSESW /* move the cursor one step to the right */ -- 2.30.2