1 /* GObject introspection: Dump introspection data
3 * Copyright (C) 2008 Colin Walters <walters@verbum.org>
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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 #include <glib-object.h>
27 #include "girepository.h"
33 escaped_printf (GOutputStream *out, const char *fmt, ...)
42 str = g_markup_vprintf_escaped (fmt, args);
43 if (!g_output_stream_write_all (out, str, strlen (str), &written, NULL, &error))
45 g_critical ("failed to write to iochannel: %s", error->message);
46 g_clear_error (&error);
54 goutput_write (GOutputStream *out, const char *str)
58 if (!g_output_stream_write_all (out, str, strlen (str), &written, NULL, &error))
60 g_critical ("failed to write to iochannel: %s", error->message);
61 g_clear_error (&error);
65 typedef GType (*GetTypeFunc)(void);
68 invoke_get_type (GModule *self, const char *symbol, GError **error)
72 if (!g_module_symbol (self, symbol, (void**)&sym))
77 "Failed to find symbol '%s'", symbol);
78 return G_TYPE_INVALID;
85 dump_properties (GType type, GOutputStream *out)
91 if (G_TYPE_FUNDAMENTAL (type) == G_TYPE_OBJECT)
94 klass = g_type_class_ref (type);
95 props = g_object_class_list_properties (klass, &n_properties);
100 klass = g_type_default_interface_ref (type);
101 props = g_object_interface_list_properties (klass, &n_properties);
104 for (i = 0; i < n_properties; i++)
109 if (prop->owner_type != type)
112 escaped_printf (out, " <property name=\"%s\" type=\"%s\" flags=\"%d\"/>\n",
113 prop->name, g_type_name (prop->value_type), prop->flags);
119 dump_signals (GType type, GOutputStream *out)
125 sig_ids = g_signal_list_ids (type, &n_sigs);
126 for (i = 0; i < n_sigs; i++)
133 g_signal_query (sigid, &query);
135 escaped_printf (out, " <signal name=\"%s\" return=\"%s\">\n",
136 query.signal_name, g_type_name (query.return_type));
138 for (j = 0; j < query.n_params; j++)
140 escaped_printf (out, " <param type=\"%s\"/>\n",
141 g_type_name (query.param_types[j]));
143 goutput_write (out, " </signal>\n");
148 dump_object_type (GType type, const char *symbol, GOutputStream *out)
154 escaped_printf (out, " <class name=\"%s\" get-type=\"%s\"",
155 g_type_name (type), symbol);
156 if (type != G_TYPE_OBJECT)
160 gboolean first = TRUE;
163 parent_str = g_string_new ("");
166 parent = g_type_parent (parent);
170 g_string_append_c (parent_str, ',');
171 g_string_append (parent_str, g_type_name (parent));
172 } while (parent != G_TYPE_OBJECT && parent != G_TYPE_INVALID);
174 escaped_printf (out, " parents=\"%s\"", parent_str->str);
176 g_string_free (parent_str, TRUE);
179 if (G_TYPE_IS_ABSTRACT (type))
180 escaped_printf (out, " abstract=\"1\"");
181 goutput_write (out, ">\n");
183 interfaces = g_type_interfaces (type, &n_interfaces);
184 for (i = 0; i < n_interfaces; i++)
186 GType itype = interfaces[i];
187 escaped_printf (out, " <implements name=\"%s\"/>\n",
188 g_type_name (itype));
190 dump_properties (type, out);
191 dump_signals (type, out);
192 goutput_write (out, " </class>\n");
196 dump_interface_type (GType type, const char *symbol, GOutputStream *out)
202 escaped_printf (out, " <interface name=\"%s\" get-type=\"%s\">\n",
203 g_type_name (type), symbol);
205 interfaces = g_type_interface_prerequisites (type, &n_interfaces);
206 for (i = 0; i < n_interfaces; i++)
208 GType itype = interfaces[i];
209 if (itype == G_TYPE_OBJECT)
211 /* Treat this as implicit for now; in theory GInterfaces are
212 * supported on things like GstMiniObject, but right now
213 * the introspection system only supports GObject.
214 * http://bugzilla.gnome.org/show_bug.cgi?id=559706
218 escaped_printf (out, " <prerequisite name=\"%s\"/>\n",
219 g_type_name (itype));
221 dump_properties (type, out);
222 dump_signals (type, out);
223 goutput_write (out, " </interface>\n");
227 dump_boxed_type (GType type, const char *symbol, GOutputStream *out)
229 escaped_printf (out, " <boxed name=\"%s\" get-type=\"%s\"/>\n",
230 g_type_name (type), symbol);
234 dump_flags_type (GType type, const char *symbol, GOutputStream *out)
239 klass = g_type_class_ref (type);
240 escaped_printf (out, " <flags name=\"%s\" get-type=\"%s\">\n",
241 g_type_name (type), symbol);
243 for (i = 0; i < klass->n_values; i++)
245 GFlagsValue *value = &(klass->values[i]);
247 escaped_printf (out, " <member name=\"%s\" nick=\"%s\" value=\"%d\"/>\n",
248 value->value_name, value->value_nick, value->value);
250 goutput_write (out, " </flags>\n");
254 dump_enum_type (GType type, const char *symbol, GOutputStream *out)
259 klass = g_type_class_ref (type);
260 escaped_printf (out, " <enum name=\"%s\" get-type=\"%s\">\n",
261 g_type_name (type), symbol);
263 for (i = 0; i < klass->n_values; i++)
265 GEnumValue *value = &(klass->values[i]);
267 escaped_printf (out, " <member name=\"%s\" nick=\"%s\" value=\"%d\"/>\n",
268 value->value_name, value->value_nick, value->value);
270 goutput_write (out, " </enum>");
274 dump_type (GType type, const char *symbol, GOutputStream *out)
276 switch (g_type_fundamental (type))
279 dump_object_type (type, symbol, out);
281 case G_TYPE_INTERFACE:
282 dump_interface_type (type, symbol, out);
285 dump_boxed_type (type, symbol, out);
288 dump_flags_type (type, symbol, out);
291 dump_enum_type (type, symbol, out);
294 /* GValue, etc. Just skip them. */
297 /* Other fundamental types such as the once GStreamer and Clutter registers
298 * are not yet interesting from an introspection perspective and should be
306 * g_irepository_dump:
307 * @arg: Comma-separated pair of input and output filenames
310 * Argument specified is a comma-separated pair of filenames; i.e. of
311 * the form "input.txt,output.xml". The input file should be a
312 * UTF-8 Unix-line-ending text file, with each line containing the name
313 * of a GType _get_type function.
315 * The output file should already exist, but be empty. This function will
316 * overwrite its contents.
318 * Returns: %TRUE on success, %FALSE on error
321 g_irepository_dump (const char *arg, GError **error)
323 GHashTable *output_types;
327 GFileInputStream *input;
328 GFileOutputStream *output;
329 GDataInputStream *in;
331 gboolean caught_error = FALSE;
333 self = g_module_open (NULL, 0);
339 "failed to open self: %s",
344 args = g_strsplit (arg, ",", 2);
346 input_file = g_file_new_for_path (args[0]);
347 output_file = g_file_new_for_path (args[1]);
349 input = g_file_read (input_file, NULL, error);
353 output = g_file_replace (output_file, NULL, FALSE, 0, NULL, error);
356 g_input_stream_close (G_INPUT_STREAM (input), NULL, NULL);
360 goutput_write (G_OUTPUT_STREAM (output), "<?xml version=\"1.0\"?>\n");
361 goutput_write (G_OUTPUT_STREAM (output), "<dump>\n");
363 output_types = g_hash_table_new (NULL, NULL);
365 in = g_data_input_stream_new (G_INPUT_STREAM (input));
366 g_object_unref (input);
371 char *line = g_data_input_stream_read_line (in, &len, NULL, NULL);
374 if (line == NULL || *line == '\0')
381 type = invoke_get_type (self, line, error);
383 if (type == G_TYPE_INVALID)
385 g_printerr ("Invalid GType: '%s'\n", line);
391 if (g_hash_table_lookup (output_types, (gpointer) type))
393 g_hash_table_insert (output_types, (gpointer) type, (gpointer) type);
395 dump_type (type, line, G_OUTPUT_STREAM (output));
401 g_hash_table_destroy (output_types);
403 goutput_write (G_OUTPUT_STREAM (output), "</dump>\n");
407 /* Avoid overwriting an earlier set error */
412 if (!g_input_stream_close (G_INPUT_STREAM (in), NULL, ioerror))
414 if (!g_output_stream_close (G_OUTPUT_STREAM (output), NULL, ioerror))
418 return !caught_error;