diff options
author | taw27 <taw27@bf6ca9ba-c028-0410-8290-897cf20841d1> | 2007-04-01 16:02:04 +0000 |
---|---|---|
committer | taw27 <taw27@bf6ca9ba-c028-0410-8290-897cf20841d1> | 2007-04-01 16:02:04 +0000 |
commit | 3a6347591e01e019d25df3faf274152cb97edb1d (patch) | |
tree | ad60bf874767cf0eb871b577f841998b620b8e13 /src/cache.c | |
parent | f72af885e7beb97127e4300d635ab156b4336094 (diff) |
A neater way of reading/writing cache files
git-svn-id: svn://cook.msm.cam.ac.uk:745/diff-tomo/dtr@20 bf6ca9ba-c028-0410-8290-897cf20841d1
Diffstat (limited to 'src/cache.c')
-rw-r--r-- | src/cache.c | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/src/cache.c b/src/cache.c index a146eb1..61fe4bd 100644 --- a/src/cache.c +++ b/src/cache.c @@ -27,6 +27,9 @@ ReflectionContext *cache_load(const char *filename) { FILE *f; CacheHeader ch; ReflectionContext *rctx; + size_t cachedreflection_size; + + cachedreflection_size = sizeof(Reflection) - sizeof(Reflection *); rctx = reflection_init(); f = fopen(filename, "rb"); @@ -35,13 +38,10 @@ ReflectionContext *cache_load(const char *filename) { int i; for ( i=0; i<ch.count; i++ ) { - CachedReflection rnp; Reflection *cr; - - fread(&rnp, sizeof(CachedReflection), 1, f); cr = malloc(sizeof(Reflection)); - memcpy(cr, &rnp, sizeof(CachedReflection)); + fread(cr, cachedreflection_size, 1, f); 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); @@ -58,9 +58,11 @@ int cache_save(const char *filename, ReflectionContext *rctx) { FILE *f; CacheHeader ch; Reflection *r; - CachedReflection *rnp; int count; - char top[16] = "DTRCACHE\0\0\0\0\0\0\0\0"; + const char top[16] = "DTRCACHE\0\0\0\0\0\0\0\0"; + size_t cachedreflection_size; + + cachedreflection_size = sizeof(Reflection) - sizeof(Reflection *); count = 0; r = rctx->reflections; @@ -76,16 +78,12 @@ int cache_save(const char *filename, ReflectionContext *rctx) { fwrite(&ch, sizeof(CacheHeader), 1, f); r = rctx->reflections; - rnp = malloc(sizeof(CachedReflection)); while ( r != NULL ) { - memcpy(rnp, r, sizeof(CachedReflection)); - //printf("writing (%f,%f,%f) i=%f (%d,%d,%d) %d\n",rnp->x,rnp->y,rnp->z,rnp->intensity,rnp->h,rnp->k,rnp->l,rnp->type); - fwrite(rnp, sizeof(CachedReflection), 1, f); + fwrite(r, cachedreflection_size, 1, f); /* Write the reflection block, stopping just short of the "next" pointer */ r = r->next; }; - free(rnp); fclose(f); |