#include "stream.h"
+#include "fileref.h"
#include <string.h>
+#include <stdio.h>
+#include <glib/gstdio.h>
/* Global current stream */
static strid_t current_stream = NULL;
g_free(s);
}
+/* Internal function: create a stream using the given parameters. */
+static strid_t
+stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
+{
+ g_return_val_if_fail(fileref != NULL, NULL);
+
+ gchar *modestr;
+ gboolean binary = fileref->usage & fileusage_BinaryMode;
+ switch(fmode)
+ {
+ case filemode_Read:
+ if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
+ g_warning("glk_stream_open_file: Tried to open a file in read "
+ "mode that didn't exist!");
+ return NULL;
+ }
+ modestr = g_strdup(binary? "rb" : "r");
+ break;
+ case filemode_Write:
+ modestr = g_strdup(binary? "wb" : "w");
+ break;
+ case filemode_WriteAppend:
+ modestr = g_strdup(binary? "ab" : "a");
+ break;
+ case filemode_ReadWrite:
+ modestr = g_strdup(binary? "r+b" : "r+");
+ 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) {
+ 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);
+ if(response != GTK_RESPONSE_YES) {
+ fclose(fp);
+ return NULL;
+ }
+ }
+
+ strid_t s = g_new0(struct glk_stream_struct, 1);
+ s->file_mode = fmode;
+ s->stream_type = unicode? STREAM_TYPE_UNICODE_FILE : STREAM_TYPE_FILE;
+ s->file_pointer = fp;
+ s->binary = binary;
+ /* Add it to the global stream list */
+ stream_list = g_list_prepend(stream_list, s);
+ s->stream_list = stream_list;
+
+ return s;
+}
+
+/**
+ * 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.
+ *
+ * The file may be written in text or binary mode; this is determined by the
+ * @fileref argument. Similarly, platform-dependent attributes such as file
+ * type are determined by @fileref.
+ *
+ * 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 ("?")
+ * characters. In text mode, Unicode values are stored in UTF-8.
+ *
+ * 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 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 UTF-8.
+ *
+ * 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 stream_new(fileref, fmode, rock, TRUE);
+}
+