1 /* -*- Mode: C; c-file-style: "gnu"; -*- */
2 /* GObject introspection: Repository implementation
4 * Copyright (C) 2005 Matthias Clasen
5 * Copyright (C) 2008 Colin Walters <walters@verbum.org>
6 * Copyright (C) 2008 Red Hat, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
29 #include <glib/gprintf.h>
31 #include "girepository.h"
37 static GStaticMutex globals_lock = G_STATIC_MUTEX_INIT;
38 static GIRepository *default_repository = NULL;
39 static GSList *search_path = NULL;
40 static GSList *override_search_path = NULL;
42 struct _GIRepositoryPrivate
44 GHashTable *typelibs; /* (string) namespace -> GTypelib */
45 GHashTable *lazy_typelibs; /* (string) namespace-version -> GTypelib */
46 GHashTable *info_by_gtype; /* GType -> GIBaseInfo */
49 G_DEFINE_TYPE (GIRepository, g_irepository, G_TYPE_OBJECT);
52 g_irepository_init (GIRepository *repository)
54 repository->priv = G_TYPE_INSTANCE_GET_PRIVATE (repository, G_TYPE_IREPOSITORY,
56 repository->priv->typelibs
57 = g_hash_table_new_full (g_str_hash, g_str_equal,
58 (GDestroyNotify) NULL,
59 (GDestroyNotify) g_typelib_free);
60 repository->priv->lazy_typelibs
61 = g_hash_table_new (g_str_hash, g_str_equal);
62 repository->priv->info_by_gtype
63 = g_hash_table_new_full (g_direct_hash, g_direct_equal,
64 (GDestroyNotify) NULL,
65 (GDestroyNotify) g_base_info_unref);
69 g_irepository_finalize (GObject *object)
71 GIRepository *repository = G_IREPOSITORY (object);
73 g_hash_table_destroy (repository->priv->typelibs);
74 g_hash_table_destroy (repository->priv->lazy_typelibs);
75 g_hash_table_destroy (repository->priv->info_by_gtype);
77 (* G_OBJECT_CLASS (g_irepository_parent_class)->finalize) (G_OBJECT (repository));
81 g_irepository_class_init (GIRepositoryClass *class)
83 GObjectClass *gobject_class;
85 gobject_class = G_OBJECT_CLASS (class);
87 gobject_class->finalize = g_irepository_finalize;
89 g_type_class_add_private (class, sizeof (GIRepositoryPrivate));
95 g_static_mutex_lock (&globals_lock);
97 if (default_repository == NULL)
99 default_repository = g_object_new (G_TYPE_IREPOSITORY, NULL);
102 if (search_path == NULL)
106 const gchar *type_lib_path_env;
108 /* This variable is intended to take precedence over both the default
109 * search path, as well as anything written into code with g_irepository_prepend_search_path.
111 type_lib_path_env = g_getenv ("GI_TYPELIB_PATH");
114 override_search_path = NULL;
115 if (type_lib_path_env)
120 custom_dirs = g_strsplit (type_lib_path_env, G_SEARCHPATH_SEPARATOR_S, 0);
125 override_search_path = g_slist_prepend (override_search_path, *d);
129 /* ownership of the array content was passed to the list */
130 g_free (custom_dirs);
133 libdir = GOBJECT_INTROSPECTION_LIBDIR;
135 typelib_dir = g_build_filename (libdir, "girepository-1.0", NULL);
137 search_path = g_slist_prepend (search_path, typelib_dir);
139 search_path = g_slist_reverse (search_path);
142 g_static_mutex_unlock (&globals_lock);
146 g_irepository_prepend_search_path (const char *directory)
149 search_path = g_slist_prepend (search_path, g_strdup (directory));
153 * g_irepository_get_search_path:
155 * Returns the search path the GIRepository will use when looking for typelibs.
156 * The string is internal to GIRespository and should not be freed, nor should
159 * Return value: (element-type filename) (transfer none): list of strings
162 g_irepository_get_search_path (void)
169 build_search_path_with_overrides (void)
172 if (override_search_path != NULL)
174 result = g_slist_copy (override_search_path);
175 g_slist_last (result)->next = g_slist_copy (search_path);
178 result = g_slist_copy (search_path);
183 build_typelib_key (const char *name, const char *source)
185 GString *str = g_string_new (name);
186 g_string_append_c (str, '\0');
187 g_string_append (str, source);
188 return g_string_free (str, FALSE);
192 get_typelib_dependencies (GTypelib *typelib)
195 const char *dependencies_glob;
197 header = (Header *)typelib->data;
199 if (header->dependencies == 0)
202 dependencies_glob = g_typelib_get_string (typelib, header->dependencies);
203 return g_strsplit (dependencies_glob, "|", 0);
206 static GIRepository *
207 get_repository (GIRepository *repository)
211 if (repository != NULL)
214 return default_repository;
218 check_version_conflict (GTypelib *typelib,
219 const gchar *namespace,
220 const gchar *expected_version,
221 char **version_conflict)
224 const char *loaded_version;
226 if (expected_version == NULL)
228 if (version_conflict)
229 *version_conflict = NULL;
233 header = (Header*)typelib->data;
234 loaded_version = g_typelib_get_string (typelib, header->nsversion);
235 g_assert (loaded_version != NULL);
237 if (strcmp (expected_version, loaded_version) != 0)
239 if (version_conflict)
240 *version_conflict = (char*)loaded_version;
243 if (version_conflict)
244 *version_conflict = NULL;
249 get_registered_status (GIRepository *repository,
250 const char *namespace,
253 gboolean *lazy_status,
254 char **version_conflict)
257 repository = get_repository (repository);
259 *lazy_status = FALSE;
260 typelib = g_hash_table_lookup (repository->priv->typelibs, namespace);
262 return check_version_conflict (typelib, namespace, version, version_conflict);
263 typelib = g_hash_table_lookup (repository->priv->lazy_typelibs, namespace);
270 return check_version_conflict (typelib, namespace, version, version_conflict);
274 get_registered (GIRepository *repository,
275 const char *namespace,
278 return get_registered_status (repository, namespace, version, TRUE, NULL, NULL);
282 load_dependencies_recurse (GIRepository *repository,
288 dependencies = get_typelib_dependencies (typelib);
290 if (dependencies != NULL)
294 for (i = 0; dependencies[i]; i++)
296 char *dependency = dependencies[i];
297 const char *last_dash;
298 char *dependency_namespace;
299 const char *dependency_version;
301 last_dash = strrchr (dependency, '-');
302 dependency_namespace = g_strndup (dependency, last_dash - dependency);
303 dependency_version = last_dash+1;
305 if (!g_irepository_require (repository, dependency_namespace, dependency_version,
308 g_free (dependency_namespace);
309 g_strfreev (dependencies);
312 g_free (dependency_namespace);
314 g_strfreev (dependencies);
320 register_internal (GIRepository *repository,
327 const gchar *namespace;
328 const gchar *version;
330 g_return_val_if_fail (typelib != NULL, FALSE);
332 header = (Header *)typelib->data;
334 g_return_val_if_fail (header != NULL, FALSE);
336 namespace = g_typelib_get_string (typelib, header->namespace);
337 version = g_typelib_get_string (typelib, header->nsversion);
341 g_assert (!g_hash_table_lookup (repository->priv->lazy_typelibs,
343 g_hash_table_insert (repository->priv->lazy_typelibs,
344 build_typelib_key (namespace, source), (void *)typelib);
351 /* First, try loading all the dependencies */
352 if (!load_dependencies_recurse (repository, typelib, error))
355 /* Check if we are transitioning from lazily loaded state */
356 if (g_hash_table_lookup_extended (repository->priv->lazy_typelibs,
358 (gpointer)&key, &value))
359 g_hash_table_remove (repository->priv->lazy_typelibs, key);
361 key = build_typelib_key (namespace, source);
363 g_hash_table_insert (repository->priv->typelibs, key, (void *)typelib);
366 if (typelib->modules == NULL)
367 typelib->modules = g_list_append(typelib->modules, g_module_open (NULL, 0));
373 * g_irepository_get_dependencies:
374 * @repository: A #GIRepository, may be %NULL for the default
375 * @namespace_: Namespace of interest
377 * Return an array of all (transitive) dependencies for namespace
378 * @namespace_, including version. The returned strings are of the
379 * form <code>namespace-version</code>.
381 * Note: The namespace must have already been loaded using a function
382 * such as #g_irepository_require before calling this function.
384 * Returns: Zero-terminated string array of versioned dependencies
387 g_irepository_get_dependencies (GIRepository *repository,
388 const char *namespace)
392 g_return_val_if_fail (namespace != NULL, NULL);
394 repository = get_repository (repository);
396 typelib = get_registered (repository, namespace, NULL);
397 g_return_val_if_fail (typelib != NULL, NULL);
399 return get_typelib_dependencies (typelib);
403 g_irepository_load_typelib (GIRepository *repository,
405 GIRepositoryLoadFlags flags,
409 const char *namespace;
410 const char *nsversion;
411 gboolean allow_lazy = flags & G_IREPOSITORY_LOAD_FLAG_LAZY;
413 char *version_conflict;
415 repository = get_repository (repository);
417 header = (Header *) typelib->data;
418 namespace = g_typelib_get_string (typelib, header->namespace);
419 nsversion = g_typelib_get_string (typelib, header->nsversion);
421 if (get_registered_status (repository, namespace, nsversion, allow_lazy,
422 &is_lazy, &version_conflict))
424 if (version_conflict != NULL)
426 g_set_error (error, G_IREPOSITORY_ERROR,
427 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
428 "Attempting to load namespace '%s', version '%s', but '%s' is already loaded",
429 namespace, nsversion, version_conflict);
434 return register_internal (repository, "<builtin>",
435 allow_lazy, typelib, error);
439 * g_irepository_is_registered:
440 * @repository: A #GIRepository, may be %NULL for the default
441 * @namespace_: Namespace of interest
442 * @version: (allow-none): Required version, may be %NULL for latest
444 * Check whether a particular namespace (and optionally, a specific
445 * version thereof) is currently loaded. This function is likely to
446 * only be useful in unusual circumstances; in order to act upon
447 * metadata in the namespace, you should call #g_irepository_require
448 * instead which will ensure the namespace is loaded, and return as
449 * quickly as this function will if it has already been loaded.
451 * Returns: %TRUE if namespace-version is loaded, %FALSE otherwise
454 g_irepository_is_registered (GIRepository *repository,
455 const gchar *namespace,
456 const gchar *version)
458 repository = get_repository (repository);
459 return get_registered (repository, namespace, version) != NULL;
463 * g_irepository_get_default:
465 * Returns the singleton process-global default #GIRepository. It is
466 * not currently supported to have multiple repositories in a
467 * particular process, but this function is provided in the unlikely
468 * eventuality that it would become possible, and as a convenience for
469 * higher level language bindings to conform to the GObject method
472 * All methods on #GIRepository also accept %NULL as an instance
473 * parameter to mean this default repository, which is usually more
476 * Returns: (transfer none): The global singleton #GIRepository
479 g_irepository_get_default (void)
481 return get_repository (NULL);
485 * g_irepository_get_n_infos:
486 * @repository: A #GIRepository, may be %NULL for the default
487 * @namespace_: Namespace to inspect
489 * This function returns the number of metadata entries in
490 * given namespace @namespace_. The namespace must have
491 * already been loaded before calling this function.
493 * Returns: number of metadata entries
496 g_irepository_get_n_infos (GIRepository *repository,
497 const gchar *namespace)
500 gint n_interfaces = 0;
502 g_return_val_if_fail (namespace != NULL, -1);
504 repository = get_repository (repository);
506 typelib = get_registered (repository, namespace, NULL);
508 g_return_val_if_fail (typelib != NULL, -1);
510 n_interfaces = ((Header *)typelib->data)->n_local_entries;
525 find_interface (gpointer key,
530 GTypelib *typelib = (GTypelib *)value;
531 IfaceData *iface_data = (IfaceData *)data;
539 n_entries = ((Header *)typelib->data)->n_local_entries;
541 if (iface_data->name)
543 for (i = 1; i <= n_entries; i++)
545 entry = g_typelib_get_dir_entry (typelib, i);
546 name = g_typelib_get_string (typelib, entry->name);
547 if (strcmp (name, iface_data->name) == 0)
554 else if (iface_data->type)
556 for (i = 1; i <= n_entries; i++)
558 RegisteredTypeBlob *blob;
560 entry = g_typelib_get_dir_entry (typelib, i);
561 if (!BLOB_IS_REGISTERED_TYPE (entry))
564 blob = (RegisteredTypeBlob *)(&typelib->data[entry->offset]);
565 if (!blob->gtype_name)
568 type = g_typelib_get_string (typelib, blob->gtype_name);
569 if (strcmp (type, iface_data->type) == 0)
576 else if (iface_data->index > n_entries)
577 iface_data->index -= n_entries;
578 else if (iface_data->index > 0)
580 index = iface_data->index;
581 iface_data->index = 0;
586 entry = g_typelib_get_dir_entry (typelib, index);
587 iface_data->iface = g_info_new_full (entry->blob_type,
589 NULL, typelib, entry->offset);
594 * g_irepository_get_info:
595 * @repository: A #GIRepository, may be %NULL for the default
596 * @namespace_: Namespace to inspect
597 * @index: Offset into namespace metadata for entry
599 * This function returns a particular metadata entry in the
600 * given namespace @namespace_. The namespace must have
601 * already been loaded before calling this function.
603 * Returns: #GIBaseInfo containing metadata
606 g_irepository_get_info (GIRepository *repository,
607 const gchar *namespace,
613 g_return_val_if_fail (namespace != NULL, NULL);
615 repository = get_repository (repository);
617 data.repo = repository;
620 data.index = index + 1;
623 typelib = get_registered (repository, namespace, NULL);
625 g_return_val_if_fail (typelib != NULL, NULL);
627 find_interface ((void *)namespace, typelib, &data);
633 * g_irepository_find_by_gtype:
634 * @repository: A #GIRepository, may be %NULL for the default
635 * @gtype: GType to search for
637 * Searches all loaded namespaces for a particular #GType. Note that
638 * in order to locate the metadata, the namespace corresponding to
639 * the type must first have been loaded. There is currently no
640 * mechanism for determining the namespace which corresponds to an
641 * arbitrary GType - thus, this function will function most reliably
642 * when you have expect the GType to be from a known namespace.
644 * Returns: #GIBaseInfo representing metadata about @type, or %NULL
647 g_irepository_find_by_gtype (GIRepository *repository,
652 repository = get_repository (repository);
654 data.iface = g_hash_table_lookup (repository->priv->info_by_gtype,
658 return g_base_info_ref (data.iface);
660 data.repo = repository;
662 data.type = g_type_name (type);
666 g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
667 g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
670 g_hash_table_insert (repository->priv->info_by_gtype,
672 g_base_info_ref (data.iface));
679 * g_irepository_find_by_name:
680 * @repository: A #GIRepository, may be %NULL for the default
681 * @namespace_: Namespace which will be searched
682 * @name: Entry name to find
684 * Searches for a particular entry in a namespace. Before calling
685 * this function for a particular namespace, you must call
686 * #g_irepository_require once to load the namespace, or otherwise
687 * ensure the namespace has already been loaded.
689 * Returns: #GIBaseInfo representing metadata about @name, or %NULL
692 g_irepository_find_by_name (GIRepository *repository,
693 const gchar *namespace,
699 g_return_val_if_fail (namespace != NULL, NULL);
701 repository = get_repository (repository);
703 data.repo = repository;
709 typelib = get_registered (repository, namespace, NULL);
711 g_return_val_if_fail (typelib != NULL, NULL);
713 find_interface ((void *)namespace, typelib, &data);
719 collect_namespaces (gpointer key,
725 *list = g_list_append (*list, key);
729 * g_irepository_get_namespaces:
730 * @repository: A #GIRepository, may be %NULL for the default
732 * Return the list of currently loaded namespaces.
734 * Returns: (utf8) (transfer full): List of namespaces
737 g_irepository_get_loaded_namespaces (GIRepository *repository)
739 GList *l, *list = NULL;
743 repository = get_repository (repository);
745 g_hash_table_foreach (repository->priv->typelibs, collect_namespaces, &list);
746 g_hash_table_foreach (repository->priv->lazy_typelibs, collect_namespaces, &list);
748 names = g_malloc0 (sizeof (gchar *) * (g_list_length (list) + 1));
750 for (l = list; l; l = l->next)
751 names[i++] = g_strdup (l->data);
758 * g_irepository_get_version:
759 * @repository: A #GIRepository, may be %NULL for the default
760 * @namespace_: Namespace to inspect
762 * This function returns the loaded version associated with the given
763 * namespace @namespace_.
765 * Note: The namespace must have already been loaded using a function
766 * such as #g_irepository_require before calling this function.
768 * Returns: Loaded version
771 g_irepository_get_version (GIRepository *repository,
772 const gchar *namespace)
777 g_return_val_if_fail (namespace != NULL, NULL);
779 repository = get_repository (repository);
781 typelib = get_registered (repository, namespace, NULL);
783 g_return_val_if_fail (typelib != NULL, NULL);
785 header = (Header *) typelib->data;
786 return g_typelib_get_string (typelib, header->nsversion);
790 * g_irepository_get_shared_library:
791 * @repository: A #GIRepository, may be %NULL for the default
792 * @namespace_: Namespace to inspect
794 * This function returns the full path to the shared C library
795 * associated with the given namespace @namespace_. There may be no
796 * shared library path associated, in which case this function will
799 * Note: The namespace must have already been loaded using a function
800 * such as #g_irepository_require before calling this function.
802 * Returns: Full path to shared library, or %NULL if none associated
805 g_irepository_get_shared_library (GIRepository *repository,
806 const gchar *namespace)
811 g_return_val_if_fail (namespace != NULL, NULL);
813 repository = get_repository (repository);
815 typelib = get_registered (repository, namespace, NULL);
817 g_return_val_if_fail (typelib != NULL, NULL);
819 header = (Header *) typelib->data;
820 if (header->shared_library)
821 return g_typelib_get_string (typelib, header->shared_library);
827 * g_irepository_get_typelib_path:
828 * @repository: Repository, may be %NULL for the default
829 * @namespace_: GI namespace to use, e.g. "Gtk"
831 * If namespace @namespace_ is loaded, return the full path to the
832 * .typelib file it was loaded from. If the typelib for
833 * namespace @namespace_ was included in a shared library, return
834 * the special string "$lt;builtin$gt;".
836 * Returns: Filesystem path (or $lt;builtin$gt;) if successful, %NULL if namespace is not loaded
840 g_irepository_get_typelib_path (GIRepository *repository,
841 const gchar *namespace)
843 gpointer orig_key, value;
845 repository = get_repository (repository);
847 if (!g_hash_table_lookup_extended (repository->priv->typelibs, namespace,
850 if (!g_hash_table_lookup_extended (repository->priv->lazy_typelibs, namespace,
855 return ((char*)orig_key) + strlen ((char *) orig_key) + 1;
858 /* This simple search function looks for a specified namespace-version;
859 it's faster than the full directory listing required for latest version. */
861 find_namespace_version (const gchar *namespace,
862 const gchar *version,
867 GError *error = NULL;
868 GMappedFile *mfile = NULL;
871 fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
873 tmp_path = build_search_path_with_overrides ();
874 for (ldir = tmp_path; ldir; ldir = ldir->next)
876 char *path = g_build_filename (ldir->data, fname, NULL);
878 mfile = g_mapped_file_new (path, FALSE, &error);
882 g_clear_error (&error);
889 g_slist_free (tmp_path);
894 parse_version (const char *version,
901 *major = strtol (version, &end, 10);
902 dot = strchr (version, '.');
910 *minor = strtol (dot+1, &end, 10);
911 if (end != (version + strlen (version)))
917 compare_version (const char *v1,
921 int v1_major, v1_minor;
922 int v2_major, v2_minor;
924 success = parse_version (v1, &v1_major, &v1_minor);
927 success = parse_version (v2, &v2_major, &v2_minor);
930 if (v1_major > v2_major)
932 else if (v2_major > v1_major)
934 else if (v1_minor > v2_minor)
936 else if (v2_minor > v1_minor)
941 struct NamespaceVersionCandidadate
950 compare_candidate_reverse (struct NamespaceVersionCandidadate *c1,
951 struct NamespaceVersionCandidadate *c2)
953 int result = compare_version (c1->version, c2->version);
954 /* First, check the version */
961 /* Now check the path index, which says how early in the search path
962 * we found it. This ensures that of equal version targets, we
963 * pick the earlier one.
965 if (c1->path_index == c2->path_index)
967 else if (c1->path_index > c2->path_index)
975 free_candidate (struct NamespaceVersionCandidadate *candidate)
977 g_mapped_file_free (candidate->mfile);
978 g_free (candidate->path);
979 g_free (candidate->version);
984 find_namespace_latest (const gchar *namespace,
990 GError *error = NULL;
991 char *namespace_dash;
992 char *namespace_typelib;
993 GSList *candidates = NULL;
994 GMappedFile *result = NULL;
1000 namespace_dash = g_strdup_printf ("%s-", namespace);
1001 namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
1004 tmp_path = build_search_path_with_overrides ();
1005 for (ldir = tmp_path; ldir; ldir = ldir->next)
1008 const char *dirname;
1011 dirname = (const char*)ldir->data;
1012 dir = g_dir_open (dirname, 0, NULL);
1015 while ((entry = g_dir_read_name (dir)) != NULL)
1018 char *path, *version;
1019 struct NamespaceVersionCandidadate *candidate;
1021 if (!g_str_has_suffix (entry, ".typelib"))
1024 if (g_str_has_prefix (entry, namespace_dash))
1026 const char *last_dash;
1027 const char *name_end;
1030 name_end = strrchr (entry, '.');
1031 last_dash = strrchr (entry, '-');
1032 version = g_strndup (last_dash+1, name_end-(last_dash+1));
1033 if (!parse_version (version, &major, &minor))
1039 path = g_build_filename (dirname, entry, NULL);
1040 mfile = g_mapped_file_new (path, FALSE, &error);
1045 g_clear_error (&error);
1048 candidate = g_new0 (struct NamespaceVersionCandidadate, 1);
1049 candidate->mfile = mfile;
1050 candidate->path_index = index;
1051 candidate->path = path;
1052 candidate->version = version;
1053 candidates = g_slist_prepend (candidates, candidate);
1059 if (candidates != NULL)
1061 struct NamespaceVersionCandidadate *elected;
1062 candidates = g_slist_sort (candidates, (GCompareFunc) compare_candidate_reverse);
1064 elected = (struct NamespaceVersionCandidadate *) candidates->data;
1065 /* Remove the elected one so we don't try to free its contents */
1066 candidates = g_slist_delete_link (candidates, candidates);
1068 result = elected->mfile;
1069 *path_ret = elected->path;
1070 *version_ret = elected->version;
1071 g_free (elected); /* just free the container */
1072 g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
1073 g_slist_free (candidates);
1075 g_free (namespace_dash);
1076 g_free (namespace_typelib);
1077 g_slist_free (tmp_path);
1082 * g_irepository_require:
1083 * @repository: (allow-none): Repository, may be %NULL for the default
1084 * @namespace_: GI namespace to use, e.g. "Gtk"
1085 * @version: (allow-none): Version of namespace, may be %NULL for latest
1086 * @flags: Set of %GIRepositoryLoadFlags, may be %0
1087 * @error: a #GError.
1089 * Force the namespace @namespace_ to be loaded if it isn't already.
1090 * If @namespace_ is not loaded, this function will search for a
1091 * ".typelib" file using the repository search path. In addition, a
1092 * version @version of namespace may be specified. If @version is
1093 * not specified, the latest will be used.
1095 * Returns: a pointer to the #GTypelib if successful, %NULL otherwise
1098 g_irepository_require (GIRepository *repository,
1099 const gchar *namespace,
1100 const gchar *version,
1101 GIRepositoryLoadFlags flags,
1105 GTypelib *ret = NULL;
1107 GTypelib *typelib = NULL;
1108 const gchar *typelib_namespace, *typelib_version;
1109 gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
1111 char *version_conflict = NULL;
1113 char *tmp_version = NULL;
1115 g_return_val_if_fail (namespace != NULL, FALSE);
1117 repository = get_repository (repository);
1119 typelib = get_registered_status (repository, namespace, version, allow_lazy,
1120 &is_lazy, &version_conflict);
1124 if (version_conflict != NULL)
1126 g_set_error (error, G_IREPOSITORY_ERROR,
1127 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
1128 "Requiring namespace '%s' version '%s', but '%s' is already loaded",
1129 namespace, version, version_conflict);
1133 if (version != NULL)
1135 mfile = find_namespace_version (namespace, version, &path);
1136 tmp_version = g_strdup (version);
1140 mfile = find_namespace_latest (namespace, &tmp_version, &path);
1145 if (version != NULL)
1146 g_set_error (error, G_IREPOSITORY_ERROR,
1147 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1148 "Typelib file for namespace '%s', version '%s' not found",
1149 namespace, version);
1151 g_set_error (error, G_IREPOSITORY_ERROR,
1152 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1153 "Typelib file for namespace '%s' (any version) not found",
1158 typelib = g_typelib_new_from_mapped_file (mfile);
1159 header = (Header *) typelib->data;
1160 typelib_namespace = g_typelib_get_string (typelib, header->namespace);
1161 typelib_version = g_typelib_get_string (typelib, header->nsversion);
1163 if (strcmp (typelib_namespace, namespace) != 0)
1165 g_set_error (error, G_IREPOSITORY_ERROR,
1166 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1167 "Typelib file %s for namespace '%s' contains "
1168 "namespace '%s' which doesn't match the file name",
1169 path, namespace, typelib_namespace);
1172 if (version != NULL && strcmp (typelib_version, version) != 0)
1174 g_set_error (error, G_IREPOSITORY_ERROR,
1175 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1176 "Typelib file %s for namespace '%s' contains "
1177 "version '%s' which doesn't match the expected version '%s'",
1178 path, namespace, typelib_version, version);
1182 if (!register_internal (repository, path, allow_lazy,
1185 g_typelib_free (typelib);
1190 g_free (tmp_version);
1196 g_irepository_introspect_cb (const char *option_name,
1201 gboolean ret = g_irepository_dump (value, error);
1205 static const GOptionEntry introspection_args[] = {
1206 { "introspect-dump", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK,
1207 g_irepository_introspect_cb, "Dump introspection information",
1208 "infile.txt,outfile.xml" },
1213 g_irepository_get_option_group (void)
1215 GOptionGroup *group;
1216 group = g_option_group_new ("girepository", "Introspection Options", "Show Introspection Options", NULL, NULL);
1218 g_option_group_add_entries (group, introspection_args);
1223 g_irepository_error_quark (void)
1225 static GQuark quark = 0;
1227 quark = g_quark_from_static_string ("g-irepository-error-quark");
1232 g_type_tag_to_string (GITypeTag type)
1236 case GI_TYPE_TAG_VOID:
1238 case GI_TYPE_TAG_BOOLEAN:
1240 case GI_TYPE_TAG_INT8:
1242 case GI_TYPE_TAG_UINT8:
1244 case GI_TYPE_TAG_INT16:
1246 case GI_TYPE_TAG_UINT16:
1248 case GI_TYPE_TAG_INT32:
1250 case GI_TYPE_TAG_UINT32:
1252 case GI_TYPE_TAG_INT64:
1254 case GI_TYPE_TAG_UINT64:
1256 case GI_TYPE_TAG_INT:
1258 case GI_TYPE_TAG_UINT:
1260 case GI_TYPE_TAG_LONG:
1262 case GI_TYPE_TAG_ULONG:
1264 case GI_TYPE_TAG_SSIZE:
1266 case GI_TYPE_TAG_SIZE:
1268 case GI_TYPE_TAG_FLOAT:
1270 case GI_TYPE_TAG_DOUBLE:
1272 case GI_TYPE_TAG_TIME_T:
1274 case GI_TYPE_TAG_GTYPE:
1276 case GI_TYPE_TAG_UTF8:
1278 case GI_TYPE_TAG_FILENAME:
1280 case GI_TYPE_TAG_ARRAY:
1282 case GI_TYPE_TAG_INTERFACE:
1284 case GI_TYPE_TAG_GLIST:
1286 case GI_TYPE_TAG_GSLIST:
1288 case GI_TYPE_TAG_GHASH:
1290 case GI_TYPE_TAG_ERROR: