Code

list_window: remove list_window_state_t
[ncmpc.git] / src / charset.c
index 701fd2105bd6eb1afaf5a4e8a67cb64170c8abc3..ccfac0ef86a454c339c2b7479c4eb801e1648fbf 100644 (file)
  */
 
 #include "charset.h"
-#include "i18n.h"
 
 #include <assert.h>
 #include <string.h>
 #include <glib.h>
 
-extern void screen_status_printf(const char *format, ...);
-
+#ifdef HAVE_LOCALE_H
 static bool noconvert = true;
 static const char *charset;
 
@@ -34,13 +32,16 @@ charset_init(void)
 {
        noconvert = g_get_charset(&charset);
        return charset;
+       return NULL;
 }
+#endif
 
 unsigned
 utf8_width(const char *str)
 {
        assert(str != NULL);
 
+#ifdef HAVE_LOCALE_H
        if (g_utf8_validate(str, -1, NULL)) {
                size_t len = g_utf8_strlen(str, -1);
                unsigned width = 0;
@@ -54,14 +55,15 @@ utf8_width(const char *str)
 
                return width;
        } else
+#endif
                return strlen(str);
 }
 
 char *
 utf8_to_locale(const char *utf8str)
 {
+#ifdef HAVE_LOCALE_H
        gchar *str;
-       gsize rb, wb;
        GError *error;
 
        assert(utf8str != NULL);
@@ -69,28 +71,26 @@ utf8_to_locale(const char *utf8str)
        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) {
-               screen_status_printf(_("Error: Unable to convert characters to %s"),
-                                    charset);
+       str = g_convert_with_fallback(utf8str, -1,
+                                     charset, "utf-8",
+                                     NULL, NULL, NULL,
+                                     &error);
+       if (str == NULL) {
                g_error_free(error);
                return g_strdup(utf8str);
        }
 
        return str;
+#else
+       return g_strdup(utf8str);
+#endif
 }
 
 char *
 locale_to_utf8(const char *localestr)
 {
+#ifdef HAVE_LOCALE_H
        gchar *str;
-       gsize rb, wb;
        GError *error;
 
        assert(localestr != NULL);
@@ -98,18 +98,17 @@ locale_to_utf8(const char *localestr)
        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"));
+       str = g_convert_with_fallback(localestr, -1,
+                                     "utf-8", charset,
+                                     NULL, NULL, NULL,
+                                     &error);
+       if (str == NULL) {
                g_error_free(error);
                return g_strdup(localestr);
        }
 
        return str;
+#else
+       return g_strdup(localestr);
+#endif
 }