aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel/src/utils.c
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2020-02-21 17:00:34 +0100
committerThomas White <taw@physics.org>2020-07-29 18:39:50 +0200
commit7724da7f1ac050838ffce551c9dad52083daf873 (patch)
tree896097fa9bc12a1d0980b03051939ab921d22ab6 /libcrystfel/src/utils.c
parent63eadd0582724e2626dddf0729ec991c1979b8cc (diff)
Move load_entire_file() to utils
Diffstat (limited to 'libcrystfel/src/utils.c')
-rw-r--r--libcrystfel/src/utils.c39
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;
+}