summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2012-11-29 16:57:19 -0500
committerColin Walters <walters@verbum.org>2012-11-29 16:57:19 -0500
commiteb4cecbc1528e1d1bdd99298d6f58768c6642b24 (patch)
treeb3256822416e37fd2cbf8fd62b4931eab1c8d632
parentab2588da8442fc6135af13ee624dd91b73a7051c (diff)
downloadlibgsystem-eb4cecbc1528e1d1bdd99298d6f58768c6642b24.tar.gz
fileutils: Add gs_file_load_contents_utf8()
Imported from ostree.
-rw-r--r--gsystem-file-utils.c41
-rw-r--r--gsystem-file-utils.h4
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