zenilib: zenilib/jni/external/lib3ds/src/lib3ds_io.c Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
28 lib3ds_io_setup(Lib3dsIo *io) {
29 assert(io);
30 io->impl = calloc(sizeof(Lib3dsIoImpl), 1);
31 }
32
33
34 void
35 lib3ds_io_cleanup(Lib3dsIo *io) {
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) {
44 lib3ds_node_free(impl->tmp_node);
45 impl->tmp_node = NULL;
46 }
47 free(impl);
48 }
49
50
51 long
52 lib3ds_io_seek(Lib3dsIo *io, long offset, Lib3dsIoSeek origin) {
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
62 lib3ds_io_tell(Lib3dsIo *io) {
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
92 lib3ds_io_log_str(Lib3dsIo *io, Lib3dsLogLevel level, const char *str) {
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
100 lib3ds_io_log(Lib3dsIo *io, Lib3dsLogLevel level, const char *format, ...) {
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
128 lib3ds_io_read_error(Lib3dsIo *io) {
129 lib3ds_io_log(io, LIB3DS_LOG_ERROR, "Reading from input stream failed.");
130 }
131
132
133 void
134 lib3ds_io_write_error(Lib3dsIo *io) {
135 lib3ds_io_log(io, LIB3DS_LOG_ERROR, "Writing to output stream failed.");
136 }
137
138
142 uint8_t
143 lib3ds_io_read_byte(Lib3dsIo *io) {
144 uint8_t b;
145
146 assert(io);
147 lib3ds_io_read(io, &b, 1);
148 return(b);
149 }
150
151
155 uint16_t
156 lib3ds_io_read_word(Lib3dsIo *io) {
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
172 lib3ds_io_read_dword(Lib3dsIo *io) {
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
190 lib3ds_io_read_intb(Lib3dsIo *io) {
191 int8_t b;
192
193 assert(io);
194 lib3ds_io_read(io, &b, 1);
195 return(b);
196 }
197
198
202 int16_t
203 lib3ds_io_read_intw(Lib3dsIo *io) {
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
219 lib3ds_io_read_intd(Lib3dsIo *io) {
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
237 lib3ds_io_read_float(Lib3dsIo *io) {
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
258 lib3ds_io_read_vector(Lib3dsIo *io, float v[3]) {
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) {
292 lib3ds_io_read_error(io);
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
310 lib3ds_io_write_byte(Lib3dsIo *io, uint8_t b) {
311 assert(io);
312 if (lib3ds_io_write(io, &b, 1) != 1) {
313 lib3ds_io_write_error(io);
314 }
315 }
316
317
321 void
322 lib3ds_io_write_word(Lib3dsIo *io, uint16_t w) {
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) {
329 lib3ds_io_write_error(io);
330 }
331 }
332
333
337 void
338 lib3ds_io_write_dword(Lib3dsIo *io, uint32_t d) {
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) {
347 lib3ds_io_write_error(io);
348 }
349 }
350
351
355 void
356 lib3ds_io_write_intb(Lib3dsIo *io, int8_t b) {
357 assert(io);
358 if (lib3ds_io_write(io, &b, 1) != 1) {
359 lib3ds_io_write_error(io);
360 }
361 }
362
363
367 void
368 lib3ds_io_write_intw(Lib3dsIo *io, int16_t w) {
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) {
375 lib3ds_io_write_error(io);
376 }
377 }
378
379
383 void
384 lib3ds_io_write_intd(Lib3dsIo *io, int32_t d) {
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) {
393 lib3ds_io_write_error(io);
394 }
395 }
396
397
401 void
402 lib3ds_io_write_float(Lib3dsIo *io, float l) {
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) {
413 lib3ds_io_write_error(io);
414 }
415 }
416
417
421 void
422 lib3ds_io_write_vector(Lib3dsIo *io, float v[3]) {
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) {
448 lib3ds_io_write_error(io);
449 }
450 }
lib3ds_io_write_intw
void lib3ds_io_write_intw(Lib3dsIo *io, int16_t w)
Definition: lib3ds_io.c:368
lib3ds_io_read_intw
int16_t lib3ds_io_read_intw(Lib3dsIo *io)
Definition: lib3ds_io.c:203
s
GLdouble s
Definition: glew.h:1376
lib3ds_io_read
size_t lib3ds_io_read(Lib3dsIo *io, void *buffer, size_t size)
Definition: lib3ds_io.c:72
lib3ds_io_write_intd
void lib3ds_io_write_intd(Lib3dsIo *io, int32_t d)
Definition: lib3ds_io.c:384
lib3ds_io_read_intd
int32_t lib3ds_io_read_intd(Lib3dsIo *io)
Definition: lib3ds_io.c:219
NULL
#define NULL
Definition: ftobjs.h:61
uint16_t
unsigned short uint16_t
Definition: SDL_config_windows.h:60
k
int32_t k
Definition: e_log.c:102
lib3ds_io_write_error
void lib3ds_io_write_error(Lib3dsIo *io)
Definition: lib3ds_io.c:134
lib3ds_io_write_byte
void lib3ds_io_write_byte(Lib3dsIo *io, uint8_t b)
Definition: lib3ds_io.c:310
lib3ds_io_log_str
static void lib3ds_io_log_str(Lib3dsIo *io, Lib3dsLogLevel level, const char *str)
Definition: lib3ds_io.c:92
free
SDL_EventEntry * free
Definition: SDL_events.c:80
lib3ds_io_read_vector
void lib3ds_io_read_vector(Lib3dsIo *io, float v[3])
Definition: lib3ds_io.c:258
lib3ds_io_read_intb
int8_t lib3ds_io_read_intb(Lib3dsIo *io)
Definition: lib3ds_io.c:190
int32_t
long int32_t
Definition: types.h:9
d
return Display return Display Bool Bool int d
Definition: SDL_x11sym.h:30
assert
#define assert(x)
Definition: SDL_malloc.c:1234
len
GLenum GLsizei len
Definition: glew.h:7035
lib3ds_io_read_dword
uint32_t lib3ds_io_read_dword(Lib3dsIo *io)
Definition: lib3ds_io.c:172
lib3ds_io_write_intb
void lib3ds_io_write_intb(Lib3dsIo *io, int8_t b)
Definition: lib3ds_io.c:356
if
if(!yyg->yy_init)
Definition: glslang_lex.cpp:1037
calloc
#define calloc
Definition: SDL_malloc.c:636
lib3ds_io_write
size_t lib3ds_io_write(Lib3dsIo *io, const void *buffer, size_t size)
Definition: lib3ds_io.c:82
lib3ds_io_read_error
void lib3ds_io_read_error(Lib3dsIo *io)
Definition: lib3ds_io.c:128
lib3ds_io_setup
void lib3ds_io_setup(Lib3dsIo *io)
Definition: lib3ds_io.c:28
lib3ds_io_write_string
void lib3ds_io_write_string(Lib3dsIo *io, const char *s)
Definition: lib3ds_io.c:443
buffer
EGLContext EGLenum EGLClientBuffer buffer
Definition: eglext.h:87
lib3ds_io_write_vector
void lib3ds_io_write_vector(Lib3dsIo *io, float v[3])
Definition: lib3ds_io.c:422
lib3ds_io_read_byte
uint8_t lib3ds_io_read_byte(Lib3dsIo *io)
Definition: lib3ds_io.c:143
lib3ds_io_tell
long lib3ds_io_tell(Lib3dsIo *io)
Definition: lib3ds_io.c:62
lib3ds_io_write_float
void lib3ds_io_write_float(Lib3dsIo *io, float l)
Definition: lib3ds_io.c:402
v
const GLdouble * v
Definition: glew.h:1377
Lib3dsIoSeek
Lib3dsIoSeek
Definition: lib3ds.h:43
c
const GLfloat * c
Definition: glew.h:14913
Lib3dsIo::self
void * self
Definition: lib3ds.h:58
Lib3dsIo::seek_func
long(* seek_func)(void *self, long offset, Lib3dsIoSeek origin)
Definition: lib3ds.h:59
lib3ds_node_free
LIB3DSAPI void lib3ds_node_free(Lib3dsNode *node)
Definition: lib3ds_node.c:346
format
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl2ext.h:845
lib3ds_io_read_word
uint16_t lib3ds_io_read_word(Lib3dsIo *io)
Definition: lib3ds_io.c:156
Lib3dsLogLevel
Lib3dsLogLevel
Definition: lib3ds.h:49
lib3ds_io_read_float
float lib3ds_io_read_float(Lib3dsIo *io)
Definition: lib3ds_io.c:237
int16_t
short int16_t
Definition: types.h:8
Lib3dsIo::tell_func
long(* tell_func)(void *self)
Definition: lib3ds.h:60
lib3ds_io_cleanup
void lib3ds_io_cleanup(Lib3dsIo *io)
Definition: lib3ds_io.c:35
lib3ds_io_log_indent
void lib3ds_io_log_indent(Lib3dsIo *io, int indent)
Definition: lib3ds_io.c:119
uint8_t
unsigned char uint8_t
Definition: SDL_config_windows.h:58
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:62
l
GLdouble l
Definition: glew.h:8383
Lib3dsIoImpl::tmp_mem
void * tmp_mem
Definition: lib3ds_impl.h:317
Lib3dsIo::log_func
void(* log_func)(void *self, Lib3dsLogLevel level, int indent, const char *msg)
Definition: lib3ds.h:63
int8_t
signed char int8_t
Definition: SDL_config_windows.h:57
Lib3dsIo::write_func
size_t(* write_func)(void *self, const void *buffer, size_t size)
Definition: lib3ds.h:62
Lib3dsIo
Definition: lib3ds.h:56
lib3ds_io_read_string
void lib3ds_io_read_string(Lib3dsIo *io, char *s, int buflen)
Definition: lib3ds_io.c:285
offset
GLintptr offset
Definition: glew.h:1668
lib3ds_io_read_rgb
void lib3ds_io_read_rgb(Lib3dsIo *io, float rgb[3])
Definition: lib3ds_io.c:267
lib3ds_io_log
void lib3ds_io_log(Lib3dsIo *io, Lib3dsLogLevel level, const char *format,...)
Definition: lib3ds_io.c:100
level
GLint level
Definition: gl2ext.h:845
Lib3dsIo::impl
void * impl
Definition: lib3ds.h:57
Lib3dsIo::read_func
size_t(* read_func)(void *self, void *buffer, size_t size)
Definition: lib3ds.h:61
Lib3dsIoImpl::tmp_node
Lib3dsNode * tmp_node
Definition: lib3ds_impl.h:318
b
GLdouble GLdouble GLdouble b
Definition: glew.h:8383
w
GLint GLint GLint GLint GLint w
Definition: gl2ext.h:1215
Lib3dsIoImpl
Definition: lib3ds_impl.h:314
lib3ds_io_seek
long lib3ds_io_seek(Lib3dsIo *io, long offset, Lib3dsIoSeek origin)
Definition: lib3ds_io.c:52
str
#define str(s)
i
int i
Definition: pngrutil.c:1377
lib3ds_io_write_dword
void lib3ds_io_write_dword(Lib3dsIo *io, uint32_t d)
Definition: lib3ds_io.c:338
lib3ds_impl.h
lib3ds_io_write_rgb
void lib3ds_io_write_rgb(Lib3dsIo *io, float rgb[3])
Definition: lib3ds_io.c:431
LIB3DS_LOG_ERROR
Definition: lib3ds.h:50
size
GLsizei size
Definition: gl2ext.h:1467
lib3ds_io_write_word
void lib3ds_io_write_word(Lib3dsIo *io, uint16_t w)
Definition: lib3ds_io.c:322