From: Daniel Carl Date: Mon, 7 Jan 2013 19:44:09 +0000 (+0100) Subject: Added new function to read file contents. X-Git-Url: https://git.owens.tech/projects.html/projects.html/git?a=commitdiff_plain;h=3b7fb4f22912df6dd6179b55596ac488065a4d1b;p=vimb.git Added new function to read file contents. --- diff --git a/src/command.c b/src/command.c index 58abc48..4645b2e 100644 --- a/src/command.c +++ b/src/command.c @@ -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); 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; +} diff --git a/src/util.h b/src/util.h index 12e6e8f..85e1db7 100644 --- a/src/util.h +++ b/src/util.h @@ -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 */