+ break;
+ default:
+ g_warning("glk_stream_open_file: Invalid file mode");
+ return NULL;
+ }
+
+ FILE *fp = g_fopen(fileref->filename, modestr);
+ g_free(modestr);
+ if(fp == NULL) {
+ g_warning("glk_stream_open_file: Error opening file");
+ return NULL;
+ }
+
+ /* If they opened a file in write mode but didn't specifically get
+ permission to do so, complain if the file already exists */
+ if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
+ gdk_threads_enter();
+
+ GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
+ GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
+ "File %s already exists. Overwrite?", fileref->filename);
+ gint response = gtk_dialog_run(GTK_DIALOG(dialog));
+ gtk_widget_destroy(dialog);
+
+ gdk_threads_leave();
+
+ if(response != GTK_RESPONSE_YES) {
+ fclose(fp);
+ return NULL;
+ }
+ }
+
+ strid_t str = g_new0(struct glk_stream_struct, 1);
+ str->rock = rock;
+ str->file_mode = fmode;
+ str->type = STREAM_TYPE_FILE;
+ str->file_pointer = fp;
+ str->binary = binary;
+ str->unicode = unicode;
+ str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
+ if(str->filename == NULL)
+ str->filename = g_strdup("Unknown file name"); /* fail silently */
+ /* Add it to the global stream list */
+ glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
+ str->stream_list = glk_data->stream_list;
+
+ return str;
+}
+
+/**
+ * glk_stream_open_file:
+ * @fileref: Indicates the file which will be opened.
+ * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
+ * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
+ * @rock: The new stream's rock value.
+ *
+ * Opens a stream which reads to or writes from a disk file. If @fmode is
+ * #filemode_Read, the file must already exist; for the other modes, an empty
+ * file is created if none exists. If @fmode is #filemode_Write, and the file
+ * already exists, it is truncated down to zero length (an empty file). If
+ * @fmode is #filemode_WriteAppend, the file mark is set to the end of the
+ * file.
+ *
+ * When writing in binary mode, Unicode values (characters greater than 255)
+ * cannot be written to the file. If you try, they will be stored as 0x3F
+ * (<code>"?"</code>) characters. In text mode, Unicode values may be stored
+ * exactly, approximated, or abbreviated, depending on what the platform's text
+ * files support.
+ *
+ * Returns: A new stream, or %NULL if the file operation failed.
+ */
+strid_t
+glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
+{
+ return file_stream_new(fileref, fmode, rock, FALSE);
+}
+
+/**
+ * glk_stream_open_file_uni:
+ * @fileref: Indicates the file which will be opened.
+ * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
+ * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
+ * @rock: The new stream's rock value.
+ *
+ * This works just like glk_stream_open_file(), except that in binary mode,
+ * characters are written and read as four-byte (big-endian) values. This
+ * allows you to write any Unicode character.
+ *
+ * In text mode, the file is written and read in a platform-dependent way, which
+ * may or may not handle all Unicode characters. A text-mode file created with
+ * glk_stream_open_file_uni() may have the same format as a text-mode file
+ * created with glk_stream_open_file(); or it may use a more Unicode-friendly
+ * format.
+ *
+ * Returns: A new stream, or %NULL if the file operation failed.
+ */
+strid_t
+glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
+{
+ return file_stream_new(fileref, fmode, rock, TRUE);
+}
+
+/**
+ * glk_stream_close:
+ * @str: Stream to close.
+ * @result: Pointer to a #stream_result_t, or %NULL.
+ *
+ * Closes the stream @str. The @result argument points to a structure which is
+ * filled in with the final character counts of the stream. If you do not care
+ * about these, you may pass %NULL as the @result argument.
+ *
+ * If @str is the current output stream, the current output stream is set to
+ * %NULL.
+ *
+ * You cannot close window streams; use glk_window_close() instead. See <link
+ * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
+ * Closing, and Constraints</link>.
+ */
+void
+glk_stream_close(strid_t str, stream_result_t *result)
+{
+ g_return_if_fail(str != NULL);
+
+ /* Free resources associated with one specific type of stream */
+ switch(str->type)
+ {
+ case STREAM_TYPE_WINDOW:
+ g_warning("%s: Attempted to close a window stream. Use glk_window_"
+ "close() instead.", __func__);
+ return;
+
+ case STREAM_TYPE_MEMORY:
+ /* Do nothing */
+ break;