2 * Copyright (C) 2003 Robert Kooima
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.
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.
19 #include <SDL_endian.h>
21 /*---------------------------------------------------------------------------*/
23 void put_float(FILE *fout, const float *f)
25 const unsigned char *p = (const unsigned char *) f;
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);
33 fputc((int) p[0], fout);
34 fputc((int) p[1], fout);
35 fputc((int) p[2], fout);
36 fputc((int) p[3], fout);
40 void put_index(FILE *fout, const int *i)
42 const unsigned char *p = (const unsigned char *) i;
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);
50 fputc((int) p[0], fout);
51 fputc((int) p[1], fout);
52 fputc((int) p[2], fout);
53 fputc((int) p[3], fout);
57 void put_short(FILE *fout, const short *s)
59 const unsigned char *p = (const unsigned char *) s;
61 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
62 fputc((int) p[1], fout);
63 fputc((int) p[0], fout);
65 fputc((int) p[0], fout);
66 fputc((int) p[1], fout);
70 void put_array(FILE *fout, const float *v, size_t n)
74 for (i = 0; i < n; i++)
75 put_float(fout, v + i);
78 /*---------------------------------------------------------------------------*/
80 void get_float(FILE *fin, float *f)
82 unsigned char *p = (unsigned char *) f;
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);
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);
97 void get_index(FILE *fin, int *i)
99 unsigned char *p = (unsigned char *) i;
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);
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);
114 void get_short(FILE *fin, short *s)
116 unsigned char *p = (unsigned char *) s;
118 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
119 p[1] = (unsigned char) fgetc(fin);
120 p[0] = (unsigned char) fgetc(fin);
122 p[0] = (unsigned char) fgetc(fin);
123 p[1] = (unsigned char) fgetc(fin);
127 void get_array(FILE *fin, float *v, size_t n)
131 for (i = 0; i < n; i++)
132 get_float(fin, v + i);
135 /*---------------------------------------------------------------------------*/
137 void put_string(FILE *fout, const char *s)
143 void get_string(FILE *fin, char *s, int max)
150 while (max && c && (c = fgetc(fin)) != EOF)
157 * Terminate the buffer ourselves, if we ran out of space without
158 * seeing a NUL character.
161 if (max == 0 && *(s - 1) != 0)
165 /*---------------------------------------------------------------------------*/