5 #include <glib/gstdio.h>
7 /* Global current stream */
8 static strid_t current_stream = NULL;
9 /* List of streams currently in existence */
10 static GList *stream_list = NULL;
12 /* Internal function: create a window stream to go with window. */
14 window_stream_new(winid_t window)
16 /* Create stream and connect it to window */
17 strid_t s = g_new0(struct glk_stream_struct, 1);
18 s->file_mode = filemode_Write;
19 s->stream_type = STREAM_TYPE_WINDOW;
21 /* Add it to the global stream list */
22 stream_list = g_list_prepend(stream_list, s);
23 s->stream_list = stream_list;
30 * @str: A stream, or #NULL.
31 * @rockptr: Return location for the next window's rock, or #NULL.
33 * Iterates over the list of streams; if @str is #NULL, it returns the first
34 * stream, otherwise the next stream after @str. If there are no more, it
35 * returns #NULL. The stream's rock is stored in @rockptr. If you don't want
36 * the rocks to be returned, you may set @rockptr to #NULL.
38 * The order in which streams are returned is arbitrary. The order may change
39 * every time you create or destroy a stream, invalidating the iteration.
41 * Returns: the next stream, or #NULL if there are no more.
44 glk_stream_iterate(strid_t str, glui32 *rockptr)
49 retnode = stream_list;
51 retnode = str->stream_list->next;
52 strid_t retval = retnode? (strid_t)retnode->data : NULL;
54 /* Store the stream's rock in rockptr */
56 *rockptr = glk_stream_get_rock(retval);
62 * glk_stream_get_rock:
65 * Returns the stream @str's rock value.
67 * Returns: A rock value.
70 glk_stream_get_rock(strid_t str)
72 g_return_val_if_fail(str != NULL, 0);
77 * glk_stream_set_current:
78 * @str: An output stream, or NULL.
80 * Sets the current stream to @str, or to nothing if @str is #NULL.
83 glk_stream_set_current(strid_t str)
85 if(str != NULL && str->file_mode != filemode_Write)
87 g_warning("glk_stream_set_current: "
88 "Cannot set current stream to non output stream");
96 * glk_stream_get_current:
98 * Returns the current stream, or #NULL if there is none.
103 glk_stream_get_current()
105 return current_stream;
110 * @ch: A character in Latin-1 encoding.
112 * Prints one character @ch to the current stream.
115 glk_put_char(unsigned char ch)
117 /* Illegal to print to the current stream if it is NULL */
118 g_return_if_fail(current_stream != NULL);
119 glk_put_char_stream(current_stream, ch);
124 * @s: A null-terminated string in Latin-1 encoding.
126 * Prints @s to the current stream.
129 glk_put_string(char *s)
131 /* Illegal to print to the current stream if it is NULL */
132 g_return_if_fail(current_stream != NULL);
133 glk_put_string_stream(current_stream, s);
138 * @buf: An array of characters in Latin-1 encoding.
139 * @len: Length of @buf.
141 * Prints @buf to the current stream.
144 glk_put_buffer(char *buf, glui32 len)
146 /* Illegal to print to the current stream if it is NULL */
147 g_return_if_fail(current_stream != NULL);
148 glk_put_buffer_stream(current_stream, buf, len);
152 * glk_put_char_stream:
153 * @str: An output stream.
154 * @ch: A character in Latin-1 encoding.
156 * Prints one character @ch to the stream @str. It is illegal for @str to be
157 * #NULL, or an input-only stream.
160 glk_put_char_stream(strid_t str, unsigned char ch)
162 g_return_if_fail(str != NULL);
163 g_return_if_fail(str->file_mode != filemode_Read);
165 /* Convert ch to a null-terminated string, call glk_put_string_stream() */
166 gchar *s = g_strndup((gchar *)&ch, 1);
167 glk_put_string_stream(str, s);
171 /* Internal function: change illegal (control) characters in a string to a
172 placeholder character. Must free returned string afterwards. */
174 remove_latin1_control_characters(gchar *s)
176 gchar *retval = g_strdup(s);
178 for(ptr = (unsigned char *)retval; *ptr != '\0'; ptr++)
179 if( (*ptr < 32 && *ptr != 10) || (*ptr >= 127 && *ptr <= 159) )
181 /* Our placeholder character is '?'; other options are possible,
182 like printing "0x7F" or something */
186 /* Internal function: convert a Latin-1 string to a UTF-8 string, replacing
187 Latin-1 control characters by a placeholder first. The UTF-8 string must be
188 freed afterwards. Returns NULL on error. */
190 convert_latin1_to_utf8(gchar *s)
192 GError *error = NULL;
194 gchar *canonical = remove_latin1_control_characters(s);
195 utf8 = g_convert(canonical, -1, "UTF-8", "ISO-8859-1", NULL, NULL, &error);
200 error_dialog(NULL, error, "Error during latin1->utf8 conversion: ");
207 /* Internal function: write a UTF-8 string to a window's text buffer. */
209 write_utf8_to_window(winid_t win, gchar *s)
211 GtkTextBuffer *buffer =
212 gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
215 gtk_text_buffer_get_end_iter(buffer, &iter);
216 gtk_text_buffer_insert(buffer, &iter, s, -1);
220 * glk_put_string_stream:
221 * @str: An output stream.
222 * @s: A null-terminated string in Latin-1 encoding.
224 * Prints @s to the stream @str. It is illegal for @str to be #NULL, or an
228 glk_put_string_stream(strid_t str, char *s)
230 g_return_if_fail(str != NULL);
231 g_return_if_fail(str->file_mode != filemode_Read);
233 switch(str->stream_type)
235 case STREAM_TYPE_WINDOW:
236 /* Each window type has a different way of printing to it */
237 switch(str->window->window_type)
239 /* Printing to a these windows' streams does nothing */
242 case wintype_Graphics:
243 current_stream->write_count += strlen(s);
245 /* Text buffer window */
246 case wintype_TextBuffer:
248 gchar *utf8 = convert_latin1_to_utf8(s);
249 write_utf8_to_window(str->window, utf8);
252 str->write_count += strlen(s);
255 g_warning("glk_put_string: "
256 "Writing to this kind of window unsupported.");
259 /* Now write the same buffer to the window's echo stream */
260 if(str->window->echo_stream != NULL)
261 glk_put_string_stream(str->window->echo_stream, s);
265 g_warning("glk_put_string: "
266 "Writing to this kind of stream unsupported.");
271 * glk_put_buffer_stream:
272 * @str: An output stream.
273 * @buf: An array of characters in Latin-1 encoding.
274 * @len: Length of @buf.
276 * Prints @buf to the stream @str. It is illegal for @str to be #NULL, or an
280 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
282 g_return_if_fail(str != NULL);
283 g_return_if_fail(str->file_mode != filemode_Read);
285 /* Convert buf to a null-terminated string, call glk_put_string_stream() */
286 gchar *s = g_strndup(buf, len);
287 glk_put_string_stream(str, s);
291 /* Internal function: create a stream using the given parameters. */
293 stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
295 g_return_val_if_fail(fileref != NULL, NULL);
298 gboolean binary = fileref->usage & fileusage_BinaryMode;
302 if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
303 g_warning("glk_stream_open_file: Tried to open a file in read "
304 "mode that didn't exist!");
307 modestr = g_strdup(binary? "rb" : "r");
310 modestr = g_strdup(binary? "wb" : "w");
312 case filemode_WriteAppend:
313 modestr = g_strdup(binary? "ab" : "a");
315 case filemode_ReadWrite:
316 modestr = g_strdup(binary? "r+b" : "r+");
319 g_warning("glk_stream_open_file: Invalid file mode");
323 FILE *fp = g_fopen(fileref->filename, modestr);
326 g_warning("glk_stream_open_file: Error opening file");
330 /* If they opened a file in write mode but didn't specifically get
331 permission to do so, complain if the file already exists */
332 if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
333 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
334 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
335 "File %s already exists. Overwrite?", fileref->filename);
336 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
337 gtk_widget_destroy(dialog);
338 if(response != GTK_RESPONSE_YES) {
344 strid_t s = g_new0(struct glk_stream_struct, 1);
345 s->file_mode = fmode;
346 s->stream_type = unicode? STREAM_TYPE_UNICODE_FILE : STREAM_TYPE_FILE;
347 s->file_pointer = fp;
349 /* Add it to the global stream list */
350 stream_list = g_list_prepend(stream_list, s);
351 s->stream_list = stream_list;
357 * glk_stream_open_file:
358 * @fileref: Indicates the file which will be opened.
359 * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
360 * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
361 * @rock: The new stream's rock value.
363 * Opens a stream which reads to or writes from a disk file. If @fmode is
364 * #filemode_Read, the file must already exist; for the other modes, an empty
365 * file is created if none exists. If @fmode is #filemode_Write, and the file
366 * already exists, it is truncated down to zero length (an empty file). If
367 * @fmode is #filemode_WriteAppend, the file mark is set to the end of the
370 * The file may be written in text or binary mode; this is determined by the
371 * @fileref argument. Similarly, platform-dependent attributes such as file
372 * type are determined by @fileref.
374 * When writing in binary mode, Unicode values (characters greater than 255)
375 * cannot be written to the file. If you try, they will be stored as 0x3F ("?")
376 * characters. In text mode, Unicode values are stored in UTF-8.
378 * Returns: A new stream, or %NULL if the file operation failed.
381 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
383 return stream_new(fileref, fmode, rock, FALSE);
387 * glk_stream_open_file_uni:
388 * @fileref: Indicates the file which will be opened.
389 * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
390 * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
391 * @rock: The new stream's rock value.
393 * This works just like glk_stream_open_file(), except that in binary mode,
394 * characters are written and read as four-byte (big-endian) values. This
395 * allows you to write any Unicode character.
397 * In text mode, the file is written and read in UTF-8.
399 * Returns: A new stream, or %NULL if the file operation failed.
402 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
404 return stream_new(fileref, fmode, rock, TRUE);