Shared code clean-up. One more and I suppose it's done.
[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.h>
20 #include <SDL_byteorder.h>
21
22 /*---------------------------------------------------------------------------*/
23
24 void put_float(FILE *fout, const float *f)
25 {
26     unsigned char *p = (unsigned char *) f;
27
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);
33 #else
34     fputc((int) p[0], fout);
35     fputc((int) p[1], fout);
36     fputc((int) p[2], fout);
37     fputc((int) p[3], fout);
38 #endif
39 }
40
41 void put_index(FILE *fout, const int *i)
42 {
43     unsigned char *p = (unsigned char *) i;
44
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);
50 #else
51     fputc((int) p[0], fout);
52     fputc((int) p[1], fout);
53     fputc((int) p[2], fout);
54     fputc((int) p[3], fout);
55 #endif
56 }
57
58 void put_array(FILE *fout, const float *v, size_t n)
59 {
60     size_t i;
61
62     for (i = 0; i < n; i++)
63         put_float(fout, v + i);
64 }
65
66 void put_string(FILE *fp, char *str)
67 {
68     int len = strlen(str) + 1;
69     fwrite(str, 1, len,  fp);
70 }
71
72
73 /*---------------------------------------------------------------------------*/
74
75 void get_float(FILE *fin, float *f)
76 {
77     unsigned char *p = (unsigned char *) f;
78
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);
84 #else
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);
89 #endif
90 }
91
92 void get_index(FILE *fin, int *i)
93 {
94     unsigned char *p = (unsigned char *) i;
95
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);
101 #else
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);
106 #endif
107 }
108
109 void get_array(FILE *fin, float *v, size_t n)
110 {
111     size_t i;
112
113     for (i = 0; i < n; i++)
114         get_float(fin, v + i);
115 }
116
117 void get_string(FILE *fp, char *str, int len)
118 /* len includes room for '\0'.  If len is too small, the string is truncated. */
119 {
120     char b;
121
122     while (1)
123     {
124         fread(&b, 1, 1, fp);
125         if (len > 0)
126         {
127             *(str++) = (len > 1 ? b : '\0');
128             len--;
129         }
130         if (b == '\0')
131             return;
132     }
133 }
134
135
136 /*---------------------------------------------------------------------------*/