+ g_return_if_fail(glk_data->current_stream != NULL);
+ glk_put_string_stream(glk_data->current_stream, s);
+}
+
+/**
+ * glk_put_buffer:
+ * @buf: An array of characters in Latin-1 encoding.
+ * @len: Length of @buf.
+ *
+ * Prints a block of characters to the current stream. It is exactly equivalent
+ * to:
+ * <informalexample><programlisting>
+ * for (i = 0; i < len; i++)
+ * glk_put_char(buf[i]);
+ * </programlisting></informalexample>
+ * However, it may be more efficient.
+ */
+void
+glk_put_buffer(char *buf, glui32 len)
+{
+ g_return_if_fail(glk_data->current_stream != NULL);
+ glk_put_buffer_stream(glk_data->current_stream, buf, len);
+}
+
+/**
+ * glk_stream_open_memory:
+ * @buf: An allocated buffer, or %NULL.
+ * @buflen: Length of @buf.
+ * @fmode: Mode in which the buffer will be opened. Must be one of
+ * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
+ * @rock: The new stream's rock value.
+ *
+ * Opens a stream which reads from or writes to a space in memory. @buf points
+ * to the buffer where output will be read from or written to. @buflen is the
+ * length of the buffer.
+ *
+ * When outputting, if more than @buflen characters are written to the stream,
+ * all of them beyond the buffer length will be thrown away, so as not to
+ * overwrite the buffer. (The character count of the stream will still be
+ * maintained correctly. That is, it will count the number of characters written
+ * into the stream, not the number that fit into the buffer.)
+ *
+ * If @buf is %NULL, or for that matter if @buflen is zero, then <emphasis>
+ * everything</emphasis> written to the stream is thrown away. This may be
+ * useful if you are interested in the character count.
+ *
+ * When inputting, if more than @buflen characters are read from the stream, the
+ * stream will start returning -1 (signalling end-of-file.) If @buf is %NULL,
+ * the stream will always return end-of-file.
+ *
+ * The data is written to the buffer exactly as it was passed to the printing
+ * functions (glk_put_char(), etc.); input functions will read the data exactly
+ * as it exists in memory. No platform-dependent cookery will be done on it.
+ * (You can write a disk file in text mode, but a memory stream is effectively
+ * always in binary mode.)
+ *
+ * Unicode values (characters greater than 255) cannot be written to the buffer.
+ * If you try, they will be stored as 0x3F ("?") characters.
+ *
+ * Whether reading or writing, the contents of the buffer are undefined until
+ * the stream is closed. The library may store the data there as it is written,
+ * or deposit it all in a lump when the stream is closed. It is illegal to
+ * change the contents of the buffer while the stream is open.
+ */
+strid_t
+glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock)
+{
+ g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
+
+ strid_t str = g_new0(struct glk_stream_struct, 1);
+ str->rock = rock;
+ str->file_mode = fmode;
+ str->type = STREAM_TYPE_MEMORY;
+ str->buffer = buf;
+ str->mark = 0;
+ str->buflen = buflen;
+ str->unicode = FALSE;
+
+ /* 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_memory_uni:
+ * @buf: An allocated buffer, or %NULL.
+ * @buflen: Length of @buf.
+ * @fmode: Mode in which the buffer will be opened. Must be one of
+ * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
+ * @rock: The new stream's rock value.
+ *
+ * Works just like glk_stream_open_memory(), except that the buffer is an array
+ * of 32-bit words, instead of bytes. This allows you to write and read any
+ * Unicode character. The @buflen is the number of words, not the number of
+ * bytes.
+ */
+strid_t
+glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
+{
+ g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
+
+ strid_t str = g_new0(struct glk_stream_struct, 1);
+ str->rock = rock;
+ str->file_mode = fmode;
+ str->type = STREAM_TYPE_MEMORY;
+ str->ubuffer = buf;
+ str->mark = 0;
+ str->buflen = buflen;
+ str->unicode = TRUE;
+
+ /* 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;
+}
+
+/* Internal function: create a stream using the given parameters. */
+static strid_t
+file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
+{
+ g_return_val_if_fail(fileref != NULL, NULL);
+
+ gchar *modestr;
+ /* Binary mode is 0x000, text mode 0x100 */
+ gboolean binary = !(fileref->usage & fileusage_TextMode);
+ 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:
+ if( g_file_test(fileref->filename, G_FILE_TEST_EXISTS) ) {
+ modestr = g_strdup(binary? "r+b" : "r+");
+ } else {
+ modestr = g_strdup(binary? "w+b" : "w+");
+ }
+ 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;
+ }