aboutsummaryrefslogtreecommitdiff
path: root/src/cache.c
diff options
context:
space:
mode:
authortaw27 <taw27@bf6ca9ba-c028-0410-8290-897cf20841d1>2007-04-01 18:30:55 +0000
committertaw27 <taw27@bf6ca9ba-c028-0410-8290-897cf20841d1>2007-04-01 18:30:55 +0000
commitcffdac83222aef2b7d6381a1bf4f639789b65d82 (patch)
tree432c8bb04cb1b2ddc71d1b4621d8753b555eedce /src/cache.c
parent506930a4e3e09276e1cf44f25820acc2e5f7f19f (diff)
Error checking input file loading
git-svn-id: svn://cook.msm.cam.ac.uk:745/diff-tomo/dtr@23 bf6ca9ba-c028-0410-8290-897cf20841d1
Diffstat (limited to 'src/cache.c')
-rw-r--r--src/cache.c26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/cache.c b/src/cache.c
index 99975c6..8aa7839 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -28,20 +28,34 @@ ReflectionContext *cache_load(const char *filename) {
CacheHeader ch;
ReflectionContext *rctx;
size_t cachedreflection_size;
+ int i;
cachedreflection_size = sizeof(Reflection) - sizeof(Reflection *);
rctx = reflection_init();
f = fopen(filename, "rb");
- fread(&ch, sizeof(CacheHeader), 1, f);
+ if ( !f ) {
+ fprintf(stderr, "Couldn't read reflections from cache\n");
+ }
+ if ( fread(&ch, sizeof(CacheHeader), 1, f) == 0 ) {
+ fprintf(stderr, "Couldn't read reflections from cache\n");
+ fclose(f);
+ return NULL;
+ }
- int i;
for ( i=0; i<ch.count; i++ ) {
Reflection *cr;
cr = malloc(sizeof(Reflection));
- fread(cr, cachedreflection_size, 1, f);
+ if ( fread(cr, cachedreflection_size, 1, f) == 0 ) {
+ fprintf(stderr, "Couldn't read reflections from cache\n");
+ fclose(f);
+ free(cr);
+ reflection_clear(rctx);
+ return NULL;
+ }
+
cr->next = NULL; /* Guarantee swift failure in the event of a screw-up */
//printf("reading (%f,%f,%f) i=%f (%d,%d,%d) %d\n",cr->x,cr->y,cr->z,cr->intensity,cr->h,cr->k,cr->l,cr->type);
reflection_add_from_reflection(rctx, cr);
@@ -64,7 +78,7 @@ int cache_save(const char *input_filename, ReflectionContext *rctx) {
char *cache_filename;
cache_filename = malloc(strlen(input_filename)+7);
- strcpy(cache_filename, ctx->filename);
+ strcpy(cache_filename, input_filename);
strcat(cache_filename, ".cache");
printf("Caching reflections to %s\n", cache_filename);
@@ -79,6 +93,10 @@ int cache_save(const char *input_filename, ReflectionContext *rctx) {
f = fopen(cache_filename, "wb");
free(cache_filename);
+ if ( f == NULL ) {
+ printf("Couldn't save reflection cache\n");
+ return -1;
+ }
memcpy(&ch.top, &top, sizeof(top));
ch.count = count;
ch.scale = 0.; //temp, currently doesn't do anything