Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / i18n.c
index 6ee159b..5a53bfa 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+/* Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -196,22 +196,11 @@ typedef struct scm_locale
   int   category_mask;
 } *scm_t_locale;
 
-
-/* Free the resources used by LOCALE.  */
-static inline void
-scm_i_locale_free (scm_t_locale locale)
-{
-  free (locale->locale_name);
-  locale->locale_name = NULL;
-}
-
 #else /* USE_GNU_LOCALE_API */
 
 /* Alias for glibc's locale type.  */
 typedef locale_t scm_t_locale;
 
-#define scm_i_locale_free freelocale
-
 #endif /* USE_GNU_LOCALE_API */
 
 
@@ -234,7 +223,7 @@ SCM_GLOBAL_VARIABLE (scm_global_locale, "%global-locale");
 #define SCM_VALIDATE_OPTIONAL_LOCALE_COPY(_pos, _arg, _c_locale)       \
   do                                                                   \
     {                                                                  \
-      if ((_arg) != SCM_UNDEFINED)                                     \
+      if (!scm_is_eq ((_arg), SCM_UNDEFINED))                           \
        SCM_VALIDATE_LOCALE_COPY (_pos, _arg, _c_locale);               \
       else                                                             \
        (_c_locale) = NULL;                                             \
@@ -244,16 +233,20 @@ SCM_GLOBAL_VARIABLE (scm_global_locale, "%global-locale");
 
 SCM_SMOB (scm_tc16_locale_smob_type, "locale", 0);
 
+#ifdef USE_GNU_LOCALE_API
+
 SCM_SMOB_FREE (scm_tc16_locale_smob_type, smob_locale_free, locale)
 {
   scm_t_locale c_locale;
 
   c_locale = (scm_t_locale) SCM_SMOB_DATA (locale);
-  scm_i_locale_free (c_locale);
+  freelocale (c_locale);
 
   return 0;
 }
 
+#endif /* USE_GNU_LOCALE_API */
+
 
 static void inline scm_locale_error (const char *, int) SCM_NORETURN;
 
@@ -400,7 +393,7 @@ install_locale (scm_t_locale locale)
         account.  */
       category_mask |= locale->category_mask;
 
-      if (locale->base_locale != SCM_UNDEFINED)
+      if (!SCM_UNBNDP (locale->base_locale))
        locale = (scm_t_locale) SCM_SMOB_DATA (locale->base_locale);
       else
        locale = NULL;
@@ -501,7 +494,6 @@ get_current_locale (SCM *result)
 
   c_locale = scm_gc_malloc (sizeof (* c_locale), "locale");
 
-
   lock_locale_mutex ();
 
   c_locale->category_mask = LC_ALL_MASK;
@@ -509,20 +501,16 @@ get_current_locale (SCM *result)
 
   current_locale = setlocale (LC_ALL, NULL);
   if (current_locale != NULL)
-    {
-      c_locale->locale_name = strdup (current_locale);
-      if (c_locale->locale_name == NULL)
-       err = ENOMEM;
-    }
+    c_locale->locale_name = scm_gc_strdup (current_locale, "locale");
   else
     err = EINVAL;
 
   unlock_locale_mutex ();
 
-  if (err)
-    scm_gc_free (c_locale, sizeof (* c_locale), "locale");
-  else
+  if (err == 0)
     SCM_NEWSMOB (*result, scm_tc16_locale_smob_type, c_locale);
+  else
+    *result = SCM_BOOL_F;
 
   return err;
 }
@@ -657,6 +645,7 @@ SCM_DEFINE (scm_make_locale, "make-locale", 2, 1, 0,
   c_locale = newlocale (c_category_mask, c_locale_name, c_base_locale);
 
   free (c_locale_name);
+  c_locale_name = NULL;
 
   if (c_locale == (locale_t) 0)
     {
@@ -672,7 +661,9 @@ SCM_DEFINE (scm_make_locale, "make-locale", 2, 1, 0,
   c_locale = scm_gc_malloc (sizeof (* c_locale), "locale");
 
   c_locale->category_mask = c_category_mask;
-  c_locale->locale_name = c_locale_name;
+  c_locale->locale_name = scm_gc_strdup (c_locale_name, "locale");
+  free (c_locale_name);
+  c_locale_name = NULL;
 
   if (scm_is_eq (base_locale, SCM_VARIABLE_REF (scm_global_locale)))
     {
@@ -1143,7 +1134,7 @@ chr_to_case (SCM chr, scm_t_locale c_locale,
   if (SCM_UNLIKELY (ret != 0))
     {
       *err = ret;
-      return NULL;
+      return SCM_BOOL_F;
     }
 
   if (convlen == 1)
@@ -1262,7 +1253,7 @@ str_to_case (SCM str, scm_t_locale c_locale,
   if (SCM_UNLIKELY (ret != 0))
     {
       *err = ret;
-      return NULL;
+      return SCM_BOOL_F;
     }
 
   convstr = scm_i_make_wide_string (convlen, &c_buf, 0);
@@ -1378,7 +1369,7 @@ SCM_DEFINE (scm_locale_string_to_integer, "locale-string->integer",
   SCM_VALIDATE_STRING (1, str);
   c_str = scm_i_string_chars (str);
 
-  if (base != SCM_UNDEFINED)
+  if (!scm_is_eq (base, SCM_UNDEFINED))
     SCM_VALIDATE_INT_COPY (2, base, c_base);
   else
     c_base = 10;
@@ -1591,7 +1582,7 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
              if (*p == 0)
                {
                  /* Cyclic grouping information.  */
-                 if (last_pair != SCM_EOL)
+                 if (!scm_is_null (last_pair))
                    SCM_SETCDR (last_pair, result);
                }
            }
@@ -1663,6 +1654,7 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
            default:
              result = scm_from_latin1_symbol ("unspecified");
            }
+         free (c_result);
          break;
 #endif
 
@@ -1755,13 +1747,23 @@ define_langinfo_items (void)
   DEFINE_NLITEM_CONSTANT (T_FMT);   /* Time format for strftime.  */
   DEFINE_NLITEM_CONSTANT (T_FMT_AMPM);/* 12-hour time format for strftime.  */
 
+#ifdef ERA
   DEFINE_NLITEM_CONSTANT (ERA);                /* Alternate era.  */
+#endif
+#ifdef ERA_D_FMT
   DEFINE_NLITEM_CONSTANT (ERA_D_FMT);  /* Date in alternate era format.  */
+#endif
+#ifdef ERA_D_T_FMT
   DEFINE_NLITEM_CONSTANT (ERA_D_T_FMT);        /* Date and time in alternate era
                                           format.  */
+#endif
+#ifdef ERA_T_FMT
   DEFINE_NLITEM_CONSTANT (ERA_T_FMT);  /* Time in alternate era format.  */
+#endif
 
+#ifdef ALT_DIGITS
   DEFINE_NLITEM_CONSTANT (ALT_DIGITS); /* Alternate symbols for digits.  */
+#endif
   DEFINE_NLITEM_CONSTANT (RADIXCHAR);
   DEFINE_NLITEM_CONSTANT (THOUSEP);