[tracker/wip/sam/meson] libtracker-extract: Add tracker_guarantee_resource_utf8_string()



commit f2bb21189bae995372551bd02eeb25eda4e488c9
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Jul 16 14:05:30 2016 +0200

    libtracker-extract: Add tracker_guarantee_resource_utf8_string()
    
    This is a wrapper to tracker_resource_set_string(), which additionally
    validates the string to be UTF-8, and inserts only as much text as
    could be validated.
    
    Can be thought of as an internal replacement to
    tracker_sparql_builder_object_unvalidated(), the fact that we deal with
    non utf-8 data sources is an implementation detail to extractors, so
    it doesn't make as much sense to make this a public function in the
    shiny new TrackerResource API.

 src/libtracker-extract/tracker-guarantee.c |   22 ++++++++++++++++++++++
 src/libtracker-extract/tracker-guarantee.h |    3 +++
 2 files changed, 25 insertions(+), 0 deletions(-)
---
diff --git a/src/libtracker-extract/tracker-guarantee.c b/src/libtracker-extract/tracker-guarantee.c
index 9b8460a..5c595c9 100644
--- a/src/libtracker-extract/tracker-guarantee.c
+++ b/src/libtracker-extract/tracker-guarantee.c
@@ -348,3 +348,25 @@ tracker_guarantee_resource_date_from_file_mtime (TrackerResource *resource,
 
        return success;
 }
+
+gboolean
+tracker_guarantee_resource_utf8_string (TrackerResource *resource,
+                                        const gchar     *key,
+                                        const gchar     *value)
+{
+       const gchar *end;
+       gchar *str;
+
+       if (!g_utf8_validate (value, -1, &end)) {
+               if (end == value)
+                       return FALSE;
+
+               str = g_strndup (value, end - value);
+               tracker_resource_set_string (resource, key, str);
+               g_free (str);
+       } else {
+               tracker_resource_set_string (resource, key, value);
+       }
+
+       return TRUE;
+}
diff --git a/src/libtracker-extract/tracker-guarantee.h b/src/libtracker-extract/tracker-guarantee.h
index 9b58a63..cdb28c2 100644
--- a/src/libtracker-extract/tracker-guarantee.h
+++ b/src/libtracker-extract/tracker-guarantee.h
@@ -49,6 +49,9 @@ gboolean tracker_guarantee_resource_date_from_file_mtime (TrackerResource  *reso
                                                           const gchar      *key,
                                                           const gchar      *current_value,
                                                           const gchar      *uri);
+gboolean tracker_guarantee_resource_utf8_string (TrackerResource *resource,
+                                                 const gchar     *key,
+                                                 const gchar     *value);
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]