4 /* Global current stream */
5 static strid_t current_stream = NULL;
6 /* List of streams currently in existence */
7 static GList *stream_list = NULL;
9 /* Internal function: create a window stream to go with window. */
11 window_stream_new(winid_t window)
13 /* Create stream and connect it to window */
14 strid_t s = g_new0(struct glk_stream_struct, 1);
15 s->file_mode = filemode_Write;
16 s->stream_type = STREAM_TYPE_WINDOW;
18 /* Add it to the global stream list */
19 stream_list = g_list_prepend(stream_list, s);
20 s->stream_list = stream_list;
27 * @str: A stream, or #NULL.
28 * @rockptr: Return location for the next window's rock, or #NULL.
30 * Iterates over the list of streams; if @str is #NULL, it returns the first
31 * stream, otherwise the next stream after @str. If there are no more, it
32 * returns #NULL. The stream's rock is stored in @rockptr. If you don't want
33 * the rocks to be returned, you may set @rockptr to #NULL.
35 * The order in which streams are returned is arbitrary. The order may change
36 * every time you create or destroy a stream, invalidating the iteration.
38 * Returns: the next stream, or #NULL if there are no more.
41 glk_stream_iterate(strid_t str, glui32 *rockptr)
46 retnode = stream_list;
48 retnode = str->stream_list->next;
49 strid_t retval = retnode? (strid_t)retnode->data : NULL;
51 /* Store the stream's rock in rockptr */
53 *rockptr = glk_stream_get_rock(retval);
59 * glk_stream_get_rock:
62 * Returns the stream @str's rock value.
64 * Returns: A rock value.
67 glk_stream_get_rock(strid_t str)
69 g_return_val_if_fail(str != NULL, 0);
74 * glk_stream_set_current:
75 * @str: An output stream, or NULL.
77 * Sets the current stream to @str, or to nothing if @str is #NULL.
80 glk_stream_set_current(strid_t str)
82 if(str != NULL && str->file_mode != filemode_Write)
84 g_warning("glk_stream_set_current: "
85 "Cannot set current stream to non output stream");
93 * glk_stream_get_current:
95 * Returns the current stream, or #NULL if there is none.
100 glk_stream_get_current()
102 return current_stream;
107 * @ch: A character in Latin-1 encoding.
109 * Prints one character @ch to the current stream.
112 glk_put_char(unsigned char ch)
114 /* Illegal to print to the current stream if it is NULL */
115 g_return_if_fail(current_stream != NULL);
116 glk_put_char_stream(current_stream, ch);
121 * @s: A null-terminated string in Latin-1 encoding.
123 * Prints @s to the current stream.
126 glk_put_string(char *s)
128 /* Illegal to print to the current stream if it is NULL */
129 g_return_if_fail(current_stream != NULL);
130 glk_put_string_stream(current_stream, s);
135 * @buf: An array of characters in Latin-1 encoding.
136 * @len: Length of @buf.
138 * Prints @buf to the current stream.
141 glk_put_buffer(char *buf, glui32 len)
143 /* Illegal to print to the current stream if it is NULL */
144 g_return_if_fail(current_stream != NULL);
145 glk_put_buffer_stream(current_stream, buf, len);
149 * glk_put_char_stream:
150 * @str: An output stream.
151 * @ch: A character in Latin-1 encoding.
153 * Prints one character @ch to the stream @str. It is illegal for @str to be
154 * #NULL, or an input-only stream.
157 glk_put_char_stream(strid_t str, unsigned char ch)
159 g_return_if_fail(str != NULL);
160 g_return_if_fail(str->file_mode != filemode_Read);
162 /* Convert ch to a null-terminated string, call glk_put_string_stream() */
163 gchar *s = g_strndup((gchar *)&ch, 1);
164 glk_put_string_stream(str, s);
168 /* Internal function: change illegal (control) characters in a string to a
169 placeholder character. Must free returned string afterwards. */
171 remove_latin1_control_characters(gchar *s)
173 gchar *retval = g_strdup(s);
175 for(ptr = (unsigned char *)retval; *ptr != '\0'; ptr++)
176 if( (*ptr < 32 && *ptr != 10) || (*ptr >= 127 && *ptr <= 159) )
178 /* Our placeholder character is '?'; other options are possible,
179 like printing "0x7F" or something */
183 /* Internal function: convert a Latin-1 string to a UTF-8 string, replacing
184 Latin-1 control characters by a placeholder first. The UTF-8 string must be
185 freed afterwards. Returns NULL on error. */
187 convert_latin1_to_utf8(gchar *s)
189 GError *error = NULL;
191 gchar *canonical = remove_latin1_control_characters(s);
192 utf8 = g_convert(canonical, -1, "UTF-8", "ISO-8859-1", NULL, NULL, &error);
197 error_dialog(NULL, error, "Error during latin1->utf8 conversion: ");
204 /* Internal function: write a UTF-8 string to a window's text buffer. */
206 write_utf8_to_window(winid_t win, gchar *s)
208 GtkTextBuffer *buffer =
209 gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
212 gtk_text_buffer_get_end_iter(buffer, &iter);
213 gtk_text_buffer_insert(buffer, &iter, s, -1);
217 * glk_put_string_stream:
218 * @str: An output stream.
219 * @s: A null-terminated string in Latin-1 encoding.
221 * Prints @s to the stream @str. It is illegal for @str to be #NULL, or an
225 glk_put_string_stream(strid_t str, char *s)
227 g_return_if_fail(str != NULL);
228 g_return_if_fail(str->file_mode != filemode_Read);
230 switch(str->stream_type)
232 case STREAM_TYPE_WINDOW:
233 /* Each window type has a different way of printing to it */
234 switch(str->window->window_type)
236 /* Printing to a these windows' streams does nothing */
239 case wintype_Graphics:
240 current_stream->write_count += strlen(s);
242 /* Text buffer window */
243 case wintype_TextBuffer:
245 gchar *utf8 = convert_latin1_to_utf8(s);
246 write_utf8_to_window(str->window, utf8);
249 str->write_count += strlen(s);
252 g_warning("glk_put_string: "
253 "Writing to this kind of window unsupported.");
256 /* Now write the same buffer to the window's echo stream */
257 if(str->window->echo_stream != NULL)
258 glk_put_string_stream(str->window->echo_stream, s);
262 g_warning("glk_put_string: "
263 "Writing to this kind of stream unsupported.");
268 * glk_put_buffer_stream:
269 * @str: An output stream.
270 * @buf: An array of characters in Latin-1 encoding.
271 * @len: Length of @buf.
273 * Prints @buf to the stream @str. It is illegal for @str to be #NULL, or an
277 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
279 g_return_if_fail(str != NULL);
280 g_return_if_fail(str->file_mode != filemode_Read);
282 /* Convert buf to a null-terminated string, call glk_put_string_stream() */
283 gchar *s = g_strndup(buf, len);
284 glk_put_string_stream(str, s);