summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: a3f209b)
raw | patch | inline | side by side (parent: a3f209b)
author | Max Kellermann <max@duempel.org> | |
Thu, 2 Oct 2008 17:00:35 +0000 (19:00 +0200) | ||
committer | Max Kellermann <max@duempel.org> | |
Thu, 2 Oct 2008 17:00:35 +0000 (19:00 +0200) |
Move everything which deals with UTF-8 strings and character set
conversion to charset.c, header charset.h.
conversion to charset.c, header charset.h.
17 files changed:
src/Makefile.am | patch | blob | history | |
src/charset.c | [new file with mode: 0644] | patch | blob |
src/charset.h | [new file with mode: 0644] | patch | blob |
src/list_window.c | patch | blob | history | |
src/main.c | patch | blob | history | |
src/mpdclient.c | patch | blob | history | |
src/options.c | patch | blob | history | |
src/screen.c | patch | blob | history | |
src/screen_artist.c | patch | blob | history | |
src/screen_browser.c | patch | blob | history | |
src/screen_file.c | patch | blob | history | |
src/screen_search.c | patch | blob | history | |
src/strfsong.c | patch | blob | history | |
src/support.c | patch | blob | history | |
src/support.h | patch | blob | history | |
src/utils.c | patch | blob | history | |
src/wreadln.c | patch | blob | history |
diff --git a/src/Makefile.am b/src/Makefile.am
index 59af3b51e3589eee1cfce111319e0e663f4abbbe..68af70ddd3b1a671fc3b457a22eafc3d0e657e4c 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
list_window.h\
colors.h\
support.h\
+ charset.h \
wreadln.h\
strfsong.h\
utils.h\
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
--- /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
--- /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 82df1350b57086df8eb5b6ae949c6e003b41315e..001b606fc814fe7aac68316662dd1c3749d0e4f0 100644 (file)
--- a/src/list_window.c
+++ b/src/list_window.c
#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 6d83dac6ca50c7311660664a675fbdec4b5f049a..0a9f7cb59eebb57fad5ef9210fe2479cb73fb508 100644 (file)
--- a/src/main.c
+++ b/src/main.c
#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 9beac9ee7e1bcdd7cb39f859bb914a3cf9a7f4ad..df636a566caea042d455aa648d673d8293c24c6c 100644 (file)
--- a/src/mpdclient.c
+++ b/src/mpdclient.c
#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 421eda7d86c65c1c9ad5cde1d797c4bba22b8cf8..9cb1b1685b2bb9188903ba466ee7ee7b4d065feb 100644 (file)
--- a/src/options.c
+++ b/src/options.c
#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 5239b82d8161bd7126cd94926fb31f6cd61c9af4..8e6ddefde4d89914588942cb48f90463914fd5b5 100644 (file)
--- a/src/screen.c
+++ b/src/screen.c
#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 351dcf081a620e9ac01894c848e17b4258dda8c0..7d3a189527c3666958e6082f022549ceab88c30d 100644 (file)
--- a/src/screen_artist.c
+++ b/src/screen_artist.c
#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 52285152b57dd679d13ee1a2fd865606f0c969c5..b32210029efe034c8708351aae96f619a8ab8279 100644 (file)
--- a/src/screen_browser.c
+++ b/src/screen_browser.c
#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 0e1800abcc645c2f362c9e8380015288ae45c4fd..baec535df502522b4738dc7a2252d306fdd04497 100644 (file)
--- a/src/screen_file.c
+++ b/src/screen_file.c
#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 af34baf7b006d2e0b5618d067c03a0687cd39bf4..a1f5045acf0b2d42905994abb305eebcb76d3947 100644 (file)
--- a/src/screen_search.c
+++ b/src/screen_search.c
#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 f6b67930e91112856ccc8652f3f9b59e9a3275e0..fcaf4ada1673cbef53401b91f3527eb2186ea11c 100644 (file)
--- a/src/strfsong.c
+++ b/src/strfsong.c
#include "strfsong.h"
#include "support.h"
+#include "charset.h"
#include <string.h>
diff --git a/src/support.c b/src/support.c
index 94aab41d149629c5353ac0b3e6444f7587ebf341..ebbff7348d10c409af5b2ba51d8b7aab5f7359c6 100644 (file)
--- a/src/support.c
+++ b/src/support.c
*/
#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)
{
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 3377f9a8099ed6e97e89399c8f7e65d0a120cf1a..0679ddc8676ba7ac395f52e8cfab2b6f7d92f38b 100644 (file)
--- a/src/support.h
+++ b/src/support.h
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 5f5940b032027bc18382bc9f7ab8ab4a11f57ced..fd7a0645b249b61865ee0e3b764e03fd1af5ab64 100644 (file)
--- a/src/utils.c
+++ b/src/utils.c
#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 cc703898bdeac1c8d72f2074490c187e63c85ccc..840b73ba004e3f72074fca35dfad8b45e5988e00 100644 (file)
--- a/src/wreadln.c
+++ b/src/wreadln.c
*
*/
-#include "config.h"
-
#include "wreadln.h"
+#include "charset.h"
+#include "config.h"
#include <stdlib.h>
#include <string.h>
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 */