Use libcharset instead of nl_langinfo(CODESET) if possible.
[ghc-base.git] / cbits / PrelIOUtils.c
index b910c28..f43f5d8 100644 (file)
@@ -24,13 +24,27 @@ void debugBelch2(const char*s, char *t)
     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__ */