zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
lib3ds_io.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
3  All rights reserved.
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 2.1 of the License, or
8  (at your option) any later version.
9 
10  Thisprogram 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 Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program; If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "lib3ds_impl.h"
19 
20 
21 typedef union {
22  uint32_t dword_value;
23  float float_value;
24 } Lib3dsDwordFloat;
25 
26 
27 void
29  assert(io);
30  io->impl = calloc(sizeof(Lib3dsIoImpl), 1);
31 }
32 
33 
34 void
36  Lib3dsIoImpl *impl;
37  assert(io);
38  impl = (Lib3dsIoImpl*)io->impl;
39  if (impl->tmp_mem) {
40  free(impl->tmp_mem);
41  impl->tmp_mem = NULL;
42  }
43  if (impl->tmp_node) {
45  impl->tmp_node = NULL;
46  }
47  free(impl);
48 }
49 
50 
51 long
53  assert(io);
54  if (!io || !io->seek_func) {
55  return 0;
56  }
57  return (*io->seek_func)(io->self, offset, origin);
58 }
59 
60 
61 long
63  assert(io);
64  if (!io || !io->tell_func) {
65  return 0;
66  }
67  return (*io->tell_func)(io->self);
68 }
69 
70 
71 size_t
72 lib3ds_io_read(Lib3dsIo *io, void *buffer, size_t size) {
73  assert(io);
74  if (!io || !io->read_func) {
75  return 0;
76  }
77  return (*io->read_func)(io->self, buffer, size);
78 }
79 
80 
81 size_t
82 lib3ds_io_write(Lib3dsIo *io, const void *buffer, size_t size) {
83  assert(io);
84  if (!io || !io->write_func) {
85  return 0;
86  }
87  return (*io->write_func)(io->self, buffer, size);
88 }
89 
90 
91 static void
93  if (!io || !io->log_func)
94  return;
95  (*io->log_func)(io->self, level, ((Lib3dsIoImpl*)io->impl)->log_indent, str);
96 }
97 
98 
99 void
101  va_list args;
102  /* FIXME */ char str[1024];
103 
104  assert(io);
105  if (!io || !io->log_func)
106  return;
107 
108  va_start(args, format);
109  /* FIXME: */ vsprintf(str, format, args);
110  lib3ds_io_log_str(io, level, str);
111 
112  if (level == LIB3DS_LOG_ERROR) {
113  longjmp(((Lib3dsIoImpl*)io->impl)->jmpbuf, 1);
114  }
115 }
116 
117 
118 void
119 lib3ds_io_log_indent(Lib3dsIo *io, int indent) {
120  assert(io);
121  if (!io)
122  return;
123  ((Lib3dsIoImpl*)io->impl)->log_indent += indent;
124 }
125 
126 
127 void
129  lib3ds_io_log(io, LIB3DS_LOG_ERROR, "Reading from input stream failed.");
130 }
131 
132 
133 void
135  lib3ds_io_log(io, LIB3DS_LOG_ERROR, "Writing to output stream failed.");
136 }
137 
138 
142 uint8_t
144  uint8_t b;
145 
146  assert(io);
147  lib3ds_io_read(io, &b, 1);
148  return(b);
149 }
150 
151 
155 uint16_t
157  uint8_t b[2];
158  uint16_t w;
159 
160  assert(io);
161  lib3ds_io_read(io, b, 2);
162  w = ((uint16_t)b[1] << 8) |
163  ((uint16_t)b[0]);
164  return(w);
165 }
166 
167 
171 uint32_t
173  uint8_t b[4];
174  uint32_t d;
175 
176  assert(io);
177  lib3ds_io_read(io, b, 4);
178  d = ((uint32_t)b[3] << 24) |
179  ((uint32_t)b[2] << 16) |
180  ((uint32_t)b[1] << 8) |
181  ((uint32_t)b[0]);
182  return(d);
183 }
184 
185 
189 int8_t
191  int8_t b;
192 
193  assert(io);
194  lib3ds_io_read(io, &b, 1);
195  return(b);
196 }
197 
198 
202 int16_t
204  uint8_t b[2];
205  uint16_t w;
206 
207  assert(io);
208  lib3ds_io_read(io, b, 2);
209  w = ((uint16_t)b[1] << 8) |
210  ((uint16_t)b[0]);
211  return((int16_t)w);
212 }
213 
214 
218 int32_t
220  uint8_t b[4];
221  uint32_t d;
222 
223  assert(io);
224  lib3ds_io_read(io, b, 4);
225  d = ((uint32_t)b[3] << 24) |
226  ((uint32_t)b[2] << 16) |
227  ((uint32_t)b[1] << 8) |
228  ((uint32_t)b[0]);
229  return((int32_t)d);
230 }
231 
232 
236 float
238  uint8_t b[4];
239  Lib3dsDwordFloat d;
240 
241  assert(io);
242  lib3ds_io_read(io, b, 4);
243  d.dword_value = ((uint32_t)b[3] << 24) |
244  ((uint32_t)b[2] << 16) |
245  ((uint32_t)b[1] << 8) |
246  ((uint32_t)b[0]);
247  return d.float_value;
248 }
249 
250 
257 void
259  assert(io);
260  v[0] = lib3ds_io_read_float(io);
261  v[1] = lib3ds_io_read_float(io);
262  v[2] = lib3ds_io_read_float(io);
263 }
264 
265 
266 void
267 lib3ds_io_read_rgb(Lib3dsIo *io, float rgb[3]) {
268  assert(io);
269  rgb[0] = lib3ds_io_read_float(io);
270  rgb[1] = lib3ds_io_read_float(io);
271  rgb[2] = lib3ds_io_read_float(io);
272 }
273 
274 
284 void
285 lib3ds_io_read_string(Lib3dsIo *io, char *s, int buflen) {
286  char c;
287  int k = 0;
288 
289  assert(io);
290  for (;;) {
291  if (lib3ds_io_read(io, &c, 1) != 1) {
293  }
294  *s++ = c;
295  if (!c) {
296  break;
297  }
298  ++k;
299  if (k >= buflen) {
300  lib3ds_io_log(io, LIB3DS_LOG_ERROR, "Invalid string in input stream.");
301  }
302  }
303 }
304 
305 
309 void
311  assert(io);
312  if (lib3ds_io_write(io, &b, 1) != 1) {
314  }
315 }
316 
317 
321 void
323  uint8_t b[2];
324 
325  assert(io);
326  b[1] = ((uint16_t)w & 0xFF00) >> 8;
327  b[0] = ((uint16_t)w & 0x00FF);
328  if (lib3ds_io_write(io, b, 2) != 2) {
330  }
331 }
332 
333 
337 void
339  uint8_t b[4];
340 
341  assert(io);
342  b[3] = (uint8_t)(((uint32_t)d & 0xFF000000) >> 24);
343  b[2] = (uint8_t)(((uint32_t)d & 0x00FF0000) >> 16);
344  b[1] = (uint8_t)(((uint32_t)d & 0x0000FF00) >> 8);
345  b[0] = (uint8_t)(((uint32_t)d & 0x000000FF));
346  if (lib3ds_io_write(io, b, 4) != 4) {
348  }
349 }
350 
351 
355 void
357  assert(io);
358  if (lib3ds_io_write(io, &b, 1) != 1) {
360  }
361 }
362 
363 
367 void
369  uint8_t b[2];
370 
371  assert(io);
372  b[1] = ((uint16_t)w & 0xFF00) >> 8;
373  b[0] = ((uint16_t)w & 0x00FF);
374  if (lib3ds_io_write(io, b, 2) != 2) {
376  }
377 }
378 
379 
383 void
385  uint8_t b[4];
386 
387  assert(io);
388  b[3] = (uint8_t)(((uint32_t)d & 0xFF000000) >> 24);
389  b[2] = (uint8_t)(((uint32_t)d & 0x00FF0000) >> 16);
390  b[1] = (uint8_t)(((uint32_t)d & 0x0000FF00) >> 8);
391  b[0] = (uint8_t)(((uint32_t)d & 0x000000FF));
392  if (lib3ds_io_write(io, b, 4) != 4) {
394  }
395 }
396 
397 
401 void
403  uint8_t b[4];
404  Lib3dsDwordFloat d;
405 
406  assert(io);
407  d.float_value = l;
408  b[3] = (uint8_t)(((uint32_t)d.dword_value & 0xFF000000) >> 24);
409  b[2] = (uint8_t)(((uint32_t)d.dword_value & 0x00FF0000) >> 16);
410  b[1] = (uint8_t)(((uint32_t)d.dword_value & 0x0000FF00) >> 8);
411  b[0] = (uint8_t)(((uint32_t)d.dword_value & 0x000000FF));
412  if (lib3ds_io_write(io, b, 4) != 4) {
414  }
415 }
416 
417 
421 void
423  int i;
424  for (i = 0; i < 3; ++i) {
425  lib3ds_io_write_float(io, v[i]);
426  }
427 }
428 
429 
430 void
431 lib3ds_io_write_rgb(Lib3dsIo *io, float rgb[3]) {
432  int i;
433  for (i = 0; i < 3; ++i) {
434  lib3ds_io_write_float(io, rgb[i]);
435  }
436 }
437 
438 
442 void
443 lib3ds_io_write_string(Lib3dsIo *io, const char *s) {
444  size_t len;
445  assert(io && s);
446  len = strlen(s);
447  if (lib3ds_io_write(io, s, len + 1) != len +1) {
449  }
450 }
void lib3ds_io_write_intw(Lib3dsIo *io, int16_t w)
Definition: lib3ds_io.c:368
int16_t lib3ds_io_read_intw(Lib3dsIo *io)
Definition: lib3ds_io.c:203
GLdouble s
Definition: glew.h:1376
size_t lib3ds_io_read(Lib3dsIo *io, void *buffer, size_t size)
Definition: lib3ds_io.c:72
void lib3ds_io_write_intd(Lib3dsIo *io, int32_t d)
Definition: lib3ds_io.c:384
int32_t lib3ds_io_read_intd(Lib3dsIo *io)
Definition: lib3ds_io.c:219
#define NULL
Definition: ftobjs.h:61
unsigned short uint16_t
int32_t k
Definition: e_log.c:102
void lib3ds_io_write_error(Lib3dsIo *io)
Definition: lib3ds_io.c:134
void lib3ds_io_write_byte(Lib3dsIo *io, uint8_t b)
Definition: lib3ds_io.c:310
static void lib3ds_io_log_str(Lib3dsIo *io, Lib3dsLogLevel level, const char *str)
Definition: lib3ds_io.c:92
SDL_EventEntry * free
Definition: SDL_events.c:80
void lib3ds_io_read_vector(Lib3dsIo *io, float v[3])
Definition: lib3ds_io.c:258
int8_t lib3ds_io_read_intb(Lib3dsIo *io)
Definition: lib3ds_io.c:190
long int32_t
Definition: types.h:9
return Display return Display Bool Bool int d
Definition: SDL_x11sym.h:30
#define assert(x)
Definition: SDL_malloc.c:1234
GLenum GLsizei len
Definition: glew.h:7035
uint32_t lib3ds_io_read_dword(Lib3dsIo *io)
Definition: lib3ds_io.c:172
void lib3ds_io_write_intb(Lib3dsIo *io, int8_t b)
Definition: lib3ds_io.c:356
if(!yyg->yy_init)
#define calloc
Definition: SDL_malloc.c:636
size_t lib3ds_io_write(Lib3dsIo *io, const void *buffer, size_t size)
Definition: lib3ds_io.c:82
void lib3ds_io_read_error(Lib3dsIo *io)
Definition: lib3ds_io.c:128
void lib3ds_io_setup(Lib3dsIo *io)
Definition: lib3ds_io.c:28
void lib3ds_io_write_string(Lib3dsIo *io, const char *s)
Definition: lib3ds_io.c:443
EGLContext EGLenum EGLClientBuffer buffer
Definition: eglext.h:87
void lib3ds_io_write_vector(Lib3dsIo *io, float v[3])
Definition: lib3ds_io.c:422
uint8_t lib3ds_io_read_byte(Lib3dsIo *io)
Definition: lib3ds_io.c:143
long lib3ds_io_tell(Lib3dsIo *io)
Definition: lib3ds_io.c:62
void lib3ds_io_write_float(Lib3dsIo *io, float l)
Definition: lib3ds_io.c:402
const GLdouble * v
Definition: glew.h:1377
Lib3dsIoSeek
Definition: lib3ds.h:43
const GLfloat * c
Definition: glew.h:14913
void * self
Definition: lib3ds.h:58
long(* seek_func)(void *self, long offset, Lib3dsIoSeek origin)
Definition: lib3ds.h:59
LIB3DSAPI void lib3ds_node_free(Lib3dsNode *node)
Definition: lib3ds_node.c:346
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl2ext.h:845
uint16_t lib3ds_io_read_word(Lib3dsIo *io)
Definition: lib3ds_io.c:156
Lib3dsLogLevel
Definition: lib3ds.h:49
float lib3ds_io_read_float(Lib3dsIo *io)
Definition: lib3ds_io.c:237
short int16_t
Definition: types.h:8
long(* tell_func)(void *self)
Definition: lib3ds.h:60
void lib3ds_io_cleanup(Lib3dsIo *io)
Definition: lib3ds_io.c:35
void lib3ds_io_log_indent(Lib3dsIo *io, int indent)
Definition: lib3ds_io.c:119
unsigned char uint8_t
unsigned int uint32_t
GLdouble l
Definition: glew.h:8383
void * tmp_mem
Definition: lib3ds_impl.h:317
void(* log_func)(void *self, Lib3dsLogLevel level, int indent, const char *msg)
Definition: lib3ds.h:63
signed char int8_t
size_t(* write_func)(void *self, const void *buffer, size_t size)
Definition: lib3ds.h:62
void lib3ds_io_read_string(Lib3dsIo *io, char *s, int buflen)
Definition: lib3ds_io.c:285
GLintptr offset
Definition: glew.h:1668
void lib3ds_io_read_rgb(Lib3dsIo *io, float rgb[3])
Definition: lib3ds_io.c:267
void lib3ds_io_log(Lib3dsIo *io, Lib3dsLogLevel level, const char *format,...)
Definition: lib3ds_io.c:100
GLint level
Definition: gl2ext.h:845
void * impl
Definition: lib3ds.h:57
size_t(* read_func)(void *self, void *buffer, size_t size)
Definition: lib3ds.h:61
Lib3dsNode * tmp_node
Definition: lib3ds_impl.h:318
GLdouble GLdouble GLdouble b
Definition: glew.h:8383
GLint GLint GLint GLint GLint w
Definition: gl2ext.h:1215
long lib3ds_io_seek(Lib3dsIo *io, long offset, Lib3dsIoSeek origin)
Definition: lib3ds_io.c:52
#define str(s)
int i
Definition: pngrutil.c:1377
void lib3ds_io_write_dword(Lib3dsIo *io, uint32_t d)
Definition: lib3ds_io.c:338
void lib3ds_io_write_rgb(Lib3dsIo *io, float rgb[3])
Definition: lib3ds_io.c:431
GLsizei size
Definition: gl2ext.h:1467
void lib3ds_io_write_word(Lib3dsIo *io, uint16_t w)
Definition: lib3ds_io.c:322