Update interpreters to latest Garglk codebase
[projects/chimara/chimara.git] / interpreters / nitfol / struct.c
1 /*  Nitfol - z-machine interpreter using Glk for output.
2     Copyright (C) 1999  Evin Robertson
3
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.
8
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.
13
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.
17
18     The author can be reached at nitfol@deja.com
19 */
20 #include "nitfol.h"
21
22
23 glui32 fillstruct(strid_t stream, const unsigned *info, glui32 *dest,
24                   glui32 (*special)(strid_t))
25 {
26   unsigned char buffer[4];
27   unsigned e;
28   glui32 len = 0;;
29
30   for(e = 0; info[e]; e++) {
31     if(info[e] == 0x8000) {
32       *dest++ = special(stream);
33       len++;
34     }
35     else if(info[e] > 4) {
36       unsigned i;
37       for(i = 0; i < info[e]; i++) {
38         *dest++ = glk_get_char_stream(stream);
39         len++;
40       }
41     } else {
42       glk_get_buffer_stream(stream, (char *) buffer, info[e]);
43
44       switch(info[e]) {
45       case 1: *dest = MSBdecode1(buffer); break;
46       case 2: *dest = MSBdecode2(buffer); break;
47       case 3: *dest = MSBdecode3(buffer); break;
48       case 4: *dest = MSBdecode4(buffer); break;
49       }
50       dest++;
51       len+=info[e];
52     }
53   }
54   return len;
55 }
56
57 glui32 emptystruct(strid_t stream, const unsigned *info, const glui32 *src)
58 {
59   unsigned char buffer[4];
60   unsigned e;
61   glui32 len = 0;;
62
63   for(e = 0; info[e]; e++) {
64     if(info[e] > 4) {
65       unsigned i;
66       for(i = 0; i < info[e]; i++) {
67         glk_put_char_stream(stream, *src++);
68         len++;
69       }
70     } else {
71       switch(info[e]) {
72       case 1: MSBencode1(buffer, *src); break;
73       case 2: MSBencode2(buffer, *src); break;
74       case 3: MSBencode3(buffer, *src); break;
75       case 4: MSBencode4(buffer, *src); break;
76       }
77
78       w_glk_put_buffer_stream(stream, (char *) buffer, info[e]);
79       
80       src++;
81       len++;
82     }
83   }
84   return len;
85 }