gio: Remove unused function
[platform/upstream/gstreamer.git] / gst-libs / gst / tag / lang.c
1 /* GStreamer language codes and names utility functions
2  * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:gsttaglanguagecodes
22  * @short_description: mappings for ISO-639 language codes and names
23  * @see_also: #GstTagList
24  *
25  * <refsect2>
26  * <para>
27  * Provides helper functions to convert between the various ISO-639 language
28  * codes, and to map language codes to language names.
29  * </para>
30  * </refsect2>
31  */
32
33 /* FIXME 0.11: maybe switch to ISO-639-2 everywhere incl. GST_TAG_LANGUAGE? */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #undef GETTEXT_PACKAGE
40 #define GETTEXT_PACKAGE "iso_639"
41
42 #define ISO_639_XML_PATH ISO_CODES_PREFIX "/share/xml/iso-codes/iso_639.xml"
43 #define ISO_CODES_LOCALEDIR ISO_CODES_PREFIX "/share/locale"
44
45 #include <gst/gst-i18n-plugin.h>
46 #include <gst/gst.h>
47 #include <string.h>
48 #include <stdlib.h>
49
50 #include "lang-tables.dat"
51
52 /* FIXME: remove once we depend on GLib >= 2.22 */
53 #if !GLIB_CHECK_VERSION (2, 22, 0)
54 #define g_mapped_file_unref g_mapped_file_free
55 #endif
56
57 #ifndef GST_DISABLE_GST_DEBUG
58
59 #define GST_CAT_DEFAULT ensure_debug_category()
60
61 static GstDebugCategory *
62 ensure_debug_category (void)
63 {
64   static gsize cat_gonce = 0;
65
66   if (g_once_init_enter (&cat_gonce)) {
67     gsize cat_done;
68
69     cat_done = (gsize) _gst_debug_category_new ("tag-langcodes", 0,
70         "GstTag language codes and names");
71
72     g_once_init_leave (&cat_gonce, cat_done);
73   }
74
75   return (GstDebugCategory *) cat_gonce;
76 }
77
78 #else
79
80 #define ensure_debug_category() /* NOOP */
81
82 #endif /* GST_DISABLE_GST_DEBUG */
83
84 /* ------------------------------------------------------------------------- */
85
86 /* Loading and initing */
87
88 #if defined(HAVE_ISO_CODES)
89 static const gchar *
90 get_val (const gchar ** names, const gchar ** vals, const gchar * name)
91 {
92   while (names != NULL && *names != NULL) {
93     if (strcmp (*names, name) == 0)
94       return *vals;
95     ++names;
96     ++vals;
97   }
98   return NULL;
99 }
100
101 static void
102 parse_start_element (GMarkupParseContext * ctx, const gchar * element_name,
103     const gchar ** attr_names, const gchar ** attr_vals,
104     gpointer user_data, GError ** error)
105 {
106   GHashTable *ht = (GHashTable *) user_data;
107   const gchar *c1, *c2t, *c2b, *name, *tname;
108
109   if (strcmp (element_name, "iso_639_entry") != 0)
110     return;
111
112   c1 = get_val (attr_names, attr_vals, "iso_639_1_code");
113
114   /* only interested in languages with an ISO 639-1 code for now */
115   if (c1 == NULL)
116     return;
117
118   c2t = get_val (attr_names, attr_vals, "iso_639_2T_code");
119   c2b = get_val (attr_names, attr_vals, "iso_639_2B_code");
120   name = get_val (attr_names, attr_vals, "name");
121
122   if (c2t == NULL || c2b == NULL || name == NULL) {
123     GST_WARNING ("broken iso_639.xml entry: c2t=%p, c2b=%p, name=%p", c2t,
124         c2b, name);
125     return;
126   }
127
128   /* translate language name */
129   tname = _(name);
130
131   /* if no translation was found, it will return the input string, which we
132    * we don't want to put into the hash table because it will be freed again */
133   if (G_UNLIKELY (tname == name))
134     tname = g_intern_string (name);
135
136   /* now overwrite default/fallback mappings with names in locale language */
137   g_hash_table_replace (ht, (gpointer) g_intern_string (c1), (gpointer) tname);
138   g_hash_table_replace (ht, (gpointer) g_intern_string (c2b), (gpointer) tname);
139   if (strcmp (c2t, c2b) != 0) {
140     g_hash_table_replace (ht, (gpointer) g_intern_string (c2t),
141         (gpointer) tname);
142   }
143
144   GST_LOG ("%s %s %s : %s - %s", c1, c2t, c2b, name, tname);
145 }
146
147 static void
148 gst_tag_load_iso_639_xml (GHashTable * ht)
149 {
150   GMappedFile *f;
151   GError *err = NULL;
152   gchar *xml_data;
153   gsize xml_len;
154
155 #ifdef ENABLE_NLS
156   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
157       ISO_CODES_LOCALEDIR);
158   bindtextdomain (GETTEXT_PACKAGE, ISO_CODES_LOCALEDIR);
159   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
160 #endif
161
162   f = g_mapped_file_new (ISO_639_XML_PATH, FALSE, NULL);
163   if (f != NULL) {
164     xml_data = (gchar *) g_mapped_file_get_contents (f);
165     xml_len = g_mapped_file_get_length (f);
166   } else {
167     if (!g_file_get_contents (ISO_639_XML_PATH, &xml_data, &xml_len, &err)) {
168       GST_WARNING ("Could not read %s: %s", ISO_639_XML_PATH, err->message);
169       g_error_free (err);
170       return;
171     }
172   }
173
174   if (g_utf8_validate (xml_data, xml_len, NULL)) {
175     GMarkupParser xml_parser = { parse_start_element, NULL, NULL, NULL, NULL };
176     GMarkupParseContext *ctx;
177
178     ctx = g_markup_parse_context_new (&xml_parser, 0, ht, NULL);
179     if (!g_markup_parse_context_parse (ctx, xml_data, xml_len, &err)) {
180       GST_WARNING ("Parsing iso_639.xml failed: %s", err->message);
181       g_error_free (err);
182     }
183     g_markup_parse_context_free (ctx);
184   } else {
185     GST_WARNING ("iso_639.xml file is not valid UTF-8");
186     GST_MEMDUMP ("iso_639.xml file", (guint8 *) xml_data, xml_len);
187   }
188
189   /* ... and clean up */
190   if (f != NULL)
191     g_mapped_file_unref (f);
192   else
193     g_free (xml_data);
194 }
195 #endif /* HAVE_ISO_CODES */
196
197 static GHashTable *
198 gst_tag_get_iso_639_ht (void)
199 {
200   static gsize once_val = 0;
201   int i;
202
203   if (g_once_init_enter (&once_val)) {
204     GHashTable *ht;
205     gsize done_val;
206
207     GST_MEMDUMP ("iso 639 language names (internal default/fallback)",
208         (guint8 *) iso_639_names, sizeof (iso_639_names));
209
210     /* maps code -> language name; all strings are either interned strings
211      * or const static strings from lang-table.c */
212     ht = g_hash_table_new (g_str_hash, g_str_equal);
213
214     /* set up default/fallback mappings */
215     for (i = 0; i < G_N_ELEMENTS (iso_639_codes); ++i) {
216       GST_LOG ("%3d %s %s %c%c 0x%04x  %s", i, iso_639_codes[i].iso_639_1,
217           iso_639_codes[i].iso_639_2,
218           ((iso_639_codes[i].flags & ISO_639_FLAG_2B)) ? 'B' : '.',
219           ((iso_639_codes[i].flags & ISO_639_FLAG_2T)) ? 'T' : '.',
220           iso_639_codes[i].name_offset,
221           iso_639_names + iso_639_codes[i].name_offset);
222
223 #ifdef HAVE_ISO_CODES
224       /* intern these in order to minimise allocations when interning strings
225        * read from the xml file later */
226       g_intern_static_string (iso_639_codes[i].iso_639_1);
227       g_intern_static_string (iso_639_codes[i].iso_639_2);
228       g_intern_static_string (iso_639_names + iso_639_codes[i].name_offset);
229 #endif
230
231       /* and add default mapping (these strings are always valid) */
232       g_hash_table_insert (ht, (gpointer) iso_639_codes[i].iso_639_1,
233           (gpointer) (iso_639_names + iso_639_codes[i].name_offset));
234       g_hash_table_insert (ht, (gpointer) iso_639_codes[i].iso_639_2,
235           (gpointer) (iso_639_names + iso_639_codes[i].name_offset));
236     }
237
238 #ifdef HAVE_ISO_CODES
239     {
240       GstClockTime ts = gst_util_get_timestamp ();
241
242       gst_tag_load_iso_639_xml (ht);
243
244       ts = gst_util_get_timestamp () - ts;
245       GST_INFO ("iso_639.xml loading took %.2gms", (double) ts / GST_MSECOND);
246     }
247 #else
248     GST_INFO ("iso-codes disabled or not available");
249 #endif
250
251     done_val = (gsize) ht;
252     g_once_init_leave (&once_val, done_val);
253   }
254
255   return (GHashTable *) once_val;
256 }
257
258 /* ------------------------------------------------------------------------- */
259
260 static int
261 qsort_strcmp_func (const void *p1, const void *p2)
262 {
263   return strcmp (*(char *const *) p1, *(char *const *) p2);
264 }
265
266 /**
267  * gst_tag_get_language_codes:
268  *
269  * Returns a list of known language codes (in form of two-letter ISO-639-1
270  * codes). This is useful for UIs to build a list of available languages for
271  * tagging purposes (e.g. to tag an audio track appropriately in a video or
272  * audio editor).
273  *
274  * Returns: NULL-terminated string array with two-letter language codes. Free
275  *     with g_strfreev() when no longer needed.
276  *
277  * Since: 0.10.26
278  */
279 gchar **
280 gst_tag_get_language_codes (void)
281 {
282   GHashTableIter iter;
283   GHashTable *ht;
284   gpointer key;
285   gchar **codes;
286   int i;
287
288   ensure_debug_category ();
289
290   ht = gst_tag_get_iso_639_ht ();
291
292   /* we have at least two keys for each language (-1 code and -2 code) */
293   codes = g_new (gchar *, (g_hash_table_size (ht) / 2) + 1);
294
295   i = 0;
296   g_hash_table_iter_init (&iter, ht);
297   while (g_hash_table_iter_next (&iter, &key, NULL)) {
298     const gchar *lang_code = key;
299
300     if (strlen (lang_code) == 2) {
301       codes[i] = g_strdup (lang_code);
302       ++i;
303     }
304   }
305   codes[i] = NULL;
306
307   /* be nice and sort the list */
308   qsort (&codes[0], i, sizeof (gchar *), qsort_strcmp_func);
309
310   return codes;
311 }
312
313 /**
314  * gst_tag_get_language_name:
315  * @language_code: two or three-letter ISO-639 language code
316  *
317  * Returns the name of the language given an ISO-639 language code, such
318  * as often found in a GST_TAG_LANGUAGE tag. The name will be translated
319  * according to the current locale (if the library was built against the
320  * iso-codes package, otherwise the English name will be returned).
321  *
322  * Language codes are case-sensitive and expected to be lower case.
323  *
324  * Returns: language name in UTF-8 format, or NULL if @language_code could
325  *     not be mapped to a language name. The returned string must not be
326  *     modified and does not need to freed; it will stay valid until the
327  *     application is terminated.
328  *
329  * Since: 0.10.26
330  */
331 const gchar *
332 gst_tag_get_language_name (const gchar * language_code)
333 {
334   const gchar *lang_name;
335   GHashTable *ht;
336
337   g_return_val_if_fail (language_code != NULL, NULL);
338
339   ensure_debug_category ();
340
341   ht = gst_tag_get_iso_639_ht ();
342
343   lang_name = g_hash_table_lookup (ht, (gpointer) language_code);
344   GST_LOG ("%s -> %s", language_code, GST_STR_NULL (lang_name));
345
346   return lang_name;
347 }
348
349 /**
350  * gst_tag_get_language_code_iso_639_1:
351  * @lang_code: ISO-639 language code (e.g. "deu" or "ger" or "de")
352  *
353  * Returns two-letter ISO-639-1 language code given a three-letter ISO-639-2
354  * language code or two-letter ISO-639-1 language code (both are accepted for
355  * convenience).
356  *
357  * Language codes are case-sensitive and expected to be lower case.
358  *
359  * Returns: two-letter ISO-639-1 language code string that maps to @lang_code,
360  *     or NULL if no mapping is known. The returned string must not be
361  *     modified or freed.
362  *
363  * Since: 0.10.26
364  */
365 const gchar *
366 gst_tag_get_language_code_iso_639_1 (const gchar * lang_code)
367 {
368   const gchar *c = NULL;
369   int i;
370
371   g_return_val_if_fail (lang_code != NULL, NULL);
372
373   ensure_debug_category ();
374
375   /* FIXME: we are being a bit inconsistent here in the sense that will only
376    * map the language codes from our static table. Theoretically the iso-codes
377    * XML file might have had additional codes that are now in the hash table.
378    * We keep it simple for now and don't waste memory on additional tables. */
379   for (i = 0; i < G_N_ELEMENTS (iso_639_codes); ++i) {
380     /* we check both codes here, so function can be used in a more versatile
381      * way, to convert a language tag to a two-letter language code and/or
382      * verify an existing code */
383     if (strcmp (lang_code, iso_639_codes[i].iso_639_1) == 0 ||
384         strcmp (lang_code, iso_639_codes[i].iso_639_2) == 0) {
385       c = iso_639_codes[i].iso_639_1;
386       break;
387     }
388   }
389
390   GST_LOG ("%s -> %s", lang_code, GST_STR_NULL (c));
391
392   return c;
393 }
394
395 static const gchar *
396 gst_tag_get_language_code_iso_639_2X (const gchar * lang_code, guint8 flags)
397 {
398   int i;
399
400   /* FIXME: we are being a bit inconsistent here in the sense that we will only
401    * map the language codes from our static table. Theoretically the iso-codes
402    * XML file might have had additional codes that are now in the hash table.
403    * We keep it simple for now and don't waste memory on additional tables.
404    * Also, we currently only parse the iso_639.xml file if language names or
405    * a list of all codes is requested, and it'd be nice to keep it like that. */
406   for (i = 0; i < G_N_ELEMENTS (iso_639_codes); ++i) {
407     /* we check both codes here, so function can be used in a more versatile
408      * way, to convert a language tag to a three-letter language code and/or
409      * verify an existing code */
410     if (strcmp (lang_code, iso_639_codes[i].iso_639_1) == 0 ||
411         strcmp (lang_code, iso_639_codes[i].iso_639_2) == 0) {
412       if ((iso_639_codes[i].flags & flags) == flags) {
413         return iso_639_codes[i].iso_639_2;
414       } else if (i > 0 && (iso_639_codes[i - 1].flags & flags) == flags &&
415           iso_639_codes[i].name_offset == iso_639_codes[i - 1].name_offset) {
416         return iso_639_codes[i - 1].iso_639_2;
417       } else if (i < G_N_ELEMENTS (iso_639_codes) &&
418           (iso_639_codes[i + 1].flags & flags) == flags &&
419           iso_639_codes[i].name_offset == iso_639_codes[i + 1].name_offset) {
420         return iso_639_codes[i + 1].iso_639_2;
421       }
422     }
423   }
424   return NULL;
425 }
426
427 /**
428  * gst_tag_get_language_code_iso_639_2T:
429  * @lang_code: ISO-639 language code (e.g. "deu" or "ger" or "de")
430  *
431  * Returns three-letter ISO-639-2 "terminological" language code given a
432  * two-letter ISO-639-1 language code or a three-letter ISO-639-2 language
433  * code (both are accepted for convenience).
434  *
435  * The "terminological" code is derived from the local name of the language
436  * (e.g. "deu" for German instead of "ger"). In most scenarios, the
437  * "terminological" codes are prefered over the "bibliographic" ones.
438  *
439  * Language codes are case-sensitive and expected to be lower case.
440  *
441  * Returns: three-letter ISO-639-2 language code string that maps to @lang_code,
442  *     or NULL if no mapping is known. The returned string must not be
443  *     modified or freed.
444  *
445  * Since: 0.10.26
446  */
447 const gchar *
448 gst_tag_get_language_code_iso_639_2T (const gchar * lang_code)
449 {
450   const gchar *c;
451
452   g_return_val_if_fail (lang_code != NULL, NULL);
453
454   ensure_debug_category ();
455
456   c = gst_tag_get_language_code_iso_639_2X (lang_code, ISO_639_FLAG_2T);
457
458   GST_LOG ("%s -> %s", lang_code, GST_STR_NULL (c));
459
460   return c;
461 }
462
463 /**
464  * gst_tag_get_language_code_iso_639_2B:
465  * @lang_code: ISO-639 language code (e.g. "deu" or "ger" or "de")
466  *
467  * Returns three-letter ISO-639-2 "bibliographic" language code given a
468  * two-letter ISO-639-1 language code or a three-letter ISO-639-2 language
469  * code (both are accepted for convenience).
470  *
471  * The "bibliographic" code is derived from the English name of the language
472  * (e.g. "ger" for German instead of "de" or "deu"). In most scenarios, the
473  * "terminological" codes are prefered.
474  *
475  * Language codes are case-sensitive and expected to be lower case.
476  *
477  * Returns: three-letter ISO-639-2 language code string that maps to @lang_code,
478  *     or NULL if no mapping is known. The returned string must not be
479  *     modified or freed.
480  *
481  * Since: 0.10.26
482  */
483 const gchar *
484 gst_tag_get_language_code_iso_639_2B (const gchar * lang_code)
485 {
486   const gchar *c;
487
488   g_return_val_if_fail (lang_code != NULL, NULL);
489
490   ensure_debug_category ();
491
492   c = gst_tag_get_language_code_iso_639_2X (lang_code, ISO_639_FLAG_2B);
493
494   GST_LOG ("%s -> %s", lang_code, GST_STR_NULL (c));
495
496   return c;
497 }