/src/glib/glib/gcharset.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* gcharset.c - Charset information |
2 | | * |
3 | | * Copyright (C) 2011 Red Hat, Inc. |
4 | | * |
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.1 of the License, or (at your option) any later version. |
9 | | * |
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. |
14 | | * |
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/>. |
17 | | */ |
18 | | |
19 | | #include "config.h" |
20 | | |
21 | | #include "gcharset.h" |
22 | | #include "gcharsetprivate.h" |
23 | | |
24 | | #include "garray.h" |
25 | | #include "genviron.h" |
26 | | #include "ghash.h" |
27 | | #include "gmessages.h" |
28 | | #include "gstrfuncs.h" |
29 | | #include "gthread.h" |
30 | | #include "gthreadprivate.h" |
31 | | #ifdef G_OS_WIN32 |
32 | | #include "gwin32.h" |
33 | | #endif |
34 | | |
35 | | #include "libcharset/libcharset.h" |
36 | | |
37 | | #include <string.h> |
38 | | #include <stdio.h> |
39 | | #ifdef G_OS_WIN32 |
40 | | #define WIN32_LEAN_AND_MEAN |
41 | | #include <windows.h> |
42 | | #endif |
43 | | |
44 | | G_LOCK_DEFINE_STATIC (aliases); |
45 | | |
46 | | static GHashTable * |
47 | | get_alias_hash (void) |
48 | 0 | { |
49 | 0 | static GHashTable *alias_hash = NULL; |
50 | 0 | const char *aliases; |
51 | |
|
52 | 0 | G_LOCK (aliases); |
53 | |
|
54 | 0 | if (!alias_hash) |
55 | 0 | { |
56 | 0 | alias_hash = g_hash_table_new (g_str_hash, g_str_equal); |
57 | |
|
58 | 0 | aliases = _g_locale_get_charset_aliases (); |
59 | 0 | while (*aliases != '\0') |
60 | 0 | { |
61 | 0 | const char *canonical; |
62 | 0 | const char *alias; |
63 | 0 | const char **alias_array; |
64 | 0 | int count = 0; |
65 | |
|
66 | 0 | alias = aliases; |
67 | 0 | aliases += strlen (aliases) + 1; |
68 | 0 | canonical = aliases; |
69 | 0 | aliases += strlen (aliases) + 1; |
70 | |
|
71 | 0 | alias_array = g_hash_table_lookup (alias_hash, canonical); |
72 | 0 | if (alias_array) |
73 | 0 | { |
74 | 0 | while (alias_array[count]) |
75 | 0 | count++; |
76 | 0 | } |
77 | |
|
78 | 0 | alias_array = g_renew (const char *, alias_array, count + 2); |
79 | 0 | alias_array[count] = alias; |
80 | 0 | alias_array[count + 1] = NULL; |
81 | |
|
82 | 0 | g_hash_table_insert (alias_hash, (char *)canonical, alias_array); |
83 | 0 | } |
84 | 0 | } |
85 | |
|
86 | 0 | G_UNLOCK (aliases); |
87 | |
|
88 | 0 | return alias_hash; |
89 | 0 | } |
90 | | |
91 | | /* As an abuse of the alias table, the following routines gets |
92 | | * the charsets that are aliases for the canonical name. |
93 | | */ |
94 | | const char ** |
95 | | _g_charset_get_aliases (const char *canonical_name) |
96 | 0 | { |
97 | 0 | GHashTable *alias_hash = get_alias_hash (); |
98 | |
|
99 | 0 | return g_hash_table_lookup (alias_hash, canonical_name); |
100 | 0 | } |
101 | | |
102 | | static gboolean |
103 | | g_utf8_get_charset_internal (const char *raw_data, |
104 | | const char **a) |
105 | 9 | { |
106 | 9 | const char *charset = g_getenv ("CHARSET"); |
107 | | |
108 | 9 | if (charset && *charset) |
109 | 0 | { |
110 | 0 | *a = charset; |
111 | |
|
112 | 0 | if (charset && strstr (charset, "UTF-8")) |
113 | 0 | return TRUE; |
114 | 0 | else |
115 | 0 | return FALSE; |
116 | 0 | } |
117 | | |
118 | | /* The libcharset code tries to be thread-safe without |
119 | | * a lock, but has a memory leak and a missing memory |
120 | | * barrier, so we lock for it |
121 | | */ |
122 | 9 | G_LOCK (aliases); |
123 | 9 | charset = _g_locale_charset_unalias (raw_data); |
124 | 9 | G_UNLOCK (aliases); |
125 | | |
126 | 9 | if (charset && *charset) |
127 | 9 | { |
128 | 9 | *a = charset; |
129 | | |
130 | 9 | if (charset && strstr (charset, "UTF-8")) |
131 | 0 | return TRUE; |
132 | 9 | else |
133 | 9 | return FALSE; |
134 | 9 | } |
135 | | |
136 | | /* Assume this for compatibility at present. */ |
137 | 0 | *a = "US-ASCII"; |
138 | |
|
139 | 0 | return FALSE; |
140 | 9 | } |
141 | | |
142 | | typedef struct _GCharsetCache GCharsetCache; |
143 | | |
144 | | struct _GCharsetCache { |
145 | | gboolean is_utf8; |
146 | | gchar *raw; |
147 | | gchar *charset; |
148 | | }; |
149 | | |
150 | | static void |
151 | | charset_cache_free (gpointer data) |
152 | 0 | { |
153 | 0 | GCharsetCache *cache = data; |
154 | 0 | g_free (cache->raw); |
155 | 0 | g_free (cache->charset); |
156 | 0 | g_free (cache); |
157 | 0 | } |
158 | | |
159 | | /** |
160 | | * g_get_charset: |
161 | | * @charset: (out) (optional) (transfer none): return location for character set |
162 | | * name, or %NULL. |
163 | | * |
164 | | * Obtains the character set for the [current locale][setlocale]; you |
165 | | * might use this character set as an argument to g_convert(), to convert |
166 | | * from the current locale's encoding to some other encoding. (Frequently |
167 | | * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts, though.) |
168 | | * |
169 | | * On Windows the character set returned by this function is the |
170 | | * so-called system default ANSI code-page. That is the character set |
171 | | * used by the "narrow" versions of C library and Win32 functions that |
172 | | * handle file names. It might be different from the character set |
173 | | * used by the C library's current locale. |
174 | | * |
175 | | * On Linux, the character set is found by consulting nl_langinfo() if |
176 | | * available. If not, the environment variables `LC_ALL`, `LC_CTYPE`, `LANG` |
177 | | * and `CHARSET` are queried in order. |
178 | | * |
179 | | * The return value is %TRUE if the locale's encoding is UTF-8, in that |
180 | | * case you can perhaps avoid calling g_convert(). |
181 | | * |
182 | | * The string returned in @charset is not allocated, and should not be |
183 | | * freed. |
184 | | * |
185 | | * Returns: %TRUE if the returned charset is UTF-8 |
186 | | */ |
187 | | gboolean |
188 | | g_get_charset (const char **charset) |
189 | 34.6k | { |
190 | 34.6k | static GPrivate cache_private = G_PRIVATE_INIT (charset_cache_free); |
191 | 34.6k | GCharsetCache *cache = g_private_get (&cache_private); |
192 | 34.6k | const gchar *raw; |
193 | | |
194 | 34.6k | if (!cache) |
195 | 9 | cache = g_private_set_alloc0 (&cache_private, sizeof (GCharsetCache)); |
196 | | |
197 | 34.6k | G_LOCK (aliases); |
198 | 34.6k | raw = _g_locale_charset_raw (); |
199 | 34.6k | G_UNLOCK (aliases); |
200 | | |
201 | 34.6k | if (cache->raw == NULL || strcmp (cache->raw, raw) != 0) |
202 | 9 | { |
203 | 9 | const gchar *new_charset; |
204 | | |
205 | 9 | g_free (cache->raw); |
206 | 9 | g_free (cache->charset); |
207 | 9 | cache->raw = g_strdup (raw); |
208 | 9 | cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset); |
209 | 9 | cache->charset = g_strdup (new_charset); |
210 | 9 | } |
211 | | |
212 | 34.6k | if (charset) |
213 | 34.6k | *charset = cache->charset; |
214 | | |
215 | 34.6k | return cache->is_utf8; |
216 | 34.6k | } |
217 | | |
218 | | /** |
219 | | * g_get_codeset: |
220 | | * |
221 | | * Gets the character set for the current locale. |
222 | | * |
223 | | * Returns: a newly allocated string containing the name |
224 | | * of the character set. This string must be freed with g_free(). |
225 | | */ |
226 | | gchar * |
227 | | g_get_codeset (void) |
228 | 0 | { |
229 | 0 | const gchar *charset; |
230 | |
|
231 | 0 | g_get_charset (&charset); |
232 | |
|
233 | 0 | return g_strdup (charset); |
234 | 0 | } |
235 | | |
236 | | /** |
237 | | * g_get_console_charset: |
238 | | * @charset: (out) (optional) (transfer none): return location for character set |
239 | | * name, or %NULL. |
240 | | * |
241 | | * Obtains the character set used by the console attached to the process, |
242 | | * which is suitable for printing output to the terminal. |
243 | | * |
244 | | * Usually this matches the result returned by g_get_charset(), but in |
245 | | * environments where the locale's character set does not match the encoding |
246 | | * of the console this function tries to guess a more suitable value instead. |
247 | | * |
248 | | * On Windows the character set returned by this function is the |
249 | | * output code page used by the console associated with the calling process. |
250 | | * If the codepage can't be determined (for example because there is no |
251 | | * console attached) UTF-8 is assumed. |
252 | | * |
253 | | * The return value is %TRUE if the locale's encoding is UTF-8, in that |
254 | | * case you can perhaps avoid calling g_convert(). |
255 | | * |
256 | | * The string returned in @charset is not allocated, and should not be |
257 | | * freed. |
258 | | * |
259 | | * Returns: %TRUE if the returned charset is UTF-8 |
260 | | * |
261 | | * Since: 2.62 |
262 | | */ |
263 | | gboolean |
264 | | g_get_console_charset (const char **charset) |
265 | 34.6k | { |
266 | | #ifdef G_OS_WIN32 |
267 | | static GPrivate cache_private = G_PRIVATE_INIT (charset_cache_free); |
268 | | GCharsetCache *cache = g_private_get (&cache_private); |
269 | | const gchar *locale; |
270 | | unsigned int cp; |
271 | | char buf[2 + 20 + 1]; /* "CP" + G_MAXUINT64 (to be safe) in decimal form (20 bytes) + "\0" */ |
272 | | const gchar *raw = NULL; |
273 | | |
274 | | if (!cache) |
275 | | cache = g_private_set_alloc0 (&cache_private, sizeof (GCharsetCache)); |
276 | | |
277 | | /* first try to query $LANG (works for Cygwin/MSYS/MSYS2 and others using mintty) */ |
278 | | locale = g_getenv ("LANG"); |
279 | | if (locale != NULL && locale[0] != '\0') |
280 | | { |
281 | | /* If the locale name contains an encoding after the dot, return it. */ |
282 | | const char *dot = strchr (locale, '.'); |
283 | | |
284 | | if (dot != NULL) |
285 | | { |
286 | | const char *modifier; |
287 | | |
288 | | dot++; |
289 | | /* Look for the possible @... trailer and remove it, if any. */ |
290 | | modifier = strchr (dot, '@'); |
291 | | if (modifier == NULL) |
292 | | raw = dot; |
293 | | else if (modifier - dot < sizeof (buf)) |
294 | | { |
295 | | memcpy (buf, dot, modifier - dot); |
296 | | buf[modifier - dot] = '\0'; |
297 | | raw = buf; |
298 | | } |
299 | | } |
300 | | } |
301 | | /* next try querying console codepage using native win32 API */ |
302 | | if (raw == NULL) |
303 | | { |
304 | | cp = GetConsoleOutputCP (); |
305 | | if (cp) |
306 | | { |
307 | | sprintf (buf, "CP%u", cp); |
308 | | raw = buf; |
309 | | } |
310 | | else if (GetLastError () != ERROR_INVALID_HANDLE) |
311 | | { |
312 | | gchar *emsg = g_win32_error_message (GetLastError ()); |
313 | | g_warning ("Failed to determine console output code page: %s. " |
314 | | "Falling back to UTF-8", emsg); |
315 | | g_free (emsg); |
316 | | } |
317 | | } |
318 | | /* fall-back to UTF-8 if the rest failed (it's a universal default) */ |
319 | | if (raw == NULL) |
320 | | raw = "UTF-8"; |
321 | | |
322 | | if (cache->raw == NULL || strcmp (cache->raw, raw) != 0) |
323 | | { |
324 | | const gchar *new_charset; |
325 | | |
326 | | g_free (cache->raw); |
327 | | g_free (cache->charset); |
328 | | cache->raw = g_strdup (raw); |
329 | | cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset); |
330 | | cache->charset = g_strdup (new_charset); |
331 | | } |
332 | | |
333 | | if (charset) |
334 | | *charset = cache->charset; |
335 | | |
336 | | return cache->is_utf8; |
337 | | #else |
338 | | /* assume the locale settings match the console encoding on non-Windows OSs */ |
339 | 34.6k | return g_get_charset (charset); |
340 | 34.6k | #endif |
341 | 34.6k | } |
342 | | |
343 | | #ifndef G_OS_WIN32 |
344 | | |
345 | | /* read an alias file for the locales */ |
346 | | static void |
347 | | read_aliases (const gchar *file, |
348 | | GHashTable *alias_table) |
349 | 0 | { |
350 | 0 | FILE *fp; |
351 | 0 | char buf[256]; |
352 | |
|
353 | 0 | fp = fopen (file,"r"); |
354 | 0 | if (!fp) |
355 | 0 | return; |
356 | 0 | while (fgets (buf, 256, fp)) |
357 | 0 | { |
358 | 0 | char *p, *q; |
359 | |
|
360 | 0 | g_strstrip (buf); |
361 | | |
362 | | /* Line is a comment */ |
363 | 0 | if ((buf[0] == '#') || (buf[0] == '\0')) |
364 | 0 | continue; |
365 | | |
366 | | /* Reads first column */ |
367 | 0 | for (p = buf, q = NULL; *p; p++) { |
368 | 0 | if ((*p == '\t') || (*p == ' ') || (*p == ':')) { |
369 | 0 | *p = '\0'; |
370 | 0 | q = p+1; |
371 | 0 | while ((*q == '\t') || (*q == ' ')) { |
372 | 0 | q++; |
373 | 0 | } |
374 | 0 | break; |
375 | 0 | } |
376 | 0 | } |
377 | | /* The line only had one column */ |
378 | 0 | if (!q || *q == '\0') |
379 | 0 | continue; |
380 | | |
381 | | /* Read second column */ |
382 | 0 | for (p = q; *p; p++) { |
383 | 0 | if ((*p == '\t') || (*p == ' ')) { |
384 | 0 | *p = '\0'; |
385 | 0 | break; |
386 | 0 | } |
387 | 0 | } |
388 | | |
389 | | /* Add to alias table if necessary */ |
390 | 0 | if (!g_hash_table_lookup (alias_table, buf)) { |
391 | 0 | g_hash_table_insert (alias_table, g_strdup (buf), g_strdup (q)); |
392 | 0 | } |
393 | 0 | } |
394 | 0 | fclose (fp); |
395 | 0 | } |
396 | | |
397 | | #endif |
398 | | |
399 | | static char * |
400 | | unalias_lang (char *lang) |
401 | 0 | { |
402 | 0 | #ifndef G_OS_WIN32 |
403 | 0 | static GHashTable *alias_table = NULL; |
404 | 0 | char *p; |
405 | 0 | int i; |
406 | |
|
407 | 0 | if (g_once_init_enter (&alias_table)) |
408 | 0 | { |
409 | 0 | GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal); |
410 | 0 | read_aliases ("/usr/share/locale/locale.alias", table); |
411 | 0 | g_once_init_leave (&alias_table, table); |
412 | 0 | } |
413 | |
|
414 | 0 | i = 0; |
415 | 0 | while ((p = g_hash_table_lookup (alias_table, lang)) && (strcmp (p, lang) != 0)) |
416 | 0 | { |
417 | 0 | lang = p; |
418 | 0 | if (i++ == 30) |
419 | 0 | { |
420 | 0 | static gboolean said_before = FALSE; |
421 | 0 | if (!said_before) |
422 | 0 | g_warning ("Too many alias levels for a locale, " |
423 | 0 | "may indicate a loop"); |
424 | 0 | said_before = TRUE; |
425 | 0 | return lang; |
426 | 0 | } |
427 | 0 | } |
428 | 0 | #endif |
429 | 0 | return lang; |
430 | 0 | } |
431 | | |
432 | | /* Mask for components of locale spec. The ordering here is from |
433 | | * least significant to most significant |
434 | | */ |
435 | | enum |
436 | | { |
437 | | COMPONENT_CODESET = 1 << 0, |
438 | | COMPONENT_TERRITORY = 1 << 1, |
439 | | COMPONENT_MODIFIER = 1 << 2 |
440 | | }; |
441 | | |
442 | | /* Break an X/Open style locale specification into components |
443 | | */ |
444 | | static guint |
445 | | explode_locale (const gchar *locale, |
446 | | gchar **language, |
447 | | gchar **territory, |
448 | | gchar **codeset, |
449 | | gchar **modifier) |
450 | 0 | { |
451 | 0 | const gchar *uscore_pos; |
452 | 0 | const gchar *at_pos; |
453 | 0 | const gchar *dot_pos; |
454 | |
|
455 | 0 | guint mask = 0; |
456 | |
|
457 | 0 | uscore_pos = strchr (locale, '_'); |
458 | 0 | dot_pos = strchr (uscore_pos ? uscore_pos : locale, '.'); |
459 | 0 | at_pos = strchr (dot_pos ? dot_pos : (uscore_pos ? uscore_pos : locale), '@'); |
460 | |
|
461 | 0 | if (at_pos) |
462 | 0 | { |
463 | 0 | mask |= COMPONENT_MODIFIER; |
464 | 0 | *modifier = g_strdup (at_pos); |
465 | 0 | } |
466 | 0 | else |
467 | 0 | at_pos = locale + strlen (locale); |
468 | |
|
469 | 0 | if (dot_pos) |
470 | 0 | { |
471 | 0 | mask |= COMPONENT_CODESET; |
472 | 0 | *codeset = g_strndup (dot_pos, at_pos - dot_pos); |
473 | 0 | } |
474 | 0 | else |
475 | 0 | dot_pos = at_pos; |
476 | |
|
477 | 0 | if (uscore_pos) |
478 | 0 | { |
479 | 0 | mask |= COMPONENT_TERRITORY; |
480 | 0 | *territory = g_strndup (uscore_pos, dot_pos - uscore_pos); |
481 | 0 | } |
482 | 0 | else |
483 | 0 | uscore_pos = dot_pos; |
484 | |
|
485 | 0 | *language = g_strndup (locale, uscore_pos - locale); |
486 | |
|
487 | 0 | return mask; |
488 | 0 | } |
489 | | |
490 | | /* |
491 | | * Compute all interesting variants for a given locale name - |
492 | | * by stripping off different components of the value. |
493 | | * |
494 | | * For simplicity, we assume that the locale is in |
495 | | * X/Open format: language[_territory][.codeset][@modifier] |
496 | | * |
497 | | * TODO: Extend this to handle the CEN format (see the GNUlibc docs) |
498 | | * as well. We could just copy the code from glibc wholesale |
499 | | * but it is big, ugly, and complicated, so I'm reluctant |
500 | | * to do so when this should handle 99% of the time... |
501 | | */ |
502 | | static void |
503 | | append_locale_variants (GPtrArray *array, |
504 | | const gchar *locale) |
505 | 0 | { |
506 | 0 | gchar *language = NULL; |
507 | 0 | gchar *territory = NULL; |
508 | 0 | gchar *codeset = NULL; |
509 | 0 | gchar *modifier = NULL; |
510 | |
|
511 | 0 | guint mask; |
512 | 0 | guint i, j; |
513 | |
|
514 | 0 | g_return_if_fail (locale != NULL); |
515 | | |
516 | 0 | mask = explode_locale (locale, &language, &territory, &codeset, &modifier); |
517 | | |
518 | | /* Iterate through all possible combinations, from least attractive |
519 | | * to most attractive. |
520 | | */ |
521 | 0 | for (j = 0; j <= mask; ++j) |
522 | 0 | { |
523 | 0 | i = mask - j; |
524 | |
|
525 | 0 | if ((i & ~mask) == 0) |
526 | 0 | { |
527 | 0 | gchar *val = g_strconcat (language, |
528 | 0 | (i & COMPONENT_TERRITORY) ? territory : "", |
529 | 0 | (i & COMPONENT_CODESET) ? codeset : "", |
530 | 0 | (i & COMPONENT_MODIFIER) ? modifier : "", |
531 | 0 | NULL); |
532 | 0 | g_ptr_array_add (array, val); |
533 | 0 | } |
534 | 0 | } |
535 | |
|
536 | 0 | g_free (language); |
537 | 0 | if (mask & COMPONENT_CODESET) |
538 | 0 | g_free (codeset); |
539 | 0 | if (mask & COMPONENT_TERRITORY) |
540 | 0 | g_free (territory); |
541 | 0 | if (mask & COMPONENT_MODIFIER) |
542 | 0 | g_free (modifier); |
543 | 0 | } |
544 | | |
545 | | /** |
546 | | * g_get_locale_variants: |
547 | | * @locale: a locale identifier |
548 | | * |
549 | | * Returns a list of derived variants of @locale, which can be used to |
550 | | * e.g. construct locale-dependent filenames or search paths. The returned |
551 | | * list is sorted from most desirable to least desirable. |
552 | | * This function handles territory, charset and extra locale modifiers. See |
553 | | * [`setlocale(3)`](man:setlocale) for information about locales and their format. |
554 | | * |
555 | | * @locale itself is guaranteed to be returned in the output. |
556 | | * |
557 | | * For example, if @locale is `fr_BE`, then the returned list |
558 | | * is `fr_BE`, `fr`. If @locale is `en_GB.UTF-8@euro`, then the returned list |
559 | | * is `en_GB.UTF-8@euro`, `en_GB.UTF-8`, `en_GB@euro`, `en_GB`, `en.UTF-8@euro`, |
560 | | * `en.UTF-8`, `en@euro`, `en`. |
561 | | * |
562 | | * If you need the list of variants for the current locale, |
563 | | * use g_get_language_names(). |
564 | | * |
565 | | * Returns: (transfer full) (array zero-terminated=1) (element-type utf8): a newly |
566 | | * allocated array of newly allocated strings with the locale variants. Free with |
567 | | * g_strfreev(). |
568 | | * |
569 | | * Since: 2.28 |
570 | | */ |
571 | | gchar ** |
572 | | g_get_locale_variants (const gchar *locale) |
573 | 0 | { |
574 | 0 | GPtrArray *array; |
575 | |
|
576 | 0 | g_return_val_if_fail (locale != NULL, NULL); |
577 | | |
578 | 0 | array = g_ptr_array_sized_new (8); |
579 | 0 | append_locale_variants (array, locale); |
580 | 0 | g_ptr_array_add (array, NULL); |
581 | |
|
582 | 0 | return (gchar **) g_ptr_array_free (array, FALSE); |
583 | 0 | } |
584 | | |
585 | | /* The following is (partly) taken from the gettext package. |
586 | | Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. */ |
587 | | |
588 | | static const gchar * |
589 | | guess_category_value (const gchar *category_name) |
590 | 0 | { |
591 | 0 | const gchar *retval; |
592 | | |
593 | | /* The highest priority value is the 'LANGUAGE' environment |
594 | | variable. This is a GNU extension. */ |
595 | 0 | retval = g_getenv ("LANGUAGE"); |
596 | 0 | if ((retval != NULL) && (retval[0] != '\0')) |
597 | 0 | return retval; |
598 | | |
599 | | /* 'LANGUAGE' is not set. So we have to proceed with the POSIX |
600 | | methods of looking to 'LC_ALL', 'LC_xxx', and 'LANG'. On some |
601 | | systems this can be done by the 'setlocale' function itself. */ |
602 | | |
603 | | /* Setting of LC_ALL overwrites all other. */ |
604 | 0 | retval = g_getenv ("LC_ALL"); |
605 | 0 | if ((retval != NULL) && (retval[0] != '\0')) |
606 | 0 | return retval; |
607 | | |
608 | | /* Next comes the name of the desired category. */ |
609 | 0 | retval = g_getenv (category_name); |
610 | 0 | if ((retval != NULL) && (retval[0] != '\0')) |
611 | 0 | return retval; |
612 | | |
613 | | /* Last possibility is the LANG environment variable. */ |
614 | 0 | retval = g_getenv ("LANG"); |
615 | 0 | if ((retval != NULL) && (retval[0] != '\0')) |
616 | 0 | return retval; |
617 | | |
618 | | #ifdef G_PLATFORM_WIN32 |
619 | | /* g_win32_getlocale() first checks for LC_ALL, LC_MESSAGES and |
620 | | * LANG, which we already did above. Oh well. The main point of |
621 | | * calling g_win32_getlocale() is to get the thread's locale as used |
622 | | * by Windows and the Microsoft C runtime (in the "English_United |
623 | | * States" format) translated into the Unixish format. |
624 | | */ |
625 | | { |
626 | | char *locale = g_win32_getlocale (); |
627 | | retval = g_intern_string (locale); |
628 | | g_free (locale); |
629 | | return retval; |
630 | | } |
631 | | #endif |
632 | | |
633 | 0 | return NULL; |
634 | 0 | } |
635 | | |
636 | | typedef struct _GLanguageNamesCache GLanguageNamesCache; |
637 | | |
638 | | struct _GLanguageNamesCache { |
639 | | gchar *languages; |
640 | | gchar **language_names; |
641 | | }; |
642 | | |
643 | | static void |
644 | | language_names_cache_free (gpointer data) |
645 | 0 | { |
646 | 0 | GLanguageNamesCache *cache = data; |
647 | 0 | g_free (cache->languages); |
648 | 0 | g_strfreev (cache->language_names); |
649 | 0 | g_free (cache); |
650 | 0 | } |
651 | | |
652 | | /** |
653 | | * g_get_language_names: |
654 | | * |
655 | | * Computes a list of applicable locale names, which can be used to |
656 | | * e.g. construct locale-dependent filenames or search paths. The returned |
657 | | * list is sorted from most desirable to least desirable and always contains |
658 | | * the default locale "C". |
659 | | * |
660 | | * For example, if LANGUAGE=de:en_US, then the returned list is |
661 | | * "de", "en_US", "en", "C". |
662 | | * |
663 | | * This function consults the environment variables `LANGUAGE`, `LC_ALL`, |
664 | | * `LC_MESSAGES` and `LANG` to find the list of locales specified by the |
665 | | * user. |
666 | | * |
667 | | * Returns: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib |
668 | | * that must not be modified or freed. |
669 | | * |
670 | | * Since: 2.6 |
671 | | */ |
672 | | const gchar * const * |
673 | | g_get_language_names (void) |
674 | 0 | { |
675 | 0 | return g_get_language_names_with_category ("LC_MESSAGES"); |
676 | 0 | } |
677 | | |
678 | | /** |
679 | | * g_get_language_names_with_category: |
680 | | * @category_name: a locale category name |
681 | | * |
682 | | * Computes a list of applicable locale names with a locale category name, |
683 | | * which can be used to construct the fallback locale-dependent filenames |
684 | | * or search paths. The returned list is sorted from most desirable to |
685 | | * least desirable and always contains the default locale "C". |
686 | | * |
687 | | * This function consults the environment variables `LANGUAGE`, `LC_ALL`, |
688 | | * @category_name, and `LANG` to find the list of locales specified by the |
689 | | * user. |
690 | | * |
691 | | * g_get_language_names() returns g_get_language_names_with_category("LC_MESSAGES"). |
692 | | * |
693 | | * Returns: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by |
694 | | * the thread g_get_language_names_with_category was called from. |
695 | | * It must not be modified or freed. It must be copied if planned to be used in another thread. |
696 | | * |
697 | | * Since: 2.58 |
698 | | */ |
699 | | const gchar * const * |
700 | | g_get_language_names_with_category (const gchar *category_name) |
701 | 0 | { |
702 | 0 | static GPrivate cache_private = G_PRIVATE_INIT ((void (*)(gpointer)) g_hash_table_unref); |
703 | 0 | GHashTable *cache = g_private_get (&cache_private); |
704 | 0 | const gchar *languages; |
705 | 0 | GLanguageNamesCache *name_cache; |
706 | |
|
707 | 0 | g_return_val_if_fail (category_name != NULL, NULL); |
708 | | |
709 | 0 | if (!cache) |
710 | 0 | { |
711 | 0 | cache = g_hash_table_new_full (g_str_hash, g_str_equal, |
712 | 0 | g_free, language_names_cache_free); |
713 | 0 | g_private_set (&cache_private, cache); |
714 | 0 | } |
715 | |
|
716 | 0 | languages = guess_category_value (category_name); |
717 | 0 | if (!languages) |
718 | 0 | languages = "C"; |
719 | |
|
720 | 0 | name_cache = (GLanguageNamesCache *) g_hash_table_lookup (cache, category_name); |
721 | 0 | if (!(name_cache && name_cache->languages && |
722 | 0 | strcmp (name_cache->languages, languages) == 0)) |
723 | 0 | { |
724 | 0 | GPtrArray *array; |
725 | 0 | gchar **alist, **a; |
726 | |
|
727 | 0 | g_hash_table_remove (cache, category_name); |
728 | |
|
729 | 0 | array = g_ptr_array_sized_new (8); |
730 | |
|
731 | 0 | alist = g_strsplit (languages, ":", 0); |
732 | 0 | for (a = alist; *a; a++) |
733 | 0 | append_locale_variants (array, unalias_lang (*a)); |
734 | 0 | g_strfreev (alist); |
735 | 0 | g_ptr_array_add (array, g_strdup ("C")); |
736 | 0 | g_ptr_array_add (array, NULL); |
737 | |
|
738 | 0 | name_cache = g_new0 (GLanguageNamesCache, 1); |
739 | 0 | name_cache->languages = g_strdup (languages); |
740 | 0 | name_cache->language_names = (gchar **) g_ptr_array_free (array, FALSE); |
741 | 0 | g_hash_table_insert (cache, g_strdup (category_name), name_cache); |
742 | 0 | } |
743 | |
|
744 | 0 | return (const gchar * const *) name_cache->language_names; |
745 | 0 | } |