diff options
author | Daniel Carl <danielcarl@gmx.de> | 2013-01-07 20:44:09 +0100 |
---|---|---|
committer | Daniel Carl <danielcarl@gmx.de> | 2013-01-07 20:44:09 +0100 |
commit | 3b7fb4f22912df6dd6179b55596ac488065a4d1b (patch) | |
tree | a88999e0af35c725930300c8413b37df90cfb278 /src/util.c | |
parent | ec4140096c39ec47fb97df32698b9d71ee15f8d0 (diff) |
Added new function to read file contents.
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -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; +} |