Prepare new maemo release
[maemo-rb.git] / apps / plugins / frotz / redirect.c
blobd81776dbcdbf81459b51c6dc5dc1c6396ecf07f2
1 /* redirect.c - Output redirection to Z-machine memory
2 * Copyright (c) 1995-1997 Stefan Jokisch
4 * This file is part of Frotz.
6 * Frotz is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Frotz is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include "frotz.h"
23 #define MAX_NESTING 16
25 extern zword get_max_width (zword);
27 static int depth = -1;
29 static struct {
30 zword xsize;
31 zword table;
32 zword width;
33 zword total;
34 } redirect[MAX_NESTING];
37 * memory_open
39 * Begin output redirection to the memory of the Z-machine.
43 void memory_open (zword table, zword xsize, bool buffering)
46 if (++depth < MAX_NESTING) {
48 if (!buffering)
49 xsize = 0xffff;
50 if (buffering && (short) xsize <= 0)
51 xsize = get_max_width ((zword) (- (short) xsize));
53 storew (table, 0);
55 redirect[depth].table = table;
56 redirect[depth].width = 0;
57 redirect[depth].total = 0;
58 redirect[depth].xsize = xsize;
60 ostream_memory = TRUE;
62 } else runtime_error (ERR_STR3_NESTING);
64 }/* memory_open */
67 * memory_new_line
69 * Redirect a newline to the memory of the Z-machine.
73 void memory_new_line (void)
75 zword size;
76 zword addr;
78 redirect[depth].total += redirect[depth].width;
79 redirect[depth].width = 0;
81 addr = redirect[depth].table;
83 LOW_WORD (addr, size)
84 addr += 2;
86 if (redirect[depth].xsize != 0xffff) {
88 redirect[depth].table = addr + size;
89 size = 0;
91 } else storeb ((zword) (addr + (size++)), 13);
93 storew (redirect[depth].table, size);
95 }/* memory_new_line */
98 * memory_word
100 * Redirect a string of characters to the memory of the Z-machine.
104 void memory_word (const zchar *s)
106 zword size;
107 zword addr;
108 zchar c;
110 if (h_version == V6) {
112 int width = os_string_width (s);
114 if (redirect[depth].xsize != 0xffff)
116 if (redirect[depth].width + width > redirect[depth].xsize) {
118 if (*s == ' ' || *s == ZC_INDENT || *s == ZC_GAP)
119 width = os_string_width (++s);
121 memory_new_line ();
125 redirect[depth].width += width;
129 addr = redirect[depth].table;
131 LOW_WORD (addr, size)
132 addr += 2;
134 while ((c = *s++) != 0)
135 storeb ((zword) (addr + (size++)), translate_to_zscii (c));
137 storew (redirect[depth].table, size);
139 }/* memory_word */
142 * memory_close
144 * End of output redirection.
148 void memory_close (void)
151 if (depth >= 0) {
153 if (redirect[depth].xsize != 0xffff)
154 memory_new_line ();
156 if (h_version == V6) {
158 h_line_width = (redirect[depth].xsize != 0xffff) ?
159 redirect[depth].total : redirect[depth].width;
161 SET_WORD (H_LINE_WIDTH, h_line_width)
165 if (depth == 0)
166 ostream_memory = FALSE;
168 depth--;
172 }/* memory_close */