Add "x" prefix to calls to plain malloc(), calloc(), strdup(), realloc().
[pspp-builds.git] / src / libpspp / i18n.c
index 1463a4eecf667e43dd8fcbbedfa35f0d527ad4d4..a4e9b63f755ed8e6abd6b611567c03e1d02b5fea 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <libintl.h>
 #include <iconv.h>
 #include <errno.h>
 #include "assertion.h"
@@ -29,6 +30,8 @@
 
 #include "i18n.h"
 
+#include "version.h"
+
 #include <localcharset.h>
 #include "xstrndup.h"
 
@@ -182,13 +185,81 @@ recode_string (const char *to, const char *from,
 void
 i18n_init (void)
 {
-  free (default_encoding);
-  default_encoding = strdup (locale_charset ());
+#if ENABLE_NLS
+  setlocale (LC_CTYPE, "");
+#if HAVE_LC_MESSAGES
+  setlocale (LC_MESSAGES, "");
+#endif
+#if HAVE_LC_PAPER
+  setlocale (LC_PAPER, "");
+#endif
+  bindtextdomain (PACKAGE, locale_dir);
+  textdomain (PACKAGE);
+#endif /* ENABLE_NLS */
+
+  assert (default_encoding == NULL);
+  default_encoding = xstrdup (locale_charset ());
 
   hmapx_init (&map);
 }
 
 
+const char *
+get_default_encoding (void)
+{
+  return default_encoding;
+}
+
+void
+set_default_encoding (const char *enc)
+{
+  free (default_encoding);
+  default_encoding = xstrdup (enc);
+}
+
+
+/* Attempts to set the encoding from a locale name
+   returns true if successfull.
+   This function does not (should not!) alter the current locale.
+*/
+bool
+set_encoding_from_locale (const char *loc)
+{
+  bool ok = true;
+  char *c_encoding;
+  char *loc_encoding;
+  char *tmp = xstrdup (setlocale (LC_CTYPE, NULL));
+
+  setlocale (LC_CTYPE, "C");
+  c_encoding = xstrdup (locale_charset ());
+
+  setlocale (LC_CTYPE, loc);
+  loc_encoding = xstrdup (locale_charset ());
+
+
+  if ( 0 == strcmp (loc_encoding, c_encoding))
+    {
+      ok = false;
+    }
+
+
+  setlocale (LC_CTYPE, tmp);
+
+  free (tmp);
+
+  if (ok)
+    {
+      free (default_encoding);
+      default_encoding = loc_encoding;
+    }
+  else
+    free (loc_encoding);
+
+  free (c_encoding);
+
+  return ok;
+}
+
 void
 i18n_done (void)
 {
@@ -205,6 +276,19 @@ i18n_done (void)
 
 
 
+bool
+valid_encoding (const char *enc)
+{
+  iconv_t conv = iconv_open ("UTF8", enc);
+
+  if ( conv == (iconv_t) -1)
+    return false;
+
+  iconv_close (conv);
+
+  return true;
+}
+
 
 /* Return the system local's idea of the
    decimal seperator character */
@@ -213,7 +297,7 @@ get_system_decimal (void)
 {
   char radix_char;
 
-  char *ol = strdup (setlocale (LC_NUMERIC, NULL));
+  char *ol = xstrdup (setlocale (LC_NUMERIC, NULL));
   setlocale (LC_NUMERIC, "");
 
 #if HAVE_NL_LANGINFO