1 /* buffer.c - Text buffering and word wrapping
2 * Copyright (c) 2003 Tor Andersson -- zapped!
3 * Copyright (c) 1995-1997 Stefan Jokisch
5 * This file is part of Frotz.
7 * Frotz is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Frotz is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 extern void stream_char (zchar);
25 extern void stream_word (const zchar *);
26 extern void stream_new_line (void);
28 static zchar buffer[TEXT_BUFFER_SIZE];
29 static int bufpos = 0;
30 static bool locked = FALSE;
32 static zchar prev_c = 0;
37 * Copy the contents of the text buffer to the output streams.
41 void flush_buffer (void)
44 /* Make sure we stop when flush_buffer is called from flush_buffer.
45 Note that this is difficult to avoid as we might print a newline
46 during flush_buffer, which might cause a newline interrupt, that
47 might execute any arbitrary opcode, which might flush the buffer. */
49 if (locked || bufpos == 0)
52 /* Send the buffer to the output streams */
62 os_speech_output(buffer);
67 /* Reset the buffer */
77 * High level output function.
81 void print_char (zchar c)
83 static bool flag = FALSE;
85 if (message || ostream_memory || enable_buffering) {
89 /* Characters 0 and ZC_RETURN are special cases */
92 { new_line (); return; }
96 /* Flush the buffer before a whitespace or after a hyphen */
98 if (c == ' ' || c == ZC_INDENT || c == ZC_GAP || (prev_c == '-' && c != '-'))
103 /* Set the flag if this is part one of a style or font change */
105 if (c == ZC_NEW_FONT || c == ZC_NEW_STYLE)
108 /* Remember the current character code */
114 /* Insert the character into the buffer */
116 buffer[bufpos++] = c;
118 if (bufpos == TEXT_BUFFER_SIZE)
119 runtime_error (ERR_TEXT_BUF_OVF);
121 } else stream_char (c);
128 * High level newline function.
135 flush_buffer (); stream_new_line ();
143 * Initialize buffer variables.
147 void init_buffer(void)
149 memset(buffer, 0, sizeof (zchar) * TEXT_BUFFER_SIZE);