summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorDaniel Carl <danielcarl@gmx.de>2013-01-07 20:44:09 +0100
committerDaniel Carl <danielcarl@gmx.de>2013-01-07 20:44:09 +0100
commit3b7fb4f22912df6dd6179b55596ac488065a4d1b (patch)
treea88999e0af35c725930300c8413b37df90cfb278 /src/util.c
parentec4140096c39ec47fb97df32698b9d71ee15f8d0 (diff)
Added new function to read file contents.
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index 4f1486d..3a84eae 100644
--- a/src/util.c
+++ b/src/util.c
@@ -61,3 +61,21 @@ void util_create_file_if_not_exists(const char* filename)
fclose(f);
}
}
+
+/**
+ * Retrieves the length bytes from given file.
+ *
+ * The memory of returned string have to be freed!
+ */
+gchar* util_get_file_contents(const gchar* filename, gsize* length)
+{
+ GError* error = NULL;
+ gchar* content = NULL;
+ if (!(g_file_test(filename, G_FILE_TEST_IS_REGULAR)
+ && g_file_get_contents(filename, &content, length, &error))
+ ) {
+ fprintf(stderr, "Cannot open %s: %s\n", filename, error ? error->message : "file not found");
+ g_clear_error(&error);
+ }
+ return content;
+}