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)
157 escaped_printf (out, " parent=\"%s\"", g_type_name (g_type_parent (type)));
159 if (G_TYPE_IS_ABSTRACT (type))
160 escaped_printf (out, " abstract=\"1\"");
161 goutput_write (out, ">\n");
163 interfaces = g_type_interfaces (type, &n_interfaces);
164 for (i = 0; i < n_interfaces; i++)
166 GType itype = interfaces[i];
167 escaped_printf (out, " <implements name=\"%s\"/>\n",
168 g_type_name (itype));
170 dump_properties (type, out);
171 dump_signals (type, out);
172 goutput_write (out, " </class>\n");
176 dump_interface_type (GType type, const char *symbol, GOutputStream *out)
182 escaped_printf (out, " <interface name=\"%s\" get-type=\"%s\">\n",
183 g_type_name (type), symbol);
185 interfaces = g_type_interface_prerequisites (type, &n_interfaces);
186 for (i = 0; i < n_interfaces; i++)
188 GType itype = interfaces[i];
189 if (itype == G_TYPE_OBJECT)
191 /* This is implicit */
194 escaped_printf (out, " <prerequisite name=\"%s\"/>\n",
195 g_type_name (itype));
197 dump_properties (type, out);
198 dump_signals (type, out);
199 goutput_write (out, " </interface>\n");
203 dump_boxed_type (GType type, const char *symbol, GOutputStream *out)
205 escaped_printf (out, " <boxed name=\"%s\" get-type=\"%s\"/>\n",
206 g_type_name (type), symbol);
210 dump_flags_type (GType type, const char *symbol, GOutputStream *out)
215 klass = g_type_class_ref (type);
216 escaped_printf (out, " <flags name=\"%s\" get-type=\"%s\">\n",
217 g_type_name (type), symbol);
219 for (i = 0; i < klass->n_values; i++)
221 GFlagsValue *value = &(klass->values[i]);
223 escaped_printf (out, " <member name=\"%s\" nick=\"%s\" value=\"%d\"/>\n",
224 value->value_name, value->value_nick, value->value);
226 goutput_write (out, " </flags>\n");
230 dump_enum_type (GType type, const char *symbol, GOutputStream *out)
235 klass = g_type_class_ref (type);
236 escaped_printf (out, " <enum name=\"%s\" get-type=\"%s\">\n",
237 g_type_name (type), symbol);
239 for (i = 0; i < klass->n_values; i++)
241 GEnumValue *value = &(klass->values[i]);
243 escaped_printf (out, " <member name=\"%s\" nick=\"%s\" value=\"%d\"/>\n",
244 value->value_name, value->value_nick, value->value);
246 goutput_write (out, " </enum>");
250 dump_type (GType type, const char *symbol, GOutputStream *out)
252 switch (g_type_fundamental (type))
255 dump_object_type (type, symbol, out);
257 case G_TYPE_INTERFACE:
258 dump_interface_type (type, symbol, out);
261 dump_boxed_type (type, symbol, out);
264 dump_flags_type (type, symbol, out);
267 dump_enum_type (type, symbol, out);
270 /* GValue, etc. Just skip them. */
273 g_warning ("unhandled gtype %s", g_type_name (type));
278 * g_irepository_dump:
279 * @arg: Comma-separated pair of input and output filenames
282 * Argument specified is a comma-separated pair of filenames; i.e. of
283 * the form "input.txt,output.xml". The input file should be a
284 * UTF-8 Unix-line-ending text file, with each line containing the name
285 * of a GType _get_type function.
287 * The output file should already exist, but be empty. This function will
288 * overwrite its contents.
290 * Returns: %TRUE on success, %FALSE on error
293 g_irepository_dump (const char *arg, GError **error)
295 GHashTable *output_types;
299 GFileInputStream *input;
300 GFileOutputStream *output;
301 GDataInputStream *in;
303 gboolean caught_error = FALSE;
305 self = g_module_open (NULL, 0);
311 "failed to open self: %s",
316 args = g_strsplit (arg, ",", 2);
318 input_file = g_file_new_for_path (args[0]);
319 output_file = g_file_new_for_path (args[1]);
321 input = g_file_read (input_file, NULL, error);
325 output = g_file_replace (output_file, NULL, FALSE, 0, NULL, error);
328 g_input_stream_close (G_INPUT_STREAM (input), NULL, NULL);
332 goutput_write (G_OUTPUT_STREAM (output), "<?xml version=\"1.0\"?>\n");
333 goutput_write (G_OUTPUT_STREAM (output), "<dump>\n");
335 output_types = g_hash_table_new (NULL, NULL);
337 in = g_data_input_stream_new (G_INPUT_STREAM (input));
338 g_object_unref (input);
343 char *line = g_data_input_stream_read_line (in, &len, NULL, NULL);
346 if (line == NULL || *line == '\0')
353 type = invoke_get_type (self, line, error);
355 if (type == G_TYPE_INVALID)
362 if (g_hash_table_lookup (output_types, (gpointer) type))
364 g_hash_table_insert (output_types, (gpointer) type, (gpointer) type);
366 dump_type (type, line, G_OUTPUT_STREAM (output));
372 g_hash_table_destroy (output_types);
374 goutput_write (G_OUTPUT_STREAM (output), "</dump>\n");
378 /* Avoid overwriting an earlier set error */
383 if (!g_input_stream_close (G_INPUT_STREAM (in), NULL, ioerror))
385 if (!g_output_stream_close (G_OUTPUT_STREAM (output), NULL, ioerror))
389 return !caught_error;