girepository: Make gi_repository_get_shared_library() return an array

And rename it to `gi_repository_get_shared_libraries()`. Previously it
returned a comma-separated string, which wasn’t particularly typesafe or
machine-friendly. Now it returns the same data as an array.

This is an API break in libgirepository, but since it’s not been in a
stable release yet, that’s fine.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3155
This commit is contained in:
Philip Withnall 2024-01-05 12:33:02 +00:00
parent bbe9046ade
commit 64725d8cae
3 changed files with 52 additions and 17 deletions

View File

@ -92,6 +92,9 @@ struct _GIRepositoryPrivate
GHashTable *info_by_error_domain; /* GQuark -> GIBaseInfo */
GHashTable *interfaces_for_gtype; /* GType -> GTypeInterfaceCache */
GHashTable *unknown_gtypes; /* hashset of GType */
char **cached_shared_libraries; /* (owned) (nullable) (array zero-terminated=1) */
size_t cached_n_shared_libraries; /* length of @cached_shared_libraries, not including NULL terminator */
};
G_DEFINE_TYPE_WITH_CODE (GIRepository, gi_repository, G_TYPE_OBJECT, G_ADD_PRIVATE (GIRepository));
@ -170,6 +173,8 @@ gi_repository_finalize (GObject *object)
g_hash_table_destroy (repository->priv->interfaces_for_gtype);
g_hash_table_destroy (repository->priv->unknown_gtypes);
g_clear_pointer (&repository->priv->cached_shared_libraries, g_strfreev);
(* G_OBJECT_CLASS (gi_repository_parent_class)->finalize) (G_OBJECT (repository));
}
@ -1201,12 +1206,14 @@ gi_repository_get_version (GIRepository *repository,
}
/**
* gi_repository_get_shared_library:
* gi_repository_get_shared_libraries:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @namespace_: Namespace to inspect
* @out_n_elements: (out) (optional): Return location for the number of elements
* in the returned array
*
* This function returns a comma-separated list of paths to the
* This function returns an array of paths to the
* shared C libraries associated with the given namespace @namespace_.
*
* There may be no shared library path associated, in which case this
@ -1216,13 +1223,17 @@ gi_repository_get_version (GIRepository *repository,
* such as [method@GIRepository.Repository.require] before calling this
* function.
*
* Returns: (nullable): Comma-separated list of paths to shared libraries,
* or `NULL` if none are associated
* The list is internal to [class@GIRepository.Repository] and should not be
* freed, nor should its string elements.
*
* Returns: (nullable) (array length=out_n_elements) (transfer none): Array of
* paths to shared libraries, or `NULL` if none are associated
* Since: 2.80
*/
const gchar *
gi_repository_get_shared_library (GIRepository *repository,
const gchar *namespace)
const char * const *
gi_repository_get_shared_libraries (GIRepository *repository,
const gchar *namespace,
size_t *out_n_elements)
{
GITypelib *typelib;
Header *header;
@ -1236,10 +1247,29 @@ gi_repository_get_shared_library (GIRepository *repository,
g_return_val_if_fail (typelib != NULL, NULL);
header = (Header *) typelib->data;
if (header->shared_library)
return gi_typelib_get_string (typelib, header->shared_library);
else
return NULL;
if (!header->shared_library)
{
if (out_n_elements != NULL)
*out_n_elements = 0;
return NULL;
}
/* Populate the cache. */
if (repository->priv->cached_shared_libraries == NULL)
{
const char *comma_separated = gi_typelib_get_string (typelib, header->shared_library);
if (comma_separated != NULL && *comma_separated != '\0')
{
repository->priv->cached_shared_libraries = g_strsplit (comma_separated, ",", -1);
repository->priv->cached_n_shared_libraries = g_strv_length (repository->priv->cached_shared_libraries);
}
}
if (out_n_elements != NULL)
*out_n_elements = repository->priv->cached_n_shared_libraries;
return (const char * const *) repository->priv->cached_shared_libraries;
}
/**

View File

@ -185,8 +185,9 @@ GI_AVAILABLE_IN_ALL
const gchar * gi_repository_get_typelib_path (GIRepository *repository,
const gchar *namespace_);
GI_AVAILABLE_IN_ALL
const gchar * gi_repository_get_shared_library (GIRepository *repository,
const gchar *namespace_);
const gchar * const *gi_repository_get_shared_libraries (GIRepository *repository,
const gchar *namespace_,
size_t *out_n_elements);
GI_AVAILABLE_IN_ALL
const gchar * gi_repository_get_c_prefix (GIRepository *repository,
const gchar *namespace_);

View File

@ -1390,7 +1390,7 @@ gi_ir_writer_write (const char *filename,
if (TRUE)
{
const gchar *shared_library;
const char * const *shared_libraries;
const gchar *c_prefix;
const char *cur_ns = ns;
const char *cur_version;
@ -1398,12 +1398,16 @@ gi_ir_writer_write (const char *filename,
cur_version = gi_repository_get_version (repository, cur_ns);
shared_library = gi_repository_get_shared_library (repository, cur_ns);
shared_libraries = gi_repository_get_shared_libraries (repository, cur_ns, NULL);
c_prefix = gi_repository_get_c_prefix (repository, cur_ns);
xml_start_element (xml, "namespace");
xml_printf (xml, " name=\"%s\" version=\"%s\"", cur_ns, cur_version);
if (shared_library)
xml_printf (xml, " shared-library=\"%s\"", shared_library);
if (shared_libraries != NULL)
{
char *shared_libraries_str = g_strjoinv (",", (char **) shared_libraries);
xml_printf (xml, " shared-library=\"%s\"", shared_libraries_str);
g_free (shared_libraries_str);
}
if (c_prefix)
xml_printf (xml, " c:prefix=\"%s\"", c_prefix);