Merge branch 'master' into msbuilddll2
[mono.git] / mono / metadata / locales.c
index 23e63c5e733e20ae7d0b240391d7bff9a16b570c..7fffad9cc8c5bf8206e0b34cefc24362c19d081a 100644 (file)
@@ -24,6 +24,7 @@
 #include <mono/metadata/locales.h>
 #include <mono/metadata/culture-info.h>
 #include <mono/metadata/culture-info-tables.h>
+#include <mono/utils/bsearch.h>
 
 #ifndef DISABLE_NORMALIZATION
 #include <mono/metadata/normalization-tables.h>
@@ -305,34 +306,12 @@ construct_region (MonoRegionInfo *this, const RegionInfoEntry *ri)
        return TRUE;
 }
 
-static gboolean
-construct_culture_from_specific_name (MonoCultureInfo *ci, gchar *name)
-{
-       const CultureInfoEntry *entry;
-       const CultureInfoNameEntry *ne;
-
-       MONO_ARCH_SAVE_REGS;
-
-       ne = bsearch (name, culture_name_entries, NUM_CULTURE_ENTRIES,
-                       sizeof (CultureInfoNameEntry), culture_name_locator);
-
-       if (ne == NULL)
-               return FALSE;
-
-       entry = &culture_entries [ne->culture_entry_index];
-
-       if (entry)
-               return construct_culture (ci, entry);
-       else
-               return FALSE;
-}
-
 static const CultureInfoEntry*
 culture_info_entry_from_lcid (int lcid)
 {
        const CultureInfoEntry *ci;
 
-       ci = bsearch (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
+       ci = mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
 
        return ci;
 }
@@ -345,7 +324,7 @@ region_info_entry_from_lcid (int lcid)
 
        MONO_ARCH_SAVE_REGS;
 
-       ne = bsearch (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
+       ne = mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
 
        if (ne == NULL)
                return FALSE;
@@ -355,47 +334,18 @@ region_info_entry_from_lcid (int lcid)
        return entry;
 }
 
-/*
- * The following two methods are modified from the ICU source code. (http://oss.software.ibm.com/icu)
- * Copyright (c) 1995-2003 International Business Machines Corporation and others
- * All rights reserved.
- */
-static gchar*
-get_posix_locale (void)
-{
-       const gchar* posix_locale = NULL;
-
-       posix_locale = g_getenv("LC_ALL");
-       if (posix_locale == 0) {
-               posix_locale = g_getenv("LANG");
-               if (posix_locale == 0) {
-                       posix_locale = setlocale(LC_ALL, NULL);
-               }
-       }
-
-       if (posix_locale == NULL)
-               return NULL;
-
-       if ((strcmp ("C", posix_locale) == 0) || (strchr (posix_locale, ' ') != NULL)
-                       || (strchr (posix_locale, '/') != NULL)) {
-               /*
-                * HPUX returns 'C C C C C C C'
-                * Solaris can return /en_US/C/C/C/C/C on the second try.
-                * Maybe we got some garbage.
-                */
-               return NULL;
-       }
-
-       return g_strdup (posix_locale);
-}
-
 #if defined (__APPLE__)
 static gchar*
 get_darwin_locale (void)
 {
        static gchar *darwin_locale = NULL;
        CFLocaleRef locale = NULL;
+       CFStringRef locale_language = NULL;
+       CFStringRef locale_country = NULL;
+       CFStringRef locale_script = NULL;
        CFStringRef locale_cfstr = NULL;
+       CFIndex bytes_converted;
+       CFIndex bytes_written;
        CFIndex len;
        int i;
 
@@ -405,21 +355,51 @@ get_darwin_locale (void)
        locale = CFLocaleCopyCurrent ();
 
        if (locale) {
-               locale_cfstr = CFLocaleGetIdentifier (locale);
-
-               if (locale_cfstr) {
-                       len = CFStringGetMaximumSizeForEncoding (CFStringGetLength (locale_cfstr), kCFStringEncodingMacRoman) + 1;
-                       darwin_locale = (char *) malloc (len);
-                       if (!CFStringGetCString (locale_cfstr, darwin_locale, len, kCFStringEncodingMacRoman)) {
-                               free (darwin_locale);
-                               CFRelease (locale);
-                               darwin_locale = NULL;
-                               return NULL;
+               locale_language = CFLocaleGetValue (locale, kCFLocaleLanguageCode);
+               if (locale_language != NULL && CFStringGetBytes(locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
+                       len = bytes_converted + 1;
+
+                       locale_country = CFLocaleGetValue (locale, kCFLocaleCountryCode);
+                       if (locale_country != NULL && CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
+                               len += bytes_converted + 1;
+
+                               locale_script = CFLocaleGetValue (locale, kCFLocaleScriptCode);
+                               if (locale_script != NULL && CFStringGetBytes (locale_script, CFRangeMake (0, CFStringGetLength (locale_script)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
+                                       len += bytes_converted + 1;
+                               }
+
+                               darwin_locale = (char *) malloc (len + 1);
+                               CFStringGetBytes (locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) darwin_locale, len, &bytes_converted);
+
+                               darwin_locale[bytes_converted] = '-';
+                               bytes_written = bytes_converted + 1;
+                               if (locale_script != NULL && CFStringGetBytes (locale_script, CFRangeMake (0, CFStringGetLength (locale_script)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) &darwin_locale[bytes_written], len - bytes_written, &bytes_converted) > 0) {
+                                       darwin_locale[bytes_written + bytes_converted] = '-';
+                                       bytes_written += bytes_converted + 1;
+                               }
+
+                               CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) &darwin_locale[bytes_written], len - bytes_written, &bytes_converted);
+                               darwin_locale[bytes_written + bytes_converted] = '\0';
                        }
+               }
+
+               if (darwin_locale == NULL) {
+                       locale_cfstr = CFLocaleGetIdentifier (locale);
+
+                       if (locale_cfstr) {
+                               len = CFStringGetMaximumSizeForEncoding (CFStringGetLength (locale_cfstr), kCFStringEncodingMacRoman) + 1;
+                               darwin_locale = (char *) malloc (len);
+                               if (!CFStringGetCString (locale_cfstr, darwin_locale, len, kCFStringEncodingMacRoman)) {
+                                       free (darwin_locale);
+                                       CFRelease (locale);
+                                       darwin_locale = NULL;
+                                       return NULL;
+                               }
 
-                       for (i = 0; i < strlen (darwin_locale); i++)
-                               if (darwin_locale [i] == '_')
-                                       darwin_locale [i] = '-';
+                               for (i = 0; i < strlen (darwin_locale); i++)
+                                       if (darwin_locale [i] == '_')
+                                               darwin_locale [i] = '-';
+                       }                       
                }
 
                CFRelease (locale);
@@ -429,14 +409,34 @@ get_darwin_locale (void)
 }
 #endif
 
-static gchar*
-get_current_locale_name (void)
+static char *
+get_posix_locale (void)
 {
-       gchar *locale;
-       gchar *corrected = NULL;
-       const gchar *p;
-       gchar *c;
+       const char *locale;
 
+       locale = g_getenv ("LC_ALL");
+       if (locale == NULL) {
+               locale = g_getenv ("LANG");
+               if (locale == NULL)
+                       locale = setlocale (LC_ALL, NULL);
+       }
+       if (locale == NULL)
+               return NULL;
+
+       /* Skip English-only locale 'C' */
+       if (strcmp (locale, "C") == 0)
+               return NULL;
+
+       return g_strdup (locale);
+}
+
+
+static gchar *
+get_current_locale_name (void)
+{
+       char *locale;
+       char *p, *ret;
+               
 #ifdef HOST_WIN32
        locale = g_win32_getlocale ();
 #elif defined (__APPLE__)      
@@ -445,69 +445,43 @@ get_current_locale_name (void)
                locale = get_posix_locale ();
 #else
        locale = get_posix_locale ();
-#endif 
+#endif
 
        if (locale == NULL)
                return NULL;
 
-       if ((p = strchr (locale, '.')) != NULL) {
-               /* assume new locale can't be larger than old one? */
-               corrected = g_malloc (strlen (locale));
-               strncpy (corrected, locale, p - locale);
-               corrected [p - locale] = 0;
-
-               /* do not copy after the @ */
-               if ((p = strchr (corrected, '@')) != NULL)
-                       corrected [p - corrected] = 0;
-       }
-
-       /* Note that we scan the *uncorrected* ID. */
-       if ((p = strrchr (locale, '@')) != NULL) {
-
-               /*
-                * In Mono we dont handle the '@' modifier because we do
-                * not have any cultures that use it. We just trim it
-                * off of the end of the name.
-                */
-
-               if (corrected == NULL) {
-                       corrected = g_malloc (strlen (locale));
-                       strncpy (corrected, locale, p - locale);
-                       corrected [p - locale] = 0;
-               }
-       }
-
-       if (corrected == NULL)
-               corrected = locale;
-       else
-               g_free (locale);
-
-       if ((c = strchr (corrected, '_')) != NULL)
-               *c = '-';
-
-       c = corrected;
-       corrected = g_ascii_strdown (c, -1);
-       g_free (c);
+       p = strchr (locale, '.');
+       if (p != NULL)
+               *p = 0;
+       p = strchr (locale, '@');
+       if (p != NULL)
+               *p = 0;
+       p = strchr (locale, '_');
+       if (p != NULL)
+               *p = '-';
+
+       ret = g_ascii_strdown (locale, -1);
+       g_free (locale);
 
-       return corrected;
-}       
+       return ret;
+}
 
-MonoBoolean
-ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_current_locale (MonoCultureInfo *ci)
+MonoString*
+ves_icall_System_Globalization_CultureInfo_get_current_locale_name (void)
 {
        gchar *locale;
-       gboolean ret;
+       MonoString* ret;
+       MonoDomain *domain;
 
        MONO_ARCH_SAVE_REGS;
 
        locale = get_current_locale_name ();
        if (locale == NULL)
-               return FALSE;
+               return NULL;
 
-       ret = construct_culture_from_specific_name (ci, locale);
+       domain = mono_domain_get ();
+       ret = mono_string_new (domain, locale);
        g_free (locale);
-       ci->is_read_only = TRUE;
-       ci->use_user_override = TRUE;
 
        return ret;
 }
@@ -537,7 +511,7 @@ ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (
        MONO_ARCH_SAVE_REGS;
 
        n = mono_string_to_utf8 (name);
-       ne = bsearch (n, culture_name_entries, NUM_CULTURE_ENTRIES,
+       ne = mono_binary_search (n, culture_name_entries, NUM_CULTURE_ENTRIES,
                        sizeof (CultureInfoNameEntry), culture_name_locator);
 
        if (ne == NULL) {
@@ -549,7 +523,7 @@ ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (
 
        return construct_culture (this, &culture_entries [ne->culture_entry_index]);
 }
-
+/*
 MonoBoolean
 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
                MonoString *name)
@@ -565,7 +539,7 @@ ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specif
 
        return ret;
 }
-
+*/
 MonoBoolean
 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_lcid (MonoRegionInfo *this,
                gint lcid)
@@ -591,7 +565,7 @@ ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_name (M
        MONO_ARCH_SAVE_REGS;
 
        n = mono_string_to_utf8 (name);
-       ne = bsearch (n, region_name_entries, NUM_REGION_ENTRIES,
+       ne = mono_binary_search (n, region_name_entries, NUM_REGION_ENTRIES,
                sizeof (RegionInfoNameEntry), region_name_locator);
 
        if (ne == NULL) {