aboutsummaryrefslogtreecommitdiff
path: root/src/elements.c
blob: 96b33e6539998387004ca695a1464d40711ce4a2 (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
/*
 * elements.c
 *
 * Elemental Data
 *
 * (c) 2006-2007 Thomas White <taw27@cam.ac.uk>
 *
 *  synth2D - Two-Dimensional Crystallographic Fourier Synthesis
 *
 */

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

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

#include "elements.h"

Element elements[255];
static unsigned int elements_initialised = 0;

void elements_initialise() {

	int i, whoops;
	FILE *fh;

	if ( elements_initialised ) return;

	i = 0;  whoops = 0;
	fh = fopen(DATADIR"/synth2d/elements", "r");
	do {

		char line[512];
		char buf[512];
		float a1, b1, a2, b2, a3, b3, a4, b4, c;

		fgets(line, 511, fh);
		if ( ferror(fh) || feof(fh) ) {
			whoops = 1;
			break;
		}
		if ( strlen(line) > 1 ) {

			if ( line[strlen(line)-1] == '\n' ) {
				line[strlen(line)-1] = '\0';
			}

			sscanf(line, "%s\t%i\t%9f\t%9f\t%9f\t%9f\t%9f\t%9f\t%9f\t%9f\t%9f", buf, &elements[i].z, &a1, &b1, &a2, &b2, &a3, &b3, &a4, &b4, &c);
			elements[i].element_name = strdup(buf);
			elements[i].sfac_a1 = a1;  elements[i].sfac_b1 = b1;
			elements[i].sfac_a2 = a2;  elements[i].sfac_b2 = b2;
			elements[i].sfac_a3 = a3;  elements[i].sfac_b3 = b3;
			elements[i].sfac_a4 = a4;  elements[i].sfac_b4 = b4;
			elements[i].sfac_c = c;
			
			i++;
		
		} else {
			line[0] = '\0';
		}
		
	}  while ( !whoops && !feof(fh) );

	elements[i].element_name = "EOF";
	elements_initialised = 1;
	
}

unsigned int elements_lookup(const char *name) {

	size_t i;
	
	i = 0;
	while ( strcmp(elements[i].element_name, "EOF") != 0 ) {
		if ( strcmp(elements[i].element_name, name) == 0 ) return i;
		i++;
	}
	
	fprintf(stderr, "Failed to look up element\n");
	return i;

}