diff options
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); |