Initial import.
[neverball] / share / binary.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #include <SDL.h>
19 #include <SDL_byteorder.h>
20
21 /*---------------------------------------------------------------------------*/
22
23 void put_float(FILE *fout, const float *f)
24 {
25     unsigned char *p = (unsigned char *) f;
26
27 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
28     fputc((int) p[3], fout);
29     fputc((int) p[2], fout);
30     fputc((int) p[1], fout);
31     fputc((int) p[0], fout);
32 #else
33     fputc((int) p[0], fout);
34     fputc((int) p[1], fout);
35     fputc((int) p[2], fout);
36     fputc((int) p[3], fout);
37 #endif
38 }
39
40 void put_index(FILE *fout, const int *i)
41 {
42     unsigned char *p = (unsigned char *) i;
43
44 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
45     fputc((int) p[3], fout);
46     fputc((int) p[2], fout);
47     fputc((int) p[1], fout);
48     fputc((int) p[0], fout);
49 #else
50     fputc((int) p[0], fout);
51     fputc((int) p[1], fout);
52     fputc((int) p[2], fout);
53     fputc((int) p[3], fout);
54 #endif
55 }
56
57 void put_array(FILE *fout, const float *v, size_t n)
58 {
59     size_t i;
60
61     for (i = 0; i < n; i++)
62         put_float(fout, v + i);
63 }
64
65 /*---------------------------------------------------------------------------*/
66
67 void get_float(FILE *fin, float *f)
68 {
69     unsigned char *p = (unsigned char *) f;
70
71 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
72     p[3] = (unsigned char) fgetc(fin);
73     p[2] = (unsigned char) fgetc(fin);
74     p[1] = (unsigned char) fgetc(fin);
75     p[0] = (unsigned char) fgetc(fin);
76 #else
77     p[0] = (unsigned char) fgetc(fin);
78     p[1] = (unsigned char) fgetc(fin);
79     p[2] = (unsigned char) fgetc(fin);
80     p[3] = (unsigned char) fgetc(fin);
81 #endif
82 }
83
84 void get_index(FILE *fin, int *i)
85 {
86     unsigned char *p = (unsigned char *) i;
87
88 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
89     p[3] = (unsigned char) fgetc(fin);
90     p[2] = (unsigned char) fgetc(fin);
91     p[1] = (unsigned char) fgetc(fin);
92     p[0] = (unsigned char) fgetc(fin);
93 #else
94     p[0] = (unsigned char) fgetc(fin);
95     p[1] = (unsigned char) fgetc(fin);
96     p[2] = (unsigned char) fgetc(fin);
97     p[3] = (unsigned char) fgetc(fin);
98 #endif
99 }
100
101 void get_array(FILE *fin, float *v, size_t n)
102 {
103     size_t i;
104
105     for (i = 0; i < n; i++)
106         get_float(fin, v + i);
107 }
108
109 /*---------------------------------------------------------------------------*/