diff options
author | Max Kellermann <max@duempel.org> | 2008-10-02 19:00:35 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2008-10-02 19:00:35 +0200 |
commit | b693269fd9740a3074b84da5645f6bd9daa6486e (patch) | |
tree | 93c83052ce5dc35155542ebbed4b2286f2c2e69a /src | |
parent | a3f209be26e588397f7b96de73fb86625c6d2bb8 (diff) | |
download | mpd-b693269fd9740a3074b84da5645f6bd9daa6486e.tar.gz mpd-b693269fd9740a3074b84da5645f6bd9daa6486e.tar.xz mpd-b693269fd9740a3074b84da5645f6bd9daa6486e.zip |
moved code to charset.c
Move everything which deals with UTF-8 strings and character set
conversion to charset.c, header charset.h.
Diffstat (limited to '')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/charset.c | 116 | ||||
-rw-r--r-- | src/charset.h | 36 | ||||
-rw-r--r-- | src/list_window.c | 1 | ||||
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/mpdclient.c | 2 | ||||
-rw-r--r-- | src/options.c | 3 | ||||
-rw-r--r-- | src/screen.c | 1 | ||||
-rw-r--r-- | src/screen_artist.c | 2 | ||||
-rw-r--r-- | src/screen_browser.c | 1 | ||||
-rw-r--r-- | src/screen_file.c | 1 | ||||
-rw-r--r-- | src/screen_search.c | 2 | ||||
-rw-r--r-- | src/strfsong.c | 1 | ||||
-rw-r--r-- | src/support.c | 96 | ||||
-rw-r--r-- | src/support.h | 13 | ||||
-rw-r--r-- | src/utils.c | 2 | ||||
-rw-r--r-- | src/wreadln.c | 5 |
17 files changed, 169 insertions, 117 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 59af3b51e..68af70ddd 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 000000000..41269f38b --- /dev/null +++ b/src/charset.c @@ -0,0 +1,116 @@ +/* + * (c) 2006 by Kalle Wallin <kaw@linux.se> + * Copyright (C) 2008 Max Kellermann <max@duempel.org> + * + * 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 <assert.h> +#include <string.h> +#include <glib.h> + +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 000000000..119937710 --- /dev/null +++ b/src/charset.h @@ -0,0 +1,36 @@ +/* + * (c) 2006 by Kalle Wallin <kaw@linux.se> + * Copyright (C) 2008 Max Kellermann <max@duempel.org> + * + * 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 <stdbool.h> +#include <stddef.h> + +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 82df1350b..001b606fc 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 6d83dac6c..0a9f7cb59 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 9beac9ee7..df636a566 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 421eda7d8..9cb1b1685 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 <stdlib.h> #include <string.h> +#include <glib.h> #define MAX_LONGOPT_LENGTH 32 diff --git a/src/screen.c b/src/screen.c index 5239b82d8..8e6ddefde 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 351dcf081..7d3a18952 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 52285152b..b32210029 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 0e1800abc..baec535df 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 af34baf7b..a1f5045ac 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 f6b67930e..fcaf4ada1 100644 --- a/src/strfsong.c +++ b/src/strfsong.c @@ -24,6 +24,7 @@ #include "strfsong.h" #include "support.h" +#include "charset.h" #include <string.h> diff --git a/src/support.c b/src/support.c index 94aab41d1..ebbff7348 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 <assert.h> -#include <time.h> #include <ctype.h> -#include <stdio.h> -#include <stdlib.h> #include <string.h> #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 3377f9a80..0679ddc86 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 5f5940b03..fd7a0645b 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 <ctype.h> #include <stdlib.h> diff --git a/src/wreadln.c b/src/wreadln.c index cc703898b..840b73ba0 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 <stdlib.h> #include <string.h> @@ -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 */ |