-G_LOCK_DEFINE_STATIC (aliases);
-
-static GHashTable *
-get_alias_hash (void)
-{
- static GHashTable *alias_hash = NULL;
- const char *aliases;
-
- G_LOCK (aliases);
-
- if (!alias_hash)
- {
- alias_hash = g_hash_table_new (g_str_hash, g_str_equal);
-
- aliases = _g_locale_get_charset_aliases ();
- while (*aliases != '\0')
- {
- const char *canonical;
- const char *alias;
- const char **alias_array;
- int count = 0;
-
- alias = aliases;
- aliases += strlen (aliases) + 1;
- canonical = aliases;
- aliases += strlen (aliases) + 1;
-
- alias_array = g_hash_table_lookup (alias_hash, canonical);
- if (alias_array)
- {
- while (alias_array[count])
- count++;
- }
-
- alias_array = g_renew (const char *, alias_array, count + 2);
- alias_array[count] = alias;
- alias_array[count + 1] = NULL;
-
- g_hash_table_insert (alias_hash, (char *)canonical, alias_array);
- }
- }
-
- G_UNLOCK (aliases);
-
- return alias_hash;
-}
-
-/* As an abuse of the alias table, the following routines gets
- * the charsets that are aliases for the canonical name.
- */
-const char **
-_g_charset_get_aliases (const char *canonical_name)
-{
- GHashTable *alias_hash = get_alias_hash ();
-
- return g_hash_table_lookup (alias_hash, canonical_name);
-}
-
-static gboolean
-g_utf8_get_charset_internal (const char **a)
-{
- const char *charset = getenv("CHARSET");
-
- if (charset && *charset)
- {
- *a = charset;
-
- if (charset && strstr (charset, "UTF-8"))
- return TRUE;
- else
- return FALSE;
- }
-
- /* The libcharset code tries to be thread-safe without
- * a lock, but has a memory leak and a missing memory
- * barrier, so we lock for it
- */
- G_LOCK (aliases);
- charset = _g_locale_charset ();
- G_UNLOCK (aliases);
-
- if (charset && *charset)
- {
- *a = charset;
-
- if (charset && strstr (charset, "UTF-8"))
- return TRUE;
- else
- return FALSE;
- }
-
- /* Assume this for compatibility at present. */
- *a = "US-ASCII";
-
- return FALSE;
-}
-
-static int utf8_locale_cache = -1;
-static const char *utf8_charset_cache = NULL;
-
-/**
- * g_get_charset:
- * @charset: return location for character set name
- *
- * Obtains the character set for the current locale; you might use
- * this character set as an argument to g_convert(), to convert from
- * the current locale's encoding to some other encoding. (Frequently
- * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
- * though.)
- *
- * The return value is %TRUE if the locale's encoding is UTF-8, in that
- * case you can perhaps avoid calling g_convert().
- *
- * The string returned in @charset is not allocated, and should not be
- * freed.
- *
- * Return value: %TRUE if the returned charset is UTF-8
- **/
-gboolean
-g_get_charset (G_CONST_RETURN char **charset)
-{
- if (utf8_locale_cache != -1)
- {
- if (charset)
- *charset = utf8_charset_cache;
- return utf8_locale_cache;
- }
- utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
- if (charset)
- *charset = utf8_charset_cache;
- return utf8_locale_cache;
-}
-