A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 152 lines 3.1 kB view raw
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 23extern void stream_char (zchar); 24extern void stream_word (const zchar *); 25extern void stream_new_line (void); 26 27static zchar buffer[TEXT_BUFFER_SIZE]; 28static int bufpos = 0; 29 30static 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 39void 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 80void 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 131void 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 146void init_buffer(void) 147{ 148 memset(buffer, 0, sizeof (zchar) * TEXT_BUFFER_SIZE); 149 bufpos = 0; 150 prev_c = 0; 151} 152