1 /* This file is generated, do not edit */
6 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
7 * GObject introspection: Dump introspection data
9 * Copyright (C) 2008 Colin Walters <walters@verbum.org>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
30 #include <glib-object.h>
33 /* This file is both compiled into libgirepository.so, and installed
34 * on the filesystem. But for the dumper, we want to avoid linking
35 * to libgirepository; see
36 * https://bugzilla.gnome.org/show_bug.cgi?id=630342
38 #ifdef G_IREPOSITORY_COMPILATION
40 #include "girepository.h"
46 escaped_printf (GOutputStream *out, const char *fmt, ...)
55 str = g_markup_vprintf_escaped (fmt, args);
56 if (!g_output_stream_write_all (out, str, strlen (str), &written, NULL, &error))
58 g_critical ("failed to write to iochannel: %s", error->message);
59 g_clear_error (&error);
67 goutput_write (GOutputStream *out, const char *str)
71 if (!g_output_stream_write_all (out, str, strlen (str), &written, NULL, &error))
73 g_critical ("failed to write to iochannel: %s", error->message);
74 g_clear_error (&error);
78 typedef GType (*GetTypeFunc)(void);
79 typedef GQuark (*ErrorQuarkFunc)(void);
82 invoke_get_type (GModule *self, const char *symbol, GError **error)
87 if (!g_module_symbol (self, symbol, (void**)&sym))
92 "Failed to find symbol '%s'", symbol);
93 return G_TYPE_INVALID;
97 if (ret == G_TYPE_INVALID)
102 "Function '%s' returned G_TYPE_INVALID", symbol);
108 invoke_error_quark (GModule *self, const char *symbol, GError **error)
112 if (!g_module_symbol (self, symbol, (void**)&sym))
117 "Failed to find symbol '%s'", symbol);
118 return G_TYPE_INVALID;
125 dump_properties (GType type, GOutputStream *out)
131 if (G_TYPE_FUNDAMENTAL (type) == G_TYPE_OBJECT)
134 klass = g_type_class_ref (type);
135 props = g_object_class_list_properties (klass, &n_properties);
140 klass = g_type_default_interface_ref (type);
141 props = g_object_interface_list_properties (klass, &n_properties);
144 for (i = 0; i < n_properties; i++)
149 if (prop->owner_type != type)
152 escaped_printf (out, " <property name=\"%s\" type=\"%s\" flags=\"%d\"/>\n",
153 prop->name, g_type_name (prop->value_type), prop->flags);
159 dump_signals (GType type, GOutputStream *out)
165 sig_ids = g_signal_list_ids (type, &n_sigs);
166 for (i = 0; i < n_sigs; i++)
173 g_signal_query (sigid, &query);
175 escaped_printf (out, " <signal name=\"%s\" return=\"%s\"",
176 query.signal_name, g_type_name (query.return_type));
178 if (query.signal_flags & G_SIGNAL_RUN_FIRST)
179 escaped_printf (out, " when=\"first\"");
180 else if (query.signal_flags & G_SIGNAL_RUN_LAST)
181 escaped_printf (out, " when=\"last\"");
182 else if (query.signal_flags & G_SIGNAL_RUN_CLEANUP)
183 escaped_printf (out, " when=\"cleanup\"");
184 #if GLIB_CHECK_VERSION(2, 29, 15)
185 else if (query.signal_flags & G_SIGNAL_MUST_COLLECT)
186 escaped_printf (out, " when=\"must-collect\"");
188 if (query.signal_flags & G_SIGNAL_NO_RECURSE)
189 escaped_printf (out, " no-recurse=\"1\"");
191 if (query.signal_flags & G_SIGNAL_DETAILED)
192 escaped_printf (out, " detailed=\"1\"");
194 if (query.signal_flags & G_SIGNAL_ACTION)
195 escaped_printf (out, " action=\"1\"");
197 if (query.signal_flags & G_SIGNAL_NO_HOOKS)
198 escaped_printf (out, " no-hooks=\"1\"");
200 goutput_write (out, ">\n");
202 for (j = 0; j < query.n_params; j++)
204 escaped_printf (out, " <param type=\"%s\"/>\n",
205 g_type_name (query.param_types[j]));
207 goutput_write (out, " </signal>\n");
212 dump_object_type (GType type, const char *symbol, GOutputStream *out)
218 escaped_printf (out, " <class name=\"%s\" get-type=\"%s\"",
219 g_type_name (type), symbol);
220 if (type != G_TYPE_OBJECT)
224 gboolean first = TRUE;
226 parent = g_type_parent (type);
227 parent_str = g_string_new ("");
228 while (parent != G_TYPE_INVALID)
233 g_string_append_c (parent_str, ',');
234 g_string_append (parent_str, g_type_name (parent));
235 parent = g_type_parent (parent);
238 escaped_printf (out, " parents=\"%s\"", parent_str->str);
240 g_string_free (parent_str, TRUE);
243 if (G_TYPE_IS_ABSTRACT (type))
244 escaped_printf (out, " abstract=\"1\"");
245 goutput_write (out, ">\n");
247 interfaces = g_type_interfaces (type, &n_interfaces);
248 for (i = 0; i < n_interfaces; i++)
250 GType itype = interfaces[i];
251 escaped_printf (out, " <implements name=\"%s\"/>\n",
252 g_type_name (itype));
254 dump_properties (type, out);
255 dump_signals (type, out);
256 goutput_write (out, " </class>\n");
260 dump_interface_type (GType type, const char *symbol, GOutputStream *out)
266 escaped_printf (out, " <interface name=\"%s\" get-type=\"%s\">\n",
267 g_type_name (type), symbol);
269 interfaces = g_type_interface_prerequisites (type, &n_interfaces);
270 for (i = 0; i < n_interfaces; i++)
272 GType itype = interfaces[i];
273 if (itype == G_TYPE_OBJECT)
275 /* Treat this as implicit for now; in theory GInterfaces are
276 * supported on things like GstMiniObject, but right now
277 * the introspection system only supports GObject.
278 * http://bugzilla.gnome.org/show_bug.cgi?id=559706
282 escaped_printf (out, " <prerequisite name=\"%s\"/>\n",
283 g_type_name (itype));
285 dump_properties (type, out);
286 dump_signals (type, out);
287 goutput_write (out, " </interface>\n");
291 dump_boxed_type (GType type, const char *symbol, GOutputStream *out)
293 escaped_printf (out, " <boxed name=\"%s\" get-type=\"%s\"/>\n",
294 g_type_name (type), symbol);
298 dump_flags_type (GType type, const char *symbol, GOutputStream *out)
303 klass = g_type_class_ref (type);
304 escaped_printf (out, " <flags name=\"%s\" get-type=\"%s\">\n",
305 g_type_name (type), symbol);
307 for (i = 0; i < klass->n_values; i++)
309 GFlagsValue *value = &(klass->values[i]);
311 escaped_printf (out, " <member name=\"%s\" nick=\"%s\" value=\"%d\"/>\n",
312 value->value_name, value->value_nick, value->value);
314 goutput_write (out, " </flags>\n");
318 dump_enum_type (GType type, const char *symbol, GOutputStream *out)
323 klass = g_type_class_ref (type);
324 escaped_printf (out, " <enum name=\"%s\" get-type=\"%s\">\n",
325 g_type_name (type), symbol);
327 for (i = 0; i < klass->n_values; i++)
329 GEnumValue *value = &(klass->values[i]);
331 escaped_printf (out, " <member name=\"%s\" nick=\"%s\" value=\"%d\"/>\n",
332 value->value_name, value->value_nick, value->value);
334 goutput_write (out, " </enum>");
338 dump_fundamental_type (GType type, const char *symbol, GOutputStream *out)
345 gboolean first = TRUE;
348 escaped_printf (out, " <fundamental name=\"%s\" get-type=\"%s\"",
349 g_type_name (type), symbol);
351 if (G_TYPE_IS_ABSTRACT (type))
352 escaped_printf (out, " abstract=\"1\"");
354 if (G_TYPE_IS_INSTANTIATABLE (type))
355 escaped_printf (out, " instantiatable=\"1\"");
357 parent = g_type_parent (type);
358 parent_str = g_string_new ("");
359 while (parent != G_TYPE_INVALID)
364 g_string_append_c (parent_str, ',');
365 if (!g_type_name (parent))
367 g_string_append (parent_str, g_type_name (parent));
368 parent = g_type_parent (parent);
371 if (parent_str->len > 0)
372 escaped_printf (out, " parents=\"%s\"", parent_str->str);
373 g_string_free (parent_str, TRUE);
375 goutput_write (out, ">\n");
377 interfaces = g_type_interfaces (type, &n_interfaces);
378 for (i = 0; i < n_interfaces; i++)
380 GType itype = interfaces[i];
381 escaped_printf (out, " <implements name=\"%s\"/>\n",
382 g_type_name (itype));
384 goutput_write (out, " </fundamental>\n");
388 dump_type (GType type, const char *symbol, GOutputStream *out)
390 switch (g_type_fundamental (type))
393 dump_object_type (type, symbol, out);
395 case G_TYPE_INTERFACE:
396 dump_interface_type (type, symbol, out);
399 dump_boxed_type (type, symbol, out);
402 dump_flags_type (type, symbol, out);
405 dump_enum_type (type, symbol, out);
408 /* GValue, etc. Just skip them. */
411 dump_fundamental_type (type, symbol, out);
417 dump_error_quark (GQuark quark, const char *symbol, GOutputStream *out)
419 escaped_printf (out, " <error-quark function=\"%s\" domain=\"%s\"/>\n",
420 symbol, g_quark_to_string (quark));
424 * g_irepository_dump:
425 * @arg: Comma-separated pair of input and output filenames
428 * Argument specified is a comma-separated pair of filenames; i.e. of
429 * the form "input.txt,output.xml". The input file should be a
430 * UTF-8 Unix-line-ending text file, with each line containing the name
431 * of a GType _get_type function.
433 * The output file should already exist, but be empty. This function will
434 * overwrite its contents.
436 * Returns: %TRUE on success, %FALSE on error
438 #ifndef G_IREPOSITORY_COMPILATION
440 dump_irepository (const char *arg, GError **error) G_GNUC_UNUSED;
442 dump_irepository (const char *arg, GError **error)
445 g_irepository_dump (const char *arg, GError **error)
448 GHashTable *output_types;
452 GFileInputStream *input;
453 GFileOutputStream *output;
454 GDataInputStream *in;
456 gboolean caught_error = FALSE;
458 self = g_module_open (NULL, 0);
464 "failed to open self: %s",
469 args = g_strsplit (arg, ",", 2);
471 input_file = g_file_new_for_path (args[0]);
472 output_file = g_file_new_for_path (args[1]);
474 input = g_file_read (input_file, NULL, error);
478 output = g_file_replace (output_file, NULL, FALSE, 0, NULL, error);
481 g_input_stream_close (G_INPUT_STREAM (input), NULL, NULL);
485 goutput_write (G_OUTPUT_STREAM (output), "<?xml version=\"1.0\"?>\n");
486 goutput_write (G_OUTPUT_STREAM (output), "<dump>\n");
488 output_types = g_hash_table_new (NULL, NULL);
490 in = g_data_input_stream_new (G_INPUT_STREAM (input));
491 g_object_unref (input);
496 char *line = g_data_input_stream_read_line (in, &len, NULL, NULL);
497 const char *function;
499 if (line == NULL || *line == '\0')
507 if (strncmp (line, "get-type:", strlen ("get-type:")) == 0)
511 function = line + strlen ("get-type:");
513 type = invoke_get_type (self, function, error);
515 if (type == G_TYPE_INVALID)
517 g_printerr ("Invalid GType function: '%s'\n", function);
523 if (g_hash_table_lookup (output_types, (gpointer) type))
525 g_hash_table_insert (output_types, (gpointer) type, (gpointer) type);
527 dump_type (type, function, G_OUTPUT_STREAM (output));
529 else if (strncmp (line, "error-quark:", strlen ("error-quark:")) == 0)
532 function = line + strlen ("error-quark:");
533 quark = invoke_error_quark (self, function, error);
537 g_printerr ("Invalid error quark function: '%s'\n", function);
543 dump_error_quark (quark, function, G_OUTPUT_STREAM (output));
551 g_hash_table_destroy (output_types);
553 goutput_write (G_OUTPUT_STREAM (output), "</dump>\n");
557 /* Avoid overwriting an earlier set error */
562 if (!g_input_stream_close (G_INPUT_STREAM (in), NULL, ioerror))
564 if (!g_output_stream_close (G_OUTPUT_STREAM (output), NULL, ioerror))
568 return !caught_error;
573 main(int argc, char **argv)
575 GError *error = NULL;
576 const char *introspect_dump_prefix = "--introspect-dump=";
582 if (argc != 2 || !g_str_has_prefix (argv[1], introspect_dump_prefix))
584 g_printerr ("Usage: %s --introspect-dump=input,output", argv[0]);
588 if (!dump_irepository (argv[1] + strlen(introspect_dump_prefix), &error))
590 g_printerr ("%s\n", error->message);