aboutsummaryrefslogtreecommitdiff
path: root/src/cache.c
blob: a146eb184a79d35758e445be9fd464e38779d200 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * cache.c
 *
 * Save the reflection datablock to save having to recalculate it
 *
 * (c) 2007 Gordon Ball <gfb21@cam.ac.uk>
 *	    Thomas White <taw27@cam.ac.uk>
 *
 *  dtr - Diffraction Tomography Reconstruction
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "reflections.h"
#include "cache.h"

ReflectionContext *cache_load(const char *filename) {

	FILE *f;
	CacheHeader ch;
	ReflectionContext *rctx;

	rctx = reflection_init();
	f = fopen(filename, "rb");
	fread(&ch, sizeof(CacheHeader), 1, f);
	
	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));
		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);
		
	}
	
	fclose(f);
	
	return rctx;
}

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";
	
	count = 0;
	r = rctx->reflections;
	while ( r != NULL ) {
		count++;
		r = r->next;
	};
	
	f = fopen(filename, "wb");
	memcpy(&ch.top, &top, sizeof(top));
	ch.count = count;
	ch.scale = 0.; //temp, currently doesn't do anything
	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);
		r = r->next;
		
	};
	free(rnp);
	
	fclose(f);
	
	return 0;
	
}