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
23 #define MAX_NESTING 16
25 /* TOR: glkify -- this is for V6 only */
26 static zword get_max_width (zword win) { return 80; }
28 static int depth = -1;
35 } redirect[MAX_NESTING];
40 * Begin output redirection to the memory of the Z-machine.
44 void memory_open (zword table, zword xsize, bool buffering)
47 if (++depth < MAX_NESTING) {
51 if (buffering && (short) xsize <= 0)
52 xsize = get_max_width ((zword) (- (short) xsize));
56 redirect[depth].table = table;
57 redirect[depth].width = 0;
58 redirect[depth].total = 0;
59 redirect[depth].xsize = xsize;
61 ostream_memory = TRUE;
63 } else runtime_error (ERR_STR3_NESTING);
70 * Redirect a newline to the memory of the Z-machine.
74 void memory_new_line (void)
79 redirect[depth].total += redirect[depth].width;
80 redirect[depth].width = 0;
82 addr = redirect[depth].table;
87 if (redirect[depth].xsize != 0xffff) {
89 redirect[depth].table = addr + size;
92 } else storeb ((zword) (addr + (size++)), 13);
94 storew (redirect[depth].table, size);
96 }/* memory_new_line */
101 * Redirect a string of characters to the memory of the Z-machine.
105 void memory_word (const zchar *s)
111 if (h_version == V6) {
113 int width = os_string_width (s);
115 if (redirect[depth].xsize != 0xffff)
117 if (redirect[depth].width + width > redirect[depth].xsize) {
119 if (*s == ' ' || *s == ZC_INDENT || *s == ZC_GAP)
120 width = os_string_width (++s);
126 redirect[depth].width += width;
130 addr = redirect[depth].table;
132 LOW_WORD (addr, size)
135 while ((c = *s++) != 0)
136 storeb ((zword) (addr + (size++)), translate_to_zscii (c));
138 storew (redirect[depth].table, size);
145 * End of output redirection.
149 void memory_close (void)
154 if (redirect[depth].xsize != 0xffff)
157 if (h_version == V6) {
159 h_line_width = (redirect[depth].xsize != 0xffff) ?
160 redirect[depth].total : redirect[depth].width;
162 SET_WORD (H_LINE_WIDTH, h_line_width)
167 ostream_memory = FALSE;