Some formatting fixes, reported by Alexander Levin
[kugel-rb.git] / apps / plugins / frotz / buffer.c
blob298ac69c20e006957076c9af23dc56a6d28d2515
1 /* buffer.c - Text buffering and word wrapping
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
21 #include "frotz.h"
23 extern void stream_char (zchar);
24 extern void stream_word (const zchar *);
25 extern void stream_new_line (void);
27 static zchar buffer[TEXT_BUFFER_SIZE];
28 static int bufpos = 0;
30 static zchar prev_c = 0;
33 * flush_buffer
35 * Copy the contents of the text buffer to the output streams.
39 void flush_buffer (void)
41 static bool locked = FALSE;
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. */
48 if (locked || bufpos == 0)
49 return;
51 /* Send the buffer to the output streams */
53 buffer[bufpos] = 0;
56 locked = TRUE;
58 stream_word (buffer);
60 #ifdef SPEECH_OUTPUT
61 os_speech_output(buffer);
62 #endif
64 locked = FALSE;
66 /* Reset the buffer */
68 bufpos = 0;
69 prev_c = 0;
71 }/* flush_buffer */
74 * print_char
76 * High level output function.
80 void print_char (zchar c)
82 static bool flag = FALSE;
84 if (message || ostream_memory || enable_buffering) {
86 if (!flag) {
88 /* Characters 0 and ZC_RETURN are special cases */
90 if (c == ZC_RETURN)
91 { new_line (); return; }
92 if (c == 0)
93 return;
95 /* Flush the buffer before a whitespace or after a hyphen */
97 if (c == ' ' || c == ZC_INDENT || c == ZC_GAP || (prev_c == '-' && c != '-'))
100 flush_buffer ();
102 /* Set the flag if this is part one of a style or font change */
104 if (c == ZC_NEW_FONT || c == ZC_NEW_STYLE)
105 flag = TRUE;
107 /* Remember the current character code */
109 prev_c = c;
111 } else flag = FALSE;
113 /* Insert the character into the buffer */
115 buffer[bufpos++] = c;
117 if (bufpos == TEXT_BUFFER_SIZE)
118 runtime_error (ERR_TEXT_BUF_OVF);
120 } else stream_char (c);
122 }/* print_char */
125 * new_line
127 * High level newline function.
131 void new_line (void)
134 flush_buffer (); stream_new_line ();
136 }/* new_line */
140 * init_buffer
142 * Initialize buffer variables.
146 void init_buffer(void)
148 memset(buffer, 0, sizeof (zchar) * TEXT_BUFFER_SIZE);
149 bufpos = 0;
150 prev_c = 0;