aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortaw27 <taw27@bf6ca9ba-c028-0410-8290-897cf20841d1>2007-04-01 16:02:04 +0000
committertaw27 <taw27@bf6ca9ba-c028-0410-8290-897cf20841d1>2007-04-01 16:02:04 +0000
commit3a6347591e01e019d25df3faf274152cb97edb1d (patch)
treead60bf874767cf0eb871b577f841998b620b8e13
parentf72af885e7beb97127e4300d635ab156b4336094 (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
-rw-r--r--src/cache.c20
-rw-r--r--src/cache.h16
2 files changed, 9 insertions, 27 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);
diff --git a/src/cache.h b/src/cache.h
index 38371e4..92145fb 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -23,22 +23,6 @@ typedef struct struct_cacheheader {
double scale;
} CacheHeader;
-/* Can't just use Reflection type because size of a pointer may vary */
-typedef struct struct_cachedreflection {
-
- double x;
- double y;
- double z;
- double intensity;
-
- signed int h;
- signed int k;
- signed int l;
-
- ReflectionType type;
-
-} CachedReflection;
-
extern ReflectionContext *cache_load(const char *filename);
extern int cache_save(const char *filename, ReflectionContext *rctx);