1 /* Nitfol - z-machine interpreter using Glk for output.
2 Copyright (C) 1999 Evin Robertson
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 The author can be reached at nitfol@deja.com
22 BOOL iffgetchunk(strid_t stream, char *desttype, glui32 *ulength,
27 unsigned char length[4];
29 c = glk_stream_get_position(stream);
31 glk_get_char_stream(stream); /* Eat padding */
35 if(glk_get_buffer_stream(stream, desttype, 4) != 4)
37 if(glk_get_buffer_stream(stream, (char *) length, 4) != 4)
40 *ulength = MSBdecode4(length);
42 for(i = 0; i < 4; i++)
43 if(desttype[i] < 0x20 || desttype[i] > 0x7e)
48 return ((c + *ulength) <= file_size);
52 BOOL ifffindchunk(strid_t stream, const char *type, glui32 *length, glui32 loc)
57 glk_stream_set_position(stream, 0, seekmode_End);
58 file_size = glk_stream_get_position(stream);
60 glk_stream_set_position(stream, loc, seekmode_Start);
63 glk_stream_set_position(stream, *length, seekmode_Current);
64 if(!iffgetchunk(stream, t, length, file_size))
66 } while((t[0] != type[0]) || (t[1] != type[1]) ||
67 (t[2] != type[2]) || (t[3] != type[3]));
73 void iffputchunk(strid_t stream, const char *type, glui32 ulength)
76 unsigned char length[4];
78 c = glk_stream_get_position(stream);
80 glk_put_char_stream(stream, 0); /* Spew padding */
82 MSBencode4(length, ulength);
84 w_glk_put_buffer_stream(stream, type, 4);
85 w_glk_put_buffer_stream(stream, (char *) length, 4);
89 void iffpadend(strid_t stream)
93 c = glk_stream_get_position(stream);
95 glk_put_char_stream(stream, 0); /* Spew padding */