diff options
author | taw27 <taw27@bf6ca9ba-c028-0410-8290-897cf20841d1> | 2007-04-01 18:30:55 +0000 |
---|---|---|
committer | taw27 <taw27@bf6ca9ba-c028-0410-8290-897cf20841d1> | 2007-04-01 18:30:55 +0000 |
commit | cffdac83222aef2b7d6381a1bf4f639789b65d82 (patch) | |
tree | 432c8bb04cb1b2ddc71d1b4621d8753b555eedce /src | |
parent | 506930a4e3e09276e1cf44f25820acc2e5f7f19f (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')
-rw-r--r-- | src/cache.c | 26 | ||||
-rw-r--r-- | src/main.c | 6 |
2 files changed, 25 insertions, 7 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 @@ -65,17 +65,17 @@ static gint main_method_window_response(GtkWidget *method_window, gint response, val=0; } - if ( ctx->inputfiletype != INPUT_CACHE ) { + if ( (ctx->inputfiletype != INPUT_CACHE) && !val && (ctx->reflectionctx) ) { cache_save(ctx->filename, ctx->reflectionctx); } - if ( !val && (ctx->rmode == RECONSTRUCTION_PREDICTION) ) { + if ( !val && (ctx->rmode == RECONSTRUCTION_PREDICTION) && (ctx->reflectionctx) ) { val = ipr_reduce(ctx); } //dump_histogram(ctx->reflectionctx); - if ( val == 0 ) { + if ( !val && (ctx->reflectionctx) ) { displaywindow_open(ctx); } else { fprintf(stderr, "Reconstruction failed.\n"); |