changeset 39842:9cd5298a6a29

localename: Revisit macOS specific code. * lib/localename.c (gl_locale_name_default): Reduce code duplication. Fix comments about Mac OS X versions.
author Bruno Haible <bruno@clisp.org>
date Sun, 16 Sep 2018 14:42:55 +0200
parents e8445477dfdd
children 55d99f7539f8
files ChangeLog lib/localename.c
diffstat 2 files changed, 24 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Sep 16 03:15:54 2018 +0200
+++ b/ChangeLog	Sun Sep 16 14:42:55 2018 +0200
@@ -1,3 +1,9 @@
+2018-09-16  Bruno Haible  <bruno@clisp.org>
+
+	localename: Revisit macOS specific code.
+	* lib/localename.c (gl_locale_name_default): Reduce code duplication.
+	Fix comments about Mac OS X versions.
+
 2018-09-15  Bruno Haible  <bruno@clisp.org>
 
 	setlocale: Improve support for locales not supported by libc.
--- a/lib/localename.c	Sun Sep 16 03:15:54 2018 +0200
+++ b/lib/localename.c	Sun Sep 16 14:42:55 2018 +0200
@@ -1143,7 +1143,7 @@
 
 
 #if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE
-/* Mac OS X 10.2 or newer */
+/* Mac OS X 10.4 or newer */
 
 /* Canonicalize a Mac OS X locale name to a Unix locale name.
    NAME is a sufficiently large buffer.
@@ -2932,7 +2932,7 @@
      codeset.  */
 
 # if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE
-  /* Mac OS X 10.2 or newer */
+  /* Mac OS X 10.4 or newer */
   {
     /* Cache the locale name, since CoreFoundation calls are expensive.  */
     static const char *cached_localename;
@@ -2940,29 +2940,28 @@
     if (cached_localename == NULL)
       {
         char namebuf[256];
-#  if HAVE_CFLOCALECOPYCURRENT /* Mac OS X 10.3 or newer */
+#  if HAVE_CFLOCALECOPYCURRENT /* Mac OS X 10.5 or newer */
         CFLocaleRef locale = CFLocaleCopyCurrent ();
         CFStringRef name = CFLocaleGetIdentifier (locale);
-
-        if (CFStringGetCString (name, namebuf, sizeof (namebuf),
-                                kCFStringEncodingASCII))
-          {
-            gl_locale_name_canonicalize (namebuf);
-            cached_localename = strdup (namebuf);
-          }
-        CFRelease (locale);
-#  elif HAVE_CFPREFERENCESCOPYAPPVALUE /* Mac OS X 10.2 or newer */
+#  elif HAVE_CFPREFERENCESCOPYAPPVALUE /* Mac OS X 10.4 or newer */
         CFTypeRef value =
           CFPreferencesCopyAppValue (CFSTR ("AppleLocale"),
                                      kCFPreferencesCurrentApplication);
-        if (value != NULL
-            && CFGetTypeID (value) == CFStringGetTypeID ()
-            && CFStringGetCString ((CFStringRef)value,
-                                   namebuf, sizeof (namebuf),
-                                   kCFStringEncodingASCII))
+        if (value != NULL && CFGetTypeID (value) == CFStringGetTypeID ())
           {
-            gl_locale_name_canonicalize (namebuf);
-            cached_localename = strdup (namebuf);
+            CFStringRef name = (CFStringRef)value;
+#  endif
+
+            if (CFStringGetCString (name, namebuf, sizeof (namebuf),
+                                    kCFStringEncodingASCII))
+              {
+                gl_locale_name_canonicalize (namebuf);
+                cached_localename = strdup (namebuf);
+              }
+
+#  if HAVE_CFLOCALECOPYCURRENT /* Mac OS X 10.5 or newer */
+        CFRelease (locale);
+#  elif HAVE_CFPREFERENCESCOPYAPPVALUE /* Mac OS X 10.4 or newer */
           }
 #  endif
         if (cached_localename == NULL)