nl_langinfo(CODESET) doesn't always return standardized variations of the encoding names. Use libcharset if possible, which is shipped together with GNU libiconv.
{-# NOINLINE localeEncoding #-}
localeEncoding :: TextEncoding
localeEncoding = unsafePerformIO $ do
-#if HAVE_LANGINFO_H
- cstr <- c_localeEncoding -- use nl_langinfo(CODESET) to get the encoding
- -- if we have it
+ -- Use locale_charset() or nl_langinfo(CODESET) to get the encoding
+ -- if we have either of them.
+ cstr <- c_localeEncoding
r <- peekCString cstr
mkTextEncoding r
-#else
- mkTextEncoding "" -- GNU iconv accepts "" to mean the -- locale encoding.
-#endif
-- We hope iconv_t is a storable type. It should be, since it has at least the
-- value -1, which is a possible return value from iconv_open.
debugBelch(s,t);
}
-// Use a C wrapper for this because we avoid hsc2hs in base
-#if HAVE_LANGINFO_H
-#include <langinfo.h>
-char *localeEncoding (void)
+#if defined(HAVE_LIBCHARSET)
+# include <libcharset.h>
+#elif defined(HAVE_LANGINFO_H)
+# include <langinfo.h>
+#endif
+
+const char* localeEncoding(void)
{
+#if defined(HAVE_LIBCHARSET)
+ return locale_charset();
+
+#elif defined(HAVE_LANGINFO_H)
return nl_langinfo(CODESET);
-}
+
+#else
+#warning Depending on the unportable behavior of GNU iconv due to absence of both libcharset and langinfo.h
+ /* GNU iconv accepts "" to mean the current locale's
+ * encoding. Warning: This isn't portable.
+ */
+ return "";
#endif
+}
#endif /* __GLASGOW_HASKELL__ */
AC_MSG_ERROR([iconv is required on non-Windows platforms]);;
esac])
+# If possible, we use libcharset instead of nl_langinfo(CODESET) to
+# determine the current locale's character encoding.
+FP_SEARCH_LIBS_PROTO(
+ [locale_charset],
+ [#include <libcharset.h>],
+ [const char* charset = locale_charset();],
+ [charset],
+ [AC_DEFINE([HAVE_LIBCHARSET], [1], [Define to 1 if you have libcharset.])
+ EXTRA_LIBS="$EXTRA_LIBS $ac_lib"])
+
+
AC_SUBST(EXTRA_LIBS)
AC_CONFIG_FILES([base.buildinfo])