1 /* gcharset.c - Charset information
3 * Copyright (C) 2011 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "gcharsetprivate.h"
26 #include "gmessages.h"
27 #include "gstrfuncs.h"
33 #include "libcharset/libcharset.h"
38 G_LOCK_DEFINE_STATIC (aliases);
43 static GHashTable *alias_hash = NULL;
50 alias_hash = g_hash_table_new (g_str_hash, g_str_equal);
52 aliases = _g_locale_get_charset_aliases ();
53 while (*aliases != '\0')
55 const char *canonical;
57 const char **alias_array;
61 aliases += strlen (aliases) + 1;
63 aliases += strlen (aliases) + 1;
65 alias_array = g_hash_table_lookup (alias_hash, canonical);
68 while (alias_array[count])
72 alias_array = g_renew (const char *, alias_array, count + 2);
73 alias_array[count] = alias;
74 alias_array[count + 1] = NULL;
76 g_hash_table_insert (alias_hash, (char *)canonical, alias_array);
85 /* As an abuse of the alias table, the following routines gets
86 * the charsets that are aliases for the canonical name.
89 _g_charset_get_aliases (const char *canonical_name)
91 GHashTable *alias_hash = get_alias_hash ();
93 return g_hash_table_lookup (alias_hash, canonical_name);
97 g_utf8_get_charset_internal (const char *raw_data,
100 const char *charset = g_getenv ("CHARSET");
102 if (charset && *charset)
106 if (charset && strstr (charset, "UTF-8"))
112 /* The libcharset code tries to be thread-safe without
113 * a lock, but has a memory leak and a missing memory
114 * barrier, so we lock for it
117 charset = _g_locale_charset_unalias (raw_data);
120 if (charset && *charset)
124 if (charset && strstr (charset, "UTF-8"))
130 /* Assume this for compatibility at present. */
136 typedef struct _GCharsetCache GCharsetCache;
138 struct _GCharsetCache {
145 charset_cache_free (gpointer data)
147 GCharsetCache *cache = data;
149 g_free (cache->charset);
155 * @charset: return location for character set name
157 * Obtains the character set for the <link linkend="setlocale">current
158 * locale</link>; you might use this character set as an argument to
159 * g_convert(), to convert from the current locale's encoding to some
160 * other encoding. (Frequently g_locale_to_utf8() and g_locale_from_utf8()
161 * are nice shortcuts, though.)
163 * On Windows the character set returned by this function is the
164 * so-called system default ANSI code-page. That is the character set
165 * used by the "narrow" versions of C library and Win32 functions that
166 * handle file names. It might be different from the character set
167 * used by the C library's current locale.
169 * The return value is %TRUE if the locale's encoding is UTF-8, in that
170 * case you can perhaps avoid calling g_convert().
172 * The string returned in @charset is not allocated, and should not be
175 * Return value: %TRUE if the returned charset is UTF-8
178 g_get_charset (const char **charset)
180 static GPrivate cache_private = G_PRIVATE_INIT (charset_cache_free);
181 GCharsetCache *cache = g_private_get (&cache_private);
186 cache = g_new0 (GCharsetCache, 1);
187 g_private_set (&cache_private, cache);
191 raw = _g_locale_charset_raw ();
194 if (!(cache->raw && strcmp (cache->raw, raw) == 0))
196 const gchar *new_charset;
199 g_free (cache->charset);
200 cache->raw = g_strdup (raw);
201 cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset);
202 cache->charset = g_strdup (new_charset);
206 *charset = cache->charset;
208 return cache->is_utf8;
214 * Gets the character set for the current locale.
216 * Return value: a newly allocated string containing the name
217 * of the character set. This string must be freed with g_free().
222 const gchar *charset;
224 g_get_charset (&charset);
226 return g_strdup (charset);
231 static GHashTable *alias_table = NULL;
233 /* read an alias file for the locales */
235 read_aliases (gchar *file)
241 alias_table = g_hash_table_new (g_str_hash, g_str_equal);
242 fp = fopen (file,"r");
245 while (fgets (buf, 256, fp))
251 /* Line is a comment */
252 if ((buf[0] == '#') || (buf[0] == '\0'))
255 /* Reads first column */
256 for (p = buf, q = NULL; *p; p++) {
257 if ((*p == '\t') || (*p == ' ') || (*p == ':')) {
260 while ((*q == '\t') || (*q == ' ')) {
266 /* The line only had one column */
267 if (!q || *q == '\0')
270 /* Read second column */
271 for (p = q; *p; p++) {
272 if ((*p == '\t') || (*p == ' ')) {
278 /* Add to alias table if necessary */
279 if (!g_hash_table_lookup (alias_table, buf)) {
280 g_hash_table_insert (alias_table, g_strdup (buf), g_strdup (q));
289 unalias_lang (char *lang)
296 read_aliases ("/usr/share/locale/locale.alias");
299 while ((p = g_hash_table_lookup (alias_table, lang)) && (strcmp (p, lang) != 0))
304 static gboolean said_before = FALSE;
306 g_warning ("Too many alias levels for a locale, "
307 "may indicate a loop");
316 /* Mask for components of locale spec. The ordering here is from
317 * least significant to most significant
321 COMPONENT_CODESET = 1 << 0,
322 COMPONENT_TERRITORY = 1 << 1,
323 COMPONENT_MODIFIER = 1 << 2
326 /* Break an X/Open style locale specification into components
329 explode_locale (const gchar *locale,
335 const gchar *uscore_pos;
337 const gchar *dot_pos;
341 uscore_pos = strchr (locale, '_');
342 dot_pos = strchr (uscore_pos ? uscore_pos : locale, '.');
343 at_pos = strchr (dot_pos ? dot_pos : (uscore_pos ? uscore_pos : locale), '@');
347 mask |= COMPONENT_MODIFIER;
348 *modifier = g_strdup (at_pos);
351 at_pos = locale + strlen (locale);
355 mask |= COMPONENT_CODESET;
356 *codeset = g_strndup (dot_pos, at_pos - dot_pos);
363 mask |= COMPONENT_TERRITORY;
364 *territory = g_strndup (uscore_pos, dot_pos - uscore_pos);
367 uscore_pos = dot_pos;
369 *language = g_strndup (locale, uscore_pos - locale);
375 * Compute all interesting variants for a given locale name -
376 * by stripping off different components of the value.
378 * For simplicity, we assume that the locale is in
379 * X/Open format: language[_territory][.codeset][@modifier]
381 * TODO: Extend this to handle the CEN format (see the GNUlibc docs)
382 * as well. We could just copy the code from glibc wholesale
383 * but it is big, ugly, and complicated, so I'm reluctant
384 * to do so when this should handle 99% of the time...
387 append_locale_variants (GPtrArray *array,
390 gchar *language = NULL;
391 gchar *territory = NULL;
392 gchar *codeset = NULL;
393 gchar *modifier = NULL;
398 g_return_if_fail (locale != NULL);
400 mask = explode_locale (locale, &language, &territory, &codeset, &modifier);
402 /* Iterate through all possible combinations, from least attractive
403 * to most attractive.
405 for (j = 0; j <= mask; ++j)
409 if ((i & ~mask) == 0)
411 gchar *val = g_strconcat (language,
412 (i & COMPONENT_TERRITORY) ? territory : "",
413 (i & COMPONENT_CODESET) ? codeset : "",
414 (i & COMPONENT_MODIFIER) ? modifier : "",
416 g_ptr_array_add (array, val);
421 if (mask & COMPONENT_CODESET)
423 if (mask & COMPONENT_TERRITORY)
425 if (mask & COMPONENT_MODIFIER)
430 * g_get_locale_variants:
431 * @locale: a locale identifier
433 * Returns a list of derived variants of @locale, which can be used to
434 * e.g. construct locale-dependent filenames or search paths. The returned
435 * list is sorted from most desirable to least desirable.
436 * This function handles territory, charset and extra locale modifiers.
438 * For example, if @locale is "fr_BE", then the returned list
441 * If you need the list of variants for the current locale,
442 * use g_get_language_names().
444 * Returns: (transfer full) (array zero-terminated=1) (element-type utf8): a newly
445 * allocated array of newly allocated strings with the locale variants. Free with
451 g_get_locale_variants (const gchar *locale)
455 g_return_val_if_fail (locale != NULL, NULL);
457 array = g_ptr_array_sized_new (8);
458 append_locale_variants (array, locale);
459 g_ptr_array_add (array, NULL);
461 return (gchar **) g_ptr_array_free (array, FALSE);
464 /* The following is (partly) taken from the gettext package.
465 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. */
468 guess_category_value (const gchar *category_name)
472 /* The highest priority value is the 'LANGUAGE' environment
473 variable. This is a GNU extension. */
474 retval = g_getenv ("LANGUAGE");
475 if ((retval != NULL) && (retval[0] != '\0'))
478 /* 'LANGUAGE' is not set. So we have to proceed with the POSIX
479 methods of looking to 'LC_ALL', 'LC_xxx', and 'LANG'. On some
480 systems this can be done by the 'setlocale' function itself. */
482 /* Setting of LC_ALL overwrites all other. */
483 retval = g_getenv ("LC_ALL");
484 if ((retval != NULL) && (retval[0] != '\0'))
487 /* Next comes the name of the desired category. */
488 retval = g_getenv (category_name);
489 if ((retval != NULL) && (retval[0] != '\0'))
492 /* Last possibility is the LANG environment variable. */
493 retval = g_getenv ("LANG");
494 if ((retval != NULL) && (retval[0] != '\0'))
497 #ifdef G_PLATFORM_WIN32
498 /* g_win32_getlocale() first checks for LC_ALL, LC_MESSAGES and
499 * LANG, which we already did above. Oh well. The main point of
500 * calling g_win32_getlocale() is to get the thread's locale as used
501 * by Windows and the Microsoft C runtime (in the "English_United
502 * States" format) translated into the Unixish format.
505 char *locale = g_win32_getlocale ();
506 retval = g_intern_string (locale);
515 typedef struct _GLanguageNamesCache GLanguageNamesCache;
517 struct _GLanguageNamesCache {
519 gchar **language_names;
523 language_names_cache_free (gpointer data)
525 GLanguageNamesCache *cache = data;
526 g_free (cache->languages);
527 g_strfreev (cache->language_names);
532 * g_get_language_names:
534 * Computes a list of applicable locale names, which can be used to
535 * e.g. construct locale-dependent filenames or search paths. The returned
536 * list is sorted from most desirable to least desirable and always contains
537 * the default locale "C".
539 * For example, if LANGUAGE=de:en_US, then the returned list is
540 * "de", "en_US", "en", "C".
542 * This function consults the environment variables <envar>LANGUAGE</envar>,
543 * <envar>LC_ALL</envar>, <envar>LC_MESSAGES</envar> and <envar>LANG</envar>
544 * to find the list of locales specified by the user.
546 * Return value: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib
547 * that must not be modified or freed.
551 const gchar * const *
552 g_get_language_names (void)
554 static GPrivate cache_private = G_PRIVATE_INIT (language_names_cache_free);
555 GLanguageNamesCache *cache = g_private_get (&cache_private);
560 cache = g_new0 (GLanguageNamesCache, 1);
561 g_private_set (&cache_private, cache);
564 value = guess_category_value ("LC_MESSAGES");
568 if (!(cache->languages && strcmp (cache->languages, value) == 0))
573 g_free (cache->languages);
574 g_strfreev (cache->language_names);
575 cache->languages = g_strdup (value);
577 array = g_ptr_array_sized_new (8);
579 alist = g_strsplit (value, ":", 0);
580 for (a = alist; *a; a++)
581 append_locale_variants (array, unalias_lang (*a));
583 g_ptr_array_add (array, g_strdup ("C"));
584 g_ptr_array_add (array, NULL);
586 cache->language_names = (gchar **) g_ptr_array_free (array, FALSE);
589 return (const gchar * const *) cache->language_names;