diff options
Diffstat (limited to 'libcrystfel/src/utils.c')
-rw-r--r-- | libcrystfel/src/utils.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/libcrystfel/src/utils.c b/libcrystfel/src/utils.c index 4873b050..de1dc2f4 100644 --- a/libcrystfel/src/utils.c +++ b/libcrystfel/src/utils.c @@ -674,3 +674,42 @@ struct rvec quat_rot(struct rvec q, struct quaternion z) return res; } + + +char *load_entire_file(const char *filename) +{ + struct stat statbuf; + int r; + char *contents; + FILE *fh; + + r = stat(filename, &statbuf); + if ( r != 0 ) { + ERROR("File '%s' not found\n", filename); + return NULL; + } + + contents = malloc(statbuf.st_size+1); + if ( contents == NULL ) { + ERROR("Failed to allocate memory for file\n"); + return NULL; + } + + fh = fopen(filename, "r"); + if ( fh == NULL ) { + ERROR("Failed to open file '%s'\n", filename); + free(contents); + return NULL; + } + + if ( fread(contents, 1, statbuf.st_size, fh) != statbuf.st_size ) { + ERROR("Failed to read file '%s'\n", filename); + free(contents); + return NULL; + } + contents[statbuf.st_size] = '\0'; + + fclose(fh); + + return contents; +} |