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 extern zword
get_max_width (zword
);
27 static int depth
= -1;
34 } redirect
[MAX_NESTING
];
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
) {
50 if (buffering
&& (short) xsize
<= 0)
51 xsize
= get_max_width ((zword
) (- (short) xsize
));
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
);
69 * Redirect a newline to the memory of the Z-machine.
73 void memory_new_line (void)
78 redirect
[depth
].total
+= redirect
[depth
].width
;
79 redirect
[depth
].width
= 0;
81 addr
= redirect
[depth
].table
;
86 if (redirect
[depth
].xsize
!= 0xffff) {
88 redirect
[depth
].table
= addr
+ size
;
91 } else storeb ((zword
) (addr
+ (size
++)), 13);
93 storew (redirect
[depth
].table
, size
);
95 }/* memory_new_line */
100 * Redirect a string of characters to the memory of the Z-machine.
104 void memory_word (const zchar
*s
)
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
);
125 redirect
[depth
].width
+= width
;
129 addr
= redirect
[depth
].table
;
131 LOW_WORD (addr
, size
)
134 while ((c
= *s
++) != 0)
135 storeb ((zword
) (addr
+ (size
++)), translate_to_zscii (c
));
137 storew (redirect
[depth
].table
, size
);
144 * End of output redirection.
148 void memory_close (void)
153 if (redirect
[depth
].xsize
!= 0xffff)
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
)
166 ostream_memory
= FALSE
;