diff options
Diffstat (limited to 'src/core/support/file.c')
-rw-r--r-- | src/core/support/file.c | 72 |
1 files changed, 33 insertions, 39 deletions
diff --git a/src/core/support/file.c b/src/core/support/file.c index 96ca7e19da..c1361d8a9e 100644 --- a/src/core/support/file.c +++ b/src/core/support/file.c @@ -42,52 +42,46 @@ #include "src/core/support/string.h" -gpr_slice -gpr_load_file (const char *filename, int add_null_terminator, int *success) -{ +gpr_slice gpr_load_file(const char *filename, int add_null_terminator, + int *success) { unsigned char *contents = NULL; size_t contents_size = 0; char *error_msg = NULL; - gpr_slice result = gpr_empty_slice (); - FILE *file = fopen (filename, "rb"); + gpr_slice result = gpr_empty_slice(); + FILE *file = fopen(filename, "rb"); size_t bytes_read = 0; - if (file == NULL) - { - gpr_asprintf (&error_msg, "Could not open file %s (error = %s).", filename, strerror (errno)); - GPR_ASSERT (error_msg != NULL); - goto end; - } - fseek (file, 0, SEEK_END); + if (file == NULL) { + gpr_asprintf(&error_msg, "Could not open file %s (error = %s).", filename, + strerror(errno)); + GPR_ASSERT(error_msg != NULL); + goto end; + } + fseek(file, 0, SEEK_END); /* Converting to size_t on the assumption that it will not fail */ - contents_size = (size_t) ftell (file); - fseek (file, 0, SEEK_SET); - contents = gpr_malloc (contents_size + (add_null_terminator ? 1 : 0)); - bytes_read = fread (contents, 1, contents_size, file); - if (bytes_read < contents_size) - { - GPR_ASSERT (ferror (file)); - gpr_asprintf (&error_msg, "Error %s occured while reading file %s.", strerror (errno), filename); - GPR_ASSERT (error_msg != NULL); - goto end; - } - if (success != NULL) - *success = 1; - if (add_null_terminator) - { - contents[contents_size++] = 0; - } - result = gpr_slice_new (contents, contents_size, gpr_free); + contents_size = (size_t)ftell(file); + fseek(file, 0, SEEK_SET); + contents = gpr_malloc(contents_size + (add_null_terminator ? 1 : 0)); + bytes_read = fread(contents, 1, contents_size, file); + if (bytes_read < contents_size) { + GPR_ASSERT(ferror(file)); + gpr_asprintf(&error_msg, "Error %s occured while reading file %s.", + strerror(errno), filename); + GPR_ASSERT(error_msg != NULL); + goto end; + } + if (success != NULL) *success = 1; + if (add_null_terminator) { + contents[contents_size++] = 0; + } + result = gpr_slice_new(contents, contents_size, gpr_free); end: - if (error_msg != NULL) - { - gpr_log (GPR_ERROR, "%s", error_msg); - gpr_free (error_msg); - if (success != NULL) - *success = 0; - } - if (file != NULL) - fclose (file); + if (error_msg != NULL) { + gpr_log(GPR_ERROR, "%s", error_msg); + gpr_free(error_msg); + if (success != NULL) *success = 0; + } + if (file != NULL) fclose(file); return result; } |