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 #include "fs.h"
22 
23 /*---------------------------------------------------------------------------*/
24 
put_float(fs_file fout,float f)25 void put_float(fs_file fout, float f)
26 {
27     unsigned char *p = (unsigned char *) &f;
28 
29 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
30     fs_putc((int) p[3], fout);
31     fs_putc((int) p[2], fout);
32     fs_putc((int) p[1], fout);
33     fs_putc((int) p[0], fout);
34 #else
35     fs_putc((int) p[0], fout);
36     fs_putc((int) p[1], fout);
37     fs_putc((int) p[2], fout);
38     fs_putc((int) p[3], fout);
39 #endif
40 }
41 
put_index(fs_file fout,int i)42 void put_index(fs_file fout, int i)
43 {
44     unsigned char *p = (unsigned char *) &i;
45 
46 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
47     fs_putc((int) p[3], fout);
48     fs_putc((int) p[2], fout);
49     fs_putc((int) p[1], fout);
50     fs_putc((int) p[0], fout);
51 #else
52     fs_putc((int) p[0], fout);
53     fs_putc((int) p[1], fout);
54     fs_putc((int) p[2], fout);
55     fs_putc((int) p[3], fout);
56 #endif
57 }
58 
put_short(fs_file fout,short s)59 void put_short(fs_file fout, short s)
60 {
61     unsigned char *p = (unsigned char *) &s;
62 
63 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
64     fs_putc((int) p[1], fout);
65     fs_putc((int) p[0], fout);
66 #else
67     fs_putc((int) p[0], fout);
68     fs_putc((int) p[1], fout);
69 #endif
70 }
71 
put_array(fs_file fout,const float * v,size_t n)72 void put_array(fs_file fout, const float *v, size_t n)
73 {
74     size_t i;
75 
76     for (i = 0; i < n; i++)
77         put_float(fout, v[i]);
78 }
79 
80 /*---------------------------------------------------------------------------*/
81 
get_float(fs_file fin)82 float get_float(fs_file fin)
83 {
84     float f;
85 
86     unsigned char *p = (unsigned char *) &f;
87 
88 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
89     p[3] = (unsigned char) fs_getc(fin);
90     p[2] = (unsigned char) fs_getc(fin);
91     p[1] = (unsigned char) fs_getc(fin);
92     p[0] = (unsigned char) fs_getc(fin);
93 #else
94     p[0] = (unsigned char) fs_getc(fin);
95     p[1] = (unsigned char) fs_getc(fin);
96     p[2] = (unsigned char) fs_getc(fin);
97     p[3] = (unsigned char) fs_getc(fin);
98 #endif
99     return f;
100 }
101 
get_index(fs_file fin)102 int get_index(fs_file fin)
103 {
104     int i;
105 
106     unsigned char *p = (unsigned char *) &i;
107 
108 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
109     p[3] = (unsigned char) fs_getc(fin);
110     p[2] = (unsigned char) fs_getc(fin);
111     p[1] = (unsigned char) fs_getc(fin);
112     p[0] = (unsigned char) fs_getc(fin);
113 #else
114     p[0] = (unsigned char) fs_getc(fin);
115     p[1] = (unsigned char) fs_getc(fin);
116     p[2] = (unsigned char) fs_getc(fin);
117     p[3] = (unsigned char) fs_getc(fin);
118 #endif
119     return i;
120 }
121 
get_short(fs_file fin)122 short get_short(fs_file fin)
123 {
124     short s;
125 
126     unsigned char *p = (unsigned char *) &s;
127 
128 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
129     p[1] = (unsigned char) fs_getc(fin);
130     p[0] = (unsigned char) fs_getc(fin);
131 #else
132     p[0] = (unsigned char) fs_getc(fin);
133     p[1] = (unsigned char) fs_getc(fin);
134 #endif
135     return s;
136 }
137 
get_array(fs_file fin,float * v,size_t n)138 void get_array(fs_file fin, float *v, size_t n)
139 {
140     size_t i;
141 
142     for (i = 0; i < n; i++)
143         v[i] = get_float(fin);
144 }
145 
146 /*---------------------------------------------------------------------------*/
147 
put_string(fs_file fout,const char * s)148 void put_string(fs_file fout, const char *s)
149 {
150     fs_puts(s, fout);
151     fs_putc('\0', fout);
152 }
153 
get_string(fs_file fin,char * s,size_t max)154 void get_string(fs_file fin, char *s, size_t max)
155 {
156     size_t pos = 0;
157     int c;
158 
159     while ((c = fs_getc(fin)) >= 0)
160     {
161         if (pos < max)
162         {
163             s[pos++] = c;
164 
165             /* Terminate the string, but keep reading until NUL. */
166 
167             if (pos == max)
168                 s[pos - 1] = 0;
169         }
170 
171         if (c == 0)
172             break;
173     }
174 }
175 
176 /*---------------------------------------------------------------------------*/
177