2 * The 3D Studio File Format Library
3 * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * $Id: chunk.c,v 1.14 2001/07/07 19:05:30 jeh Exp $
23 #include <lib3ds/chunk.h>
24 #include <lib3ds/io.h>
25 #include <lib3ds/chunktable.h>
30 /*#define LIB3DS_CHUNK_DEBUG*/
31 /*#define LIB3DS_CHUNK_WARNING*/
35 * \defgroup chunk Chunk Handling
37 * \author J.E. Hoffmann <je-h@gmx.net>
41 static Lib3dsBool enable_dump=LIB3DS_FALSE;
42 static Lib3dsBool enable_unknown=LIB3DS_FALSE;
43 static char lib3ds_chunk_level[128]="";
47 lib3ds_chunk_debug_enter(Lib3dsChunk *c)
49 strcat(lib3ds_chunk_level, " ");
54 lib3ds_chunk_debug_leave(Lib3dsChunk *c)
56 lib3ds_chunk_level[strlen(lib3ds_chunk_level)-2]=0;
61 lib3ds_chunk_debug_dump(Lib3dsChunk *c)
64 printf("%s%s (0x%X) size=%lu\n",
66 lib3ds_chunk_name(c->chunk),
78 lib3ds_chunk_enable_dump(Lib3dsBool enable, Lib3dsBool unknown)
81 enable_unknown=unknown;
88 * Reads a 3d-Studio chunk header from a little endian file stream.
90 * \param c The chunk to store the data.
91 * \param f The file stream.
93 * \return True on success, False otherwise.
96 lib3ds_chunk_read(Lib3dsChunk *c, Lib3dsIo *io)
100 c->cur=lib3ds_io_tell(io);
101 c->chunk=lib3ds_io_read_word(io);
102 c->size=lib3ds_io_read_dword(io);
103 c->end=c->cur+c->size;
105 if (lib3ds_io_error(io) || (c->size<6)) {
106 return(LIB3DS_FALSE);
117 lib3ds_chunk_read_start(Lib3dsChunk *c, Lib3dsWord chunk, Lib3dsIo *io)
121 if (!lib3ds_chunk_read(c, io)) {
122 return(LIB3DS_FALSE);
124 lib3ds_chunk_debug_enter(c);
125 return((chunk==0) || (c->chunk==chunk));
133 lib3ds_chunk_read_tell(Lib3dsChunk *c, Lib3dsIo *io)
135 c->cur=lib3ds_io_tell(io);
143 lib3ds_chunk_read_next(Lib3dsChunk *c, Lib3dsIo *io)
147 if (c->cur>=c->end) {
148 ASSERT(c->cur==c->end);
152 lib3ds_io_seek(io, (long)c->cur, LIB3DS_SEEK_SET);
153 d.chunk=lib3ds_io_read_word(io);
154 d.size=lib3ds_io_read_dword(io);
155 lib3ds_chunk_debug_dump(&d);
165 lib3ds_chunk_read_reset(Lib3dsChunk *c, Lib3dsIo *io)
167 lib3ds_io_seek(io, -6, LIB3DS_SEEK_CUR);
175 lib3ds_chunk_read_end(Lib3dsChunk *c, Lib3dsIo *io)
177 lib3ds_chunk_debug_leave(c);
178 lib3ds_io_seek(io, c->end, LIB3DS_SEEK_SET);
185 * Writes a 3d-Studio chunk header into a little endian file stream.
187 * \param c The chunk to be written.
188 * \param f The file stream.
190 * \return True on success, False otherwise.
193 lib3ds_chunk_write(Lib3dsChunk *c, Lib3dsIo *io)
196 if (!lib3ds_io_write_word(io, c->chunk)) {
198 return(LIB3DS_FALSE);
200 if (!lib3ds_io_write_dword(io, c->size)) {
202 return(LIB3DS_FALSE);
212 lib3ds_chunk_write_start(Lib3dsChunk *c, Lib3dsIo *io)
216 c->cur=lib3ds_io_tell(io);
217 if (!lib3ds_io_write_word(io, c->chunk)) {
218 return(LIB3DS_FALSE);
220 if (!lib3ds_io_write_dword(io, c->size)) {
221 return(LIB3DS_FALSE);
231 lib3ds_chunk_write_end(Lib3dsChunk *c, Lib3dsIo *io)
234 c->size=lib3ds_io_tell(io) - c->cur;
235 lib3ds_io_seek(io, c->cur+2, LIB3DS_SEEK_SET);
236 if (!lib3ds_io_write_dword(io, c->size)) {
238 return(LIB3DS_FALSE);
242 lib3ds_io_seek(io, c->cur, LIB3DS_SEEK_SET);
243 if (lib3ds_io_error(io)) {
245 return(LIB3DS_FALSE);
255 lib3ds_chunk_name(Lib3dsWord chunk)
259 for (p=lib3ds_chunk_table; p->name!=0; ++p) {
260 if (p->chunk==chunk) {
264 return("***UNKNOWN***");
272 lib3ds_chunk_unknown(Lib3dsWord chunk)
274 if (enable_unknown) {
275 printf("%s***WARNING*** Unknown Chunk: %s (0x%X)\n",
277 lib3ds_chunk_name(chunk),
288 lib3ds_chunk_dump_info(const char *format, ...)
294 va_start(marker, format);
295 vsprintf(s, format, marker);
298 printf("%s%s\n", lib3ds_chunk_level, s);