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.
20 #include <SDL_byteorder.h>
22 /*---------------------------------------------------------------------------*/
24 void put_float(FILE *fout, const float *f)
26 unsigned char *p = (unsigned char *) f;
28 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
29 fputc((int) p[3], fout);
30 fputc((int) p[2], fout);
31 fputc((int) p[1], fout);
32 fputc((int) p[0], fout);
34 fputc((int) p[0], fout);
35 fputc((int) p[1], fout);
36 fputc((int) p[2], fout);
37 fputc((int) p[3], fout);
41 void put_index(FILE *fout, const int *i)
43 unsigned char *p = (unsigned char *) i;
45 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
46 fputc((int) p[3], fout);
47 fputc((int) p[2], fout);
48 fputc((int) p[1], fout);
49 fputc((int) p[0], fout);
51 fputc((int) p[0], fout);
52 fputc((int) p[1], fout);
53 fputc((int) p[2], fout);
54 fputc((int) p[3], fout);
58 void put_array(FILE *fout, const float *v, size_t n)
62 for (i = 0; i < n; i++)
63 put_float(fout, v + i);
66 void put_string(FILE *fp, char * str)
68 int len = strlen(str) + 1;
69 fwrite(str, 1, len, fp);
73 /*---------------------------------------------------------------------------*/
75 void get_float(FILE *fin, float *f)
77 unsigned char *p = (unsigned char *) f;
79 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
80 p[3] = (unsigned char) fgetc(fin);
81 p[2] = (unsigned char) fgetc(fin);
82 p[1] = (unsigned char) fgetc(fin);
83 p[0] = (unsigned char) fgetc(fin);
85 p[0] = (unsigned char) fgetc(fin);
86 p[1] = (unsigned char) fgetc(fin);
87 p[2] = (unsigned char) fgetc(fin);
88 p[3] = (unsigned char) fgetc(fin);
92 void get_index(FILE *fin, int *i)
94 unsigned char *p = (unsigned char *) i;
96 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
97 p[3] = (unsigned char) fgetc(fin);
98 p[2] = (unsigned char) fgetc(fin);
99 p[1] = (unsigned char) fgetc(fin);
100 p[0] = (unsigned char) fgetc(fin);
102 p[0] = (unsigned char) fgetc(fin);
103 p[1] = (unsigned char) fgetc(fin);
104 p[2] = (unsigned char) fgetc(fin);
105 p[3] = (unsigned char) fgetc(fin);
109 void get_array(FILE *fin, float *v, size_t n)
113 for (i = 0; i < n; i++)
114 get_float(fin, v + i);
117 void get_string(FILE *fp, char * str, int len)
118 /* len include the room for the \0 */
119 /* if len is too small, the string is truncated. */
127 *(str++) = (len > 1 ? b : '\0');
136 /*---------------------------------------------------------------------------*/