Added new function to read file contents.
authorDaniel Carl <danielcarl@gmx.de>
Mon, 7 Jan 2013 19:44:09 +0000 (20:44 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Mon, 7 Jan 2013 19:44:09 +0000 (20:44 +0100)
src/command.c
src/util.c
src/util.h

index 58abc48..4645b2e 100644 (file)
@@ -23,6 +23,7 @@
 #include "setting.h"
 #include "completion.h"
 #include "hints.h"
+#include "util.h"
 
 static CommandInfo cmd_list[] = {
     /* command              function             arg                                                                                 mode */
@@ -144,7 +145,7 @@ gboolean command_open_closed(const Arg* arg)
     gboolean result;
 
     Arg a = {arg->i};
-    g_file_get_contents(vp.files[FILES_CLOSED], &a.s, NULL, NULL);
+    a.s = util_get_file_contents(vp.files[FILES_CLOSED], NULL);
     result = vp_load_uri(&a);
     g_free(a.s);
 
index 4f1486d..3a84eae 100644 (file)
@@ -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;
+}
index 12e6e8f..85e1db7 100644 (file)
@@ -27,5 +27,6 @@ gchar* util_get_cache_dir(void);
 const gchar* util_get_home_dir(void);
 void util_create_dir_if_not_exists(const gchar* dirpath);
 void util_create_file_if_not_exists(const gchar* filename);
+gchar* util_get_file_contents(const gchar* filename, gsize* length);
 
 #endif /* end of include guard: UTIL_H */