1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
reverse_float(float x)5 float reverse_float (float x)
6 {
7     float y;
8     char *src = (char *) &x;
9     char *targ = (char *) &y;
10 
11     /* swap the bytes */
12     targ[0] = src[3];
13     targ[1] = src[2];
14     targ[2] = src[1];
15     targ[3] = src[0];
16 
17     return y;
18 }
19 
main(int argc,char ** argv)20 int main (int argc, char **argv)
21 {
22     const char *binfile = "fedstl.bin";
23     const char *datfile = "fedstl.dat";
24     char datpath[512];
25     int swap_ends = 0;
26     FILE *fdat, *fbin;
27     double xx;
28     float x;
29 
30     if (argc == 2 && !strcmp(argv[1], "--swap-ends")) {
31 	swap_ends = 1;
32 	argc--;
33     } else if (argc == 3 && !strcmp(argv[2], "--swap-ends")) {
34 	swap_ends = 1;
35 	argc--;
36     }
37 
38     if (swap_ends) {
39 	puts("*** making data file with swapped endianness");
40     }
41 
42     if (argc > 1) {
43 	sprintf(datpath, "%s/%s", argv[1], datfile);
44     } else {
45 	strcpy(datpath, datfile);
46     }
47 
48     fdat = fopen(datpath, "r");
49     if (fdat == NULL) {
50 	fprintf(stderr, "Couldn't open %s\n", datfile);
51 	exit(EXIT_FAILURE);
52     }
53 
54     fbin = fopen(binfile, "wb");
55     if (fbin == NULL) {
56 	fprintf(stderr, "Couldn't open %s\n", binfile);
57 	exit(EXIT_FAILURE);
58     }
59 
60     while (fscanf(fdat, "%lf", &xx) == 1) {
61 	x = xx;
62 	if (swap_ends) {
63 	    reverse_float(x);
64 	}
65 	fwrite(&x, sizeof x, 1, fbin);
66     }
67 
68     fclose(fbin);
69     fclose(fdat);
70 
71     return 0;
72 }
73