1 /* GStreamer Language Tag Utility Functions
2 * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
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.
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.
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.
21 * little program that reads iso_639.xml and outputs tables for us as fallback
22 * for when iso-codes are not available or we fail to read the file for some
23 * reason, and so we don't have to parse the xml file just to map codes.
33 #define ISO_639_XML_PATH ISO_CODES_PREFIX "/share/xml/iso-codes/iso_639.xml"
37 gchar code_1[3]; /* de */
38 gchar code_2t[4]; /* deu */
39 gchar code_2b[4]; /* ger */
40 const gchar *name; /* German */
41 guint name_offset; /* offset into string table */
44 static GArray *languages = NULL;
53 g_assert (languages != NULL);
55 names = g_string_new ("");
57 g_print ("/* generated by " __FILE__ " iso-codes " ISO_CODES_VERSION " */\n");
59 g_print ("#include <glib.h>\n");
61 g_print ("#define ISO_639_FLAG_2T (1 << 0)\n");
62 g_print ("#define ISO_639_FLAG_2B (1 << 1)\n");
64 g_print ("/* *INDENT-OFF* */\n");
66 g_print ("static const struct\n");
68 g_print (" const gchar iso_639_1[3];\n");
69 g_print (" const gchar iso_639_2[4];\n");
70 g_print (" guint8 flags;\n");
71 g_print (" guint16 name_offset;\n");
72 g_print ("} iso_639_codes[] = {\n");
74 for (i = 0, num_escaped = 0; i < languages->len; ++i) {
75 IsoLang *lang = &g_array_index (languages, IsoLang, i);
77 /* For now just print those where there's both a ISO-639-1 and -2 code */
78 if (lang->code_1[0] == '\0')
81 /* save current offset */
82 lang->name_offset = names->len;
84 /* adjust for fact that \000 is 4 chars now but will take up only 1 later */
85 lang->name_offset -= num_escaped * 3;
87 /* append one char at a time, making sure to escape UTF-8 characters */
88 for (s = lang->name; s != NULL && *s != '\0'; ++s) {
89 if (g_ascii_isprint (*s) && *s != '"' && *s != '\\') {
90 g_string_append_c (names, *s);
92 g_string_append_printf (names, "\\%03o", (unsigned char) *s);
96 g_string_append (names, "\\000");
99 g_print (" /* %s */\n", lang->name);
100 if (strcmp (lang->code_2b, lang->code_2t) == 0) {
101 g_print (" { \"%s\", \"%s\", ISO_639_FLAG_2T | ISO_639_FLAG_2B, %u },\n",
102 lang->code_1, lang->code_2t, lang->name_offset);
104 /* if 639-2T and 639-2B differ, put 639-2T first */
105 g_print (" { \"%s\", \"%s\", ISO_639_FLAG_2T, %u },\n",
106 lang->code_1, lang->code_2t, lang->name_offset);
107 g_print (" { \"%s\", \"%s\", ISO_639_FLAG_2B, %u },\n",
108 lang->code_1, lang->code_2b, lang->name_offset);
114 g_print ("static const gchar iso_639_names[] =\n");
116 while (s != NULL && *s != '\0') {
117 gchar line[74], *lastesc;
121 g_strlcpy (line, s, MIN (left, sizeof (line)));
122 s += sizeof (line) - 1;
123 /* avoid partial escaped codes at the end of a line */
124 if ((lastesc = strrchr (line, '\\')) && strlen (lastesc) < 4) {
125 s -= strlen (lastesc);
128 g_print (" \"%s\"", line);
135 g_print ("/* *INDENT-ON* */\n");
137 g_string_free (names, TRUE);
141 copy_attribute (gchar * dest, guint dest_len, const gchar ** attr_names,
142 const gchar ** attr_vals, const gchar * needle)
144 while (attr_names != NULL && *attr_names != NULL) {
145 if (strcmp (*attr_names, needle) == 0) {
146 g_strlcpy (dest, *attr_vals, dest_len);
157 xml_start_element (GMarkupParseContext * ctx, const gchar * element_name,
158 const gchar ** attr_names, const gchar ** attr_vals,
159 gpointer user_data, GError ** error)
164 if (strcmp (element_name, "iso_639_entry") != 0)
167 copy_attribute (lang.code_1, 3, attr_names, attr_vals, "iso_639_1_code");
168 copy_attribute (lang.code_2t, 4, attr_names, attr_vals, "iso_639_2T_code");
169 copy_attribute (lang.code_2b, 4, attr_names, attr_vals, "iso_639_2B_code");
171 copy_attribute (name, sizeof (name), attr_names, attr_vals, "name");
172 lang.name = g_intern_string (name);
174 g_array_append_val (languages, lang);
178 parse_iso_639_xml (const gchar * data, gsize len)
180 GMarkupParser xml_parser = { xml_start_element, NULL, NULL, NULL, NULL };
181 GMarkupParseContext *ctx;
184 g_return_if_fail (g_utf8_validate (data, len, NULL));
186 ctx = g_markup_parse_context_new (&xml_parser, 0, NULL, NULL);
187 if (!g_markup_parse_context_parse (ctx, data, len, &err))
188 g_error ("Parsing failed: %s", err->message);
190 g_markup_parse_context_free (ctx);
194 languages_sort_func (IsoLang * l1, IsoLang * l2)
199 if (l1->code_1[0] == '\0' && l2->code_1[0] != '\0')
202 return strcmp (l1->code_1, l2->code_1);
206 main (int argc, char **argv)
212 f = g_mapped_file_new (ISO_639_XML_PATH, FALSE, NULL);
214 xml_data = (gchar *) g_mapped_file_get_contents (f);
215 xml_len = g_mapped_file_get_length (f);
219 if (!g_file_get_contents (ISO_639_XML_PATH, &xml_data, &xml_len, &err))
220 g_error ("Could not read %s: %s", ISO_639_XML_PATH, err->message);
223 languages = g_array_new (FALSE, TRUE, sizeof (IsoLang));
225 parse_iso_639_xml (xml_data, xml_len);
227 g_array_sort (languages, (GCompareFunc) languages_sort_func);
231 g_array_free (languages, TRUE);
234 g_mapped_file_unref (f);