tennis.map: Goal chair tweaks
[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 #include <string.h>
18
19 #include <SDL_endian.h>
20
21 /*---------------------------------------------------------------------------*/
22
23 void put_float(FILE *fout, const float *f)
24 {
25     const unsigned char *p = (const 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     const unsigned char *p = (const 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_short(FILE *fout, const short *s)
58 {
59     const unsigned char *p = (const unsigned char *) s;
60
61 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
62     fputc((int) p[1], fout);
63     fputc((int) p[0], fout);
64 #else
65     fputc((int) p[0], fout);
66     fputc((int) p[1], fout);
67 #endif
68 }
69
70 void put_array(FILE *fout, const float *v, size_t n)
71 {
72     size_t i;
73
74     for (i = 0; i < n; i++)
75         put_float(fout, v + i);
76 }
77
78 /*---------------------------------------------------------------------------*/
79
80 void get_float(FILE *fin, float *f)
81 {
82     unsigned char *p = (unsigned char *) f;
83
84 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
85     p[3] = (unsigned char) fgetc(fin);
86     p[2] = (unsigned char) fgetc(fin);
87     p[1] = (unsigned char) fgetc(fin);
88     p[0] = (unsigned char) fgetc(fin);
89 #else
90     p[0] = (unsigned char) fgetc(fin);
91     p[1] = (unsigned char) fgetc(fin);
92     p[2] = (unsigned char) fgetc(fin);
93     p[3] = (unsigned char) fgetc(fin);
94 #endif
95 }
96
97 void get_index(FILE *fin, int *i)
98 {
99     unsigned char *p = (unsigned char *) i;
100
101 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
102     p[3] = (unsigned char) fgetc(fin);
103     p[2] = (unsigned char) fgetc(fin);
104     p[1] = (unsigned char) fgetc(fin);
105     p[0] = (unsigned char) fgetc(fin);
106 #else
107     p[0] = (unsigned char) fgetc(fin);
108     p[1] = (unsigned char) fgetc(fin);
109     p[2] = (unsigned char) fgetc(fin);
110     p[3] = (unsigned char) fgetc(fin);
111 #endif
112 }
113
114 void get_short(FILE *fin, short *s)
115 {
116     unsigned char *p = (unsigned char *) s;
117
118 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
119     p[1] = (unsigned char) fgetc(fin);
120     p[0] = (unsigned char) fgetc(fin);
121 #else
122     p[0] = (unsigned char) fgetc(fin);
123     p[1] = (unsigned char) fgetc(fin);
124 #endif
125 }
126
127 void get_array(FILE *fin, float *v, size_t n)
128 {
129     size_t i;
130
131     for (i = 0; i < n; i++)
132         get_float(fin, v + i);
133 }
134
135 /*---------------------------------------------------------------------------*/
136
137 void put_string(FILE *fout, const char *s)
138 {
139     fputs(s, fout);
140     fputc('\0', fout);
141 }
142
143 void get_string(FILE *fin, char *s, int max)
144 {
145     do
146         *s = (char) fgetc(fin);
147     while (*s++ != '\0' && max-- > 0);
148
149     if(*(s - 1) != '\0')
150         *(s - 1) = '\0';
151 }
152
153 /*---------------------------------------------------------------------------*/