1 /* -*- Mode: C; c-file-style: "gnu"; -*- */
2 /* GObject introspection: IDL generator
4 * Copyright (C) 2005 Matthias Clasen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
27 #include <glib-object.h>
28 #include <glib/gstdio.h>
30 #include "girepository.h"
31 #include "gmetadata.h"
38 write_type_name (const gchar *namespace,
42 if (strcmp (namespace, g_base_info_get_namespace (info)) != 0)
43 g_fprintf (file, "%s.", g_base_info_get_namespace (info));
45 g_fprintf (file, "%s", g_base_info_get_name (info));
49 write_type_info (const gchar *namespace,
57 const gchar* basic[] = {
80 tag = g_type_info_get_tag (info);
82 if (tag < TYPE_TAG_UTF8)
83 g_fprintf (file, "%s%s", basic[tag], g_type_info_is_pointer (info) ? "*" : "");
84 else if (tag <= TYPE_TAG_FILENAME)
85 g_fprintf (file, "%s", basic[tag]);
86 else if (tag == TYPE_TAG_ARRAY)
90 type = g_type_info_get_param_type (info, 0);
91 write_type_info (namespace, type, file);
92 g_fprintf (file, "[");
94 length = g_type_info_get_array_length (info);
97 g_fprintf (file, "length=%d", length);
99 if (g_type_info_is_zero_terminated (info))
100 g_fprintf (file, "%szero-terminated=1", length >= 0 ? "," : "");
102 g_fprintf (file, "]");
103 g_base_info_unref ((GIBaseInfo *)type);
105 else if (tag == TYPE_TAG_SYMBOL)
107 GIBaseInfo *iface = g_type_info_get_interface (info);
108 write_type_name (namespace, iface, file);
109 if (g_type_info_is_pointer (info))
110 g_fprintf (file, "*");
111 g_base_info_unref (iface);
113 else if (tag == TYPE_TAG_LIST)
115 type = g_type_info_get_param_type (info, 0);
116 g_fprintf (file, "GList");
119 g_fprintf (file, "<");
120 write_type_info (namespace, type, file);
121 g_fprintf (file, ">");
122 g_base_info_unref ((GIBaseInfo *)type);
124 g_fprintf (file, "*");
126 else if (tag == TYPE_TAG_SLIST)
128 type = g_type_info_get_param_type (info, 0);
129 g_fprintf (file, "GSList");
132 g_fprintf (file, "<");
133 write_type_info (namespace, type, file);
134 g_fprintf (file, ">");
135 g_base_info_unref ((GIBaseInfo *)type);
137 g_fprintf (file, "*");
139 else if (tag == TYPE_TAG_HASH)
141 type = g_type_info_get_param_type (info, 0);
142 g_fprintf (file, "GHashTable");
145 g_fprintf (file, "<");
146 write_type_info (namespace, type, file);
147 g_base_info_unref ((GIBaseInfo *)type);
148 type = g_type_info_get_param_type (info, 1);
149 g_fprintf (file, ",");
150 write_type_info (namespace, type, file);
151 g_fprintf (file, ">");
152 g_base_info_unref ((GIBaseInfo *)type);
154 g_fprintf (file, "*");
156 else if (tag == TYPE_TAG_ERROR)
160 g_fprintf (file, "GError");
161 n = g_type_info_get_n_error_domains (info);
164 g_fprintf (file, "<");
165 for (i = 0; i < n; i++)
167 GIErrorDomainInfo *ed = g_type_info_get_error_domain (info, i);
169 g_fprintf (file, ",");
170 write_type_name (namespace, (GIBaseInfo *)ed, file);
171 g_base_info_unref ((GIBaseInfo *)ed);
173 g_fprintf (file, ">");
175 g_fprintf (file, "*");
180 write_constant_value (const gchar *namespace,
186 write_field_info (const gchar *namespace,
188 GIConstantInfo *branch,
192 GIFieldInfoFlags flags;
198 name = g_base_info_get_name ((GIBaseInfo *)info);
199 flags = g_field_info_get_flags (info);
200 size = g_field_info_get_size (info);
201 offset = g_field_info_get_offset (info);
204 " <field name=\"%s\" readable=\"%s\" writable=\"%s\" ",
206 flags & GI_FIELD_IS_READABLE ? "1" : "0",
207 flags & GI_FIELD_IS_WRITABLE ? "1" : "0");
209 g_fprintf (file, "bits=\"%d\" ", size);
210 g_fprintf (file, "offset=\"%d\" ", offset);
212 g_fprintf (file, "type=\"");
214 type = g_field_info_get_type (info);
215 write_type_info (namespace, type, file);
216 g_base_info_unref ((GIBaseInfo *)type);
218 g_fprintf (file, "\"");
222 g_fprintf (file, " branch=\"");
223 type = g_constant_info_get_type (branch);
224 g_constant_info_get_value (branch, &value);
225 write_constant_value (namespace, type, &value, file);
226 g_fprintf (file, "\"");
229 g_fprintf (file," />\n");
233 write_callable_info (const gchar *namespace,
234 GICallableInfo *info,
241 g_fprintf (file, "%*s <return-type type=\"", indent, "");
243 type = g_callable_info_get_return_type (info);
244 write_type_info (namespace, type, file);
246 g_fprintf (file, "\"");
248 if (g_type_info_is_pointer (type))
250 switch (g_callable_info_get_caller_owns (info))
252 case GI_TRANSFER_NOTHING:
253 g_fprintf (file, " transfer=\"none\"");
255 case GI_TRANSFER_CONTAINER:
256 g_fprintf (file, " transfer=\"shallow\"");
258 case GI_TRANSFER_EVERYTHING:
259 g_fprintf (file, " transfer=\"full\"");
262 g_assert_not_reached ();
265 g_base_info_unref ((GIBaseInfo *)type);
266 if (g_callable_info_may_return_null (info))
267 g_fprintf (file, " null-ok=\"1\"");
269 g_fprintf (file, " />\n");
271 if (g_callable_info_get_n_args (info) > 0)
273 g_fprintf (file, "%*s <parameters>\n", indent, "");
274 for (i = 0; i < g_callable_info_get_n_args (info); i++)
276 GIArgInfo *arg = g_callable_info_get_arg (info, i);
278 g_fprintf (file, "%*s <parameter name=\"%s\" type=\"",
279 indent, "", g_base_info_get_name ((GIBaseInfo *) arg));
281 type = g_arg_info_get_type (arg);
282 write_type_info (namespace, type, file);
283 g_fprintf (file, "\"");
285 if (g_type_info_is_pointer (type))
287 switch (g_arg_info_get_ownership_transfer (arg))
289 case GI_TRANSFER_NOTHING:
290 g_fprintf (file, " transfer=\"none\"");
292 case GI_TRANSFER_CONTAINER:
293 g_fprintf (file, " transfer=\"shallow\"");
295 case GI_TRANSFER_EVERYTHING:
296 g_fprintf (file, " transfer=\"full\"");
299 g_assert_not_reached ();
302 g_base_info_unref ((GIBaseInfo *)type);
304 g_fprintf (file, " direction=\"");
305 switch (g_arg_info_get_direction (arg))
307 case GI_DIRECTION_IN:
308 g_fprintf (file, "in");
310 case GI_DIRECTION_OUT:
311 g_fprintf (file, "out");
313 case GI_DIRECTION_INOUT:
314 g_fprintf (file, "inout");
317 g_fprintf (file, "\"");
319 if (g_arg_info_may_be_null (arg))
320 g_fprintf (file, " null-ok=\"1\"");
322 if (g_arg_info_is_dipper (arg))
323 g_fprintf (file, " dipper=\"1\"");
325 if (g_arg_info_is_return_value (arg))
326 g_fprintf (file, " retval=\"1\"");
328 if (g_arg_info_is_optional (arg))
329 g_fprintf (file, " optional=\"1\"");
331 g_fprintf (file, " />\n");
333 g_base_info_unref ((GIBaseInfo *)arg);
336 g_fprintf (file, "%*s </parameters>\n", indent, "");
341 write_function_info (const gchar *namespace,
342 GIFunctionInfo *info,
346 GIFunctionInfoFlags flags;
352 flags = g_function_info_get_flags (info);
353 name = g_base_info_get_name ((GIBaseInfo *)info);
354 symbol = g_function_info_get_symbol (info);
355 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
357 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
359 else if (flags & GI_FUNCTION_IS_METHOD)
364 g_fprintf (file, "%*s<%s name=\"%s\" symbol=\"%s\"",
365 indent, "", tag, name, symbol);
367 if (flags & GI_FUNCTION_IS_SETTER)
368 g_fprintf (file, " type=\"setter\"");
369 else if (flags & GI_FUNCTION_IS_GETTER)
370 g_fprintf (file, " type=\"getter\"");
373 g_fprintf (file, " deprecated=\"1\"");
375 g_fprintf (file, ">\n");
376 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
377 g_fprintf (file, "%*s</%s>\n", indent, "", tag);
381 write_callback_info (const gchar *namespace,
382 GICallbackInfo *info,
389 name = g_base_info_get_name ((GIBaseInfo *)info);
390 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
392 g_fprintf (file, "%*s<callback name=\"%s\"", indent, "", name);
395 g_fprintf (file, " deprecated=\"1\"");
397 g_fprintf (file, ">\n");
398 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
399 g_fprintf (file, "%*s</callback>\n", indent, "");
403 write_struct_info (const gchar *namespace,
408 const gchar *type_name;
409 const gchar *type_init;
413 name = g_base_info_get_name ((GIBaseInfo *)info);
414 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
416 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
418 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
419 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
421 g_fprintf (file, " <boxed name=\"%s\" type-name=\"%s\" get-type=\"%s\"", name, type_name, type_init);
424 g_fprintf (file, " <struct name=\"%s\"", name);
427 g_fprintf (file, " deprecated=\"1\"");
429 g_fprintf (file, ">\n");
431 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
433 GIFieldInfo *field = g_struct_info_get_field (info, i);
434 write_field_info (namespace, field, NULL, file);
435 g_base_info_unref ((GIBaseInfo *)field);
438 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
440 GIFunctionInfo *function = g_struct_info_get_method (info, i);
441 write_function_info (namespace, function, file, 6);
442 g_base_info_unref ((GIBaseInfo *)function);
445 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
446 g_fprintf (file, " </boxed>\n");
448 g_fprintf (file, " </struct>\n");
452 write_value_info (const gchar *namespace,
460 name = g_base_info_get_name ((GIBaseInfo *)info);
461 value = g_value_info_get_value (info);
462 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
464 g_fprintf (file, " <member name=\"%s\" value=\"%ld\"", name, value);
467 g_fprintf (file, " deprecated=\"1\"");
469 g_fprintf (file, " />\n");
473 write_constant_value (const gchar *namespace,
478 switch (g_type_info_get_tag (type))
480 case GI_TYPE_TAG_BOOLEAN:
481 g_fprintf (file, "%d", value->v_boolean);
483 case GI_TYPE_TAG_INT8:
484 g_fprintf (file, "%d", value->v_int8);
486 case GI_TYPE_TAG_UINT8:
487 g_fprintf (file, "%d", value->v_uint8);
489 case GI_TYPE_TAG_INT16:
490 g_fprintf (file, "%" G_GINT16_FORMAT, value->v_int16);
492 case GI_TYPE_TAG_UINT16:
493 g_fprintf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
495 case GI_TYPE_TAG_INT32:
496 g_fprintf (file, "%" G_GINT32_FORMAT, value->v_int32);
498 case GI_TYPE_TAG_UINT32:
499 g_fprintf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
501 case GI_TYPE_TAG_INT64:
502 g_fprintf (file, "%" G_GINT64_FORMAT, value->v_int64);
504 case GI_TYPE_TAG_UINT64:
505 g_fprintf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
507 case GI_TYPE_TAG_INT:
508 g_fprintf (file, "%d", value->v_int);
510 case GI_TYPE_TAG_UINT:
511 g_fprintf (file, "%d", value->v_uint);
513 case GI_TYPE_TAG_LONG:
514 g_fprintf (file, "%ld", value->v_long);
516 case GI_TYPE_TAG_ULONG:
517 g_fprintf (file, "%ld", value->v_ulong);
519 case GI_TYPE_TAG_SSIZE:
520 g_fprintf (file, "%zd", value->v_ssize);
522 case GI_TYPE_TAG_SIZE:
523 g_fprintf (file, "%zd", value->v_size);
525 case GI_TYPE_TAG_FLOAT:
526 g_fprintf (file, "%f", value->v_float);
528 case GI_TYPE_TAG_DOUBLE:
529 g_fprintf (file, "%f", value->v_double);
531 case GI_TYPE_TAG_UTF8:
532 case GI_TYPE_TAG_FILENAME:
533 g_fprintf (file, "%s", value->v_string);
535 case GI_TYPE_TAG_SYMBOL:
536 g_fprintf (file, "%s", value->v_string);
539 g_warning ("Could not get type tag for constant");
544 write_constant_info (const gchar *namespace,
545 GIConstantInfo *info,
554 name = g_base_info_get_name ((GIBaseInfo *)info);
555 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
557 g_fprintf (file, "%*s<constant name=\"%s\" type=\"", indent, "", name);
559 type = g_constant_info_get_type (info);
560 write_type_info (namespace, type, file);
561 g_fprintf (file, "\" value=\"");
563 g_constant_info_get_value (info, &value);
564 write_constant_value (namespace, type, &value, file);
565 g_fprintf (file, "\" />\n");
567 g_base_info_unref ((GIBaseInfo *)type);
572 write_enum_info (const gchar *namespace,
577 const gchar *type_name = NULL;
578 const gchar *type_init = NULL;
582 name = g_base_info_get_name ((GIBaseInfo *)info);
583 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
585 /* Make sure this is a registered enum before filling out the
588 if (g_enum_info_is_registered ((GIEnumInfo *)info))
590 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
591 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
594 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
595 g_fprintf (file, " <enum ");
597 g_fprintf (file, " <flags ");
598 g_fprintf (file, "name=\"%s\"", name);
601 g_fprintf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
604 g_fprintf (file, " deprecated=\"1\"");
606 g_fprintf (file, ">\n");
608 for (i = 0; i < g_enum_info_get_n_values (info); i++)
610 GIValueInfo *value = g_enum_info_get_value (info, i);
611 write_value_info (namespace, value, file);
612 g_base_info_unref ((GIBaseInfo *)value);
615 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
616 g_fprintf (file, " </enum>\n");
618 g_fprintf (file, " </flags>\n");
622 write_signal_info (const gchar *namespace,
630 name = g_base_info_get_name ((GIBaseInfo *)info);
631 flags = g_signal_info_get_flags (info);
632 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
634 g_fprintf (file, " <signal name=\"%s\"", name);
637 g_fprintf (file, " deprecated=\"1\"");
639 if (flags & G_SIGNAL_RUN_FIRST)
640 g_fprintf (file, " when=\"FIRST\"");
641 else if (flags & G_SIGNAL_RUN_LAST)
642 g_fprintf (file, " when=\"LAST\"");
643 else if (flags & G_SIGNAL_RUN_CLEANUP)
644 g_fprintf (file, " when=\"CLEANUP\"");
646 if (flags & G_SIGNAL_NO_RECURSE)
647 g_fprintf (file, " no-recurse=\"1\"");
649 if (flags & G_SIGNAL_DETAILED)
650 g_fprintf (file, " detailed=\"1\"");
652 if (flags & G_SIGNAL_ACTION)
653 g_fprintf (file, " action=\"1\"");
655 if (flags & G_SIGNAL_NO_HOOKS)
656 g_fprintf (file, " no-hooks=\"1\"");
658 g_fprintf (file, ">\n");
660 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
661 g_fprintf (file, " </signal>\n");
665 write_vfunc_info (const gchar *namespace,
669 GIVFuncInfoFlags flags;
674 name = g_base_info_get_name ((GIBaseInfo *)info);
675 flags = g_vfunc_info_get_flags (info);
676 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
677 offset = g_vfunc_info_get_offset (info);
679 g_fprintf (file, " <vfunc name=\"%s\"", name);
682 g_fprintf (file, " deprecated=\"1\"");
684 if (flags & GI_VFUNC_MUST_CHAIN_UP)
685 g_fprintf (file, " must-chain-up=\"1\"");
687 if (flags & GI_VFUNC_MUST_OVERRIDE)
688 g_fprintf (file, " override=\"always\"");
689 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
690 g_fprintf (file, " override=\"never\"");
692 g_fprintf (file, " offset=\"%d\"", offset);
693 g_fprintf (file, ">\n");
695 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
696 g_fprintf (file, " </vfunc>\n");
700 write_property_info (const gchar *namespace,
701 GIPropertyInfo *info,
709 name = g_base_info_get_name ((GIBaseInfo *)info);
710 flags = g_property_info_get_flags (info);
711 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
713 g_fprintf (file, " <property name=\"%s\"", name);
716 g_fprintf (file, " deprecated=\"1\"");
718 if (flags & G_PARAM_READABLE)
719 g_fprintf (file, " readable=\"1\"");
721 g_fprintf (file, " readable=\"0\"");
723 if (flags & G_PARAM_WRITABLE)
724 g_fprintf (file, " writable=\"1\"");
726 g_fprintf (file, " writable=\"0\"");
728 if (flags & G_PARAM_CONSTRUCT)
729 g_fprintf (file, " construct=\"1\"");
731 if (flags & G_PARAM_CONSTRUCT_ONLY)
732 g_fprintf (file, " construct-only=\"1\"");
734 type = g_property_info_get_type (info);
735 g_fprintf (file, " type=\"");
736 write_type_info (namespace, type, file);
737 g_fprintf (file, "\"");
739 g_fprintf (file, " />\n");
743 write_object_info (const gchar *namespace,
748 const gchar *type_name;
749 const gchar *type_init;
754 name = g_base_info_get_name ((GIBaseInfo *)info);
755 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
757 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
758 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
759 g_fprintf (file, " <object name=\"%s\"", name);
761 pnode = g_object_info_get_parent (info);
764 g_fprintf (file, " parent=\"");
765 write_type_name (namespace, (GIBaseInfo *)pnode, file);
766 g_fprintf (file, "\"" );
767 g_base_info_unref ((GIBaseInfo *)pnode);
770 g_fprintf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
773 g_fprintf (file, " deprecated=\"1\"");
775 g_fprintf (file, ">\n");
777 if (g_object_info_get_n_interfaces (info) > 0)
779 g_fprintf (file, " <implements>\n");
780 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
782 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
783 g_fprintf (file, " <interface name=\"");
784 write_type_name (namespace, (GIBaseInfo*)imp, file);
785 g_fprintf (file,"\" />\n");
786 g_base_info_unref ((GIBaseInfo*)imp);
788 g_fprintf (file, " </implements>\n");
791 for (i = 0; i < g_object_info_get_n_fields (info); i++)
793 GIFieldInfo *field = g_object_info_get_field (info, i);
794 write_field_info (namespace, field, NULL, file);
795 g_base_info_unref ((GIBaseInfo *)field);
798 for (i = 0; i < g_object_info_get_n_methods (info); i++)
800 GIFunctionInfo *function = g_object_info_get_method (info, i);
801 write_function_info (namespace, function, file, 6);
802 g_base_info_unref ((GIBaseInfo *)function);
805 for (i = 0; i < g_object_info_get_n_properties (info); i++)
807 GIPropertyInfo *prop = g_object_info_get_property (info, i);
808 write_property_info (namespace, prop, file);
809 g_base_info_unref ((GIBaseInfo *)prop);
812 for (i = 0; i < g_object_info_get_n_signals (info); i++)
814 GISignalInfo *signal = g_object_info_get_signal (info, i);
815 write_signal_info (namespace, signal, file);
816 g_base_info_unref ((GIBaseInfo *)signal);
819 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
821 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
822 write_vfunc_info (namespace, vfunc, file);
823 g_base_info_unref ((GIBaseInfo *)vfunc);
826 for (i = 0; i < g_object_info_get_n_constants (info); i++)
828 GIConstantInfo *constant = g_object_info_get_constant (info, i);
829 write_constant_info (namespace, constant, file, 6);
830 g_base_info_unref ((GIBaseInfo *)constant);
833 g_fprintf (file, " </object>\n");
837 write_interface_info (const gchar *namespace,
838 GIInterfaceInfo *info,
842 const gchar *type_name;
843 const gchar *type_init;
847 name = g_base_info_get_name ((GIBaseInfo *)info);
848 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
850 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
851 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
852 g_fprintf (file, " <interface name=\"%s\" type-name=\"%s\" get-type=\"%s\"",
853 name, type_name, type_init);
856 g_fprintf (file, " deprecated=\"1\"");
858 g_fprintf (file, ">\n");
860 if (g_interface_info_get_n_prerequisites (info) > 0)
862 g_fprintf (file, " <requires>\n");
863 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
865 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
867 if (g_base_info_get_type (req) == GI_INFO_TYPE_INTERFACE)
868 g_fprintf (file, " <interface name=\"");
870 g_fprintf (file, " <object name=\"");
871 write_type_name (namespace, req, file);
872 g_fprintf (file, "\" />\n");
873 g_base_info_unref (req);
875 g_fprintf (file, " </requires>\n");
878 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
880 GIFunctionInfo *function = g_interface_info_get_method (info, i);
881 write_function_info (namespace, function, file, 6);
882 g_base_info_unref ((GIBaseInfo *)function);
885 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
887 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
888 write_property_info (namespace, prop, file);
889 g_base_info_unref ((GIBaseInfo *)prop);
892 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
894 GISignalInfo *signal = g_interface_info_get_signal (info, i);
895 write_signal_info (namespace, signal, file);
896 g_base_info_unref ((GIBaseInfo *)signal);
899 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
901 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
902 write_vfunc_info (namespace, vfunc, file);
903 g_base_info_unref ((GIBaseInfo *)vfunc);
906 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
908 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
909 write_constant_info (namespace, constant, file, 6);
910 g_base_info_unref ((GIBaseInfo *)constant);
913 g_fprintf (file, " </interface>\n");
917 write_error_domain_info (const gchar *namespace,
918 GIErrorDomainInfo *info,
922 const gchar *name, *quark;
924 name = g_base_info_get_name ((GIBaseInfo *)info);
925 quark = g_error_domain_info_get_quark (info);
926 enum_ = (GIBaseInfo *)g_error_domain_info_get_codes (info);
928 " <errordomain name=\"%s\" get-quark=\"%s\" codes=\"",
930 write_type_name (namespace, enum_, file);
931 g_fprintf (file, "\" />\n");
932 g_base_info_unref (enum_);
936 write_union_info (const gchar *namespace,
941 const gchar *type_name;
942 const gchar *type_init;
946 name = g_base_info_get_name ((GIBaseInfo *)info);
947 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
949 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
950 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
952 g_fprintf (file, " <union name=\"%s\"", name);
955 g_fprintf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
958 g_fprintf (file, " deprecated=\"1\"");
960 g_fprintf (file, ">\n");
962 if (g_union_info_is_discriminated (info))
967 offset = g_union_info_get_discriminator_offset (info);
968 type = g_union_info_get_discriminator_type (info);
970 g_fprintf (file, " <discriminator offset=\"%d\" type=\"", offset);
971 write_type_info (namespace, type, file);
972 g_fprintf (file, "\" />\n");
973 g_base_info_unref ((GIBaseInfo *)type);
976 for (i = 0; i < g_union_info_get_n_fields (info); i++)
978 GIFieldInfo *field = g_union_info_get_field (info, i);
979 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
980 write_field_info (namespace, field, constant, file);
981 g_base_info_unref ((GIBaseInfo *)field);
983 g_base_info_unref ((GIBaseInfo *)constant);
986 for (i = 0; i < g_union_info_get_n_methods (info); i++)
988 GIFunctionInfo *function = g_union_info_get_method (info, i);
989 write_function_info (namespace, function, file, 6);
990 g_base_info_unref ((GIBaseInfo *)function);
993 g_fprintf (file, " </union>\n");
997 write_repository (GIRepository *repository,
998 gboolean needs_prefix)
1005 namespaces = g_irepository_get_namespaces (repository);
1014 filename = g_strdup_printf ("%s-%s", namespaces[0], output);
1016 filename = g_strdup (output);
1017 file = g_fopen (filename, "w");
1021 g_fprintf (stderr, "failed to open '%s': %s\n",
1022 filename, g_strerror (errno));
1031 g_fprintf (file, "<?xml version=\"1.0\"?>\n");
1032 g_fprintf (file, "<api version=\"1.0\">\n");
1034 for (i = 0; namespaces[i]; i++)
1036 const gchar *shared_library;
1038 shared_library = g_irepository_get_shared_library (repository, ns);
1040 g_fprintf (file, " <namespace name=\"%s\" shared-library=\"%s\">\n",
1041 ns, shared_library);
1043 g_fprintf (file, " <namespace name=\"%s\">\n", ns);
1045 for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
1047 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1048 switch (g_base_info_get_type (info))
1050 case GI_INFO_TYPE_FUNCTION:
1051 write_function_info (ns, (GIFunctionInfo *)info, file, 4);
1054 case GI_INFO_TYPE_CALLBACK:
1055 write_callback_info (ns, (GICallbackInfo *)info, file, 4);
1058 case GI_INFO_TYPE_STRUCT:
1059 case GI_INFO_TYPE_BOXED:
1060 write_struct_info (ns, (GIStructInfo *)info, file);
1063 case GI_INFO_TYPE_UNION:
1064 write_union_info (ns, (GIUnionInfo *)info, file);
1067 case GI_INFO_TYPE_ENUM:
1068 case GI_INFO_TYPE_FLAGS:
1069 write_enum_info (ns, (GIEnumInfo *)info, file);
1072 case GI_INFO_TYPE_CONSTANT:
1073 write_constant_info (ns, (GIConstantInfo *)info, file, 4);
1076 case GI_INFO_TYPE_OBJECT:
1077 write_object_info (ns, (GIObjectInfo *)info, file);
1080 case GI_INFO_TYPE_INTERFACE:
1081 write_interface_info (ns, (GIInterfaceInfo *)info, file);
1084 case GI_INFO_TYPE_ERROR_DOMAIN:
1085 write_error_domain_info (ns, (GIErrorDomainInfo *)info, file);
1089 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1092 g_base_info_unref (info);
1095 g_fprintf (file, " </namespace>\n");
1098 g_fprintf (file, "</api>\n");
1103 g_strfreev (namespaces);
1106 static GOptionEntry options[] =
1108 { "raw", 0, 0, G_OPTION_ARG_NONE, &raw, "handle raw metadata", NULL },
1109 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
1110 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
1114 static const guchar *
1115 load_metadata (const gchar *filename,
1120 gsize *metadata_size;
1123 handle = g_module_open (filename, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
1124 if (!g_module_symbol (handle, "_G_METADATA", (gpointer *) &metadata))
1126 g_printerr ("Could not load metadata from '%s': %s\n",
1127 filename, g_module_error ());
1131 if (!g_module_symbol (handle, "_G_METADATA_SIZE", (gpointer *) &metadata_size))
1133 g_printerr ("Could not load metadata from '%s': %s\n",
1134 filename, g_module_error ());
1138 *len = *metadata_size;
1147 main (int argc, char *argv[])
1149 GOptionContext *context;
1150 GError *error = NULL;
1151 gboolean needs_prefix;
1157 context = g_option_context_new ("");
1158 g_option_context_add_main_entries (context, options, NULL);
1159 g_option_context_parse (context, &argc, &argv, &error);
1163 g_fprintf (stderr, "no input files\n");
1168 for (i = 0; input[i]; i++)
1170 GModule *dlhandle = NULL;
1171 const guchar *metadata;
1176 if (!g_file_get_contents (input[i], (gchar **)&metadata, &len, &error))
1178 g_fprintf (stderr, "failed to read '%s': %s\n",
1179 input[i], error->message);
1180 g_clear_error (&error);
1186 metadata = load_metadata (input[i], &dlhandle, &len);
1189 g_fprintf (stderr, "failed to load metadata from '%s'\n",
1195 if (input[i + 1] && output)
1196 needs_prefix = TRUE;
1198 needs_prefix = FALSE;
1200 data = g_metadata_new_from_const_memory (metadata, len);
1202 GError *error = NULL;
1203 if (!g_metadata_validate (data, &error)) {
1204 g_printerr ("metadata not valid: %s\n", error->message);
1205 g_clear_error (&error);
1208 g_irepository_register (g_irepository_get_default (), data);
1209 write_repository (g_irepository_get_default (), needs_prefix);
1210 g_irepository_unregister (g_irepository_get_default (),
1211 g_metadata_get_namespace (data));
1215 g_module_close (dlhandle);
1219 /* when writing to stdout, stop after the first module */
1220 if (input[i + 1] && !output)
1222 g_fprintf (stderr, "warning, %d modules omitted\n",
1223 g_strv_length (input) - 1);