[tracker/wip/carlosg/namespace-api-updates: 1/4] libtracker-sparql: Add public TrackerNamespaceManager method to compress URIs
- From: Carlos Garnacho <carlosg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/wip/carlosg/namespace-api-updates: 1/4] libtracker-sparql: Add public TrackerNamespaceManager method to compress URIs
- Date: Sat, 13 Nov 2021 15:15:24 +0000 (UTC)
commit e44d78ed93cffd34298dc2f8fdb94796287abf4f
Author: Carlos Garnacho <carlosg gnome org>
Date: Sun Nov 7 11:26:53 2021 +0100
libtracker-sparql: Add public TrackerNamespaceManager method to compress URIs
It's sometimes also useful (mostly, printing purposes) to compress full URIs
into a $prefix:$suffix form. Add the missing TrackerNamespaceManager method
to help do that.
src/libtracker-sparql/tracker-namespace-manager.c | 38 +++++++++++++++++++++++
src/libtracker-sparql/tracker-namespace-manager.h | 3 ++
2 files changed, 41 insertions(+)
---
diff --git a/src/libtracker-sparql/tracker-namespace-manager.c
b/src/libtracker-sparql/tracker-namespace-manager.c
index 832be6e27..f89e16f9e 100644
--- a/src/libtracker-sparql/tracker-namespace-manager.c
+++ b/src/libtracker-sparql/tracker-namespace-manager.c
@@ -286,6 +286,44 @@ tracker_namespace_manager_expand_uri (TrackerNamespaceManager *self,
}
}
+/**
+ * tracker_namespace_manager_compress_uri:
+ * @self: a #TrackerNamespaceManager
+ * @uri: a URI or compact URI
+ *
+ * If @uri begins with one of the namespaces known to this
+ * #TrackerNamespaceManager, then the return value will be the
+ * compressed URI. Otherwise, a copy of @uri will be returned.
+ *
+ * Returns: (transfer full): a newly-allocated string
+ *
+ * Since: 3.3
+ **/
+char *
+tracker_namespace_manager_compress_uri (TrackerNamespaceManager *self,
+ const char *uri)
+{
+ TrackerNamespaceManagerPrivate *priv;
+ GHashTableIter iter;
+ const char *prefix, *namespace, *suffix;
+
+ g_return_val_if_fail (TRACKER_IS_NAMESPACE_MANAGER (self), NULL);
+ g_return_val_if_fail (uri != NULL, NULL);
+
+ priv = GET_PRIVATE (self);
+
+ g_hash_table_iter_init (&iter, priv->prefix_to_namespace);
+
+ while (g_hash_table_iter_next (&iter, (gpointer *) &prefix, (gpointer *) &namespace)) {
+ if (g_str_has_prefix (uri, namespace)) {
+ suffix = &uri[strlen(namespace) + 1];
+ return g_strdup_printf ("%s:%s", prefix, suffix);
+ }
+ }
+
+ return g_strdup (uri);
+}
+
/**
* tracker_namespace_manager_print_turtle:
* @self: a #TrackerNamespaceManager
diff --git a/src/libtracker-sparql/tracker-namespace-manager.h
b/src/libtracker-sparql/tracker-namespace-manager.h
index 85a30a74d..ce123aabb 100644
--- a/src/libtracker-sparql/tracker-namespace-manager.h
+++ b/src/libtracker-sparql/tracker-namespace-manager.h
@@ -48,6 +48,9 @@ TrackerNamespaceManager *tracker_namespace_manager_get_default (void);
TRACKER_AVAILABLE_IN_ALL
char *tracker_namespace_manager_expand_uri (TrackerNamespaceManager *self, const char *compact_uri);
+TRACKER_AVAILABLE_IN_3_3
+char *tracker_namespace_manager_compress_uri (TrackerNamespaceManager *self, const char *uri);
+
TRACKER_AVAILABLE_IN_ALL
gboolean tracker_namespace_manager_has_prefix (TrackerNamespaceManager *self, const char *prefix);
TRACKER_AVAILABLE_IN_ALL
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]