[libgsystem] fileutils: Add gs_file_load_contents_utf8()



commit eb4cecbc1528e1d1bdd99298d6f58768c6642b24
Author: Colin Walters <walters verbum org>
Date:   Thu Nov 29 16:57:19 2012 -0500

    fileutils: Add gs_file_load_contents_utf8()
    
    Imported from ostree.

 gsystem-file-utils.c |   41 +++++++++++++++++++++++++++++++++++++++++
 gsystem-file-utils.h |    4 ++++
 2 files changed, 45 insertions(+), 0 deletions(-)
---
diff --git a/gsystem-file-utils.c b/gsystem-file-utils.c
index 0ff6abd..081cf18 100644
--- a/gsystem-file-utils.c
+++ b/gsystem-file-utils.c
@@ -239,3 +239,44 @@ gs_file_ensure_directory (GFile         *dir,
  out:
   return ret;
 }
+
+/**
+ * gs_file_load_contents_utf8:
+ * @file: Path to file whose contents must be UTF-8
+ * @cancellable:
+ * @error:
+ *
+ * Like g_file_load_contents(), except validates the contents are
+ * UTF-8.
+ */
+gchar *
+gs_file_load_contents_utf8 (GFile         *file,
+                            GCancellable  *cancellable,
+                            GError       **error)
+{
+  gboolean ret = FALSE;
+  gsize len;
+  char *ret_contents = NULL;
+
+  if (!g_file_load_contents (file, cancellable, &ret_contents, &len,
+                             NULL, error))
+    goto out;
+  if (!g_utf8_validate (ret_contents, len, NULL))
+    {
+      g_set_error (error,
+                   G_IO_ERROR,
+                   G_IO_ERROR_INVALID_DATA,
+                   "Invalid UTF-8");
+      goto out;
+    }
+
+  ret = TRUE;
+ out:
+  if (!ret)
+    {
+      g_free (ret_contents);
+      return NULL;
+    }
+  return ret_contents;
+}
+
diff --git a/gsystem-file-utils.h b/gsystem-file-utils.h
index ff87c6e..47d3c4e 100644
--- a/gsystem-file-utils.h
+++ b/gsystem-file-utils.h
@@ -47,6 +47,10 @@ gboolean gs_file_ensure_directory (GFile          *dir,
                                    GCancellable   *cancellable,
                                    GError        **error);
 
+gchar *gs_file_load_contents_utf8 (GFile         *file,
+                                   GCancellable  *cancellable,
+                                   GError       **error);
+
 
 G_END_DECLS
 



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