diff options
author | Torne Wuff <torne@wolfpuppy.org.uk> | 2010-01-17 22:15:13 +0000 |
---|---|---|
committer | Torne Wuff <torne@wolfpuppy.org.uk> | 2010-01-17 22:15:13 +0000 |
commit | 7f28c94eda576e3f972fc05468188986f2e45885 (patch) | |
tree | e03b94613028d16855a5d3df0f4853e077931214 /apps/plugins/frotz/redirect.c | |
parent | 563f2602f471208cb8544a36539a79dcceaad643 (diff) | |
download | rockbox-7f28c94eda576e3f972fc05468188986f2e45885.tar.gz rockbox-7f28c94eda576e3f972fc05468188986f2e45885.zip |
New plugin: frotz, a Z-machine interpreter, for playing interactive fiction.
The interpreter more or less passes all the tests in the z-machine test suite.
It should build for every target except Archos (for which it is disabled).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24267 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/frotz/redirect.c')
-rw-r--r-- | apps/plugins/frotz/redirect.c | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/apps/plugins/frotz/redirect.c b/apps/plugins/frotz/redirect.c new file mode 100644 index 0000000000..d81776dbcd --- /dev/null +++ b/apps/plugins/frotz/redirect.c | |||
@@ -0,0 +1,172 @@ | |||
1 | /* redirect.c - Output redirection to Z-machine memory | ||
2 | * Copyright (c) 1995-1997 Stefan Jokisch | ||
3 | * | ||
4 | * This file is part of Frotz. | ||
5 | * | ||
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. | ||
10 | * | ||
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. | ||
15 | * | ||
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 | ||
19 | */ | ||
20 | |||
21 | #include "frotz.h" | ||
22 | |||
23 | #define MAX_NESTING 16 | ||
24 | |||
25 | extern zword get_max_width (zword); | ||
26 | |||
27 | static int depth = -1; | ||
28 | |||
29 | static struct { | ||
30 | zword xsize; | ||
31 | zword table; | ||
32 | zword width; | ||
33 | zword total; | ||
34 | } redirect[MAX_NESTING]; | ||
35 | |||
36 | /* | ||
37 | * memory_open | ||
38 | * | ||
39 | * Begin output redirection to the memory of the Z-machine. | ||
40 | * | ||
41 | */ | ||
42 | |||
43 | void memory_open (zword table, zword xsize, bool buffering) | ||
44 | { | ||
45 | |||
46 | if (++depth < MAX_NESTING) { | ||
47 | |||
48 | if (!buffering) | ||
49 | xsize = 0xffff; | ||
50 | if (buffering && (short) xsize <= 0) | ||
51 | xsize = get_max_width ((zword) (- (short) xsize)); | ||
52 | |||
53 | storew (table, 0); | ||
54 | |||
55 | redirect[depth].table = table; | ||
56 | redirect[depth].width = 0; | ||
57 | redirect[depth].total = 0; | ||
58 | redirect[depth].xsize = xsize; | ||
59 | |||
60 | ostream_memory = TRUE; | ||
61 | |||
62 | } else runtime_error (ERR_STR3_NESTING); | ||
63 | |||
64 | }/* memory_open */ | ||
65 | |||
66 | /* | ||
67 | * memory_new_line | ||
68 | * | ||
69 | * Redirect a newline to the memory of the Z-machine. | ||
70 | * | ||
71 | */ | ||
72 | |||
73 | void memory_new_line (void) | ||
74 | { | ||
75 | zword size; | ||
76 | zword addr; | ||
77 | |||
78 | redirect[depth].total += redirect[depth].width; | ||
79 | redirect[depth].width = 0; | ||
80 | |||
81 | addr = redirect[depth].table; | ||
82 | |||
83 | LOW_WORD (addr, size) | ||
84 | addr += 2; | ||
85 | |||
86 | if (redirect[depth].xsize != 0xffff) { | ||
87 | |||
88 | redirect[depth].table = addr + size; | ||
89 | size = 0; | ||
90 | |||
91 | } else storeb ((zword) (addr + (size++)), 13); | ||
92 | |||
93 | storew (redirect[depth].table, size); | ||
94 | |||
95 | }/* memory_new_line */ | ||
96 | |||
97 | /* | ||
98 | * memory_word | ||
99 | * | ||
100 | * Redirect a string of characters to the memory of the Z-machine. | ||
101 | * | ||
102 | */ | ||
103 | |||
104 | void memory_word (const zchar *s) | ||
105 | { | ||
106 | zword size; | ||
107 | zword addr; | ||
108 | zchar c; | ||
109 | |||
110 | if (h_version == V6) { | ||
111 | |||
112 | int width = os_string_width (s); | ||
113 | |||
114 | if (redirect[depth].xsize != 0xffff) | ||
115 | |||
116 | if (redirect[depth].width + width > redirect[depth].xsize) { | ||
117 | |||
118 | if (*s == ' ' || *s == ZC_INDENT || *s == ZC_GAP) | ||
119 | width = os_string_width (++s); | ||
120 | |||
121 | memory_new_line (); | ||
122 | |||
123 | } | ||
124 | |||
125 | redirect[depth].width += width; | ||
126 | |||
127 | } | ||
128 | |||
129 | addr = redirect[depth].table; | ||
130 | |||
131 | LOW_WORD (addr, size) | ||
132 | addr += 2; | ||
133 | |||
134 | while ((c = *s++) != 0) | ||
135 | storeb ((zword) (addr + (size++)), translate_to_zscii (c)); | ||
136 | |||
137 | storew (redirect[depth].table, size); | ||
138 | |||
139 | }/* memory_word */ | ||
140 | |||
141 | /* | ||
142 | * memory_close | ||
143 | * | ||
144 | * End of output redirection. | ||
145 | * | ||
146 | */ | ||
147 | |||
148 | void memory_close (void) | ||
149 | { | ||
150 | |||
151 | if (depth >= 0) { | ||
152 | |||
153 | if (redirect[depth].xsize != 0xffff) | ||
154 | memory_new_line (); | ||
155 | |||
156 | if (h_version == V6) { | ||
157 | |||
158 | h_line_width = (redirect[depth].xsize != 0xffff) ? | ||
159 | redirect[depth].total : redirect[depth].width; | ||
160 | |||
161 | SET_WORD (H_LINE_WIDTH, h_line_width) | ||
162 | |||
163 | } | ||
164 | |||
165 | if (depth == 0) | ||
166 | ostream_memory = FALSE; | ||
167 | |||
168 | depth--; | ||
169 | |||
170 | } | ||
171 | |||
172 | }/* memory_close */ | ||