1 /*
2 * Serialize.cpp
3 * Copyright (C) 2007 by Bryan Duff <duff0097@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20 #include <unistd.h>
21
22 #include "Models.h"
23 #include "Quaternions.h"
24 #include "Serialize.h"
25
26 /* these all read big-endian data */
27
ReadBool(int fd,int count,bool * b)28 int ReadBool(int fd, int count, bool * b)
29 {
30 while(count--) {
31 unsigned char buf[1];
32
33 if(read(fd, buf, 1) != 1) {
34 STUB_FUNCTION;
35 }
36
37 *b = (buf[0] != 0) ? true : false;
38
39 #if DEBUG2
40 printf("ReadBool(): %d: bool: %u\n", count, *b);
41 #endif
42 b++;
43 }
44
45 return 1;
46 }
47
ReadShort(int fd,int count,short * s)48 int ReadShort(int fd, int count, short *s)
49 {
50 while(count--) {
51 unsigned char buf[2];
52
53 if(read(fd, buf, 2) != 2) {
54 STUB_FUNCTION;
55 }
56
57 *s = (short)((buf[0] << 8) | buf[1]);
58
59 #if DEBUG2
60 printf("ReadShort(): %d: short: %d\n", count, *s);
61 #endif
62 s++;
63 }
64
65 return 1;
66 }
67
ReadInt(int fd,int count,int * s,bool rf)68 int ReadInt(int fd, int count, int *s, bool rf)
69 {
70 while(count--) {
71 unsigned char buf[4];
72
73 if(read(fd, buf, 4) != 4) {
74 STUB_FUNCTION;
75 }
76
77 *s = (int)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
78
79 #if DEBUG2
80 if(!rf)
81 printf("ReadInt(): %d: int: %d\n", count, *s);
82 #endif
83 s++;
84 }
85
86 return 1;
87 }
88
89 union intfloat {
90 int i;
91 float f;
92 } intfloat;
93
ReadFloat(int fd,int count,float * f)94 int ReadFloat(int fd, int count, float *f)
95 {
96 union intfloat infl;
97
98 while(count--) {
99 ReadInt(fd, 1, &(infl.i), true);
100
101 *f = infl.f;
102
103 #if DEBUG2
104 printf("ReadFloat(): %d: float: %f\n", count, *f);
105 #endif
106 f++;
107 }
108
109 return 1;
110 }
111
ReadXYZ(int fd,int count,XYZ * xyz)112 int ReadXYZ(int fd, int count, XYZ * xyz)
113 {
114 while(count--) {
115 ReadFloat(fd, 1, &(xyz->x));
116 ReadFloat(fd, 1, &(xyz->y));
117 ReadFloat(fd, 1, &(xyz->z));
118
119 #if DEBUG2
120 printf("ReadXYZ(): %d: \n", count);
121 #endif
122 xyz++;
123 }
124
125 return 1;
126 }
127
ReadTexturedTriangle(int fd,int count,TexturedTriangle * tt)128 int ReadTexturedTriangle(int fd, int count, TexturedTriangle * tt)
129 {
130 while(count--) {
131 #if DEBUG2
132 printf("ReadTexturedTriangle(): %d: \n", count);
133 #endif
134 short pad;
135 ReadShort(fd, 3, tt->vertex);
136 ReadShort(fd, 1, &pad); /* crud */
137 ReadFloat(fd, 1, &(tt->r));
138 ReadFloat(fd, 1, &(tt->g));
139 ReadFloat(fd, 1, &(tt->b));
140
141 #if DEBUG2
142 printf("ReadTexturedTriangle(): %d: done.\n", count);
143 #endif
144 tt++;
145 }
146
147 return count;
148 }
149
WriteBool(int fd,int count,bool * b)150 int WriteBool(int fd, int count, bool * b)
151 {
152 while(count--) {
153 unsigned char buf[1];
154 b[0] = (unsigned char)*b;
155
156 if(write(fd, buf, 1) != 1) {
157 STUB_FUNCTION;
158 }
159
160 #if DEBUG2
161 printf("WriteBool(): %d: bool: %u\n", count, *b);
162 #endif
163 b++;
164 }
165
166 return 1;
167 }
168
WriteShort(int fd,int count,short * s)169 int WriteShort(int fd, int count, short *s)
170 {
171 while(count--) {
172 unsigned char buf[2];
173 buf[0] = s[0];
174 buf[1] = s[1];
175
176 if(read(fd, buf, 2) != 2) {
177 STUB_FUNCTION;
178 }
179
180 #if DEBUG2
181 printf("WriteShort(): %d: short: %d\n", count, *s);
182 #endif
183 s++;
184 }
185
186 return 1;
187 }
188
WriteInt(int fd,int count,int * s,bool rf)189 int WriteInt(int fd, int count, int *s, bool rf)
190 {
191 while(count--) {
192 unsigned char buf[4];
193 buf[0] = s[0];
194 buf[1] = s[1];
195 buf[2] = s[2];
196 buf[3] = s[3];
197
198 if(read(fd, buf, 4) != 4) {
199 STUB_FUNCTION;
200 }
201
202 #if DEBUG2
203 if(!rf)
204 printf("WriteInt(): %d: int: %d\n", count, *s);
205 #endif
206 s++;
207 }
208
209 return 1;
210 }
211
WriteFloat(int fd,int count,float * f)212 int WriteFloat(int fd, int count, float *f)
213 {
214 union intfloat infl;
215
216 while(count--) {
217 infl.f = *f;
218 WriteInt(fd, 1, &(infl.i), true);
219
220 #if DEBUG2
221 printf("WriteFloat(): %d: float: %f\n", count, *f);
222 #endif
223 f++;
224 }
225
226 return 1;
227 }
228
WriteXYZ(int fd,int count,XYZ * xyz)229 int WriteXYZ(int fd, int count, XYZ * xyz)
230 {
231 while(count--) {
232 WriteFloat(fd, 1, &(xyz->x));
233 WriteFloat(fd, 1, &(xyz->y));
234 WriteFloat(fd, 1, &(xyz->z));
235
236 #if DEBUG2
237 printf("WriteXYZ(): %d: \n", count);
238 #endif
239 xyz++;
240 }
241
242 return 1;
243 }
244
WriteTexturedTriangle(int fd,int count,TexturedTriangle * tt)245 int WriteTexturedTriangle(int fd, int count, TexturedTriangle * tt)
246 {
247 while(count--) {
248 #if DEBUG2
249 printf("WriteTexturedTriangle(): %d: \n", count);
250 #endif
251 short pad = 0;
252 WriteShort(fd, 3, tt->vertex);
253 WriteShort(fd, 1, &pad); /* crud */
254 WriteFloat(fd, 1, &(tt->r));
255 WriteFloat(fd, 1, &(tt->g));
256 WriteFloat(fd, 1, &(tt->b));
257
258 #if DEBUG2
259 printf("WriteTexturedTriangle(): %d: done.\n", count);
260 #endif
261 tt++;
262 }
263
264 return count;
265 }
266
267