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/buffer.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/buffer.c')
-rw-r--r-- | apps/plugins/frotz/buffer.c | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/apps/plugins/frotz/buffer.c b/apps/plugins/frotz/buffer.c new file mode 100644 index 0000000000..298ac69c20 --- /dev/null +++ b/apps/plugins/frotz/buffer.c | |||
@@ -0,0 +1,152 @@ | |||
1 | /* buffer.c - Text buffering and word wrapping | ||
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 | extern void stream_char (zchar); | ||
24 | extern void stream_word (const zchar *); | ||
25 | extern void stream_new_line (void); | ||
26 | |||
27 | static zchar buffer[TEXT_BUFFER_SIZE]; | ||
28 | static int bufpos = 0; | ||
29 | |||
30 | static zchar prev_c = 0; | ||
31 | |||
32 | /* | ||
33 | * flush_buffer | ||
34 | * | ||
35 | * Copy the contents of the text buffer to the output streams. | ||
36 | * | ||
37 | */ | ||
38 | |||
39 | void flush_buffer (void) | ||
40 | { | ||
41 | static bool locked = FALSE; | ||
42 | |||
43 | /* Make sure we stop when flush_buffer is called from flush_buffer. | ||
44 | Note that this is difficult to avoid as we might print a newline | ||
45 | during flush_buffer, which might cause a newline interrupt, that | ||
46 | might execute any arbitrary opcode, which might flush the buffer. */ | ||
47 | |||
48 | if (locked || bufpos == 0) | ||
49 | return; | ||
50 | |||
51 | /* Send the buffer to the output streams */ | ||
52 | |||
53 | buffer[bufpos] = 0; | ||
54 | |||
55 | |||
56 | locked = TRUE; | ||
57 | |||
58 | stream_word (buffer); | ||
59 | |||
60 | #ifdef SPEECH_OUTPUT | ||
61 | os_speech_output(buffer); | ||
62 | #endif | ||
63 | |||
64 | locked = FALSE; | ||
65 | |||
66 | /* Reset the buffer */ | ||
67 | |||
68 | bufpos = 0; | ||
69 | prev_c = 0; | ||
70 | |||
71 | }/* flush_buffer */ | ||
72 | |||
73 | /* | ||
74 | * print_char | ||
75 | * | ||
76 | * High level output function. | ||
77 | * | ||
78 | */ | ||
79 | |||
80 | void print_char (zchar c) | ||
81 | { | ||
82 | static bool flag = FALSE; | ||
83 | |||
84 | if (message || ostream_memory || enable_buffering) { | ||
85 | |||
86 | if (!flag) { | ||
87 | |||
88 | /* Characters 0 and ZC_RETURN are special cases */ | ||
89 | |||
90 | if (c == ZC_RETURN) | ||
91 | { new_line (); return; } | ||
92 | if (c == 0) | ||
93 | return; | ||
94 | |||
95 | /* Flush the buffer before a whitespace or after a hyphen */ | ||
96 | |||
97 | if (c == ' ' || c == ZC_INDENT || c == ZC_GAP || (prev_c == '-' && c != '-')) | ||
98 | |||
99 | |||
100 | flush_buffer (); | ||
101 | |||
102 | /* Set the flag if this is part one of a style or font change */ | ||
103 | |||
104 | if (c == ZC_NEW_FONT || c == ZC_NEW_STYLE) | ||
105 | flag = TRUE; | ||
106 | |||
107 | /* Remember the current character code */ | ||
108 | |||
109 | prev_c = c; | ||
110 | |||
111 | } else flag = FALSE; | ||
112 | |||
113 | /* Insert the character into the buffer */ | ||
114 | |||
115 | buffer[bufpos++] = c; | ||
116 | |||
117 | if (bufpos == TEXT_BUFFER_SIZE) | ||
118 | runtime_error (ERR_TEXT_BUF_OVF); | ||
119 | |||
120 | } else stream_char (c); | ||
121 | |||
122 | }/* print_char */ | ||
123 | |||
124 | /* | ||
125 | * new_line | ||
126 | * | ||
127 | * High level newline function. | ||
128 | * | ||
129 | */ | ||
130 | |||
131 | void new_line (void) | ||
132 | { | ||
133 | |||
134 | flush_buffer (); stream_new_line (); | ||
135 | |||
136 | }/* new_line */ | ||
137 | |||
138 | |||
139 | /* | ||
140 | * init_buffer | ||
141 | * | ||
142 | * Initialize buffer variables. | ||
143 | * | ||
144 | */ | ||
145 | |||
146 | void init_buffer(void) | ||
147 | { | ||
148 | memset(buffer, 0, sizeof (zchar) * TEXT_BUFFER_SIZE); | ||
149 | bufpos = 0; | ||
150 | prev_c = 0; | ||
151 | } | ||
152 | |||