Code

disable more features with --enable-mini
[ncmpc.git] / src / charset.c
index aaec39c72c8a16e6772df16e3a41eef81a93a0e6..69e15eb6e042b6047ac52bbec4be8a6f8041e51a 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, ...);
-
+#if defined(HAVE_LOCALE_H) && !defined(NCMPC_MINI)
 static bool noconvert = true;
+static const char *charset;
 
-void
-charset_init(bool disable)
+const char *
+charset_init(void)
 {
-       noconvert = disable;
+       noconvert = g_get_charset(&charset);
+       return charset;
+       return NULL;
 }
+#endif
 
 unsigned
 utf8_width(const char *str)
 {
        assert(str != NULL);
 
+#ifdef ENABLE_WIDE
        if (g_utf8_validate(str, -1, NULL)) {
                size_t len = g_utf8_strlen(str, -1);
                unsigned width = 0;
@@ -52,65 +55,52 @@ utf8_width(const char *str)
 
                return width;
        } else
+#endif
                return strlen(str);
 }
 
 char *
 utf8_to_locale(const char *utf8str)
 {
+#if defined(HAVE_LOCALE_H) && !defined(NCMPC_MINI)
        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);
+       str = g_convert_with_fallback(utf8str, -1,
+                                     charset, "utf-8",
+                                     NULL, NULL, NULL, NULL);
+       if (str == NULL)
                return g_strdup(utf8str);
-       }
 
        return str;
+#else
+       return g_strdup(utf8str);
+#endif
 }
 
 char *
 locale_to_utf8(const char *localestr)
 {
+#if defined(HAVE_LOCALE_H) && !defined(NCMPC_MINI)
        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);
+       str = g_convert_with_fallback(localestr, -1,
+                                     "utf-8", charset,
+                                     NULL, NULL, NULL, NULL);
+       if (str == NULL)
                return g_strdup(localestr);
-       }
 
        return str;
+#else
+       return g_strdup(localestr);
+#endif
 }